Exemplo n.º 1
0
def freqfilt_eeg(fn_in,fn_out,btype="lp",fl=None,fh=None,border=3,windowed=False):
    eeg = eegpy.open_eeg(fn_in)
    out = eegpy.F32(fn_out,"w+",shape=eeg.shape,cNames=eeg.channel_names,Fs=eeg.Fs)
    if btype == "lp":
        if not windowed:    
            out[:,:] = filtfilt_low(fl,eeg[:,:],Fs=eeg.Fs,border=border)
        else:
            for i in range(0,out.num_datapoints,100000):
                out[i:min(i+100000,out.num_datapoints),:] = filtfilt_low(fl,eeg[i:min(i+100000,out.num_datapoints),:],Fs=eeg.Fs,border=border)
    elif btype == "hp":
        #for i_c in range(eeg.num_channels):
        if not windowed:    
            out[:,:] = filtfilt_high(fh,eeg[:,:],Fs=eeg.Fs,border=border)
        else:
            for i in range(0,out.num_datapoints,100000):
                out[i:min(i+100000,out.num_datapoints),:] = filtfilt_high(fh,eeg[i:min(i+100000,out.num_datapoints),:],Fs=eeg.Fs,border=border)
    elif btype == "bp":
        if not windowed:    
            out[:,:] = filtfilt_band(fl,fh,eeg[:,:],Fs=eeg.Fs,border=border)
        else:
            for i in range(0,out.num_datapoints,100000):
                out[i:min(i+100000,out.num_datapoints),:] = filtfilt_band(fl,fh,eeg[i:min(i+100000,out.num_datapoints),:],Fs=eeg.Fs,border=border)
    elif btype == "bs":
        if not windowed:    
            out[:,:] = filtfilt_bandstop(fl,fh,eeg[:,:],Fs=eeg.Fs,border=border)
        else:
            for i in range(0,out.num_datapoints,100000):
                out[i:min(i+100000,out.num_datapoints),:] = filtfilt_bandstop(fl,fh,eeg[i:min(i+100000,out.num_datapoints),:],Fs=eeg.Fs,border=border)
Exemplo n.º 2
0
 def integrate(self,ts):
     #First, calculate EEG-timecourse (independently from BOLD)
     self._ts = ts
     self._Fs = 1./(ts[1]-ts[0])
     self._signal_rstc = self._RSTC.integrate(ts)[:,::3]
     self._spls = []
     for in_bd in self._input_bands:
         tmp = filtfilt_band(in_bd[0],in_bd[1],self._signal_rstc[:,0],Fs=self._Fs,border=2)
         power = abs(hilbert(tmp))**2
         smooth_power = smooth(power,int(round(self._smooth_width*self._Fs)))
         self._spls.append(splrep(ts,smooth_power))
     #print "Anzahl spls:", len(self._spls)
     rv = odeint(self.ode,self.y,self._ts)
     return rv
Exemplo n.º 3
0
 def integrate(self,ts):
     #First, calculate EEG-timecourse (independently from BOLD)
     self._ts = ts
     self._Fs = 1./(ts[1]-ts[0])
     self._signal_rstc = self._RSTC.integrate(ts)[:,:]
     print self._signal_rstc.shape
     self._spls = np.zeros((len(self._input_bands),self._n_nodes),"O")
     for i_b, in_bd in enumerate(self._input_bands):
         for i_n in range(self._n_nodes):
             tmp = filtfilt_band(in_bd[0],in_bd[1],self._signal_rstc[:,i_n],Fs=self._Fs,border=2)
             power = abs(hilbert(tmp))**2
             smooth_power = smooth(power,int(round(self._smooth_width*self._Fs)))
             self._spls[i_b,i_n] = splrep(ts,smooth_power)
     #print "Anzahl spls:", len(self._spls)
     rv = odeint(self.ode,self.y,self._ts)
     return rv
