-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmgData.py
878 lines (739 loc) · 28.1 KB
/
EmgData.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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
"""
EmgData: class to load, preprocess,and plot EMG data
(c) Felipe Moreira Ramos - Tohoku University, Sendai, Japan -
Date: 20180910
"""
import warnings
import matplotlib.pyplot as plt
from scipy import signal
from scipy import interpolate
from SizBox import *
from Traces import Traces
from Data import Data
class EmgData:
class Obj:
data = None
time = None
trialId = None
chlabels = None
obj = None
def __init__(self, data=None, trialId=None, chlabels=None):
if data is None:
self.obj = None
return
# if not type(data) is list or not 'emg' in data[0] or not 'emgtime' in data[0]:
# warnings.warn('Data input must be a structure with .emg and .emgtime fields',stacklevel=2)
# return
ntrial = len(data)
self.obj = np.array([self.Obj() for i in range(ntrial)])
for i in range(ntrial):
print("emgtime", data[i].emgtime.size)
print("emg", data[i].emg.shape)
if data[i].emg.shape[1] != data[i].emgtime.size:
warnings.warn(
"data[i].emg.shape[1] must be equal to data[i].emgtime.size)"
% (i, i),
stacklevel=2,
)
data[i].emgtime = np.array([j for j in range(data[i].emg.shape[1])])
self.obj[i].data = data[i].emg
self.obj[i].time = data[i].emgtime
if not trialId is None and len(trialId) == ntrial:
self.obj[i].trialId = trialId[i]
else:
self.obj[i].trialId = i + 1
nch = data[i].emg.shape[0]
if not chlabels is None and len(chlabels) == nch:
self.obj[i].chlabels = chlabels
else:
self.obj[i].chlabels = np.array(
["EMG%02i" % (j + 1) for j in range(nch)]
)
def __getitem__(self, key):
return self.obj[key]
def __setitem__(self, key, item):
# self.obj[key] = item
self.obj[key].data = item.data
self.obj[key].time = item.time
self.obj[key].trialId = item.trialId
self.obj[key].chlabels = item.chlabels
def __len__(self):
return self.obj.shape[0]
"""
filter EMG data
type par notes
=============================================================
'fir1' [N Wn] low pass finite impulse response filter
'fir1' [N W1 W2] band pass finite impulse response filter
'butter' [N Wn] low pass Nth order Butterworth filter
'butter' [N W1 W2] band pass 2Nth order Butterworth filter
'rectify' [] rectification
'rectify' [n] rectification and resampling
'submean' [] rectification after mean subtraction
'resample' [N WN n] resample
'rms' [N n] root mean square and resample
'average' [N n] moving average and resample
'high' [N W2] high pass FIR1 on NON-rectified EMGs
'notch' [W0 Q] IIR notch filer with notch freqency (W0*Fs/2)
quality factor Q (Q = W0/bw)
'cliptozero' clip negative values to zero
"""
def filter(self, opt=None):
# set options or use defaults
if opt is None:
defopt = self.FilterOpt().getDefFilterOpt()
else:
defopt = self.FilterOpt()
defopt.type = opt.type
defopt.par = opt.par
opt = defopt
nemg = self.obj.shape[0]
for i in range(nemg):
nch, nsamp = self.obj[i].data.shape
if opt.type == "fir1":
if opt.par.size < 2:
warnings.warn("fir1 type requires two parms (N,Wn)", stacklevel=2)
return
N = opt.par[0].astype(int)
if opt.par.size == 2:
Wn = opt.par[1]
pz = True
else:
Wn = opt.par[1:3] # (2:3) in Matlab
pz = False
if N > (3 * nsamp):
warnings.warn(
"filter order too large for given data!", stacklevel=2
)
return
B = signal.firwin(N, Wn, pass_zero=pz)
A = np.array([1])
filtereddata = signal.filtfilt(B, A, np.abs(self.obj[i].data), axis=1)
self.obj[i].data = filtereddata
elif opt.type == "butter":
if opt.par.size < 2:
warnings.warn("butter type requires two parms (N,Wn)", stacklevel=2)
return
N = opt.par[0].astype(int)
if opt.par.size == 2:
Wn = opt.par[1]
b = "lowpass"
else:
Wn = opt.par[1:3] # (2:3) in Matlab
b = "bandpass"
if N > (3 * nsamp):
warnings.warn(
"filter order too large for given data!", stacklevel=2
)
return
# Note N is order!!
B, A = signal.butter(N, Wn, btype=b)
filtereddata = signal.filtfilt(B, A, np.abs(self.obj[i].data), axis=1)
self.obj[i].data = filtereddata
elif opt.type == "rectify":
print("rectifyfilter not implemented")
elif opt.type == "submean":
print("submean filter not implemented")
elif opt.type == "resample":
print("resample filter not implemented")
elif opt.type == "rms":
print("rms filter not implemented")
elif opt.type == "average":
print("average filter not implemented")
elif opt.type == "high":
print("high filter not implemented")
elif opt.type == "notch ":
print("notch filter not implemented")
elif opt.type == "cliptozero":
print("cliptozero filter not implemented")
class FilterOpt:
type = None
par = None
def __init__(self):
self.type = "rectify"
self.par = np.array([])
def getDefFilterOpt(self):
self.type = "rectify"
self.par = np.array([])
return self
def selectCh(self, child):
print("selectCh not implemented")
"""
average emgs over time interval dt
e = mean(e,dt) computes mean of adjacent intervals of duration dt [s]
OR
e = mean(e,t_range) compute mean values between t_range(1) and
t_range(2); m -> [nch,nemg]
"""
def mean(self, dt=None, index=None):
if dt is None:
dt = self.timerange() # 0->dt, 1->t_range
if index is None:
nemg = range(self.obj.shape[0])
else:
nemg = [index]
if dt.size == 1: # dt
for i in nemg:
nch, nsamptot = self.obj[i].data.shape
t_sample = self.tsamp(i) # sampling interval
nsamp = np.round(dt[0] / t_sample).astype(
int
) # number of original samples to sum for each integrated sample
nintervals = np.floor(nsamptot / nsamp).astype(
int
) # number of integrated intervals
datatemp = np.zeros((nch, nintervals)) # allocate space
for k in range(nintervals):
ind = np.array([nsamp * k + n for n in range(nsamp)])
datatemp[:, k] = np.mean(self.obj[i].data[:, ind], 1)
self.obj[i].data = datatemp
ii = np.arange(0, nsamp * nintervals, nsamp)
self.obj[i].time = (
self.obj[i].time[ii] + t_sample * (nsamp - 1) / 2
) # if nsamp==1 -> e.time does not change
# return data,time
elif dt.size == 2: # mean over t_range
# if dt.shape[0]==nemg:
# t_range = dt;
# else:
# t_range = np.ones((nemg,1))*dt[0,:];
t_range = dt
nch = self.obj[0].data.shape[0]
m = np.zeros((nch, len(nemg)))
j = 0
for i in nemg:
ind = np.where(
(self.obj[i].time >= t_range[0]) & (self.obj[i].time <= t_range[1])
)
m[:, j] = np.mean(self.obj[i].data[:, ind[0]], 1)
j = j + 1
return m
else:
warnings.warn("Invalid Input (dt)", stacklevel=2)
return
"""
subtract tonic or baseline level to EMG data
"""
def subtract(self, t=None):
# set options or use defaults
if t is None:
defopt = self.SubtractOpt().getDefSubtractOpt()
else:
defopt = self.SubtractOpt()
defopt.t_onset = t[0]
defopt.t_end = t[1]
opt = defopt
# check options
nemg = self.obj.shape[0]
if opt.type == "tonic":
if not opt.t_onset.size == nemg:
warnings.warn(
"onset times must be provided for tonic subtraction", stacklevel=2
)
return
# loop on trials
for i in range(nemg):
if opt.type == "tonic":
t_on = opt.t_onset[i]
t_off = opt.t_end[i]
val_on = self.mean(dt=opt.t_pre + t_on, index=i)
val_off = self.mean(dt=opt.t_post + t_off, index=i)
datatonic = np.zeros(self.obj[i].data.shape)
time = self.obj[i].time
ntime = self.obj[i].time.size
ind_on = np.argmin(np.abs(time - t_on))
ind_off = np.argmin(np.abs(time - t_off))
datatonic[:, np.arange(0, ind_on)] = val_on * np.ones((1, ind_on))
for j in range(self.obj[i].data.shape[0]): # for all channels
datatonic[j, np.arange(ind_on, ind_off + 1)] = np.interp(
list(range(ind_on, ind_off + 1)),
[ind_on, ind_off],
[val_on[j][0], val_off[j][0]],
)
datatonic[:, np.arange(ind_off + 1, ntime)] = val_off * np.ones(
(1, ntime - ind_off - 1)
)
self.obj[i].data = self.obj[i].data - datatonic
if opt.clip:
self.obj[i].data = np.multiply(
self.obj[i].data, self.obj[i].data > 0
) # clip to zero any negative value
elif opt.type == "mean":
self.obj[i].data = (
self.obj[i].data - np.array([np.mean(self.obj[i].data, 1)]).T
) # * np.ones((1,self.obj[i].data.shape[1]))
class SubtractOpt:
type = None
t_pre = None
t_post = None
t_onset = None
t_end = None
clip = None
def __init__(self):
self.type = "tonic" # subtract tonic activity to get phasic EMG data
self.t_pre = np.array(
[-0.4, -0.2]
) # interval before onset for initial level
self.t_post = np.array([0.2, 0.4]) # interval after end for final level
self.t_onset = np.array([])
self.t_end = np.array([])
self.clip = 1 # clip to zero after subtraction
def getDefSubtractOpt(self):
self.type = "tonic" # subtract tonic activity to get phasic EMG data
self.t_pre = np.array(
[-0.4, -0.2]
) # interval before onset for initial level
self.t_post = np.array([0.2, 0.4]) # interval after end for final level
self.t_onset = np.array([])
self.t_end = np.array([])
self.clip = 1 # clip to zero after subtraction
return self
"""
average EMG data across trials
"""
def average(self, grOpt=None):
# set options or use defaults
if grOpt is None:
defopt = self.AverageOpt().getDefAverageOpt(self.obj)
else:
defopt = self.AverageOpt()
defopt.gr = grOpt[0]
defopt.tref = grOpt[1]
defopt.trange = grOpt[2]
opt = defopt
# loop on groups
gr = opt.gr
tref = opt.tref
trange = opt.trange
ngr = len(gr)
ts = self.tsamp(gr[0][0])
nch = self.obj[0].data.shape[0] # number of channels
tav = np.arange(trange[0], trange[1] + ts, ts) # times of averaged emgs
nsampav = tav.size
eavtemp = np.array([self.Obj() for i in range(ngr)])
for i in range(ngr):
datatemp = np.zeros((nch, nsampav))
ndata = np.zeros((1, nsampav))
chlabels = self.obj[gr[i][0]].chlabels
for j in range(len(gr[i])):
jj = gr[i][j]
time = self.obj[jj].time - tref[jj]
ind = np.where((time >= tav[0]) & (time <= tav[-1]))[
0
] # find samples contained into chosen interval
if ind.size:
if nsampav == ind.size:
indav = np.arange(nsampav)
else:
im = np.argmin(np.abs(tav - time[ind[0]]))
indav = np.arange(ind.size) + im
data_j = self.obj[jj].data[:, ind]
datatemp[:, indav] = datatemp[:, indav] + data_j
ndata[0, indav] = ndata[0, indav] + 1
# mean
datam = np.divide(datatemp, np.dot(np.ones((nch, 1)), ndata + (ndata == 0)))
eavtemp[i].data = datam
eavtemp[i].time = tav
eavtemp[i].trialId = i + 1
eavtemp[i].chlabels = chlabels
self.obj = eavtemp
class AverageOpt:
gr = None
tref = None
trange = None
def __init__(self):
self.gr = []
self.tref = np.array([])
self.trange = np.array([])
def getDefAverageOpt(self, obj):
nemg = obj.size()
self.gr = []
self.gr.append(range(nemg))
self.tref = np.zeros((1, nemg))
tr = self.timerange(obj)
trc = np.array([np.max(tr[:, 0]), np.min(tr[:, 1])])
if np.diff(trc) > 0:
self.trange = trc
else:
self.trange = np.array([])
"""
get time range of EMG data of each trial
"""
def timerange(self, obj):
nemg = obj.size
tr = []
for i in range(nemg):
tr.append(obj[i].time[[0, -1]])
return np.array(tr)
"""
normalize data in emg amplitude
type action
-------------------------------------------------------------
0 use normdata [nch,1] for normalization of each channel
1 normalize to max of any channel
2 normalize each channel to max in that channel
"""
def normalize(self, opt=None):
# set options or use defaults
if opt is None:
defopt = self.NormalizeOpt().getDefNormalizeOpt(self.obj)
else:
defopt = self.NormalizeOpt()
defopt.type = opt.type
defopt.isect = opt.isect
defopt.normdata = np.array([])
opt = defopt
nemg = self.obj.shape[0]
opt.isect = np.intersect1d(np.arange(nemg), opt.isect)
if not opt.isect.size: # is empty
warnings.warn("empty isect, using all sections", stacklevel=2)
opt.isect = np.arange(nemg)
if opt.type == 1:
normdata = np.max(self.max(self.obj[opt.isect]))
elif opt.type == 2:
normdata = self.max(self.obj[opt.isect])
for i in range(nemg):
nch, nsamp = self.obj[i].data.shape
if opt.type == 0:
if not opt.normdata.shape[0] == nch:
warnings.warn("normdata missing or not valid!", stacklevel=2)
return
self.obj[i].data = np.divide(
self.obj[i].data, np.dot(opt.normdata, np.ones((1, nsamp)))
)
elif opt.type == 1:
self.obj[i].data = self.obj[i].data / normdata
elif opt.type == 2:
self.obj[i].data = np.divide(
self.obj[i].data, np.dot(normdata, np.ones((1, nsamp)))
)
# return enorm
class NormalizeOpt:
type = None
isect = None
normadata = None
def __init__(self):
self.type = 2
self.isect = np.array([])
self.normdata = np.array([])
def getNormalizeOpt(self, obj):
nemg = obj.shape[0]
self.type = 2 # max absolute value of each channel
self.isect = np.arange(nemg) # sections to use for computing max
self.normdata = np.array([])
"""
get data matrix
"""
def getData(self, findtype=None, isect=None, ssect=None):
nemg = self.obj.shape[0]
if findtype is None:
findtype = "spatial"
if isect is None:
isect = np.arange(nemg)
isect = np.intersect1d(np.arange(nemg), isect)
nsect = isect.shape[0]
if ssect is None or not ssect.size == nsect:
ssect = np.ones(nsect)
nch = self.obj[isect[0]].data.shape[0]
if findtype == "temporal" or findtype == "spatiotemporal":
# rows are channels, columns are time samples x trials
data = np.zeros((nch, self.getNsamp(isect, ssect)))
inds = []
isamp = 0
for ii in range(nsect):
i = isect[ii]
scale = ssect[ii]
dsamp = self.obj[i].time.size
nsamp = int(dsamp * scale)
jj = isamp + np.arange(nsamp)
dataInterval = np.arange(dsamp)
dataScaledInterval = np.linspace(0, dsamp - 1, nsamp)
interpFunction = interpolate.interp1d(dataInterval, self.obj[i].data)
data[:, jj] = interpFunction(dataScaledInterval)
inds.append(jj)
isamp = isamp + nsamp
# inds = np.array(inds)
return data, inds, nch
"""
get number of samples
"""
def getNsamp(self, isect=None, ssect=None):
nemg = self.obj.shape[0]
if isect is None:
isect = np.arange(nemg)
isect = np.intersect1d(np.arange(nemg), isect)
nsect = isect.shape[0]
if ssect is None:
ssect = isect.shape[0]
nsamp = 0
for ii in range(nsect):
i = isect[ii]
scale = ssect[ii]
nsamp = nsamp + int(self.obj[i].time.size * scale)
return nsamp
"""
plot EMG data
"""
def plot(self, ind, opt=None):
# e = self.obj[ind].flatten() #convert ndarray to array
# get only the elements of the indexes in "ind"
e = self.obj[ind]
if opt is None or not isinstance(opt, EmgData.PlotOpt):
opt = self.PlotOpt().getDefPlotOpt(e)
#
# Figure and axes
#
nsect = opt.isect.size
"""@TODO
ishandle(axes)
"""
ha = []
if not opt.axes: # is empty
if not opt.figure: # is empty
hf = plt.figure()
else:
hf = plt.figure(num=opt.figure[0])
if opt.overlap:
for i in range(opt.pos.shape[0]):
hsect = hf.add_subplot(111, label="%d" % (i + 1))
hsect.set_position(opt.pos[i])
ha.append(hsect)
else:
dur = self.duration(e[opt.isect])
for i in range(nsect):
pos_i = SizBox(
opt.pos, np.matrix([1]), dur, 0, i, np.array([opt.spacing, 0])
)
hsect = hf.add_subplot(111, label="%d" % (i + 1))
hsect.set_position(pos_i)
ha.append(hsect)
else:
hf = opt.axes[0].get_figure()
ha = opt.axes
hf.patch.set_facecolor((0.9, 0.9, 0.9))
if opt.ylim.size < 2:
yl = self.emglim(e[opt.isect])
else:
yl = opt.ylim
if opt.xlim.size < 2:
xl = self.timelim(e[opt.isect])
#
# loop on trials
#
for ii in range(nsect):
i = opt.isect[ii]
tt_data = e[i].data[opt.emgsel]
tt_time = e[i].time
if opt.tref and opt.tref.size == nsect:
tt_time = tt_time - opt.tref[i]
t = Traces(tt_data, tt_time)
if ii == 0:
t.chlabels = e[i].chlabels[opt.emgsel]
if opt.usetitle:
if not opt.emgtitle or not opt.emgtitle[i]:
label = "%i" % (e[i].trialId)
else:
label = opt.emgtitle[i] + " (%i)" % (e[i].trialId)
ha[i].set_title(label)
if opt.xlim: # not empty
if opt.xlim.shape[0] == nsect:
xl = opt.xlim[i]
else:
xl = opt.xlim[0]
t.opt.autotrange = 0
t.opt.xlim = xl
t.opt.autoscale = 0
t.opt.ylim = yl
t.opt.fill = opt.fill
if (opt.overlap and ii == 0) or (not opt.overlap and ii == nsect - 1):
t.opt.yscale = opt.emgscale
t.opt.yscalelabel = opt.emgscalelabel
if opt.color:
t.prop.color = opt.color
if opt.linewidth:
t.prop.linewidth = opt.linewitdh
if len(ha) == nsect: # one trace per axis
t.plot(ii, ha[ii])
hf.canvas.draw()
hf.canvas.flush_events()
#
# events
#
if opt.event_code: # is not empty
print("plot events not implemented")
class PlotOpt:
# emgsel
emgsel = None
# isect
isect = None
# tref
tref = None
# events
event_code = None
event_time = None
events_color = None
events_style = None
# title
usetitle = None
emgtitle = None
# plotting options
figure = None
axes = None
pos = None
overlap = None
spacing = None
xlim = None
ylim = None # for each individual trace
emgscale = None
emgscalelabel = None
fill = None
color = None
linewidth = None
def __init__(self):
# emgsel
self.emgsel = np.array([])
# isect
self.isect = np.array([])
# tref
self.tref = np.array([])
# events
self.event_code = []
self.event_time = []
self.events_color = []
self.events_style = []
# title
self.usetitle = 1
self.emgtitle = []
# plotting options
self.figure = []
self.axes = []
self.pos = np.array([0.09, 0.08, 0.88, 0.86])
self.overlap = 0
self.spacing = 0.01
self.xlim = np.array([])
self.ylim = np.array([]) # for each individual trace
self.emgscale = 0
self.emgscalelabel = ""
self.fill = 0
self.color = "black"
self.linewidth = np.array([])
def getDefPlotOpt(self, obj):
nemg = obj.size
nch = obj[0].data.shape[0] # number of channels
# emgsel
self.emgsel = np.arange(nch)
# isect
self.isect = np.arange(nemg)
# tref
self.tref = np.array([])
# events
self.event_code = []
self.event_time = []
self.events_color = []
self.events_style = []
# title
self.usetitle = 1
self.emgtitle = []
# plotting options
self.figure = []
self.axes = []
self.pos = np.array([0.09, 0.08, 0.88, 0.86])
self.overlap = 0
self.spacing = 0.01
self.xlim = np.array([])
self.ylim = np.array([]) # for each individual trace
self.emgscale = 0
self.emgscalelabel = ""
self.fill = 0
self.color = "black"
self.linewidth = np.array([])
return self
"""
compute duration of EMG data for each trials
"""
def duration(self, e):
nemg = e.size
du = []
for i in range(nemg):
du.append(np.ptp(e[i].time)) # range of time
return np.matrix(du)
"""
check if trials have the same time samples
"""
def isequalinterval(self, e):
nemg = e.shape[0]
val = True
timeref = e[0].time
for i in range(1, nemg):
if np.not_equal(e[i].time, timeref).any():
val = False
return val
return val
def emglim(self, e):
nemg = e.size
valmin = []
valmax = []
for i in range(nemg):
valmin.append(e[i].data.min()) # min from all channels
valmax.append(e[i].data.max()) # max from all channels
valmin = np.array(valmin)
valmax = np.array(valmax)
return np.array([valmin.min(), valmax.max()])
def timelim(self, e):
nemg = e.size
tmin = []
tmax = []
for i in range(nemg):
tmin.append(e[i].time.min())
tmax.append(e[i].time.max())
tmin = np.array(tmin)
tmax = np.array(tmax)
return np.array([tmin.min(), tmax.max()])
"""
returns data mean sampling interval (rounded to us)
"""
def tsamp(self, ind, prec=None):
if prec is None:
prec = 10e-6
t = np.mean(np.round(np.diff(self.obj[ind].time) / prec)) * prec
return t
"""
get time range of EMG data of each trial
"""
def timerange(self):
nemg = self.obj.size
tr = []
for i in range(nemg):
tr.append(self.obj[i].time[[0, -1]])
return np.array(tr)
"""
compute min of each channel
"""
def min(self, e):
nemg = e.shape[0]
for i in range(nemg):
nch, nsamptot = e[i].data.shape
if i == 0:
val = np.nan * np.ones((nch, 1))
val = np.fmin(val, e[i].data.min(axis=1, keepdims=True))
return val
"""
compute max of each channel
type = 0 => max of all trials
type = 1 => max of each trial
"""
def max(self, e, maxtype=None):
if maxtype is None:
maxtype = 0
nemg = e.shape[0]
for i in range(nemg):
nch, nsamptot = e[i].data.shape
if maxtype == 0:
if i == 0:
val = np.nan * np.ones((nch, 1))
val = np.fmax(val, e[i].data.max(axis=1, keepdims=True))
elif maxtype == 1:
if i == 0:
val = np.nan * np.ones((nch, nemg))
val[:, i] = np.max(e[i].data.max(axis=1, keepdims=True))
return val