-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10bSeg_DataPrep.py
489 lines (395 loc) · 13.6 KB
/
10bSeg_DataPrep.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# %% [markdown]
# # Look at accelerometer data
#
# Finding Zero velocity times by rail axis acceleration noise levels, making summary statistics for the noise levels across the whole day files. Spot check graphs to see what works
# %%
#Standard Header used on the projects
#first the major packages used for math and graphing
import numpy as np
import matplotlib.pyplot as plt
from cycler import cycler
import scipy.special as sp
import pickle
#Custome graph format style sheet
#plt.style.use('Prospectus.mplstyle')
#If being run by a seperate file, use the seperate file's graph format and saving paramaeters
#otherwise set what is needed
if not 'Saving' in locals():
Saving = False
if not 'Titles' in locals():
Titles = True
if not 'Ledgends' in locals():
Ledgends = True
if not 'FFormat' in locals():
FFormat = '.png'
#Standard cycle to make black and white images and dashed and line styles
default_cycler = (cycler('color', ['0.00', '0.40', '0.60', '0.70']) + cycler(linestyle=['-', '-', '-', '-']))
plt.rc('axes', prop_cycle=default_cycler)
my_cmap = plt.get_cmap('gray')
# %%
#Extra Headers:
import os as os
import pywt as py
import statistics as st
import os as os
import random
import multiprocessing
from joblib import Parallel, delayed
import platform
from time import time as ti
# %%
import CoreFunctions as cf
from skimage.restoration import denoise_wavelet
# %%
from sklearn.model_selection import train_test_split
import dask.dataframe as dd
# %%
from keras.layers import LSTM, Dense, RepeatVector, TimeDistributed, Masking
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
import tensorflow as tf
# %% [markdown]
# ## Choosing Platform
# Working is beinging conducted on several computers, and author needs to be able to run code on all without rewriting.. This segment of determines which computer is being used, and sets the directories accordingly.
# %%
HostName = platform.node()
if HostName == "Server":
Computer = "Desktop"
elif HostName[-6:] == 'wm.edu':
Computer = "SciClone"
elif HostName == "SchoolLaptop":
Computer = "LinLap"
elif HostName == "WTC-TAB-512":
Computer = "PortLap"
else:
Computer = "WinLap"
# %%
if Computer == "SciClone":
location = '/sciclone/home/dchendrickson01/image/'
elif Computer == "WinLap":
location = 'C:\\Data\\'
elif Computer == "Desktop":
location = "E:\\Backups\\Dan\\CraneData\\"
elif Computer == "LinLap":
location = '/home/dan/Output/'
elif Computer == 'PortLap':
location = 'C:\\users\\dhendrickson\\Desktop\\AccelData\\'
# %%
if Computer == "SciClone":
rootfolder = '/sciclone/home/dchendrickson01/'
folder = '/scratch/Recordings2/'
imageFolder = '/scratch/Move3Dprint/'
elif Computer == "Desktop":
rootfolder = location
folder = rootfolder + "Recordings2\\"
elif Computer =="WinLap":
rootfolder = location
folder = rootfolder + "Recordings2\\"
elif Computer == "LinLap":
rootfolder = '/home/dan/Data/'
folder = rootfolder + 'Recordings2/'
elif Computer =='PortLap':
rootfolder = location
folder = rootfolder + 'Recordings2\\'
# %% [markdown]
# ## Global Variables
# %%
Saving = False
location = folder
Titles = True
Ledgends = True
f = 0
# %%
files = ['230418 recording1.csv','230419 recording1.csv','230420 recording1.csv','230421 recording1.csv',
'230418 recording2.csv','230419 recording2.csv','230420 recording2.csv','230421 recording2.csv',
'230425 recording1.csv','230425 recording2.csv','230426 recording2.csv','230427 recording2.csv',
'230428 recording2.csv','230509 recording1.csv','230510 recording1.csv','230511 recording1.csv']
# %%
BeforeTamping = ['221206 recording1.csv','221207 recording1.csv','221208 recording1.csv','221209 recording1.csv',
'221206 recording2.csv','221207 recording2.csv','221208 recording2.csv','221209 recording2.csv']
# %% [markdown]
# ## Project Specific Functions
# %%
def RollingStdDev(RawData, SmoothData, RollSize = 25):
StdDevs = []
for i in range(RollSize):
Diffs = RawData[0:i+1]-SmoothData[0:i+1]
Sqs = Diffs * Diffs
Var = sum(Sqs) / (i+1)
StdDev = np.sqrt(Var)
StdDevs.append(StdDev)
for i in range(len(RawData)-RollSize-1):
j = i + RollSize
Diffs = RawData[i:j]-SmoothData[i:j]
Sqs = Diffs * Diffs
Var = sum(Sqs) / RollSize
StdDev = np.sqrt(Var)
StdDevs.append(StdDev)
return StdDevs
def RollingSum(Data, Length = 100):
RollSumStdDev = []
for i in range(Length):
RollSumStdDev.append(sum(Data[0:i+1]))
for i in range(len(Data) - Length):
RollSumStdDev.append(sum(Data[i:i+Length]))
return RollSumStdDev
def SquelchPattern(DataSet, StallRange = 5000, SquelchLevel = 0.02):
SquelchSignal = np.ones(len(DataSet))
for i in range(len(DataSet)-2*StallRange):
if np.average(DataSet[i:i+StallRange]) < SquelchLevel:
SquelchSignal[i+StallRange]=0
return SquelchSignal
def getVelocity(Acceleration, Timestamps = 0.003, Squelch = [], corrected = 0):
velocity = np.zeros(len(Acceleration))
Acceleration -= np.average(Acceleration)
if len(Timestamps) == 1:
dTime = np.ones(len(Acceleration),dtype=float) * Timestamps
elif len(Timestamps) == len(Acceleration):
dTime = np.zeros(len(Timestamps), dtype=float)
dTime[0]=1
for i in range(len(Timestamps)-1):
j = i+1
if Timestamps[j] > Timestamps[i]:
dTime[j]=Timestamps[j]-Timestamps[i]
else:
dTime[j]=Timestamps[j]-Timestamps[i]+10000.0
dTime /= 10000.0
velocity[0] = Acceleration[0] * (dTime[0])
for i in range(len(Acceleration)-1):
j = i + 1
if corrected ==2:
if Squelch[j]==0:
velocity[j]=0
else:
velocity[j] = velocity[i] + Acceleration[j] * dTime[j]
else:
velocity[j] = velocity[i] + Acceleration[j] * dTime[j]
if corrected == 1:
PointVairance = velocity[-1:] / len(velocity)
for i in range(len(velocity)):
velocity[i] -= PointVairance * i
velocity *= 9.81
return velocity
def MakeDTs(Seconds, Miliseconds):
dts = np.zeros(len(Miliseconds), dtype=float)
dts[0]=1
for i in range(len(MiliSeconds)-1):
j = i+1
if Seconds[j]==Seconds[i]:
dts[j]=Miliseconds[j]-Miliseconds[i]
else:
dts[j]=Miliseconds[j]-Miliseconds[i]+1000
dts /= 10000
return dts
# %%
#Smooth = cf.Smoothing(ODataSet[:,3],2) #,50)
def DeviationVelocity(file):
if file[-3:] =='csv':
ODataSet = np.genfromtxt(open(folder+file,'r'), delimiter=',',skip_header=0,missing_values=0,invalid_raise=False)
SmoothX = denoise_wavelet(ODataSet[:,3], method='VisuShrink', mode='soft', wavelet_levels=3, wavelet='sym2', rescale_sigma='True')
SmoothY = denoise_wavelet(ODataSet[:,4], method='VisuShrink', mode='soft', wavelet_levels=3, wavelet='sym2', rescale_sigma='True')
SmoothZ = denoise_wavelet(ODataSet[:,5], method='VisuShrink', mode='soft', wavelet_levels=3, wavelet='sym2', rescale_sigma='True')
SmoothX -= np.average(SmoothX)
SmoothY -= np.average(SmoothY)
SmoothZ -= np.average(SmoothZ)
StdDevsX = RollingStdDev(ODataSet[:,3],SmoothX)
StdDevsX.append(0)
StdDevsX = np.asarray(StdDevsX)
SmoothDevX = denoise_wavelet(StdDevsX, method='VisuShrink', mode='soft', wavelet_levels=3, wavelet='sym2', rescale_sigma='True')
SquelchSignal = SquelchPattern(SmoothDevX, 2000, 0.03)
#Velocity = getVelocity(ODataSet[:,3], ODataSet[:,2],SquelchSignal, 2)
#Velocity = np.asarray(Velocity)
MoveMatrix = np.matrix([SmoothX, SmoothY, SmoothZ])
return [SquelchSignal,MoveMatrix,SmoothDevX,file[:-4]]
else:
pass
# %%
def SepreateMovements(SquelchSignal, RawData, FileName):
Moves= []
MoveNames = []
Move = np.zeros((1,3), dtype=float)
i = 0
for j in range(len(SquelchSignal)-1):
if SquelchSignal[j] == 1:
#try:
Move = np.concatenate((Move, RawData[j,:]), axis=0)
#except:
# print(j)
if SquelchSignal[j+1] == 0:
#Move = np.matrix(Move)
Moves.append(Move)
MoveNames.append(FileName + str(i).zfill(3))
i+=1
Move = np.zeros((1,3), dtype=float)
#Move[0,2]=0
Moves.append(Move)
MoveNames.append(FileName + str(i).zfill(3))
return (Moves, MoveNames)
# %%
def splitLong(Moves, maxLength = 4000, minLength = 1000, MoveNames = []):
if len(MoveNames) <=1:
MoveNames = ['null' for x in range(len(Moves))]
Xmoves = []
Xnames = []
for i in range(len(Moves)):
if np.shape(Moves[i])[0] > maxLength:
Xmoves.append(Moves[i][:int(len(Moves[i])/2),:])
Xnames.append(MoveNames[i] + 'a')
Xmoves.append(Moves[i][int(len(Moves[i])/2):,:])
Xnames.append(MoveNames[i] + 'b')
else:
if np.shape(Moves[i])[0] < minLength:
pass
else:
Xmoves.append(Moves[i])
Xnames.append(MoveNames[i])
return Xmoves, Xnames
def findMaxLength(Moves):
maxLength = 0
LongMove = 0
for i in range(len(Moves)):
if np.shape(Moves[i])[0] > maxLength:
maxLength = np.shape(Moves[i])[0]
LongMove = i
return maxLength, LongMove
def findMinLength(Moves):
minLength = 9999999
SmallMove = 0
for i in range(len(Moves)):
if np.shape(Moves[i])[0] < minLength:
minLength = np.shape(Moves[i])[0]
SmallMove = i
return minLength, SmallMove
# %% [markdown]
# ## Process Files
# %%
LoopFiles = 16
loops = int(len(files) / LoopFiles)
if len(files)%LoopFiles != 0:
loops += 1
print('files', len(files), loops)
# %%
SquelchSignal = []
RawData=[]
OrderedFileNames=[]
# %%
st = ti()
for k in range(loops):
if k == loops -1:
tfiles = files[k*LoopFiles:]
else:
tfiles = files[k*LoopFiles:(k+1)*LoopFiles]
Results = Parallel(n_jobs=LoopFiles)(delayed(DeviationVelocity)(file) for file in tfiles)
#Results =[]
#for file in tfiles:
# Results.append(DeviationVelocity(file))
# print(file, (ti()-st)/60.0)
for i in range(len(Results)):
SquelchSignal.append(Results[i][0])
RawData.append(Results[i][1])
OrderedFileNames.append(Results[i][3])
print(k, len(Results), (ti()-st)/60.0)
# %%
print('got data', len(RawData), len(SquelchSignal), np.shape(RawData[0]))
# %%
#MoveData = Parallel(n_jobs=31)(delayed(SepreateMovements)(SquelchSignal[i], RawData[i], OrderedFileNames[i])
# for i in range(len(RawData)))
MoveData = []
Moves = []
MoveNames = []
for i in range(len(RawData)):
temp = SepreateMovements(SquelchSignal[i], RawData[i].T, OrderedFileNames[i])
print(i, len(temp), len(temp[0]), len(temp[1]))
for j in range(len(temp[0])):
Moves.append(temp[0][j])
MoveNames.append(temp[1][j])
#MoveData.append(temp)
print('Moves Seperated', len(Moves), (ti()-st)/60.0)
# %%
#Movements = []
#GroupNames = []
#for move in MoveData:
# Movements.append(move[0])
# GroupNames.append(move[1])
#print('Move and Name Sepreated', len(Movements), (ti()-st)/60.0)
# %%
#Moves=[]
#for Groups in Movements:
# for Move in Groups:
# Moves.append(np.asarray(Move).astype('float32'))
#Moves = np.asarray(Moves)
#MoveNames = []
#for Groups in GroupNames:
# for name in Groups:
# MoveNames.append(name)
# %%
#print('made moves')
del SquelchSignal
del RawData
#del Movements
#del GroupNames
#del MoveData
#del OrderedFileNames
# %%
longMove, MoveNumb = findMaxLength(Moves)
# %%
minLength = 750
# %%
Moves, MoveNames = splitLong(Moves, longMove+1, minLength, MoveNames)
print('Post split length', len(Moves), np.shape(Moves[0]))
# %%
#padding moves. Not needed, need sequences, not moves
#Moves2 = []
#for move in Moves:
# if np.shape(move)[0] < longMove:
# padding = np.zeros((longMove-np.shape(move)[0], 3))
# tempMove = np.concatenate((move, padding), axis=0)
# Moves2.append(tempMove)
# else:
# Moves2.append(move)
#Moves = Moves2
#del Moves2
# %% [markdown]
# ## Try LSTM Stuff
# %% [markdown]
# https://machinelearningmastery.com/how-to-develop-lstm-models-for-time-series-forecasting/
# %%
TimeSteps = 250
Features = np.shape(Moves[0])[1]
# %%
# split a multivariate sequence into samples
def split_sequences(sequences, n_steps):
X, y = list(), list()
for i in range(len(sequences)):
# find the end of this pattern
end_ix = i + n_steps
# check if we are beyond the dataset
if end_ix > len(sequences)-1:
break
# gather input and output parts of the pattern
seq_x, seq_y = sequences[i:end_ix, :], sequences[end_ix, :]
X.append(seq_x)
y.append(seq_y)
return np.array(X), np.array(y)
# %%
Sequences = []
Outputs = []
for move in Moves:
Seq, Out = split_sequences(move,TimeSteps)
Sequences.append(Seq)
Outputs.append(Out)
# %%
MoveSegments = []
for seq in Sequences:
for mv in seq:
MoveSegments.append(mv)
NextDataPoint = []
for out in Outputs:
for pt in out:
NextDataPoint.append(pt)
# %%
print('data split', len(MoveSegments))
print('Shape next data point', np.shape(NextDataPoint))
file=open('/scratch/PrepAccel/Data-20240307-16files-250pts.p','wb')
pickle.dump([MoveSegments, NextDataPoint],file)
file.close()