-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspin_extractor.py
171 lines (130 loc) · 4.89 KB
/
spin_extractor.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import pandas, numpy, datetime
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit as fit
from matplotlib import rc
#rc('text', usetex=True)
#font = {'size' : 30}
#rc('font', **font)
def spin_down(t, pmax, t0, tau, d):
# = args[0], args[1], args[2]
return pmax*(1-numpy.exp(-(t-t0)/tau))+d
def spin_up(t, p0, t0, tau, d):
# = args[0], args[1], args[2]
return p0*numpy.exp(-(t-t0)/tau)+d
def file_fetcher(filename, t):
with open(filename, 'r') as f:
df = pandas.read_csv(f)
df[t] = pandas.to_datetime(df[t], format="%Y-%m-%d %H:%M:%S")
df = df.sort_values(t)
return df
def df_trimmer(df, Sd, Sm, Sy, sh, sm, fh,fm, t, ss=0, fs=0, Fd=None,Fm=None,Fy=None):
if Fd is None:
Fd = Sd
if Fm is None:
Fm = Sm
if Fy is None:
Fy = Sy
startdate = datetime.datetime(year=Sy, month=Sm, day=Sd, hour=sh, minute=sm, second=ss)
enddate = datetime.datetime(year=Fy, month=Fm, day=Fd, hour=fh, minute=fm, second=fs)
df = df[(df[t]>startdate)&(df[t]<enddate)].sort_values(t)
return df
def get_x_for_fit(trimmed, Sd, Sm, Sy, t):
"""
Get the x data (of type datetime) into a format that scipy can interpret (seconds from the start of the day)
"""
end_time = (max(trimmed[t].tolist())-datetime.datetime(year=Sy, month=Sm, day=Sd)).total_seconds()
start_time = (min(trimmed[t].tolist())-datetime.datetime(year=Sy, month=Sm, day=Sd)).total_seconds()
timestamp_list = trimmed[t].tolist()
timesteps = []
for index, val in enumerate(timestamp_list):
if index == len(timestamp_list)-1:
break
timesteps.append((timestamp_list[index+1]-val).total_seconds())
avg_timestep = numpy.mean(timesteps)
xdata_for_fit = numpy.arange(start_time, end_time, avg_timestep).tolist()
xdata_for_fit.append(xdata_for_fit[-1]+avg_timestep)
return xdata_for_fit
def previewdata_gui(*args, **kwargs):
filename, title, Sd, Sm, Sy, sh, sm, fh,fm, p,t = args
ss=kwargs.pop('ss', 0)
fs=kwargs.pop('fs',0)
Fd=kwargs.pop('Fd',None)
Fm=kwargs.pop('Fm',None)
Fy=kwargs.pop('Fy',None)
preview=kwargs.pop('preview',True)
bounds=kwargs.pop('bounds',[1, 1, 1, 1])
up=kwargs.pop('up',True)
datetimeaxis = kwargs.pop('datetimeaxis', True)
df = file_fetcher(filename, t)
trimmed = df_trimmer(df, Sd, Sm, Sy, sh, sm, fh,fm, t, ss=ss, fs=fs, Fd=Fd,Fm=Fm,Fy=Fy)
fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(8.5, 11), constrained_layout=True)
ax[0].set_title(title)
ax[1].set_xlabel("Datetime")
ax[0].set_xlabel("Datetime")
ax[0].plot(df[t], df[p], c='b', label=title)
ax[0].plot(trimmed[t], trimmed[p], c='red')
ax[1].plot(trimmed[t], trimmed[p], c='red')
ax[0].grid(True)
ax[1].grid(True)
return fig
def getupdown(*args, **kwargs):
filename, title, Sd, Sm, Sy, sh, sm, fh,fm, p,t = args
ss=kwargs.pop('ss', 0)
fs=kwargs.pop('fs',0)
Fd=kwargs.pop('Fd',None)
Fm=kwargs.pop('Fm',None)
Fy=kwargs.pop('Fy',None)
preview=kwargs.pop('preview',False)
bounds=kwargs.pop('bounds',[1, 1, 1, 1])
up=kwargs.pop('up',True)
datetimeaxis = kwargs.pop('datetimeaxis', False)
df = file_fetcher(filename, t)
trimmed = df_trimmer(df, Sd, Sm, Sy, sh, sm, fh,fm, t, ss=ss, fs=fs, Fd=Fd,Fm=Fm,Fy=Fy)
xforfit = get_x_for_fit(trimmed, Sd, Sm, Sy, t)
yforfit = trimmed[p].to_list()
xforplot = trimmed[t]
fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(8.5, 11), constrained_layout=True)
ax[0].set_title(title)
ax[0].set_xlabel("Datetime")
ax[0].plot(df[t], df[p], c='b', label=title)
ax[0].plot(trimmed[t], trimmed[p], c='red')
ax[0].grid(True)
if up:
fitvars, _ = fit(spin_up, xforfit, yforfit)#, bounds=[(0, 83500, 0),(numpy.inf, 100000, numpy.inf)])
print("\n\nSpin up:", "pmax*(1-numpy.exp(-(t-t0)/tau))+shift\n")
print(title)
print("pmax", "t0", "tau", 'shift')
for i, val in enumerate(["pmax:", "t0:", "tau:", "shift:"]):
print(fitvars[i],end='\t')
print('')
fy = spin_up(xforfit, fitvars[0], fitvars[1], fitvars[2], fitvars[3])
if datetimeaxis:
ax[1].plot(xforplot, fy, c='green', label="Spin Up")
else:
ax[1].plot(xforfit, fy, c='green', label="Spin Up")
else:
fitvars, _ = fit(spin_down, xforfit, yforfit)#, bounds=[(0, 83500, 0),(numpy.inf, 100000, numpy.inf)])
print("\n\nSpin down:", "p0*numpy.exp(-(t-t0)/tau)+shift\n")
print(title)
print(["p0", "t0", "tau", "shift"])
for i, val in enumerate(["p0:", "t0:", "tau:","shift:"]):
print(fitvars[i],end='\t')
fy = spin_up(xforfit, fitvars[0], fitvars[1], fitvars[2], fitvars[3])
if datetimeaxis:
ax[1].plot(xforplot, fy, c='green', label="Spin Down")
else:
ax[1].plot(xforfit, fy, c='green', label="Spin Down")
if datetimeaxis:
ax[1].set_xlabel("Datetime")
ax[1].scatter(trimmed[t], trimmed[p], c='red')
else:
ax[1].set_xlabel("Seconds since Midnight")
ax[1].scatter(xforfit, trimmed[p], c='red')
ax[1].plot(xforfit, fy, c='green', label=("Spin "+("Up" if up else "Down")))
ax[1].grid(True)
ax[1].legend(loc='best')
ax[0].legend(loc='best')
return fig
if __name__ == "__main__":
#Needs some work
pass