Exemplo n.º 4
0
 def find_all_slice_artifacts(self):
     def update_pbar(num):
         """Callback for find_all_maxs"""
         if show_progressbar:
             pbar.update(num/2)
         
     eeg = self.eeg       
     #y = abs(smooth_windowed_eeg(eeg,[self.cfsa],self._slice_width))[:,0]
     #y = smooth_windowed_eeg_power(eeg,[self.cfsa],self._slice_width)[:,0]
     y = filtfilt_band(1,eeg.Fs/self._slice_width,eeg[:,self.cfsa])
     #pylab.plot(y[::10])
     #pylab.plot(eeg[:,14])
     print y.shape, self._slice_width
     #import pylab
     #pylab.ion()
     #pylab.plot(y[0:20000:1])
     #pylab.show()
     #raw_input()
     #pylab.plot(y[13000:20000:1])
     slcs_raw = find_all_maxs(y[:1000],ratio=0.6) # First segment
     slcs_raw.sort()
     #print "t=", t
     #slcs_raw.append(t)
     offset=0
     t=int(0.5*self._slice_width)
     while (t>0.4*self._slice_width or (y.shape[0]-offset)>10000) and (y.shape[0]-offset)>self._slice_width*2: 
         #print (y.shape[0]-offset)
         #print t, offset, "-",
         offset = slcs_raw[-1]+self._slice_width/2
         #print t, offset, "-",
         #pylab.plot(y[offset:offset+self._slice_width])
         #pylab.show()
         #raw_input()
         t=y[offset:offset+self._slice_width].argmax()
         slcs_raw.append(offset+t)   
         #print slcs_raw[-1], slcs_raw[-1]-slcs_raw[-2], " - ",
         #time.sleep(0.1)
         #print t, offset
     print ""
     #pylab.plot(y[::10])
     if show_progressbar:
         pbar = ProgressBar(maxval=eeg.shape[0]/self._slice_width).start()
     #slcs_raw = find_all_maxs(y[:,0],0.3,self._slice_width,20,callback=update_pbar)
     print "Raw slice-positions found", len(slcs_raw), np.mean(np.diff(slcs_raw)), np.min(slcs_raw), np.max(slcs_raw)
     slcs_raw_diff = np.diff(slcs_raw)
     print "slcs_raw_diff: ", scoreatpercentile(slcs_raw_diff,5), scoreatpercentile(slcs_raw_diff,50), scoreatpercentile(slcs_raw_diff,95)
     #raise Exception("Abbruch")
     y , fn = upsample_to_memmap(eeg[:,self.cfsa],10)
     slcs_raw_ups = [x*10 for x in slcs_raw]
     t = slcs_raw_ups[len(slcs_raw)/2]
     template = y[t-self._slice_width*10/2:t+self._slice_width*10/2]
     for i in range(5):
         t = slcs_raw_ups[len(slcs_raw)/2+i]
         template += y[t-self._slice_width*10/2:t+self._slice_width*10/2]
     template /= 6
     offsets = []
     for i,t in enumerate(slcs_raw_ups):
         #offset = find_max_overlap(template, eeg[t-self._slice_width/2:t+self._slice_width/2,self.cfsa], 100)
         offset = find_max_overlap(template, y[t-self._slice_width*10/2:t+self._slice_width*10/2], 100)
         offsets.append(offset)
     self.slcs_ups = [slcs_raw_ups[i]+offsets[i]+self.slice_shift for i in range(len(slcs_raw_ups))]
     if show_progressbar:
         pbar.finish()
     print "Refined slice-positions found. Finished.", len(offsets), np.mean(offsets), np.median(offsets), np.min(offsets), np.max(offsets)
     print "Percentile 0.5,5,95,99.5 of offsets: ", scoreatpercentile(offsets,0.5), scoreatpercentile(offsets,5), scoreatpercentile(offsets,95), scoreatpercentile(offsets,99.5)
     #Adjusting _slice_width...
     print "Old slice_width:", self._slice_width
     self._new_slice_width = int(n.ceil(n.mean(n.diff(self.slcs_ups))))/10
     self._new_slice_width += 3 # Make slice wider to have no zombie-timepoints
     self._new_slice_width = self._new_slice_width+self._new_slice_width%2
     #self._new_slice_width = (self._new_slice_width/2)*2 # make sw%2==0 (divisible by 2)
     print "New slice_width:", self._new_slice_width
     #raise Exception("Abbruch")
     return [x/10 for x in self.slcs_ups]
Exemplo n.º 5
0
def freqfilt_eeg(fn_in,
                 fn_out,
                 btype="lp",
                 fl=None,
                 fh=None,
                 border=3,
                 windowed=False):
    eeg = eegpy.open_eeg(fn_in)
    out = eegpy.F32(fn_out,
                    "w+",
                    shape=eeg.shape,
                    cNames=eeg.channel_names,
                    Fs=eeg.Fs)
    if btype == "lp":
        if not windowed:
            out[:, :] = filtfilt_low(fl, eeg[:, :], Fs=eeg.Fs, border=border)
        else:
            for i in range(0, out.num_datapoints, 100000):
                out[i:min(i + 100000, out.num_datapoints), :] = filtfilt_low(
                    fl,
                    eeg[i:min(i + 100000, out.num_datapoints), :],
                    Fs=eeg.Fs,
                    border=border)
    elif btype == "hp":
        #for i_c in range(eeg.num_channels):
        if not windowed:
            out[:, :] = filtfilt_high(fh, eeg[:, :], Fs=eeg.Fs, border=border)
        else:
            for i in range(0, out.num_datapoints, 100000):
                out[i:min(i + 100000, out.num_datapoints), :] = filtfilt_high(
                    fh,
                    eeg[i:min(i + 100000, out.num_datapoints), :],
                    Fs=eeg.Fs,
                    border=border)
    elif btype == "bp":
        if not windowed:
            out[:, :] = filtfilt_band(fl,
                                      fh,
                                      eeg[:, :],
                                      Fs=eeg.Fs,
                                      border=border)
        else:
            for i in range(0, out.num_datapoints, 100000):
                out[i:min(i + 100000, out.num_datapoints), :] = filtfilt_band(
                    fl,
                    fh,
                    eeg[i:min(i + 100000, out.num_datapoints), :],
                    Fs=eeg.Fs,
                    border=border)
    elif btype == "bs":
        if not windowed:
            out[:, :] = filtfilt_bandstop(fl,
                                          fh,
                                          eeg[:, :],
                                          Fs=eeg.Fs,
                                          border=border)
        else:
            for i in range(0, out.num_datapoints, 100000):
                out[i:min(i +
                          100000, out.num_datapoints), :] = filtfilt_bandstop(
                              fl,
                              fh,
                              eeg[i:min(i + 100000, out.num_datapoints), :],
                              Fs=eeg.Fs,
                              border=border)
