-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.py
1634 lines (1301 loc) · 56.4 KB
/
misc.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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Catchall module within the catchall module for really one-off stuff."""
from __future__ import print_function
from __future__ import division
from future import standard_library
standard_library.install_aliases()
from builtins import zip
from builtins import str
from builtins import map
from builtins import range
from past.builtins import basestring
from builtins import object
from past.utils import old_div
import numpy as np
import warnings
import matplotlib.mlab as mlab
import matplotlib # for spectrogrammer
import scipy.signal
import os
import re
import datetime
import glob
import my
import pandas
def globjoin(dirname, pattern, normalize=True):
"""Join dirname to pattern, and glob it
If normalize: calls os.path.abspath on every result
"""
res = glob.glob(os.path.join(dirname, pattern))
if normalize:
res = list(map(os.path.abspath, res))
return res
def time_of_file(filename, fmt='%Y%m%d%H%M%S'):
"""Return the modification time of the file as a datetime.
If fmt is not None: apply strftime(fmt) and return the string
"""
dt = datetime.datetime.fromtimestamp(os.path.getmtime(filename))
if fmt is None:
return dt
else:
return dt.strftime(fmt)
class Spectrogrammer(object):
"""Turns a waveform into a spectrogram"""
def __init__(self, NFFT=256, downsample_ratio=1, new_bin_width_sec=None,
max_freq=None, min_freq=None, Fs=1.0, noverlap=None, normalization=0,
detrend=mlab.detrend_mean, **kwargs):
"""Object to turn waveforms into spectrograms.
This is a wrapper around mlab.specgram. What this object provides
is slightly more intelligent parameter choice, and a nicer way
to trade off resolution in frequency and time. It also remembers
parameter choices, so that the same object can be used to batch
analyze a bunch of waveforms using the `transform` method.
Arguments passed to mlab.specgram
----------------------------------
NFFT - number of points used in each segment
Determines the number of frequency bins, which will be
NFFT / 2 before stripping out those outside min_freq and max_freq
noverlap - int, number of samples of overlap between segments
Default is NFFT / 2
Fs - sampling rate
detrend - detrend each segment before FFT
Default is to remove the mean (DC component)
**kwargs - anything else you want to pass to mlab.specgram
Other arguments
---------------
downsample_ratio - int, amount to downsample in time
After all other calculations are done, the temporal resolution
new_bin_width_sec - float, target temporal resolution
The returned spectrogram will have a temporal resolution as
close to this as possible.
If this is specified, then the downsample_ratio is adjusted
as necessary to achieve it. If noverlap is left as default,
it will try 50% first and then 0, to achieve the desired resolution.
If it is not possible to achieve within a factor of 2 of this
resolution, a warning is issued.
normalization - the power in each frequency bin is multiplied by
the frequency raised to this power.
0 means do nothing.
1 means that 1/f noise becomes white.
min_freq, max_freq - discard frequencies outside of this range
Returns
-------
Pxx - 2d array of power in dB. Shape (n_freq_bins, n_time_bins)
May contain -np.inf where the power was exactly zero.
freqs - 1d array of frequency bins
t - 1d array of times
Theory
------
The fundamental tradeoff is between time and frequency resolution and
is set by NFFT.
For instance, consider a 2-second signal, sampled at 1024Hz, chosen
such that the number of samples is 2048 = 2**11.
* If NFFT is 2048, you will have 1024 frequency bins (spaced
between 0KHz and 0.512KHz) and 1 time bin.
This is a simple windowed FFT**2, with the redundant negative
frequencies discarded since the waveform is real.
Note that the phase information is lost.
* If NFFT is 256, you will have 128 frequency bins and 8 time bins.
* If NFFT is 16, you will have 8 freqency bins and 128 time bins.
In each case the FFT-induced trade-off is:
n_freq_bins * n_time_bins_per_s = Fs / 2
n_freq_bins = NFFT / 2
So far, using only NFFT, we have traded off time resolution for
frequency resolution. We can achieve greater noise reduction with
appropriate choice of noverlap and downsample_ratio. The PSD
function achieves this by using overlapping segments, then averaging
the FFT of each segment. The use of noverlap in mlab.specgram is
a bit of a misnomer, since no temporal averaging occurs there!
But this object can reinstate this temporal averaging.
For our signal above, if our desired temporal resolution is 64Hz,
that is, 128 samples total, and NFFT is 16, we have a choice.
* noverlap = 0. Non-overlapping segments. As above, 8 frequency
bins and 128 time bins. No averaging
* noverlap = 64. 50% overlap. Now we will get 256 time bins.
We can then average together each pair of adjacent bins
by downsampling, theoretically reducing the noise. Note that
this will be a biased estimate since the overlapping windows
are not redundant.
* noverlap = 127. Maximal overlap. Now we will get about 2048 bins,
which we can then downsample by 128 times to get our desired
time resolution.
The trade-off is now:
overlap_factor = (NFFT - overlap) / NFFT
n_freq_bins * n_time_bins_per_s * overlap_factor = Fs / downsample_ratio / 2
Since we always do the smoothing in the time domain, n_freq bins = NFFT / 2
and the tradeoff becomes
n_time_bins_per_s = Fs / downsample_ratio / (NFFT - overlap)
That is, to increase the time resolution, we can:
* Decrease the frequency resolution (NFFT)
* Increase the overlap, up to a maximum of NFFT - 1
This is a sort of spurious improvement because adjacent windows
are highly correlated.
* Decrease the downsample_ratio (less averaging)
To decrease noise, we can:
* Decrease the frequency resolution (NFFT)
* Increase the downsample_ratio (more averaging, fewer timepoints)
How to choose the overlap, or the downsample ratio? In general,
50% overlap seems good, since we'd like to use some averaging, but
we get limited benefit from averaging many redundant samples.
This object tries for 50% overlap and adjusts the downsample_ratio
(averaging) to achieve the requested temporal resolution. If this is
not possible, then no temporal averaging is done (just like mlab.specgram)
and the overlap is increased as necessary to achieve the requested
temporal resolution.
"""
self.downsample_ratio = downsample_ratio # until set otherwise
# figure out downsample_ratio
if new_bin_width_sec is not None:
# Set noverlap to default
if noverlap is None:
# Try to do it with 50% overlap
noverlap = old_div(NFFT, 2)
# Calculate downsample_ratio to achieve this
self.downsample_ratio = \
Fs * new_bin_width_sec / float(NFFT - noverlap)
# If this is not achievable, then try again with minimal downsampling
if np.rint(self.downsample_ratio).astype(int) < 1:
self.downsample_ratio = 1
noverlap = np.rint(NFFT - Fs * new_bin_width_sec).astype(int)
# Convert to nearest int and test if possible
self.downsample_ratio = np.rint(self.downsample_ratio).astype(int)
if self.downsample_ratio == 0:
print("requested temporal resolution too high, using maximum")
self.downsample_ratio = 1
# Default value for noverlap if still None
if noverlap is None:
noverlap = old_div(NFFT, 2)
self.noverlap = noverlap
# store other defaults
self.NFFT = NFFT
self.max_freq = max_freq
self.min_freq = min_freq
self.Fs = Fs
self.normalization = normalization
self.detrend = detrend
self.specgram_kwargs = kwargs
def transform(self, waveform):
"""Converts a waveform to a suitable spectrogram.
Removes high and low frequencies, rebins in time (via median)
to reduce data size. Returned times are the midpoints of the new bins.
Returns: Pxx, freqs, t
Pxx is an array of dB power of the shape (len(freqs), len(t)).
It will be real but may contain -infs due to log10
"""
# For now use NFFT of 256 to get appropriately wide freq bands, then
# downsample in time
Pxx, freqs, t = mlab.specgram(waveform, NFFT=self.NFFT,
noverlap=self.noverlap, Fs=self.Fs, detrend=self.detrend,
**self.specgram_kwargs)
# Apply the normalization
Pxx = Pxx * np.tile(freqs[:, np.newaxis] ** self.normalization,
(1, Pxx.shape[1]))
# strip out unused frequencies
if self.max_freq is not None:
Pxx = Pxx[freqs < self.max_freq, :]
freqs = freqs[freqs < self.max_freq]
if self.min_freq is not None:
Pxx = Pxx[freqs > self.min_freq, :]
freqs = freqs[freqs > self.min_freq]
# Rebin in size "downsample_ratio". If last bin is not full, discard.
Pxx_rebinned = []
t_rebinned = []
for n in range(0, len(t) - self.downsample_ratio + 1,
self.downsample_ratio):
Pxx_rebinned.append(
np.median(Pxx[:, n:n+self.downsample_ratio], axis=1).flatten())
t_rebinned.append(
np.mean(t[n:n+self.downsample_ratio]))
# Convert to arrays
Pxx_rebinned_a = np.transpose(np.array(Pxx_rebinned))
t_rebinned_a = np.array(t_rebinned)
# log it and deal with infs
Pxx_rebinned_a_log = -np.inf * np.ones_like(Pxx_rebinned_a)
Pxx_rebinned_a_log[np.nonzero(Pxx_rebinned_a)] = \
10 * np.log10(Pxx_rebinned_a[np.nonzero(Pxx_rebinned_a)])
self.freqs = freqs
self.t = t_rebinned_a
return Pxx_rebinned_a_log, freqs, t_rebinned_a
def fix_pandas_display_width(meth=1, dw=0, max_columns=12):
"""Various fixes"""
import pandas
if meth == 0:
pandas.set_option('display.width', 0)
elif meth == 1:
pandas.set_option('display.max_columns', max_columns)
pandas.set_option('display.width', None)
class UniquenessError(Exception):
pass
def only_one(l):
"""Returns the only value in l, or l itself if non-iterable.
Compare 'unique_or_error', which allows multiple identical etnries.
"""
# listify
if not hasattr(l, '__len__'):
l = [l]
# check length
if len(l) != 1:
raise UniquenessError("must contain exactly one value; instead: %r" % l)
# return entry
return l[0]
def unique_or_error(a):
"""Asserts that `a` contains only one unique value and returns it
Compare 'only_one' which does not allow repeats.
"""
u = np.unique(np.asarray(a))
if len(u) == 0:
raise UniquenessError("no unique values found, should be one")
if len(u) > 1:
raise UniquenessError("%d unique values found, should be one" % len(u))
else:
return u[0]
def printnow(s):
"""Write string to stdout and flush immediately"""
import sys
sys.stdout.write(str(s) + "\n")
sys.stdout.flush()
def get_file_time(filename, human=False):
import time
# Get modification time
res = os.path.getmtime(filename)
# Convert to human-readable
if human:
res = time.ctime(res)
return res
def pickle_load(filename):
import pickle
with open(filename, 'rb') as fi:
res = pickle.load(fi)
return res
def pickle_dump(obj, filename):
import pickle
with open(filename, 'wb') as fi:
pickle.dump(obj, fi)
def invert_linear_poly(p):
"""Helper function for inverting fit.coeffs"""
return old_div(np.array([1, -p[1]]).astype(float), p[0])
def apply_and_filter_by_regex(pattern, list_of_strings, sort=True):
"""Apply regex pattern to each string and return result.
Non-matches are ignored.
If multiple matches, the first is returned.
"""
res = []
for s in list_of_strings:
m = re.match(pattern, s)
if m is None:
continue
else:
res.append(m.groups()[0])
if sort:
return sorted(res)
else:
return res
def regex_filter(pattern, list_of_strings):
"""Apply regex pattern to each string and return those that match.
See also regex_capture
"""
return [s for s in list_of_strings if re.match(pattern, s) is not None]
def regex_capture(pattern, list_of_strings, take_index=0):
"""Apply regex pattern to each string and return a captured group.
Same as old apply_and_filter_by_regex, but without the sorting.
See also regex_filter. This will match that order.
"""
# Apply filter to each string
res_l = []
for s in list_of_strings:
m = re.match(pattern, s)
# Append the capture, if any
if m is not None:
res_l.append(m.groups()[take_index])
return res_l
def rint(arr):
"""Round with rint and cast to int
If `arr` contains NaN, casting it to int causes a spuriously negative
number, because NaN cannot be an int. In this case we raise ValueError.
"""
if np.any(np.isnan(np.asarray(arr))):
raise ValueError("cannot convert arrays containing NaN to int")
return np.rint(arr).astype(int)
def is_nonstring_iter(val):
"""Check if the input is iterable, but not a string.
Recently changed this to work for Unicode.
This should catch a subset of the old way, because previously Unicode
strings caused this to return True, but now they should return False.
Will print a warning if this is not the case.
"""
# Old way
res1 = hasattr(val, '__len__') and not isinstance(val, str)
# New way
res2 = hasattr(val, '__len__') and not isinstance(val, basestring)
if res2 and not res1:
print("warning: check is_nonstring_iter")
return res2
def pick(df, isnotnull=None, **kwargs):
"""Function to pick row indices from DataFrame.
Copied from kkpandas
This method provides a nicer interface to choose rows from a DataFrame
that satisfy specified constraints on the columns.
isnotnull : column name, or list of column names, that should not be null.
See pandas.isnull for a defintion of null
All additional kwargs are interpreted as {column_name: acceptable_values}.
For each column_name, acceptable_values in kwargs.items():
The returned indices into column_name must contain one of the items
in acceptable_values.
If acceptable_values is None, then that test is skipped.
Note that this means there is currently no way to select rows that
ARE none in some column.
If acceptable_values is a single string or value (instead of a list),
then the returned rows must contain that single string or value.
TODO:
add flags for string behavior, AND/OR behavior, error if item not found,
return unique, ....
"""
msk = np.ones(len(df), dtype=bool)
for key, val in list(kwargs.items()):
if val is None:
continue
elif is_nonstring_iter(val):
msk &= df[key].isin(val)
else:
msk &= (df[key] == val)
if isnotnull is not None:
# Edge case
if not is_nonstring_iter(isnotnull):
isnotnull = [isnotnull]
# Filter by not null
for key in isnotnull:
msk &= -pandas.isnull(df[key])
return df.index[msk]
def pick_rows(df, **kwargs):
"""Returns sliced DataFrame based on indexes from pick"""
return df.loc[pick(df, **kwargs)]
def no_warn_rs():
warnings.filterwarnings('ignore', module='ns5_process.RecordingSession$')
def parse_by_block(lb_counts, pb_counts, lb_trial_numbers, pb_trial_numbers,
start_trial=None, last_trial=None, session_name=None):
"""Parses counts into each block (with corresponding trial numbers)
Using the known block structure (80 trials LB, 80 trials PB, etc)
and the trial labels in the folded, split the counts into each
sequential block.
parse_folded_by_block could almost be reimplemented with this function
if only it accepted __getslice__[trial_number]
Instead the code is almost duplicated.
This one is overridden because it starts at 157:
YT6A_120201_behaving, start at 161
lb_counts, pb_counts : Array-like, list of counts, one per trial
lb_trial_numbers, pb_trial_numbers : Array-like, same as counts, but
contianing trial numbers.
start_trial : where to start counting up by 80
if None, auto-get from session_db and unit_db (or just use 1 if
session name not provided)
last_trial : last trial to include, inclusive
if None, use the max trial in either set of labels
session_name : if start_trial is None and you specify this, it will
auto grab start_trial from session_db
Returns: counts_by_block
A list of arrays, always beginning with LB, eg LB1, PB1, LB2, PB2...
"""
# Auto-get first trial
if start_trial is None:
if session_name is None:
start_trial = 1
elif session_name == 'YT6A_120201_behaving':
# Forcible override
start_trial = 161
else:
import my.dataload
session_db = my.dataload.getstarted()['session_db']
first_trial = int(round(session_db['first_trial'][session_name]))
# Convert to beginning of first LBPB with any trials
# The first block might be quite short
# Change the final +1 to +161 to start at the first full block
start_trial = (old_div((first_trial - 1), 160)) * 160 + 1
# Arrayify
lb_counts = np.asarray(lb_counts)
pb_counts = np.asarray(pb_counts)
lb_trial_numbers = np.asarray(lb_trial_numbers)
pb_trial_numbers = np.asarray(pb_trial_numbers)
# Where to stop putting trials into blocks
if last_trial is None:
last_trial = np.max([lb_trial_numbers.max(), pb_trial_numbers.max()])
# Initialize return variable
res_by_block = []
# Parse by block
for block_start in range(start_trial, last_trial + 1, 80):
# Counts from lb in this block
lb_this_block_msk = (
(lb_trial_numbers >= block_start) &
(lb_trial_numbers < block_start + 80))
lb_this_block = lb_counts[lb_this_block_msk]
# Counts from pb in this block
pb_this_block_msk = (
(pb_trial_numbers >= block_start) &
(pb_trial_numbers < block_start + 80))
pb_this_block = pb_counts[pb_this_block_msk]
# Error check
if np.mod(block_start - start_trial, 160) == 0:
# Should be in an LB block
assert len(pb_this_block) == 0
if len(lb_this_block) == 0:
print("warning: no trials around trial %d" % block_start)
res_by_block.append(lb_this_block)
else:
# Should be in a PB block
assert len(lb_this_block) == 0
if len(pb_this_block) == 0:
print("warning: no trials around trial %d" % block_start)
res_by_block.append(pb_this_block)
# Error check that all counts were included and ordering maintained
assert np.all(np.concatenate(res_by_block[::2]) ==
lb_counts[lb_trial_numbers >= start_trial])
assert np.all(np.concatenate(res_by_block[1::2]) ==
pb_counts[pb_trial_numbers >= start_trial])
return res_by_block
def parse_folded_by_block(lb_folded, pb_folded, start_trial=1, last_trial=None,
session_name=None):
"""Parses Folded into each block
parse_by_block is now more feature-ful
TODO: reimplement parse_by_block to just return trial numbers, then
this function can wrap that and use the trial numbers to slice the foldeds
Using the known block structure (80 trials LB, 80 trials PB, etc)
and the trial labels in the folded, split the counts into each
sequential block.
lb_folded, pb_folded : Folded with `label` attribute set with trial number
start_trial : where to start counting up by 80
last_trial : last trial to include, inclusive
if None, use the max trial in either set of labels
session_name : no longer used. should make this load trials_info and
grab first trial, if anything.
Returns: counts_by_block
A list of arrays, always beginning with LB, eg LB1, PB1, LB2, PB2...
"""
# Override start trial for some munged sessions
#~ if session_name in ['YT6A_120201_behaving', 'CR24A_121019_001_behaving']:
#~ print "overriding start trial"
#~ start_trial = 161
# Where to stop putting trials into blocks
if last_trial is None:
last_trial = np.max([lb_folded.labels.max(), pb_folded.labels.max()])
# Initialize return variable
res_by_block = []
# Parse by block
for block_start in range(start_trial, last_trial + 1, 80):
# Counts from lb in this block
lb_this_block_msk = (
(lb_folded.labels >= block_start) &
(lb_folded.labels < block_start + 80))
lb_this_block = lb_folded.get_slice(lb_this_block_msk)
# Counts from pb in this block
pb_this_block_msk = (
(pb_folded.labels >= block_start) &
(pb_folded.labels < block_start + 80))
pb_this_block = pb_folded.get_slice(pb_this_block_msk)
# Error check
if np.mod(block_start - start_trial, 160) == 0:
# Should be in an LB block
assert len(pb_this_block) == 0
#assert len(lb_this_block) > 0
if len(lb_this_block) == 0:
print("warning: no trials around trial %d" % block_start)
res_by_block.append(lb_this_block)
else:
# Should be in a PB block
assert len(lb_this_block) == 0
#assert len(pb_this_block) > 0
if len(pb_this_block) == 0:
print("warning: no trials around trial %d" % block_start)
res_by_block.append(pb_this_block)
return res_by_block
def yoked_zscore(list_of_arrays, axis=1):
"""Concatenate arrays, z-score together, break apart again"""
concatted = np.concatenate(list_of_arrays, axis=axis)
means = np.mean(concatted, axis=axis)
stdevs = np.std(concatted, axis=axis)
res = []
for arr in list_of_arrays:
if axis == 1:
res.append(old_div((arr - means[:, None]), stdevs[:, None]))
elif axis == 0:
res.append(old_div((arr - means[None, :]), stdevs[None, :]))
else:
raise ValueError("axis must be 0 or 1")
return res
def gaussian_smooth(signal, gstd=100, glen=None, axis=1, **filtfilt_kwargs):
"""Smooth a signal with a Gaussian window
signal : array-like, to be filtered
gstd : standard deviation of Gaussian in samples (can be float)
glen : half-width of (truncated) Gaussian
Default is int(2.5 * gstd)
If you are using padding (on by default) and the pad length which
is a function of `glen` is longer than the data, you will get a
smoothing error. Lower `glen` or lower `padlen`.
axis : 0 or 1
Default is to filter the columns of 2d data
filtfilt_kwargs : other kwargs to pass to filtfilt
padtype - 'odd', 'even', 'constant', None
Default is 'odd', that is, continuing the signal at either end
with odd symmetry
padlen - int or None
Default is None, which is 3 * max(len(signal), glen)
NaNs will cause problems. You should probably interpolate them, using
perhaps interp_nans in this module.
"""
import scipy.signal
# Defaults
signal = np.asarray(signal)
if glen is None:
glen = int(2.5 * gstd)
# Incantation such that b[0] == 1.0
b = scipy.signal.gaussian(glen * 2, gstd, sym=False)[glen:]
b = old_div(b, b.sum())
# Smooth
if signal.ndim == 1:
res = scipy.signal.filtfilt(b, [1], signal, **filtfilt_kwargs)
elif signal.ndim == 2:
if axis == 0:
res = np.array([scipy.signal.filtfilt(b, [1], sig, **filtfilt_kwargs)
for sig in signal])
elif axis == 1:
res = np.array([scipy.signal.filtfilt(b, [1], sig, **filtfilt_kwargs)
for sig in signal.T]).T
else:
raise ValueError("axis must be 0 or 1")
else:
raise ValueError("signal must be 1d or 2d")
return res
def interp_nans(signal, axis=1, left=None, right=None, dtype=float):
"""Replaces nans in signal by interpolation along axis
signal : array-like, containing NaNs
axis : 0 or 1
Default is to interpolate along columns
left, right : to be passed to interp
dtype : Signal is first converted to this type, mainly to avoid
conversion to np.object
"""
# Convert to array
res = np.asarray(signal, dtype=float).copy()
# 1d or 2d behavior
if res.ndim == 1:
# Inner loop
nan_mask = np.isnan(res)
res[nan_mask] = np.interp(
np.where(nan_mask)[0], # x-coordinates where we need a y
np.where(~nan_mask)[0], # x-coordinates where we know y
res[~nan_mask], # known y-coordinates
left=left, right=right)
elif res.ndim == 2:
if axis == 0:
res = np.array([
interp_nans(sig, left=left, right=right, dtype=dtype)
for sig in res])
elif axis == 1:
res = np.array([
interp_nans(sig, left=left, right=right, dtype=dtype)
for sig in res.T]).T
else:
raise ValueError("axis must be 0 or 1")
else:
raise ValueError("signal must be 1d or 2d")
return res
# Correlation and coherence functions
def correlate(v0, v1, mode='same', auto=False):
"""Wrapper around np.correlate to calculate the timepoints
See scipy.signal.correlate for details on `mode`. I like to use 'same'
because it avoids the very large lags, but note that the length of the
returned data will be equal to the length of `v1`.
'valid' avoid zero-padding but can only be used when one signal is
longer than the other.
auto: sets the center peak to zero
Positive peaks (latter half of the array) mean that the second array
leads the first array.
Return: counts, corrn
counts: the counts
corrn: the offset
"""
# Calculate the correlation and the lags
corr_vals = scipy.signal.correlate(v0, v1, mode=mode, method='auto')
corr_lags = scipy.signal.correlation_lags(len(v0), len(v1), mode=mode)
if auto:
corr_vals[corr_lags == 0] = 0
return corr_vals, corr_lags
def binned_pair2cxy(binned0, binned1, Fs=1000., NFFT=256, noverlap=None,
windw=mlab.window_hanning, detrend=mlab.detrend_mean, freq_high=100,
average_over_trials=True):
"""Given 2d array of binned times, return Cxy
Helper function to ensure faking goes smoothly
binned: 2d array, trials on rows, timepoints on cols
Keep trials lined up!
rest : psd_kwargs. noverlap defaults to NFFT/2
Trial averaging, if any, is done between calculating the spectra and
normalizing them.
Will Cxy each trial with psd_kwargs, then mean over trials, then slice out
frequencies below freq_high and return.
"""
# Set up psd_kwargs
if noverlap is None:
noverlap = old_div(NFFT, 2)
psd_kwargs = {'Fs': Fs, 'NFFT': NFFT, 'noverlap': noverlap,
'detrend': detrend, 'window': windw}
# Cxy each trial
ppxx_l, ppyy_l, ppxy_l = [], [], []
for row0, row1 in zip(binned0, binned1):
ppxx, freqs = mlab.psd(row0, **psd_kwargs)
ppyy, freqs = mlab.psd(row1, **psd_kwargs)
ppxy, freqs = mlab.csd(row0, row1, **psd_kwargs)
ppxx_l.append(ppxx); ppyy_l.append(ppyy); ppxy_l.append(ppxy)
# Optionally mean over trials, then normalize
S12 = np.real_if_close(np.array(ppxy_l))
S1 = np.array(ppxx_l)
S2 = np.array(ppyy_l)
if average_over_trials:
S12 = S12.mean(0)
S1 = S1.mean(0)
S2 = S2.mean(0)
Cxy = old_div(S12, np.sqrt(S1 * S2))
# Truncate unnecessary frequencies
if freq_high:
topbin = np.where(freqs > freq_high)[0][0]
freqs = freqs.T[1:topbin].T
Cxy = Cxy.T[1:topbin].T
return Cxy, freqs
def binned2pxx(binned, Fs=1000., NFFT=256, noverlap=None,
windw=mlab.window_hanning, detrend=mlab.detrend_mean, freq_high=100):
"""Given 2d array of binned times, return Pxx
Helper function to ensure faking goes smoothly
binned: 2d array, trials on rows, timepoints on cols
rest : psd_kwargs. noverlap defaults to NFFT/2
Will Pxx each trial separately with psd_kwargs, then slice out
frequencies below freq_high and return.
"""
# Set up psd_kwargs
if noverlap is None:
noverlap = old_div(NFFT, 2)
psd_kwargs = {'Fs': Fs, 'NFFT': NFFT, 'noverlap': noverlap,
'detrend': detrend, 'window': windw}
# Pxx each trial
ppxx_l = []
for row in binned:
ppxx, freqs = mlab.psd(row, **psd_kwargs)
ppxx_l.append(ppxx)
# Truncate unnecessary frequencies
if freq_high:
topbin = np.where(freqs > freq_high)[0][0]
freqs = freqs[1:topbin]
Pxx = np.asarray(ppxx_l)[:, 1:topbin]
return Pxx, freqs
def sem(data, axis=None):
"""Standard error of the mean"""
if axis is None:
N = len(data)
else:
N = np.asarray(data).shape[axis]
return old_div(np.std(np.asarray(data), axis), np.sqrt(N))
def take_equally_spaced(arr, n):
"""Take n equally spaced elements from arr
We avoid the endpoints. So, divide the array into n+1 segments, and
take the highest point of each segment, discarding the last.
"""
# e.g., we want 2 equally spaced, so they are at 1/3 and 2/3
arr = np.asarray(arr)
first_element_relative = old_div(1.0, (n + 1))
relative_pos = np.linspace(
first_element_relative, 1 - first_element_relative, n)
absolute_pos = my.rint((len(arr) - 1) * relative_pos)
return arr[absolute_pos]
def define_integer_bin_edges(start, stop, n_bins=None, binwidth=None,
error_on_uneven_bins=True):
"""Return integer bin edges from start to stop
start : start of first bin
stop : stop of last bin
Either n_bins or binwidth should be provided, but not both.
If n_bins is provided but the bin edges do not fall on integers, they
will be rounded off, which will result in uneven bins.
An exception is raised if error_on_uneven_bins.
If binwidth is provided but the range does not divide evenly, an
exception is raised.
Returns: bin edges
The first will be start and the last will be stop.
Each value will be an integer.
"""
# Error check
if stop < start:
raise ValueError("stop must be greater than start")
# Convert start and stop to integers
if start != int(start):
raise ValueError("start is not an integer")
if stop != int(stop):
raise ValueError("start is not an integer")
start = int(start)
stop = int(stop)
# Operation depends on which parameter was provided
if n_bins is not None and binwidth is None:
# number of bins was provided
if n_bins <= 0:
raise ValueError("n_bins must be non-negative")
if int(n_bins) != n_bins:
raise ValueError("n_bins must be integer")
if np.mod(stop - start, n_bins) != 0:
# Does not divide evenly
if error_on_uneven_bins:
raise ValueError("specified n_bins does not divide evenly")
# Do float and then round off
fres = np.linspace(start, stop, n_bins)
res = np.round(fres).astype(int)
else:
# Divides evenly so use arange
binwidth = (stop - start) // n_bins
res = np.arange(start, stop + 1, binwidth, dtype=int)
elif binwidth is not None and n_bins is None:
# bin width was provided
if binwidth <= 0:
raise ValueError("binwidth must be non-negative")
if int(binwidth) != binwidth:
raise ValueError("binwidth must be integer")
res = np.arange(start, stop + 1, binwidth, dtype=int)
if res[-1] != stop:
raise ValueError("specified bin width does not divide evenly")
else:
raise ValueError("specify n_bins or binwidth but not both")
return res
def times_near_times(x, y, dstart, dstop, sort_x=True):
"""Identify times in 'y' that are within [dstart, dstop) of times in x
The algorithm is: does adding dstop to y, versus adding dstart to y,
change the way it is sorted within x? If so, then it must be close enough
to some value in x that it jumps over it.
x : times to compare to, e.g., times of "bad events"
y : probe times, e.g., data
We want to filter y and include only values in y that are far from
bad events.
dstart, dstop : times relatives to the times in x.
These should be integers. Ideally they would be variable size
intervals, but this messes up searchsorted.
Returns: boolean array, same shape as y
res[n] is True if
there is some value x' in x such that
y[n] is in the range [x' + dstart, x' + dstop]
Example:
bad_times = np.array([0, 5, 15])
probe_times = np.array([1, 6, 11, 16])
# All except the 3rd are within [-2, 2)
In [152]: times_near_times(bad_times, probe_times, -2, 2)
Out[152]: array([ True, True, False, True], dtype=bool)
# This interval is impossible
In [153]: times_near_times(bad_times, probe_times, 2, -2)
Out[153]: array([False, False, False, False], dtype=bool)
# Because it's half-open, none are within this interval
In [165]: times_near_times(bad_times, probe_times, 0, 1)
Out[165]: array([False, False, False, False], dtype=bool)
# Because it's half-open, it will trigger on the interval start
In [188]: times_near_times(bad_times + 1, probe_times, 0, 1)
Out[188]: array([ True, True, False, True], dtype=bool)
"""
# Convert to array
x = np.asarray(x)
y = np.asarray(y)
# Sort x if necessary
if sort_x:
x = np.sort(x)
# Use 'right' to make the half-open interval work out correctly
v0 = np.searchsorted(x + dstart, y, side='right')
v1 = np.searchsorted(x + dstop, y, side='right')
# Compare v0 and v1 to get the "sign" correct (e.g., if dstop < dstart,
# nothing will ever trigger)
return v0 > v1
def find_interval(event_times, interval_starts, interval_stops,
stop_type='open'):
"""Find the interval containing the events
event_times : array-like
An array of event times
interval_starts, interval_stops : array-like
The stops and starts of each intervals. They should be of equal
length and they should be sorted. Not sure what happens if the