Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbee Question
#1
I am trying to make a class I can use to save a dataframe, two strings and two floats.
I cant seem to get it to work.

    import numpy as np
    import os
    import matplotlib
    import matplotlib.pyplot as plt
    matplotlib.use('wxAgg')  # matplotlib backend needed to work with wxPython GUI
    import time
    from datetime import datetime
    import pandas as pd
    
    selected_dir = r"C:\Users\cstreb\Desktop\Data Visualization"
    path = r"C:\Users\cstreb\Desktop\Data Visualization\Test1.xlsm"
    df = pd.read_excel (path, sheet_name='DefocusData', names = ["Description","Defocus","Tan","Sag"])
    Title = os.path.basename(path) 
    print (Title)
    Time = os.path.getmtime(path)
    local_time = time.ctime(Time) 
    print("Last modification time(Local time):", local_time) 

    FieldAngles = ((df.loc[df['Description'] == 'THF Field Angle, Roll Angle']).iloc[:,1])
    DefocusPlanes = ((df.loc[df['Description'] == 'Number of THF Planes']).iloc[:,1])
    DefocusFrequency = ((df.loc[df['Description'] == 'Defocus Frequency']).iloc[:,1])
    print (DefocusFrequency)
    for x in DefocusPlanes.index:
        print (x)
        print (df.iloc[(x+3):(x+3+DefocusPlanes[x]), 1:4])
    
    class geeks:  
        def __init__(self):  
            self.DFAngSagTang = pd.dataframe
            self.FileName = ""
            self.Date = ""
            self.FieldAngle = -1
            self.LP = -1
        def AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time):
            self.DFAngSagTang = DefocusPlane
            self.FileName = Title
            self.Date = local_time
            self.FieldAngle = FieldAngle
            self.LP = DefocusFrequency

    for x in DefocusPlanes.index:
        FieldAngle = (FieldAngles[x-1])
        DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4])
        print (DefocusPlane)
        SingleRunData.AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
        RunData.append(SingleRunData)
I get the following error -=-=-=-=-=-=-=-=-

Defocus Tan Sag
8 -50.8 9.2 10.1
9 -24.7 29.8 30.1
10 0.7 39.4 39.5
11 25.6 29.9 31.5
12 50.1 11.5 14.5

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-197-163bba125617> in <module>
3 DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4])
4 print (DefocusPlane)
----> 5 SingleRunData.AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
6 RunData.append(SingleRunData)

<ipython-input-194-9edb5b7dd6ac> in AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
7 self.LP = -1
8 def AddDefocusPlane(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time):
----> 9 self.DFAngSagTang = DefocusPlane
10 self.FileName = Title
11 self.Date = local_time

NameError: name 'self' is not defined

-=-=-=

I get the following errors. Any help would be great , thank you. New Bee Angel
Reply
#2
You forgot self in your second class method. Should be:
def AddDefocusPlane(self, DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time):
Reply
#3
class OneDataSet:  
    def __init__(self, DFAngSagTang, FieldAngle, DefocusFrequency, Title, local_time):  
        self.DFAngSagTang = DFAngSagTang
        self.FileName = Title
        self.Date = local_time
        self.FieldAngle = FieldAngle
        self.LP = DefocusFrequency
    def __call__(self, DFAngSagTang, FieldAngle, DefocusFrequency, Title, local_time):  
        self.DFAngSagTang = DFAngSagTang
        self.FileName = Title
        self.Date = local_time
        self.FieldAngle = FieldAngle
        self.LP = DefocusFrequency
SingleRunData = OneDataSet
RunData = []
for x in DefocusPlanes.index:
    FieldAngle = (FieldAngles[x-1])
    DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4])
    SingleRunData(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
    RunData.append(SingleRunData)
Now I am trying to get data out ! I write
len (RunData)
and get 3
I try
RunData[0]
and get __main__.OneDataSet
I try
RunData[0].DFAngSagTang
and it doesnt like me !

Last hertel thank you
Reply
#4
One other mistake I missed. When you instantiate a class you must include parenthesis:
SingleRunData = OneDataSet()
Reply
#5
Thanks for that but "SingleRunData = OneDataSet" seems to work

But I am not able to get data back out ! I am trying though

Yeahhhhhhh. Thank you for the help @BrendanD. You were correct after all. if I instantiate , I need all my parameters !!!

RunData = []

for x in DefocusPlanes.index:
    FieldAngle = (FieldAngles[x-1])
    DefocusPlane = (df.iloc[(x+2):(x+2+DefocusPlanes[x]), 1:4])
    SingleRunData = OneDataSet(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
    RunData.append(SingleRunData)

for onedata in RunData:
    print (onedata.FieldAngle)
So I moved it to one line !
SingleRunData = OneDataSet(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
Now I am happy !!! thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  file handling Newbee question middlecope 2 745 Jan-18-2023, 03:09 PM
Last Post: middlecope
  Useless Newbee craigpusey 2 56,518 Mar-04-2020, 02:13 PM
Last Post: craigpusey
  Newbee - substing of column andrewjmdata 2 2,653 Feb-17-2018, 07:04 PM
Last Post: andrewjmdata

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020