Ejemplo n.º 1
0
 def find_slices_in_mean(self,num_slices = None,ch_num=None, width=None):
     """In the calculated average data, find all positions of slice-acquisition artifacts relative to trigger.
     Two rounds: first, use a smoothed version (lower risk of failure), 
     second, improve values on unsmoothed signal."""
     if num_slices==None:
         num_slices=self._num_slices
     if width==None:
         width=self._slice_width
     if ch_num==None:
         ch_num=self._ch_for_search
     #First round
     smooth_data = smooth(self._mean_data[:,ch_num]**2,width)
     #global test
     #test=smooth_data
     #smooth_data = (smooth_data**2).mean(axis=1)
     #if debug:
         #print smooth_data.shape, smooth_data
         #pylab.clf()
         #pylab.ioff()
         #pylab.plot(smooth_data[:])
         #pylab.show()
         #raw_input()
     maxs = find_maxs(smooth_data,num_maxs = num_slices, width=width)
     if debug:
         print "Coarse Maxs: ", maxs
     template = self._mean_data[maxs[3]-width/2:maxs[3]+width/2]
     for i in range(len(maxs)):
         try:
             maxs[i] += find_max_overlap(template,self._mean_data[maxs[i]-width/2:maxs[i]+width/2])
         except Exception, e:
             print "Error while refining max-position no. ", i, ", ", e
Ejemplo n.º 2
0
 def set_timepoints_auto(self,
                         start,
                         end=10e50,
                         step=5500,
                         ch_num=0,
                         width=100):
     """If triggers are not good, one can try to automatically find the timepoints"""
     #print "Setting timepoints automatically"
     assert ch_num < self._data.shape[
         1] and ch_num >= 0, "ch_num is not valid"
     ts = []
     t = int(start)
     offset = 0
     while t < self._data.shape[0] - step and t < end:
         if t == int(start):
             #template = self._data[t-width/2:t+width/2,ch_num]
             template = self._data[t:t + step, ch_num]
             ts.append(t)
         else:
             #offset = find_max_overlap(template, self._data[t-width/2:t+width/2,ch_num], width/2)
             offset = find_max_overlap(template, self._data[t:t + step,
                                                            ch_num],
                                       width / 2)
             #print offset
             ts.append(t + offset)
         if debug:
             print ts[-1],
         t += step + offset
     self.set_timepoints(ts)
     return ts
Ejemplo n.º 3
0
 def set_timepoints_auto(self,start,end=10e50,step=5500,ch_num=0,width=100):
     """If triggers are not good, one can try to automatically find the timepoints.
     For fMRI, search for first volume artifact."""
     #print "Setting timepoints automatically"
     assert ch_num<self._data.shape[1] and ch_num>=0, "ch_num is not valid"
     ts = []
     t = int(start)
     offset=0
     while t<self._data.shape[0]-step and t<end:
         if t==int(start):
             #template = self._data[t-width/2:t+width/2,ch_num]
             searchdata = abs(hilbert(self._data[t:t+500,ch_num]))
             searchdata = smooth(searchdata,self._slice_width/2)
             bp = np.where(searchdata>200)[0][0]
             t = (bp+searchdata[bp:bp+self._slice_width/2].argmax())-self._slice_width/2
             template = self._data[t:t+step,ch_num]
             ts.append(t)
         else:
             #offset = find_max_overlap(template, self._data[t-width/2:t+width/2,ch_num], width/2)
             offset = find_max_overlap(template, self._data[t:t+step,ch_num], width/2)
             #print offset
             ts.append(t+offset)
         if debug:
             print ts[-1],
         t+=step+offset
     self.set_timepoints(ts)
     return ts
Ejemplo n.º 4
0
 def find_slices_in_mean(self, num_slices=None, ch_num=None, width=None):
     """In the calculated average data, find all positions of slice-acquisition artifacts relative to trigger.
     Two rounds: first, use a smoothed version (lower risk of failure), 
     second, improve values on unsmoothed signal."""
     if num_slices == None:
         num_slices = self._num_slices
     if width == None:
         width = self._slice_width
     if ch_num == None:
         ch_num = self._ch_for_search
     #First round
     smooth_data = smooth(self._mean_data[:, ch_num]**2, width)
     #global test
     #test=smooth_data
     #smooth_data = (smooth_data**2).mean(axis=1)
     #if debug:
     #print smooth_data.shape, smooth_data
     #pylab.clf()
     #pylab.ioff()
     #pylab.plot(smooth_data[:])
     #pylab.show()
     #raw_input()
     maxs = find_maxs(smooth_data, num_maxs=num_slices, width=width)
     if debug:
         print "Coarse Maxs: ", maxs
     template = self._mean_data[maxs[3] - width / 2:maxs[3] + width / 2]
     for i in range(len(maxs)):
         try:
             maxs[i] += find_max_overlap(
                 template,
                 self._mean_data[maxs[i] - width / 2:maxs[i] + width / 2])
         except Exception, e:
             print "Error while refining max-position no. ", i, ", ", e
