-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13asPython.py
513 lines (398 loc) · 14.1 KB
/
13asPython.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# %% [markdown]
# # All Together Now
# ## Fingerprints for many wavelets, clustering, then sorting
#
# Combines the code from 10, 11, 12. Temporary in Jupyter Notebook, probably going to be converted to .py so it can run headless once it is trustworthy. Tested on 3 files, going to go to 16. Will do same stack, 2 cranes before and after tamping, 4 days of each set.
# %%
#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
#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
# Imports
#from keras.preprocessing import image
import keras.utils as image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
from sklearn.cluster import KMeans
import os, shutil, glob, os.path
from PIL import Image as pil_image
image.LOAD_TRUNCATED_IMAGES = True
model = VGG16(weights='imagenet', include_top=False)
# %% [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/home20/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/home20/dchendrickson01/'
folder = '/sciclone/scr10/dchendrickson01/Recordings2/'
imageFolder = '/sciclone/scr10/dchendrickson01/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]
# ## Set up variables
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
# %%
# Maunally chooseing before and after tamping for same track
files = ['221206 recording1.csv','221207 recording1.csv','221208 recording1.csv','221209 recording1.csv',
'221206 recording2.csv','221207 recording2.csv','221208 recording2.csv','221209 recording2.csv',
'230418 recording1.csv','230419 recording1.csv','230420 recording1.csv','230421 recording1.csv',
'230418 recording2.csv','230419 recording2.csv','230420 recording2.csv','230421 recording2.csv']
# %%
ClustersWanted = 11
scales= 100
skips = 1
minLength = 750
# %% [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[:-3]]
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(move)[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
# %%
import scipy as ss
def MakeSpectrogramImages(data, title, something=300, nperseg = 512, novrelap=256):
f, t, Szz = ss.signal.spectrogram(data,something,nperseg = nperseg, noverlap=novrelap)
fig = plt.figure(figsize=(8,3), dpi=800)
ax = plt.axes()
ax.set_axis_off()
plt.pcolormesh(t, f, Szz[0],cmap='gist_ncar')
plt.savefig(imageFolder+'spec/'+title+'.png',bbox_inches='tight', pad_inches=0)
return 1
# %%
def sortClusters(folder):
sampleName = folder.split('/')[-2]
print(sampleName)
if os.path.exists(imageFolder+'wvltSort/'+sampleName+'/') == False:
os.mkdir(imageFolder+'wvltSort/'+sampleName+'/')
# Variables
imdir = folder # DIR containing images
targetdir =imageFolder+'wvltSort/'+sampleName+'/' # DIR to copy clustered images to
number_clusters = ClustersWanted
# Loop over files and get features
filelist = glob.glob(os.path.join(imdir, '*.png'))
filelist.sort()
featurelist = []
for i, imagepath in enumerate(filelist):
#try:
#if i %100 == 0 : print(" Status: %s / %s" %(i, len(filelist)), end="\r")
img = image.load_img(imagepath, target_size=(224, 224))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
features = np.array(model.predict(img_data))
featurelist.append(features.flatten())
#except:
# continue
# Clustering
kmeans = KMeans(n_clusters=number_clusters, random_state=0).fit(np.array(featurelist), verbose=0)
# Copy images renamed by cluster
# Check if target dir exists
try:
os.makedirs(targetdir)
except OSError:
pass
# Copy with cluster name
print("\n")
for i, m in enumerate(kmeans.labels_):
try:
shutil.copy(filelist[i], targetdir + str(m) + "_" + filelist[i].split('/')[-1])
except:
continue
return 1
# %% [markdown]
# ## Process Files
#
# %%
LoopFiles = 3
loops = int(len(files) / LoopFiles)
if len(files)%LoopFiles != 0:
loops += 1
# %%
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)
for i in range(len(Results)):
SquelchSignal.append(Results[i][0])
RawData.append(np.matrix(Results[i][1]).T)
OrderedFileNames.append(Results[i][3])
print(k, np.shape(Results), (ti()-st)/60.0)
# %%
MoveData = Parallel(n_jobs=31)(delayed(SepreateMovements)(SquelchSignal[i], RawData[i], OrderedFileNames[i])
for i in range(len(RawData)))
# %%
Movements = []
GroupNames = []
for move in MoveData:
Movements.append(move[0])
GroupNames.append(move[1])
# %%
Moves=[]
for Groups in Movements:
for Move in Groups:
Moves.append(Move)
MoveNames = []
for Groups in GroupNames:
for name in Groups:
MoveNames.append(name)
# %%
del SquelchSignal
del RawData
del Movements
del GroupNames
del MoveData
del OrderedFileNames
# %%
longMove, MoveNumb = findMaxLength(Moves)
# %%
WvltFam = py.families()
Wvlts = []
for Fam in WvltFam:
temp = py.wavelist(Fam)
for wvlt in temp:
Wvlts.append(wvlt)
trys = Wvlts
trys.append('beta')
# %%
Moves, MoveNames = splitLong(Moves, longMove+1, minLength, MoveNames)
# %%
imageFolder += 'wvltTest/'
# %%
f = 0
#wvlt = 'beta'
for tri in trys:
if os.path.exists(imageFolder+tri+'/') == False:
os.mkdir(imageFolder+tri+'/')
FPimages = Parallel(n_jobs=37)(delayed(cf.makeMPFast)(Moves[i].T, wvlt, scales, skips,imageFolder+tri + '/Move '+ MoveNames[i], False) for i in range(len(Moves)))
##FPimages = Parallel(n_jobs=60)(delayed(cf.makeMPFast)(Moves[MoveNum].T, tri, scales, skips, imageFolder+'wvltTest/' + tri + '_LongMove') for tri in trys)
print('made it through wavelets')
# %%
if os.path.exists(imageFolder+'spec/') == False:
os.mkdir(imageFolder+'spec/')
FPimages = Parallel(n_jobs=37)(delayed(MakeSpectrogramImages)(Moves[i].T, 'Move '+ MoveNames[i], 300, 512, 505) for i in range(len(Moves)))
print('Made it through spectragram')
# %%
del FPimages
del Moves
del MoveNames
# %% [markdown]
# ## Started the Unsupervised Clustering
# %%
folders = glob.glob(imageFolder + 'wvltTest/*/')
# %%
sorting = Parallel(n_jobs=31)(delayed(sortClusters)(folder) for folder in folders)
# %%
del sorting
# %% [markdown]
# ## Now for comaprison of the results
# %%
# %%