Exemplo n.º 6
0
    def find_all_slice_artifacts(self):
        def update_pbar(num):
            """Callback for find_all_maxs"""
            if show_progressbar:
                pbar.update(num / 2)

        eeg = self.eeg
        #y = abs(smooth_windowed_eeg(eeg,[self.cfsa],self._slice_width))[:,0]
        #y = smooth_windowed_eeg_power(eeg,[self.cfsa],self._slice_width)[:,0]
        y = filtfilt_band(1, eeg.Fs / self._slice_width, eeg[:, self.cfsa])
        #pylab.plot(y[::10])
        #pylab.plot(eeg[:,14])
        print y.shape, self._slice_width
        #import pylab
        #pylab.ion()
        #pylab.plot(y[0:20000:1])
        #pylab.show()
        #raw_input()
        #pylab.plot(y[13000:20000:1])
        slcs_raw = find_all_maxs(y[:1000], ratio=0.6)  # First segment
        slcs_raw.sort()
        #print "t=", t
        #slcs_raw.append(t)
        offset = 0
        t = int(0.5 * self._slice_width)
        while (t > 0.4 * self._slice_width or (y.shape[0] - offset) > 10000
               ) and (y.shape[0] - offset) > self._slice_width * 2:
            #print (y.shape[0]-offset)
            #print t, offset, "-",
            offset = slcs_raw[-1] + self._slice_width / 2
            #print t, offset, "-",
            #pylab.plot(y[offset:offset+self._slice_width])
            #pylab.show()
            #raw_input()
            t = y[offset:offset + self._slice_width].argmax()
            slcs_raw.append(offset + t)
            #print slcs_raw[-1], slcs_raw[-1]-slcs_raw[-2], " - ",
            #time.sleep(0.1)
            #print t, offset
        print ""
        #pylab.plot(y[::10])
        if show_progressbar:
            pbar = ProgressBar(maxval=eeg.shape[0] / self._slice_width).start()
        #slcs_raw = find_all_maxs(y[:,0],0.3,self._slice_width,20,callback=update_pbar)
        print "Raw slice-positions found", len(slcs_raw), np.mean(
            np.diff(slcs_raw)), np.min(slcs_raw), np.max(slcs_raw)
        slcs_raw_diff = np.diff(slcs_raw)
        print "slcs_raw_diff: ", scoreatpercentile(
            slcs_raw_diff,
            5), scoreatpercentile(slcs_raw_diff,
                                  50), scoreatpercentile(slcs_raw_diff, 95)
        #raise Exception("Abbruch")
        y, fn = upsample_to_memmap(eeg[:, self.cfsa], 10)
        slcs_raw_ups = [x * 10 for x in slcs_raw]
        t = slcs_raw_ups[len(slcs_raw) / 2]
        template = y[t - self._slice_width * 10 / 2:t +
                     self._slice_width * 10 / 2]
        for i in range(5):
            t = slcs_raw_ups[len(slcs_raw) / 2 + i]
            template += y[t - self._slice_width * 10 / 2:t +
                          self._slice_width * 10 / 2]
        template /= 6
        offsets = []
        for i, t in enumerate(slcs_raw_ups):
            #offset = find_max_overlap(template, eeg[t-self._slice_width/2:t+self._slice_width/2,self.cfsa], 100)
            offset = find_max_overlap(
                template, y[t - self._slice_width * 10 / 2:t +
                            self._slice_width * 10 / 2], 100)
            offsets.append(offset)
        self.slcs_ups = [
            slcs_raw_ups[i] + offsets[i] + self.slice_shift
            for i in range(len(slcs_raw_ups))
        ]
        if show_progressbar:
            pbar.finish()
        print "Refined slice-positions found. Finished.", len(
            offsets), np.mean(offsets), np.median(offsets), np.min(
                offsets), np.max(offsets)
        print "Percentile 0.5,5,95,99.5 of offsets: ", scoreatpercentile(
            offsets, 0.5), scoreatpercentile(offsets, 5), scoreatpercentile(
                offsets, 95), scoreatpercentile(offsets, 99.5)
        #Adjusting _slice_width...
        print "Old slice_width:", self._slice_width
        self._new_slice_width = int(n.ceil(n.mean(n.diff(self.slcs_ups)))) / 10
        self._new_slice_width += 3  # Make slice wider to have no zombie-timepoints
        self._new_slice_width = self._new_slice_width + self._new_slice_width % 2
        #self._new_slice_width = (self._new_slice_width/2)*2 # make sw%2==0 (divisible by 2)
        print "New slice_width:", self._new_slice_width
        #raise Exception("Abbruch")
        return [x / 10 for x in self.slcs_ups]