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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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:
1
def AddDefocusPlane(self, DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time):
Reply
#3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
1
len (RunData)
and get 3
I try
1
RunData[0]
and get __main__.OneDataSet
I try
1
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:
1
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 !!!

1
2
3
4
5
6
7
8
9
10
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 !
1
SingleRunData = OneDataSet(DefocusPlane, FieldAngle, DefocusFrequency, Title, local_time)
Now I am happy !!! thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  bytearray weirdness or newbee newness Curbie 3 1,082 Sep-18-2024, 01:25 AM
Last Post: deanhystad
  file handling Newbee question middlecope 2 1,576 Jan-18-2023, 03:09 PM
Last Post: middlecope
  Useless Newbee craigpusey 2 98,172 Mar-04-2020, 02:13 PM
Last Post: craigpusey
  Newbee - substing of column andrewjmdata 2 3,306 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