-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaptest_fft.py
executable file
·782 lines (703 loc) · 34.4 KB
/
maptest_fft.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
#!/usr/bin/env python
"""
maptest.py: Compute phase maps from streams of images.
Algorithm:
1. compute (or get) period of image sequence and number of repeats
2. fold data
3. take mean across all sequences (reduction to one sequence or cycle)
4. compute phase as a function of (x,y) within the map. We use an FFT
for this, as it seems to be faster than anything else.
The result is both plotted (matplotlib) and written to disk.
Includes test routine (call with '-t' flag) to generate a noisy image with
superimposed sinusoids in blocks with different phases.
To do:
1. sum phase maps (allow two inputs) to get absolute delay
2. (done)
3. data reduction in image to nxn blocks in (x,y)
5/12/2010 Paul B. Manis
UNC Chapel Hill
"""
import sys, os
import numpy
import numpy as np
import scipy.signal
import scipy.ndimage
#import scipy.stsci.convolve
#import astropy.convolution
from astropy.convolution import convolve_fft, convolve, Box2DKernel, Box1DKernel
import pickle
try:
import matplotlib
#TFR 11/13/15 inserted the following line to try to resolve issue with pylab.show
#matplotlib.rcParams['backend'] = "QtAgg"
import matplotlib.mlab as mlab
import pylab
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
## for Palatino and other serif fonts use:
#rc('font',**{'family':'serif','serif':['Palatino']})
rc('text', usetex=True)
HAVE_MPL = True
except:
HAVE_MPL = False
import pyqtgraph as pg #added to deal with plottng issues TFR 11/13/15
app = pg.Qt.QtGui.QApplication([])
from pyqtgraph.metaarray import MetaArray
#import pylibrary.Utility as Utils
#from pylibrary.Utility import SignalFilter_LPFBessel
from optparse import OptionParser
# frequency list for runs 14 March 2016
freqlist = np.logspace(0, 4, num=9, base=2.0)
fl1 = [3000*x for x in freqlist]
print "frequency list: ",fl1
#fl1=[1, 1.414, 2.0, 2.828, 4.0, 5.656, 8.0, 11.318, 16.0, 22.627, 32.0, 45.254]
# frequency list for runs 2-June 2010, starting at #60 (heavier coverage of higher frequencies) and text note
fl2 = [4.0, 4.756, 5.656, 6.727, 8.0, 9.5, 11.3, 13.45, 16.0, 19.02, 22.62, 26.91, 31.99, 38.09, 45.25, 53.8]
# dictionary of data sets
# Keys are first file #. Data are file name (up, down), wavelength, attn, period, date, frequency list, comment
# 15 May 10: used amber LED (noisy) for 610 illumination
DB = {4: ('004', '003', 610, 10.0, 2.5, '14Mar16', fl1, 'thinned skull')}
DB[5] = ('005', '002', 610, 20.0, 2.5, '14Mar16', fl1, 'thinned skull')
DB[6] = ('006', '001', 610, 30.0, 2.5, '14Mar16', fl1, 'thinned skull')
D = []
d = []
measuredPeriod = 2.5
binsize = 4
gfilt = 0
basepath = '/Volumes/TRoppData/2016.03.14_000/Sound_Stimulation_'
class testAnalysis():
def __init__(self):
global d
global measuredPeriod
global gfilt
global binsize
self.times = []
self.upfile = []
self.downfile = []
self.avgimg = []
self.imageData = []
self.phasex = []
self.phasey = []
self.nPhases = 17
self.nCycles = 3
def parse_and_go(self, argsin = None):
global measuredPeriod
global period
parser=OptionParser() # command line options
##### parses all of the options inputted at the command line TFR 11/13/2015
parser.add_option("-u", "--upfile", dest="upfile", metavar='FILE',
help="load the up-file")
parser.add_option("-d", "--downfile", dest="downfile", metavar='FILE',
help="load the down-file")
parser.add_option("-D", "--directory", dest="directory", metavar='FILE',
help="Use directory for data")
parser.add_option("-t", "--test", dest="test", action='store_true',
help="Test mode to check calculations", default=False)
parser.add_option("-p", '--period', dest = "period", type="float",
help = "Stimulus cycle period")
parser.add_option("-c", '--cycles', dest = "cycles", default=0, type="int",
help = "# cycles to analyze")
parser.add_option("-b", '--binning', dest = "binsize", default=0, type="int",
help = "bin reduction x,y")
parser.add_option("-g", '--gfilter', dest = "gfilt", default=0, type="float",
help = "gaussian filter width")
parser.add_option("-f", '--fdict', dest = "fdict", default=0, type="int",
help = "Use dictionary entry")
if argsin is not None:
(options, args) = parser.parse_args(argsin)
else:
(options, args) = parser.parse_args()
if options.period is not None:
measuredPeriod = options.period
if options.cycles is not None:
self.nCycles = options.cycles
if options.binsize is not None:
binsize = options.binsize
if options.gfilt is not None:
gfilt = options.gfilt
#TFR- this code generates a test signal for running a test analysis sequence
print "Run test?", options.test
if options.test is True:
print "Running Test Sample"
period = 8.0 # period and frame sample rate can be different
framerate = 8.0
nper = 1
d = 10.0*numpy.random.normal(size=(2500,128,128)).astype('float32')
ds = d.shape
self.nFrames = d.shape[0]
self.nPhases = 10
maxdel = 50
self.phasex = []
self.phasey = []
for i in range(0,self.nPhases):
dx = i*ds[1]/self.nPhases # each phase is assigned to a region
baseline = 0.0
self.resp = numpy.zeros((self.nFrames,))
phaseDelay = 0.25*period+period*(float(i)/self.nPhases) # phase delay for this region from 0 to nearly the stimulus repeat period
# print '********phase delay: ', phaseDelay
for j in range(0, nper): # for each period
tdelay = (float(j) * period) + phaseDelay # time to phase delay point
idelay = int(numpy.floor(tdelay*framerate)) # convert to frame position in frame space
# print ' tdel: ', tdelay, ' idel: ', idelay
# if idelay < self.nFrames-maxdel:
# self.resp[idelay:idelay+maxdel] = (i+1)*numpy.exp(-numpy.linspace(0, 2, maxdel)) # marks amplitudes as well
self.resp = numpy.sin(
numpy.linspace(0, 2.0*numpy.pi*self.nFrames/(period*framerate), self.nFrames)+i*numpy.pi/8 - numpy.pi/2.0)
d[:, dx:dx+int(ds[1]/self.nPhases), 5:int(ds[2]/2)] += self.resp[:, numpy.newaxis, numpy.newaxis]
self.phasex.append( (2+(dx+int(ds[1]/self.nPhases))/2))
self.phasey.append((6+int(ds[2]/2)/2)) # make the signal equivalent of digitized one (baseline 3000, signal at 1e-4 of baseline)
d = (d*3000.0*1e-4)+3000.0 # scale and offset to match data scaling coming in
self.imageData = d.astype('int16') # reduce to a 16-bit map to match camera data type
self.times = numpy.arange(0, self.nFrames/framerate, 1.0/framerate)
print "Test Image Created"
self.Analysis_FourierMap(period = period, target = 1, mode=1, bins=binsize)
print "Completed Analysis FourierMap"
self.plotmaps_pg(mode = 2, gfilter = gfilt)
print "Completed plot maps"
return
if options.fdict is not None:
if options.fdict in DB.keys(): # populate options
options.upfile = DB[options.fdict][0]
options.downfile = DB[options.fdict][1]
options.period = DB[options.fdict][4]
else:
print "File %d NOT in DBase\n" % options.fdict
return
if options.directory is not None:
self.directory = options.directory
if options.upfile is not None:
self.upfile = options.upfile
target = 1
if options.downfile is not None:
self.downfile = options.downfile
target = 2
target = 0
upf = None
dwnf = None
if options.upfile is not None:
upf = basepath + options.upfile + '/000/Camera/frames.ma'
if options.downfile is not None:
dwnf = basepath + options.downfile + '/000/Camera/frames.ma'
for file in (upf, dwnf):
#if options.upfile is not None and options.downfile is not None:
if file is None:
break
im=[]
self.imageData = []
print "loading data from ", file
try:
im = MetaArray(file = file, subset=(slice(0,2), slice(64,128), slice(64,128)))
except:
print "Error loading upfile: %s\n" % file
return
print "data loaded"
target = target + 1
self.times = im.axisValues('Time').astype('float32')
self.imageData = im.view(np.ndarray).astype('float32')
pg.plot(self.imageData[10,3,:],title='(3,:)')
print '(10,3,15)', self.imageData[10,3,15]
pg.plot(self.imageData[10,:,3],title='(:,3)')
pg.image(self.imageData[10,:,60:160],title='(10,:,60:160)')
pg.image(self.imageData[10,55:125,:],title='(10,65:105,:)')
self.imageData=self.imageData[:,55:125,60:160]
im=[]
if file is upf:
upflag = 1
else:
upflag = 0
self.Analysis_FourierMap(period=measuredPeriod, target = target, bins=binsize, up=upflag)
if target > 0:
self.plotmaps_pg(mode = 1, target = target, gfilter = gfilt)
def Analysis_FourierMap(self, period = 2.5, target = 1, mode=0, bins = 1, up=1):
global D
D = []
self.DF = []
self.avgimg = []
self.stdimg = []
self.nFrames =self.imageData.shape[0]
self.imagePeriod = 0
if HAVE_MPL:
pylab.figure(2)
print "Analysis Starting"
# first squeeze the image to 3d if it is 4d
maxt = self.times[-1] # find last image time
print "Duration of Image Stack: %9.3f s (%8.3f min)\n" % (maxt, maxt/60.0)
sh = self.imageData.shape
if len(sh) == 4:
self.imageData = self.imageData.squeeze()
sh = self.imageData.shape
dt = numpy.mean(numpy.diff(self.times)) # get the mean dt
self.imagePeriod = period# image period in seconds.
w = 2.0 * numpy.pi * self.imagePeriod
n_Periods = int(numpy.floor(maxt/self.imagePeriod)) # how many full periods in the image set?
if self.nCycles > 0 and self.nCycles < n_Periods:
n_Periods = self.nCycles
n_PtsPerCycle = int(numpy.floor(self.imagePeriod/dt)); # estimate image points in a stimulus cycle
ndt = self.imagePeriod/n_PtsPerCycle
self.imageData = self.imageData[range(0, n_Periods*n_PtsPerCycle),:,:] # reduce to only what we need
self.timebase = numpy.arange(0, self.imageData.shape[0]*dt, dt)# reduce data in blocks by averaging
if mode == 0:
ipx = self.imageData.shape[1]/2
ipy = self.imageData.shape[2]/2
else:
ipx = 64
ipy = 64
if bins > 1:
redx=bins
redy=bins
nredx = int(sh[1]/redx)
nredy = int(sh[2]/redy)
newImage = numpy.zeros((self.imageData.shape[0], nredx, nredy))
print sh, nredx, nredy
print self.imageData.shape, newImage.shape
for i in range(0, nredx-1):
for j in range(0, nredy-1):
# print i,j,i*redx,(i+1)*redx-1,j*redx,(j+1)*redy-1
newImage[:,i,j] = numpy.mean(numpy.mean(self.imageData[:,i*redx:(i+1)*redx-1, j*redy:(j+1)*redy-1],axis=2),axis=1)
self.imageData = newImage
sh = self.imageData.shape
ipx = ipx/redx
ipy = ipy/redy
else:
redx = bins
redy = bins
print "# Periods: %d Pts/cycle: %d Cycle dt %8.4fs (%8.3fHz) Cycle: %7.4fs" %(n_Periods, n_PtsPerCycle, ndt, 1.0/ndt, self.imagePeriod)
# get the average image and the average of the whole image over time
self.avgimg = numpy.mean(self.imageData, axis=0) # get mean image for reference later: average across all time
self.stdimg = numpy.std(self.imageData, axis= 0) # and standard deviation
# timeavg is calculated on the central region only:
self.timeavg = numpy.mean(numpy.mean(self.imageData[:,int(sh[1]*0.25):int(sh[1]*0.75),int(sh[2]*0.25):int(sh[2]*0.75)], axis=2),axis=1) # return average of entire image over time
print " >>Before HPF: Noise floor (std/mean): %12.6f largest std: %12.6f" % (numpy.mean(self.stdimg)/numpy.mean(self.avgimg),
numpy.amax(self.stdimg)/numpy.mean(self.avgimg))
print 'shape of self.timeavg:', np.shape(self.timeavg)
print 'shape of self.imageData', np.shape(self.imageData)
zid = self.imageData[:,ipx,ipy]-self.timeavg
#pg.image(zid, title='subtracted area') doesn't make sense to plot it this way- change this later
mta = scipy.signal.detrend(self.timeavg)
mtaa = numpy.mean(mta, axis=0)
stdta = numpy.std(mta)
rjstd = 2.0*stdta
pts = len(self.timeavg)
reject = numpy.where(numpy.abs(mta) > rjstd)
trej = numpy.array(self.timebase[reject])
LPF = 0.2/dt
lfilt = SignalFilter_LPFBessel(scipy.signal.detrend(zid, axis=0), LPF, samplefreq=1.0/dt , NPole = 8, reduce = False)
if HAVE_MPL:
p1=pylab.subplot(3,1,1)
p1.plot(self.timebase, self.imageData[:,ipx,ipy] - numpy.mean(self.imageData[:,ipx,ipy]), 'mo-') # prior to any correction
p1.plot(self.timebase, zid-numpy.mean(zid), 'k-') # after subtracting time averaged
p3=pylab.subplot(3,1,2)
p3.plot(self.timebase, mta, 'g-')
p3.plot([0,numpy.amax(self.timebase)], [mtaa+rjstd,mtaa+rjstd], 'g--' )
p3.plot([0,numpy.amax(self.timebase)], [mtaa-rjstd,mtaa-rjstd], 'g--')
# print trej.shape()
# print mta[:,reject].shape()
# p3.plot(trej, mta[:,reject], 'rx')
# calculate PSD of data
amp, freqs = mlab.psd(scipy.signal.detrend(zid, axis=0), Fs=1.0/dt )
amp2, freqs2 = mlab.psd(scipy.signal.detrend(self.imageData[:,ipx,ipy], axis=0), Fs=1.0/dt )
amp3, freqs3 = mlab.psd(scipy.signal.detrend(lfilt, axis=0), Fs=1.0/dt )
p2 = pylab.subplot(3,1,3)
p2.loglog(freqs, amp, 'k-')
p2.loglog(freqs2, amp2, 'mo-')
p2.loglog(freqs3, amp3, 'cs-')
# subtract slow fluctuations
flpf = float(LPF)
sf = float(1.0/dt)
wn = [flpf/(sf/2.0)]
NPole=8
filter_b,filter_a=scipy.signal.bessel(
NPole,
wn,
btype = 'low',
output = 'ba')
print "boxcar HPF"
for i in range(0, self.imageData.shape[1]):
for j in range(0, self.imageData.shape[2]):
self.imageData[:,i,j] = self.imageData[:,i,j] - self.timeavg
# OLD: stsci not available anymore
# box_2D_kernel = astropy.convolve.Box2DKernel(2*n_PtsPerCycle)
# box_2D_kernel = Box2DKernel(5)
box_2D_kernel = Box1DKernel(5)
# print self.imageData[:,i,j]
# print len(self.imageData[:,i,j])
# print box_2D_kernel
self.imageData[:,i,j] = self.imageData[:,i,j] - convolve_fft(self.imageData[:,i,j], box_2D_kernel)
# self.imageData[:,i,j] = self.imageData[:,i,j] - scipy.stsci.convolve.boxcar(self.imageData[:,i,j], (2*n_PtsPerCycle,))
self.imageData[:,i,j]=scipy.signal.lfilter(filter_b, filter_a, scipy.signal.detrend(self.imageData[:,i,j], axis=0)) # filter the incoming signal
zid = self.imageData[:,ipx,ipy]
lfilt = SignalFilter_LPFBessel(scipy.signal.detrend(zid, axis=0), LPF, samplefreq=1.0/dt , NPole = 8, reduce = False)
if HAVE_MPL:
p1.plot(self.timebase, zid - numpy.mean(zid), 'r-')
p1.plot(self.timebase, lfilt - numpy.mean(lfilt), 'c-')
amp2, freqs2 = mlab.psd(scipy.signal.detrend(self.imageData[:,ipx,ipy], axis=0), Fs=1.0/dt )
p2.loglog(freqs2, amp2, 'r')
ymin, ymax = p2.get_ylim()
p2.set_ylim((0.01, ymax))
self.stdimg = numpy.std(self.imageData, axis= 0) # and standard deviation
print " >>after HPF: Noise floor (std/mean): %12.6f largest std: %12.6f" % (numpy.mean(self.stdimg)/numpy.mean(self.avgimg),
numpy.amax(self.stdimg)/numpy.mean(self.avgimg))
# print "now reshaping"
self.n_times = numpy.arange(0, n_PtsPerCycle*ndt, ndt) # just one cycle
# # put data into new shape to prepare for mean. "Folds" data by cycles". Also multiply to make average work
# self.imageData = numpy.reshape(self.imageData,
# (n_Periods, n_PtsPerCycle, sh[1], sh[2])).astype('float32')
# print "now calculating mean"
# # excluding bad trials
# trials = range(0, n_Periods)
# reject = reject[0]
# for i in range(0,len(reject)):
# t = reject[i]/n_PtsPerCycle
# if t in trials:
# trials.remove(t)
# print "retaining trials: ", trials
#D = numpy.mean(self.imageData[trials,:,:,:], axis=0).astype('float32') # /divider # get mean of the folded axes.
# print "mean calculated, now detrend and fft"
#edge detect
D = self.imageData
# detrend before taking fft
D = scipy.signal.detrend(D, axis=0)
# calculate FFT and get amplitude and phase
self.DF = numpy.fft.fft(D, axis = 0)
ampimg = numpy.abs(self.DF[1,:,:]).astype('float32')
phaseimg = numpy.angle(self.DF[1,:,:]).astype('float32')
if target == 1:
f = open('img_phase1.dat', 'w')
pickle.dump(phaseimg, f)
f.close()
f = open('img_amplitude1.dat', 'w')
pickle.dump(ampimg, f)
f.close()
self.amplitudeImage1 = ampimg
self.phaseImage1 = phaseimg
if target == 2:
f = open('img_phase2.dat', 'w')
pickle.dump(phaseimg, f)
f.close()
f = open('img_amplitude2.dat', 'w')
pickle.dump(ampimg, f)
f.close()
self.amplitudeImage2 = ampimg
self.phaseImage2 = phaseimg
print "fft calculated, data saveddata"
# save most recent calculation to disk
def sub_func(self, a, avg):
return(a - avg)
# plot data
def plotmaps(self, mode = 0, target = 1, gfilter = 0):
global D
max1 = numpy.amax(self.amplitudeImage1)
if target > 1:
max1 = numpy.amax([max1, numpy.amax(self.amplitudeImage2)])
max1 = 10.0*int(max1/10.0)
pylab.figure(1)
pylab.subplot(2,3,1)
pylab.title('Amplitude Map1')
#scipy.ndimage.gaussian_filter(self.amplitudeImage1, 2, order=0, output=self.amplitudeImage1, mode='reflect')
imga1 = pylab.imshow(scipy.ndimage.gaussian_filter(self.amplitudeImage1,gfilt, order=0, mode='reflect'))
pylab.colorbar()
imga1.set_clim = (0.0, max1)
pylab.subplot(2,3,4)
pylab.title('Phase Map1')
imgp1 = pylab.imshow(scipy.ndimage.gaussian_filter(self.phaseImage1, gfilt, order=0,mode='reflect'), cmap=matplotlib.cm.hsv)
imgp1.set_clim=(-numpy.pi/2.0, numpy.pi/2.0)
pylab.colorbar()
print "plotmaps Block 1"
print "mode:", mode
if mode == 0:
pylab.subplot(2,3,3)
for i in range(0, self.nPhases):
pylab.plot(ta.n_times, D[:,5,5].view(ndarray))
#pylab.plot(self.n_times, D[:,i*55+20, 60])
pylab.hold('on')
pylab.title('Waveforms')
pylab.subplot(2,3,6)
for i in range(0, self.nPhases):
pylab.plot(ta.n_times, self.DF[:,5,5].view(ndarray))
#pylab.plot(self.DF[:,i*55+20, 60])
pylab.hold('on')
pylab.title('FFTs')
print "plotmaps Block 2"
if mode == 1 and target > 1:
pylab.subplot(2,3,2)
pylab.title('Amplitude Map2')
#scipy.ndimage.gaussian_filter(self.amplitudeImage2, 2, order=0, output=self.amplitudeImage2, mode='reflect')
imga2 = pylab.imshow(scipy.ndimage.gaussian_filter(self.amplitudeImage2,gfilt, order=0, mode='reflect'))
imga2.set_clim = (0.0, max1)
pylab.colorbar()
pylab.subplot(2,3,5)
imgp2 = pylab.imshow(scipy.ndimage.gaussian_filter(self.phaseImage2, gfilt, order=0,mode='reflect'), cmap=matplotlib.cm.hsv)
pylab.colorbar()
imgp2.set_clim=(-numpy.pi/2.0, numpy.pi/2.0)
pylab.title('Phase Map2')
# doubled phase map
pylab.subplot(2,3,6)
#scipy.ndimage.gaussian_filter(self.phaseImage2, 2, order=0, output=self.phaseImage2, mode='reflect')
np1 = scipy.ndimage.gaussian_filter(self.phaseImage1, gfilt, order=0, mode='reflect')
np2 = scipy.ndimage.gaussian_filter(self.phaseImage2, gfilt, order=0, mode='reflect')
dphase = np1 + np2
#dphase = self.phaseImage1 - self.phaseImage2
#scipy.ndimage.gaussian_filter(dphase, 2, order=0, output=dphase, mode='reflect')
imgpdouble = pylab.imshow(dphase, cmap=matplotlib.cm.hsv)
pylab.title('2x Phi map')
pylab.colorbar()
imgpdouble.set_clim=(-numpy.pi, numpy.pi)
print "plotmaps Block 3"
if mode == 2 or mode == 1:
if self.phasex == []:
self.phasex = numpy.random.randint(0, high=D.shape[1], size=D.shape[1])
self.phasey = numpy.random.randint(0, high=D.shape[2], size=D.shape[2])
pylab.subplot(2,3,3)
sh = D.shape
spr = sh[2]/self.nPhases
for i in range(0, self.nPhases):
Dm = self.avgimg[i*spr,i*spr] # diagonal run
pylab.plot(self.n_times, 100.0*(D[:,self.phasex[i], self.phasey[i]]/Dm))
pylab.hold('on')
pylab.title('Waveforms')
print "plotmaps Block 4"
if mode == 2:
pylab.subplot(2,3,6)
for i in range(0, self.nPhases):
pylab.plot(self.DF[1:,80, 80])
pylab.hold('on')
pylab.title('FFTs')
print "plotmaps Block 5"
pylab.show()
def plotmaps_pg(self, mode = 0, target = 1, gfilter = 0):
# # ## Set up plots/images in window
# self.view = pg.GraphicsView()
# l = pg.GraphicsLayout(border=(100,100,100))
# self.view.setCentralItem(l)
# self.amp1View = l.addViewBox(lockAspect=True)
# self.amp2View = l.addViewBox(lockAspect=True)
# self.waveformPlot = l.addPlot(title="Waveforms")
# l.nextRow()
# self.phase1View = l.addViewBox(lockAspect=True)
# self.phase2View = l.addViewBox(lockAspect=True)
# self.fftPlot = l.addPlot(title="FFTs")
# self.phiView = l.addViewBox(lockAspect=True)
global D
max1 = numpy.amax(self.amplitudeImage1)
if target > 1:
max1 = numpy.amax([max1, numpy.amax(self.amplitudeImage2)])
max1 = 10.0*int(max1/10.0)
# pylab.figure(1)
# pylab.subplot(2,3,1)
# pylab.title('Amplitude Map1')
# #scipy.ndimage.gaussian_filter(self.amplitudeImage1, 2, order=0, output=self.amplitudeImage1, mode='reflect')
ampimg = scipy.ndimage.gaussian_filter(self.amplitudeImage1,gfilt, order=0, mode='reflect')
#self.amp1View.addItem(pg.ImageItem(ampimg))
self.amp1 = pg.image(ampimg, title="Amplitude Map 1", levels=(0.0, max1))
#imga1 = pylab.imshow(ampimg)
#pylab.colorbar()
#imga1.set_clim = (0.0, max1)
#pylab.subplot(2,3,4)
#pylab.title('Phase Map1')
phsmap=scipy.ndimage.gaussian_filter(self.phaseImage1, gfilt, order=0,mode='reflect')
#self.phase1View.addItem(pg.ImageItem(phsmap))
self.phs1 = pg.image(phsmap, title='Phase Map 1')
#self.phs1.getHistogramWidget().item.gradient.
#imgp1 = pylab.imshow(phsmap, cmap=matplotlib.cm.hsv)
#pylab.colorbar()
print "plotmaps Block 1"
print "mode:", mode
self.wavePlt = pg.plot(title='Waveforms')
if mode == 0 or mode == 2:
self.fftPlt = pg.plot(title = 'FFTs')
if mode == 0:
#pylab.subplot(2,3,3)
for i in range(0, self.nPhases):
self.wavePlt.plot(ta.n_times, D[:,5,5].view(ndarray))
#pylab.plot(ta.n_times, D[:,5,5].view(ndarray))
#pylab.plot(self.n_times, D[:,i*55+20, 60])
#pylab.hold('on')
#pylab.title('Waveforms')
#pylab.subplot(2,3,6)
for i in range(0, self.nPhases):
self.fftPlt.plot(ta.n_times, self.DF[:,5,5].view(ndarray))
#pylab.plot(ta.n_times, self.DF[:,5,5].view(ndarray))
#pylab.plot(self.DF[:,i*55+20, 60])
#pylab.hold('on')
#pylab.title('FFTs')
print "plotmaps Block 2"
if mode == 1 and target > 1:
#pylab.subplot(2,3,2)
#pylab.title('Amplitude Map2')
#scipy.ndimage.gaussian_filter(self.amplitudeImage2, 2, order=0, output=self.amplitudeImage2, mode='reflect')
ampImg2 = scipy.ndimage.gaussian_filter(self.amplitudeImage2,gfilt, order=0, mode='reflect')
#imga2 = pylab.imshow(ampImg2)
#self.amp2View.addItem(pg.ImageItem(ampImg2))
self.amp2 = pg.image(ampImg2, title='Amplitude Map 2', levels=(0.0, max1))
#imga2.set_clim = (0.0, max1)
#pylab.colorbar()
#pylab.subplot(2,3,5)
phaseImg2 = scipy.ndimage.gaussian_filter(self.phaseImage2, gfilt, order=0,mode='reflect')
#self.phase2View.addItem(pg.ImageItem(phaseImg2))
self.phs2 = pg.image(phaseImg2, title="Phase Map 2", levels=(-np.pi, np.pi))
#imgp2 = pylab.imshow(phaseImg2, cmap=matplotlib.cm.hsv)
#pylab.colorbar()
#imgp2.set_clim=(-numpy.pi/2.0, numpy.pi/2.0)
#pylab.title('Phase Map2')
### doubled phase map
#pylab.subplot(2,3,6)
#scipy.ndimage.gaussian_filter(self.phaseImage2, 2, order=0, output=self.phaseImage2, mode='reflect')
np1 = scipy.ndimage.gaussian_filter(self.phaseImage1, gfilt, order=0, mode='reflect')
np2 = scipy.ndimage.gaussian_filter(self.phaseImage2, gfilt, order=0, mode='reflect')
dphase = np1 + np2
# for i in range(dphase.shape[0]):
# for j in range(dphase.shape[1]):
# #for k in range(dphase.shape[2]):
# if dphase[i,j]<0:
# dphase[i,j] = dphase[i,j]+2*np.pi
# if dphase[i,j]<2*np.pi/5:
# dphase[i,j]=0
# elif dphase[i,j]<4*np.pi/5:
# dphase[i,j]=1
# elif dphase[i,j]<6*np.pi/5:
# dphase[i,j]=2
# elif dphase[i,j]<8*np.pi/5:
# dphase[i,j]=3
# else:
# dphase[i,j]=4
#dphase = self.phaseImage1 - self.phaseImage2
#scipy.ndimage.gaussian_filter(dphase, 2, order=0, output=dphase, mode='reflect')
#self.phiView.addItem(pg.ImageItem(dphase))
self.phi = pg.image(dphase, title="2x Phi map", levels=(0, 2*np.pi))
#imgpdouble = pylab.imshow(dphase, cmap=matplotlib.cm.hsv)
#pylab.title('2x Phi map')
#pylab.colorbar()
#imgpdouble.set_clim=(-numpy.pi, numpy.pi)
print "plotmaps Block 3"
if mode == 2 or mode == 1:
if self.phasex == []:
self.phasex = numpy.random.randint(0, high=D.shape[1], size=D.shape[1])
self.phasey = numpy.random.randint(0, high=D.shape[2], size=D.shape[2])
#pylab.subplot(2,3,3)
sh = D.shape
spr = sh[2]/self.nPhases
wvfms=[]
# for i in range(0, self.nPhases):
# Dm = self.avgimg[i*spr,i*spr] # diagonal run
# wvfms=self.n_times, 100.0*(D[:,self.phasex[i], self.phasey[i]]/Dm)
# #pylab.plot(self.n_times, 100.0*(D[:,self.phasex[i], self.phasey[i]]/Dm))
# self.wavePlt.plot(self.n_times, 100.0*(D[:,self.phasex[i], self.phasey[i]]/Dm))
# #pylab.hold('on')
# #self.plotlist.append(pg.image(wvfms, title="Waveforms"))
# #print "it worked"
# #pylab.title('Waveforms')
print "plotmaps Block 4"
if mode == 2:
#pylab.subplot(2,3,6)
for i in range(0, self.nPhases):
#pylab.plot(self.DF[1:,80, 80])
#self.fftPlt.plot(self.DF[1:,80,80]) ## causing errors and i'm not sure what the desired thing is, Exception: Can not plot complex data types.
pass
#pylab.hold('on')
#pylab.title('FFTs')
print "plotmaps Block 5"
#pylab.show()
#self.view.show()
def meanxy(self, indata, n, m):
""" compute a mean in the xy plane of indata, over an area nxm
the return is the reduced mean array. Note that rHS and bottom parts
may be lost, depending on whether n and/or m are equally divisible into
the x and y dimensions """
# there must be a more efficient way to do this... this is SLOW...
sh = indata.shape
newsh = (sh[0], sh[1]/n, sh[2]/m)
result = numpy.zeros(newsh) # the new array
ji=[]
ki = []
for j in range(0, newsh[1]): # precalc indices
ji.append(range(j*n,(j+1)*n))
for k in range(0, newsh[2]):
ki.append(range(k*m,(k+1)*m))
for i in range(0, sh[0]): # do not flattend the planes
for j in range(0, newsh[1]):
for k in range(0, newsh[2]):
result[i, j, k] = indata[i, ji[j], ki[k]].mean()
return result
#### This function is copied from pylibrary.Utility. It is here locally so we don't need the dependencies that pylibrary requires
def SignalFilter_LPFBessel(signal, LPF, samplefreq, NPole=8, reduce=False, debugFlag=False):
"""Low pass filter a signal with a Bessel filter
Digitally low-pass filter a signal using a multipole Bessel filter
filter. Does not apply reverse filtering so that result is causal.
Possibly reduce the number of points in the result array.
Parameters
----------
signal : a numpy array of dim = 1, 2 or 3. The "last" dimension is filtered.
The signal to be filtered.
LPF : float
The low-pass frequency of the filter (Hz)
samplefreq : float
The uniform sampling rate for the signal (in seconds)
NPole : int
Number of poles for Butterworth filter. Positive integer.
reduce : boolean (default: False)
If True, subsample the signal to the lowest frequency needed to
satisfy the Nyquist critera.
If False, do not subsample the signal.
Returns
-------
w : array
Filtered version of the input signal
"""
if debugFlag:
print "sfreq: %f LPF: %f HPF: %f" % (samplefreq, LPF)
flpf = float(LPF)
sf = float(samplefreq)
wn = [flpf/(sf/2.0)]
reduction = 1
if reduce:
if LPF <= samplefreq/2.0:
reduction = int(samplefreq/LPF)
if debugFlag is True:
print "signalfilter: samplef: %f wn: %f, lpf: %f, NPoles: %d " % (
sf, wn, flpf, NPole)
filter_b,filter_a=scipy.signal.bessel(
NPole,
wn,
btype = 'low',
output = 'ba')
if signal.ndim == 1:
sm = np.mean(signal)
w=scipy.signal.lfilter(filter_b, filter_a, signal-sm) # filter the incoming signal
w = w + sm
if reduction > 1:
w = scipy.signal.resample(w, reduction)
return(w)
if signal.ndim == 2:
sh = np.shape(signal)
for i in range(0, np.shape(signal)[0]):
sm = np.mean(signal[i,:])
w1 = scipy.signal.lfilter(filter_b, filter_a, signal[i,:]-sm)
w1 = w1 + sm
if reduction == 1:
w1 = scipy.signal.resample(w1, reduction)
if i == 0:
w = np.empty((sh[0], np.shape(w1)[0]))
w[i,:] = w1
return w
if signal.ndim == 3:
sh = np.shape(signal)
for i in range(0, np.shape(signal)[0]):
for j in range(0, np.shape(signal)[1]):
sm = np.mean(signal[i,j,:])
w1 = scipy.signal.lfilter(filter_b, filter_a, signal[i,j,:]-sm)
w1 = w1 + sm
if reduction == 1:
w1 = scipy.signal.resample(w1, reduction)
if i == 0 and j == 0:
w = np.empty((sh[0], sh[1], np.shape(w1)[0]))
w[i,j,:] = w1
return(w)
if signal.ndim > 3:
print "Error: signal dimesions of > 3 are not supported (no filtering applied)"
return signal
if __name__ == "__main__":
ta=testAnalysis() # create instance (for debugging)
ta.parse_and_go(sys.argv[1:])
app.exec_()
# ta.Analysis_FourierMap(sys.argv[1:])