-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrentDataProcessing.py
298 lines (242 loc) · 8.99 KB
/
CurrentDataProcessing.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import numpy as np
import matplotlib.pyplot as plt
import csv
def calculate_mse(y,y_estimated):
return (np.sum((y-y_estimated)**2)/len(y))**0.5
def constant_speed(show = False):
"""
Very specific function for this project only
"""
files = ['0gSpeed.csv','50gDownSpeed.csv','50gUpSpeed.csv','100gDownSpeed.csv','100gUpSpeed.csv','200gDownSpeed.csv','200gUpSpeed.csv']
files = ['Data/Power/' + file for file in files]
x = [0]
y = [0]
for file in files:
with open(file, 'r') as f:
data = f.read()
x += [int(line.split(',')[0]) for line in data.split('\n') if line]
y += [round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
set_x = sorted(set(x))
x = np.array(x)
y = np.array(y)
# Creating average line and std
mean_data = {x: [] for x in set_x}
length = len(x)
for index in range(length):
mean_data[x[index]].append(y[index])
x_averaged = np.array(list(mean_data.keys()))
y_averaged = np.array([np.average(mean_data[key]) for key in mean_data])
y_std = np.array([np.std(mean_data[key]) for key in mean_data])
# Fitting degree 1 polynomial to average line
coeffs = np.polyfit(x,y,1)
y_estimate = np.polyval(coeffs,x)
slope = coeffs[0]
mse = calculate_mse(y,y_estimate)
if show:
plt.scatter(x,y,c='gray',label='Scatter')
plt.plot(x_averaged,y_averaged,label = 'Mean')
plt.plot(x_averaged,y_averaged+2*y_std,label = '+ 2std')
plt.plot(x_averaged,y_averaged-2*y_std,label = '- 2std')
plt.plot(x,y_estimate,label=f'Degree (Slope, MSE): {1} ({round(slope,3)},{round(mse,3)})')
plt.xticks(np.arange(0,set_x[-1]+1,1))
plt.xlabel('Volts')
plt.ylabel('Speed (m/s)')
plt.title('Speed v. Volts')
plt.legend()
plt.grid()
plt.show()
return set_x,coeffs
def all_downs(plot = False, show = False):
files = ['0gCurrent1.csv','50gDownCurrent1.csv','100gDownCurrent1.csv','200gDownCurrent1.csv']
files = ['Data/Power/' + file for file in files]
y_s = []
for file in files:
with open(file, 'r') as f:
data = f.read()
x = [0]+[int(line.split(',')[0]) for line in data.split('\n') if line]
y = [0]+[round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
y_s.append(y)
grams = file[11:file.index('g')+1]
if plot:
plt.plot(x,y,label = f'{grams} (Down)')
if show:
plt.xlabel('Volts')
plt.ylabel('Current (A)')
plt.xticks(np.arange(0,x[-1]+1,1))
plt.ylim(0, 0.04)
plt.title('Current Drawn while Lowering Weights')
plt.grid()
plt.legend()
plt.show()
return y_s
def all_ups(plot = False, show = False):
files = ['0gCurrent.csv','50gUpCurrent.csv','100gUpCurrent.csv','200gUpCurrent.csv']
files = ['Data/Power/' + file for file in files]
x = [0]
y = [0]
y_s = []
for file in files:
with open(file, 'r') as f:
data = f.read()
x = [0]+[int(line.split(',')[0]) for line in data.split('\n') if line]
y = [0]+[round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
y_s.append(y)
grams = file[11:file.index('g')+1]
if plot:
plt.plot(x,y,label = f'{grams} (Up)')
if show:
plt.xlabel('Volts')
plt.ylabel('Current (A)')
plt.xticks(np.arange(0,x[-1]+1,1))
plt.ylim(0, 0.04)
plt.title('Current Drawn while Raising Weights')
plt.grid()
plt.legend()
plt.show()
return y_s
def all_together():
all_downs(True)
all_ups(True)
plt.xlabel('Volts')
plt.ylabel('Current (A)')
plt.ylim(0, 0.04)
plt.xticks(np.arange(0,16,1))
plt.title('Current Drawn while Raising and Lowering Weights')
plt.legend()
plt.grid()
plt.show()
def power_in_downs(plot = False, show = False):
files = ['50gDownSpeed.csv','100gDownSpeed.csv','200gDownSpeed.csv']
files = ['Data/Power/' + file for file in files]
for i in range(len(files)):
with open(files[i], 'r') as f:
data = f.read()
x = [0]+[int(line.split(',')[0]) for line in data.split('\n') if line]
y = [0]+[round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
y = np.array(all_downs()[i+1][:11]) * np.array(x)
grams = files[i][11:files[i].index('g')+1]
print(y)
if plot:
plt.plot(x,y,label = f'{grams} (Down)')
if show:
plt.xlabel('Volts')
plt.ylabel('Power (W)')
plt.xticks(np.arange(0,x[-1]+1,1))
plt.ylim(0, 0.35)
plt.title('Power Drawn while Lowering Weights')
plt.grid()
plt.legend()
plt.show()
def power_in_ups(plot = False, show = False):
files = ['50gUpSpeed.csv','100gUpSpeed.csv','200gUpSpeed.csv']
files = ['Data/Power/' + file for file in files]
for i in range(len(files)):
with open(files[i], 'r') as f:
data = f.read()
x = [0]+[int(line.split(',')[0]) for line in data.split('\n') if line]
y = [0]+[round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
y = np.array(all_ups()[i+1][:11]) * np.array(x)
grams = files[i][11:files[i].index('g')+1]
if plot:
plt.plot(x,y,label = f'{grams} (Up)')
if show:
plt.xlabel('Volts')
plt.ylabel('Power (W)')
plt.xticks(np.arange(0,x[-1]+1,1))
plt.ylim(0, 0.35)
plt.title('Power Drawn while Raising Weights')
plt.grid()
plt.legend()
plt.show()
def power_in_all_together():
power_in_downs(True)
power_in_ups(True)
plt.xlabel('Volts')
plt.ylabel('Power (W)')
plt.xticks(np.arange(0,11,1))
plt.ylim(0, 0.35)
plt.title('Power Drawn while Raising and Lowering Weights')
plt.grid()
plt.legend()
plt.show()
def speed_fucntion():
return np.polyval(constant_speed()[1],constant_speed()[0])
def power_out_downs(plot = False,show = False,constant = False):
files = ['50gDownSpeed.csv','100gDownSpeed.csv','200gDownSpeed.csv']
files = ['Data/Power/' + file for file in files]
for i in range(len(files)):
with open(files[i], 'r') as f:
data = f.read()
x = [0]+[int(line.split(',')[0]) for line in data.split('\n') if line]
if constant:
y = [0] + speed_fucntion()
else:
y = [0]+[round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
mass = int(files[i][11:files[i].index('g')])/1000
y = np.array(y) * mass * 9.8
grams = files[i][11:files[i].index('g')+1]
if plot:
plt.plot(x,y,label = f'{grams} (Down)')
if show:
plt.xlabel('Volts')
plt.ylabel('Power (W)')
plt.xticks(np.arange(0,x[-1]+1,1))
plt.ylim(0, 0.04)
plt.title('Power Output while Lowering Weights')
plt.grid()
plt.legend()
plt.show()
def power_out_ups(plot = False,show = False,constant = False):
files = ['50gUpSpeed.csv','100gUpSpeed.csv','200gUpSpeed.csv']
files = ['Data/Power/' + file for file in files]
for i in range(len(files)):
with open(files[i], 'r') as f:
data = f.read()
x = [0]+[int(line.split(',')[0]) for line in data.split('\n') if line]
if constant:
y = [0] + speed_fucntion()
print(y)
else:
y = [0]+[round(float(line.split(',')[1]),4) for line in data.split('\n') if line]
mass = int(files[i][11:files[i].index('g')])/1000
y = np.array(y) * mass * 9.8
grams = files[i][11:files[i].index('g')+1]
if plot:
plt.plot(x,y,label = f'{grams} (Up)')
if show:
plt.xlabel('Volts')
plt.ylabel('Power (W)')
plt.xticks(np.arange(0,x[-1]+1,1))
plt.ylim(0, 0.04)
plt.title('Power Output while Raising Weights')
plt.grid()
plt.legend()
plt.show()
def power_out_all_together(constant = False):
power_out_downs(True,0,constant)
power_out_ups(True,0,constant)
plt.xlabel('Volts')
plt.ylabel('Power (W)')
plt.xticks(np.arange(0,11,1))
plt.ylim(0, 0.04)
plt.title('Power Output while Raising and Lowering Weights')
plt.grid()
plt.legend()
plt.show()
if __name__ == '__main__':
# for y in np.polyval(constant_speed()[1],constant_speed()[0]):
# with open('bullshit.csv', 'a', newline='') as csvfile:
# # creating a csv writer object
# csvwriter = csv.writer(csvfile)
# # writing the data rows
# csvwriter.writerow([y.item()])
# all_downs(True,True)
# all_ups(True,True)
# all_together()
# power_in_downs(True,True)
# power_in_ups(True,True)
# power_in_all_together()
# power_out_downs(True,True)
# power_out_ups(True,True)
power_out_all_together()
pass