def calculate_power(self, data): rv = np.zeros( (data.shape[0], data.shape[1], data.shape[2], len(self._freqs))) #Shorthands bli = self._baseline_interval di = self._data_interval #iterate over channels and trials for i_d in range(data.shape[0] ): #Loop over samples (e.g. trials, not timepoints!) #print "MPM.c_p: i_d, data[i_d,:,:].shape", i_d, data[i_d,:,:].shape for i_ch in range(data.shape[2]): rv[i_d, :, i_ch, :] = abs(wt_analyze(data[i_d, :, i_ch], self._freqs))**2 #p.plot(np.arange(rv.shape[1]),rv[i_d,:,:,i_b]) #assert 1==0 #Baseline correction for i_f in range(len(self._freqs)): if bli != None: rv[i_d, :, :, i_f] /= rv[i_d, bli[0]:bli[1], :, i_f].mean(axis=0).reshape( 1, -1).repeat(rv.shape[1], axis=0) if di == None: return rv else: #Return only the selected data interval return rv[:, di[0]:di[1], ...]
def calculate_power(self, data): rv = np.zeros((data.shape[0],data.shape[1],data.shape[2],len(self._freqs))) #Shorthands bli = self._baseline_interval di = self._data_interval #iterate over channels and trials for i_d in range(data.shape[0]): #Loop over samples (e.g. trials, not timepoints!) #print "MPM.c_p: i_d, data[i_d,:,:].shape", i_d, data[i_d,:,:].shape for i_ch in range(data.shape[2]): rv[i_d,:,i_ch,:] = abs(wt_analyze(data[i_d,:,i_ch],self._freqs))**2 #p.plot(np.arange(rv.shape[1]),rv[i_d,:,:,i_b]) #assert 1==0 #Baseline correction for i_f in range(len(self._freqs)): if bli!=None: rv[i_d,:,:,i_f] /= rv[i_d,bli[0]:bli[1],:,i_f].mean(axis=0).reshape(1,-1).repeat(rv.shape[1],axis=0) if di == None: return rv else: #Return only the selected data interval return rv[:,di[0]:di[1],...]
def _calculate_wavelets(self, data): rv = np.zeros((data.shape[0],data.shape[2],len(self._freqs)),"D") for j in range(data.shape[2]): rv[:,j,:] = wt_analyze(data[:,0,j],self._freqs, self.Fs) return rv
def calculate(self, cond_name=None): """After doing the setup, this method actually calculates the ERPs. If no argument is supplied, then the data for all conditions are calculated.""" if cond_name == None: cnames = self._times.keys() else: cnames = [cond_name] #Check if settings for single-trial-power are o.k. if "st_power" in self._measures or "st_power_nb" in self._measures: if self.st_power_start_end[0] > self.st_power_start_end[ 1] or self.st_power_start_end[0] < self._start_end[ 0] or self.st_power_start_end[1] > self._start_end[1]: raise ValueError( "The settings for the interval for the single-trial power are incorrect: %s" % (str(self.st_power_start_end))) for c in cnames: #assert self._datadict.has_key(c) and self._is_calculated.has_key(c), "Key in dictionary missing!" #TODO: open file f = eegpy.open_eeg(self._dfile_name) #TODO: check timepoints ndp = f.numDatapoints tps_ok = True for t in self._times[c]: if t + self._start_end[0] < 0 or t + self._start_end[1] > ndp: tps_ok = False break if tps_ok: self._datadict[c] = {} if "mean_power" in self._measures: self._datadict[c]["mean_power"] = n.zeros( (self._start_end[1] - self._start_end[0], f.numChannels, len(self._freqs)), "d") if "itpc" in self._measures: self._datadict[c]["itpc"] = n.zeros( (self._start_end[1] - self._start_end[0], f.numChannels, len(self._freqs)), "d") if "st_power" in self._measures: self._datadict[c]["st_power"] = n.zeros( (f.numChannels, len(self._times[c]), len(self._freqs)), "d") if "st_power_nb" in self._measures: self._datadict[c]["st_power_nb"] = n.zeros( (f.numChannels, len(self._times[c]), len(self._freqs)), "d") for ch in range(f.numChannels): if debug: print "Condition", c, "Channel", ch #getData data = f.get_data_for_events(self._times[c], self._start_end, [ch]) #calculate wavelets wts = n.zeros( (data.shape[0], data.shape[2], len(self._freqs)), "D") for j in range(data.shape[2]): wts[:, j, :] = wt_analyze(data[:, 0, j], self._freqs, self._Fs) #print wt_analyze(data[:,i,j],self._freqs, self._Fs).shape, self._datadict[c][:,i,j,:].shape if "mean_power" in self._measures: tmp = (abs( wts[:, ~self._is_artifact[c], :])**2).mean(axis=1) #print "tmp.shape:", tmp.shape for fr in range(tmp.shape[1]): tmp[:, fr] = 10 * (n.log10(tmp[:, fr]) - n.log10( tmp[-self._start_end[0] - 200:-self._start_end[0], fr].mean())) self._datadict[c]["mean_power"][:, ch, :] = tmp if "itpc" in self._measures: tmp = wts[:, :, :] / abs(wts[:, :, :]) self._datadict[c]["itpc"][:, ch, :] = abs( tmp.mean(axis=1)) if "st_power" in self._measures: tmp = abs(wts[:, :, :])**2 for tr in range(tmp.shape[1]): for fr in range(tmp.shape[2]): tmp[:, tr, fr] = 10 * ( n.log10(tmp[:, tr, fr]) - n.log10(tmp[-self._start_end[0] - 200:-self._start_end[0], tr, fr].mean())) self._datadict[c]["st_power"][ch, :, :] = tmp[ -self._start_end[0] + self.st_power_start_end[0]:-self._start_end[0] + self.st_power_start_end[1], :, :].mean(axis=0) if "st_power_nb" in self._measures: tmp = abs(wts[:, :, :])**2 #for tr in range(tmp.shape[1]): # for fr in range(tmp.shape[2]): # tmp[:,tr,fr] = 10*( n.log10(tmp[:,tr,fr]) -n.log10 (tmp[-self._start_end[0]-200:-self._start_end[0],tr,fr].mean())) self._datadict[c]["st_power_nb"][ch, :, :] = tmp[ -self._start_end[0] + self.st_power_start_end[0]:-self._start_end[0] + self.st_power_start_end[1], :, :].mean(axis=0) #print "Shape datadict:", self._datadict[c].shape self._is_calculated[c] = True else: #TODO: some kind of error message pass
for i_t in range(ar1.shape[1]): phase = np.random.random()*np.pi*2 ar1[:,i_t] += np.sin(2*np.pi*freq1*xs+phase)*mask1*ampl1 ar2[:,i_t] += np.sin(2*np.pi*freq2*xs+phase)*mask2*ampl2 p.subplot(211) p.imshow(ar1.T,aspect="auto", interpolation="nearest") p.subplot(212) p.imshow(ar2.T,aspect="auto", interpolation="nearest") wt_freqs = np.linspace(1,50,50,True) wts1 = np.zeros((ar1.shape[0],len(wt_freqs),ar1.shape[1]),"d") wts2 = np.zeros((ar2.shape[0],len(wt_freqs),ar2.shape[1]),"d") for i_t in range(ar1.shape[1]): print i_t wts1[:,:,i_t] = abs(wt_analyze(ar1[:,i_t],wt_freqs,Fs=1000.0))**2 wts1[:,:,i_t] /= wts1[500:800,:,i_t].mean(axis=0).reshape(1,-1).repeat(wts1.shape[0],axis=0) wts1[:,:,i_t] = 10*np.log10(wts1[:,:,i_t]) #Calculate dB? wts2[:,:,i_t] = abs(wt_analyze(ar2[:,i_t],wt_freqs,Fs=1000.0))**2 wts2[:,:,i_t] /= wts2[500:800,:,i_t].mean(axis=0).reshape(1,-1).repeat(wts2.shape[0],axis=0) wts2[:,:,i_t] = 10*np.log10(wts2[:,:,i_t]) #Calculate dB? p.figure(2) p.subplot(2,1,1) p.imshow((wts1.mean(axis=2)).T,interpolation="nearest",aspect="auto",origin="lower",cmap=p.cm.jet) p.colorbar() p.subplot(2,1,2) p.imshow((wts2.mean(axis=2)).T,interpolation="nearest",aspect="auto",origin="lower",cmap=p.cm.jet) p.colorbar() #print "Pause" #time.sleep(5)
if do_detrend: phasen = detrend(phasen,axis=0) return phasen else: raise ValueError("Only 1d and 2d arrays supported") if __name__ == "__main__": import pylab as p from eegpy.analysis.wavelet import wt_analyze eeg = eegpy.F32("/media/story/SchlafGed/iEEG/data/canseven_bp.f32") #p.plot(eeg[10000:20000,:]) data = eeg[21000:30000,20] freqs=n.arange(1,15,5) wts = wt_analyze(data,freqs=freqs) for i in range(wts.shape[1]): p.subplot(511) p.plot(phases_from_complex(wts[:,i],continuous=False)) p.subplot(512) phase_cont = phases_from_complex(wts[:,i],continuous=True) p.plot(phase_cont) xs = n.arange(phase_cont.shape[0]) pfacs = p.polyfit(xs[0:500],phase_cont[0:500],1) print pfacs p.plot(xs,p.polyval(pfacs,xs),"k--",) p.subplot(513) p.plot(phase_cont-p.polyval(pfacs,xs)) p.subplot(514) pfacs = ((freqs[i]*2*np.pi)/1000.0 , 0) print pfacs
for i_t in range(ar1.shape[1]): phase = np.random.random() * np.pi * 2 ar1[:, i_t] += np.sin(2 * np.pi * freq1 * xs + phase) * mask1 * ampl1 ar2[:, i_t] += np.sin(2 * np.pi * freq2 * xs + phase) * mask2 * ampl2 p.subplot(211) p.imshow(ar1.T, aspect="auto", interpolation="nearest") p.subplot(212) p.imshow(ar2.T, aspect="auto", interpolation="nearest") wt_freqs = np.linspace(1, 50, 50, True) wts1 = np.zeros((ar1.shape[0], len(wt_freqs), ar1.shape[1]), "d") wts2 = np.zeros((ar2.shape[0], len(wt_freqs), ar2.shape[1]), "d") for i_t in range(ar1.shape[1]): print i_t wts1[:, :, i_t] = abs(wt_analyze(ar1[:, i_t], wt_freqs, Fs=1000.0))**2 wts1[:, :, i_t] /= wts1[500:800, :, i_t].mean(axis=0).reshape(1, -1).repeat(wts1.shape[0], axis=0) wts1[:, :, i_t] = 10 * np.log10(wts1[:, :, i_t]) #Calculate dB? wts2[:, :, i_t] = abs(wt_analyze(ar2[:, i_t], wt_freqs, Fs=1000.0))**2 wts2[:, :, i_t] /= wts2[500:800, :, i_t].mean(axis=0).reshape(1, -1).repeat(wts2.shape[0], axis=0) wts2[:, :, i_t] = 10 * np.log10(wts2[:, :, i_t]) #Calculate dB? p.figure(2)
if do_detrend: phasen = detrend(phasen, axis=0) return phasen else: raise ValueError("Only 1d and 2d arrays supported") if __name__ == "__main__": import pylab as p from eegpy.analysis.wavelet import wt_analyze eeg = eegpy.F32("/media/story/SchlafGed/iEEG/data/canseven_bp.f32") #p.plot(eeg[10000:20000,:]) data = eeg[21000:30000, 20] freqs = n.arange(1, 15, 5) wts = wt_analyze(data, freqs=freqs) for i in range(wts.shape[1]): p.subplot(511) p.plot(phases_from_complex(wts[:, i], continuous=False)) p.subplot(512) phase_cont = phases_from_complex(wts[:, i], continuous=True) p.plot(phase_cont) xs = n.arange(phase_cont.shape[0]) pfacs = p.polyfit(xs[0:500], phase_cont[0:500], 1) print pfacs p.plot( xs, p.polyval(pfacs, xs), "k--", ) p.subplot(513)