Ejemplo n.º 5
0
 def realign_triggers(self):
     if self.Ya == None:
         raise ValueError("OBSFilter: No upsampled data are available - self.Ya not set yet")
     self._ups_ts = [self._ts[0]*self.upsf]
     template = self.Ya[self._ts[0]*self.upsf:(self._ts[0]+self._len)*self.upsf]
     for i,t in enumerate(self._ts[1:]):
         #pylab.clf()
         artifact = self.Ya[t*self.upsf:(t+self._len)*self.upsf]
         shift = find_max_overlap(template, artifact, self.upsf*10)
         #pylab.plot(template[1000:3000])
         #pylab.plot(artifact[1000:3000])
         #pylab.text(40000,500,str(shift))
         #raw_input("Press enter")
         self._ups_ts.append(t*self.upsf+shift)
     print "_ts", len(self._ts), self._ts
     print "_ups_ts", len(self._ups_ts), self._ups_ts
Ejemplo n.º 6
0
 def realign_triggers(self):
     if self.Ya == None:
         raise ValueError(
             "OBSFilter: No upsampled data are available - self.Ya not set yet"
         )
     self._ups_ts = [self._ts[0] * self.upsf]
     template = self.Ya[self._ts[0] * self.upsf:(self._ts[0] + self._len) *
                        self.upsf]
     for i, t in enumerate(self._ts[1:]):
         #pylab.clf()
         artifact = self.Ya[t * self.upsf:(t + self._len) * self.upsf]
         shift = find_max_overlap(template, artifact, self.upsf * 10)
         #pylab.plot(template[1000:3000])
         #pylab.plot(artifact[1000:3000])
         #pylab.text(40000,500,str(shift))
         #raw_input("Press enter")
         self._ups_ts.append(t * self.upsf + shift)
     print "_ts", len(self._ts), self._ts
     print "_ups_ts", len(self._ups_ts), self._ups_ts
Ejemplo n.º 7
0
 def set_timepoints_auto(self,start,end=10e50,step=5500,ch_num=0,width=100):
     """If triggers are not good, one can try to automatically find the timepoints"""
     #print "Setting timepoints automatically"
     assert ch_num<self._data.shape[1] and ch_num>=0, "ch_num is not valid"
     ts = []
     t = int(start)
     offset=0
     while t<self._data.shape[0]-step and t<end:
         if t==int(start):
             #template = self._data[t-width/2:t+width/2,ch_num]
             template = self._data[t:t+step,ch_num]
             ts.append(t)
         else:
             #offset = find_max_overlap(template, self._data[t-width/2:t+width/2,ch_num], width/2)
             offset = find_max_overlap(template, self._data[t:t+step,ch_num], width/2)
             #print offset
             ts.append(t+offset)
         if debug:
             print ts[-1],
         t+=step+offset
     self.set_timepoints(ts)
     return ts
Ejemplo n.º 8
0
 def set_timepoints_auto(self,
                         start,
                         end=10e50,
                         step=5500,
                         ch_num=0,
                         width=100):
     """If triggers are not good, one can try to automatically find the timepoints.
     For fMRI, search for first volume artifact."""
     #print "Setting timepoints automatically"
     assert ch_num < self._data.shape[
         1] and ch_num >= 0, "ch_num is not valid"
     ts = []
     t = int(start)
     offset = 0
     while t < self._data.shape[0] - step and t < end:
         if t == int(start):
             #template = self._data[t-width/2:t+width/2,ch_num]
             searchdata = abs(hilbert(self._data[t:t + 500, ch_num]))
             searchdata = smooth(searchdata, self._slice_width / 2)
             bp = np.where(searchdata > 200)[0][0]
             t = (bp + searchdata[bp:bp + self._slice_width / 2].argmax()
                  ) - self._slice_width / 2
             template = self._data[t:t + step, ch_num]
             ts.append(t)
         else:
             #offset = find_max_overlap(template, self._data[t-width/2:t+width/2,ch_num], width/2)
             offset = find_max_overlap(template, self._data[t:t + step,
                                                            ch_num],
                                       width / 2)
             #print offset
             ts.append(t + offset)
         if debug:
             print ts[-1],
         t += step + offset
     self.set_timepoints(ts)
     return ts
Ejemplo n.º 9
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]
Ejemplo n.º 10
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]