Example #1
0
 def classify_Spindles(self):
  f=avg_q.avg_q_file(self.filteredfile,'BrainVision')
  self.Spindles=[] # Tuples (start_s,duration_ms,Spindle_info dict)
  for collapsedchannel,(start,end) in self.Spindleranges:
   duration_ms=(end-start)/self.sfreq*1000
   # Count the zero crossings
   detector=avg_q.Detector.Detector(self.avg_q_instance)
   epochsource=avg_q.Epochsource(f,'100ms','%gms' % (duration_ms+200)) # Add 100ms on both ends
   epochsource.set_trigpoints('%gs' % (start/self.sfreq))
   detector.add_Epochsource(epochsource)
   detector.add_transform('''
write_crossings ALL 0 stdout
''')
   crossings=detector.detect()
   trigger_dict=avg_q.Detector.get_trigger_dict(crossings)
   Spindle_info={}  # Will contain lists [freq_Hz,amplitude]
   for channel,cr in trigger_dict.items():
    period_s=2*(cr[-1][0]-cr[0][0])/(len(cr)-1)/detector.sfreq
    Spindle_info[channel]=[1.0/period_s]

   script=avg_q.Script(self.avg_q_instance)
   script.add_Epochsource(epochsource)
   script.add_transform('''
calc abs
trim -a 0 0
write_generic -P -N stdout string
''')
   for line in script.runrdr():
    channel,amp=line.split('\t')
    Spindle_info[channel].append(float(amp))
   self.Spindles.append((start/self.sfreq,duration_ms,Spindle_info))
Example #2
0
 def average_ECG(self):
  '''Average ECG events with rejection.
     Returns the number of accepted ECGs.'''
  if os.path.exists(self.avgECGfile):
   ascfile=avg_q.avg_q_file(self.avgECGfile)
   return self.avg_q_instance.get_description(ascfile,'nrofaverages')
  self.detect_ECG()
  epochsource=avg_q.Epochsource(self.Epochsource_list[0].infile,self.ECG_beforetrig, self.ECG_aftertrig)
  epochsource.set_trigpoints(self.ECGtriggers)
  script=avg_q.Script(self.avg_q_instance)
  script.add_Epochsource(epochsource)
  if self.protect_channels is not None and len(self.protect_channels)>0:
   script.add_transform('''
scale_by -n ?%(protect_channels)s 0
''' % {
   'protect_channels': channel_list2arg(self.protect_channels),
   })
  script.add_transform('''
baseline_subtract
reject_bandwidth %(reject_bandwidth)f
''' % {
  'reject_bandwidth': self.ECG_reject_bandwidth,
  })
  script.set_collect('average')
  script.add_postprocess('''
query nrofaverages stdout
writeasc -b %(avgECGfile)s
''' % {
  'avgECGfile': self.avgECGfile})
  rdr=script.runrdr()
  nrofaverages=0
  for line in rdr:
   nrofaverages=int(line)
  return nrofaverages
Example #3
0
    def sessionaverage_HEOG(self, HEOGfiles):
        '''Average the HEOG files. The output file self.sessionaverage_HEOGfile is default_sessionaverage_HEOGfile
  in the current directory, unless set differently by the caller.
  Returns the total number of averages.'''
        if not self.sessionaverage_HEOGfile:
            self.sessionaverage_HEOGfile = os.path.join(
                self.indir, default_sessionaverage_HEOGfile)
        if os.path.exists(self.sessionaverage_HEOGfile):
            ascfile = avg_q.avg_q_file(self.sessionaverage_HEOGfile)
            return self.avg_q_instance.get_description(ascfile, 'nrofaverages')
        nrofaverages = 0
        for fromepoch in range(1, 3):
            readasc = ''
            for HEOGfile in HEOGfiles:
                if os.path.exists(HEOGfile):
                    readasc += 'readasc -f %d -e 1 %s\n' % (fromepoch,
                                                            HEOGfile)
            if readasc != '':
                self.avg_q_instance.write(readasc + '''
average -W
Post:
query nrofaverages stdout
writeasc -a -b %s
-
''' % self.sessionaverage_HEOGfile)
                rdr = self.avg_q_instance.runrdr()
                for line in rdr:
                    nrofaverages += int(line)
        return nrofaverages
Example #4
0
 def sessionaverage_ECG(self,ECGfiles):
  '''Average the ECG files. The output file self.sessionaverage_ECGfile is default_sessionaverage_ECGfile
  in the current directory, unless set differently by the caller.
  Returns the total number of averages.'''
  if not self.sessionaverage_ECGfile:
   self.sessionaverage_ECGfile=os.path.join(self.indir,default_sessionaverage_ECGfile)
  if os.path.exists(self.sessionaverage_ECGfile):
   ascfile=avg_q.avg_q_file(self.sessionaverage_ECGfile)
   return self.avg_q_instance.get_description(ascfile,'nrofaverages')
  readasc=''
  for ECGfile in ECGfiles:
   if os.path.exists(ECGfile):
    readasc+='readasc %s\n' % ECGfile
  nrofaverages=0
  if readasc!='':
   self.avg_q_instance.write(readasc+'''
average -W
Post:
query nrofaverages stdout
writeasc -b %s
-
''' % self.sessionaverage_ECGfile)
   rdr=self.avg_q_instance.runrdr()
   for line in rdr:
    nrofaverages=int(line)
  return nrofaverages
Example #5
0
    def classify_Spindles(self):
        f = avg_q.avg_q_file(self.filteredfile, 'BrainVision')
        self.Spindles = []  # Tuples (start_s,duration_ms,Spindle_info dict)
        for collapsedchannel, (start, end) in self.Spindleranges:
            duration_ms = (end - start) / self.sfreq * 1000
            # Count the zero crossings
            detector = avg_q.Detector.Detector(self.avg_q_instance)
            epochsource = avg_q.Epochsource(
                f, '100ms',
                '%gms' % (duration_ms + 200))  # Add 100ms on both ends
            epochsource.set_trigpoints('%gs' % (start / self.sfreq))
            detector.add_Epochsource(epochsource)
            detector.add_transform('''
write_crossings ALL 0 stdout
''')
            crossings = detector.detect()
            trigger_dict = avg_q.Detector.get_trigger_dict(crossings)
            Spindle_info = {}  # Will contain lists [freq_Hz,amplitude]
            for channel, cr in trigger_dict.items():
                period_s = 2 * (cr[-1][0] - cr[0][0]) / (len(cr) -
                                                         1) / detector.sfreq
                Spindle_info[channel] = [1.0 / period_s]

            script = avg_q.Script(self.avg_q_instance)
            script.add_Epochsource(epochsource)
            script.add_transform('''
calc abs
trim -a 0 0
write_generic -P -N stdout string
''')
            for line in script.runrdr():
                channel, amp = line.split('\t')
                Spindle_info[channel].append(float(amp))
            self.Spindles.append(
                (start / self.sfreq, duration_ms, Spindle_info))
Example #6
0
File: EOG.py Project: berndf/avg_q
 def sessionaverage_HEOG(self,HEOGfiles):
  '''Average the HEOG files. The output file self.sessionaverage_HEOGfile is default_sessionaverage_HEOGfile
  in the current directory, unless set differently by the caller.
  Returns the total number of averages.'''
  if not self.sessionaverage_HEOGfile:
   self.sessionaverage_HEOGfile=os.path.join(self.indir,default_sessionaverage_HEOGfile)
  if os.path.exists(self.sessionaverage_HEOGfile):
   ascfile=avg_q.avg_q_file(self.sessionaverage_HEOGfile)
   return self.avg_q_instance.get_description(ascfile,'nrofaverages')
  nrofaverages=0
  for fromepoch in range(1,3):
   readasc=''
   for HEOGfile in HEOGfiles:
    if os.path.exists(HEOGfile):
     readasc+='readasc -f %d -e 1 %s\n' % (fromepoch,HEOGfile)
   if readasc!='':
    self.avg_q_instance.write(readasc+'''
average -W
Post:
query nrofaverages stdout
writeasc -a -b %s
-
''' % self.sessionaverage_HEOGfile)
    rdr=self.avg_q_instance.runrdr()
    for line in rdr:
     nrofaverages+=int(line)
  return nrofaverages
Example #7
0
    def average_EOG(self):
        '''Average EOG events with strict checks for duration and surroundings.
     Returns the number of accepted EOGs.'''
        if os.path.exists(self.avgEOGfile):
            ascfile = avg_q.avg_q_file(self.avgEOGfile)
            return self.avg_q_instance.get_description(ascfile, 'nrofaverages')
        self.detect_VEOG()
        epochsource = avg_q.Epochsource(self.Epochsource_list[0].infile,
                                        self.VEOG_beforetrig,
                                        self.VEOG_aftertrig,
                                        offset=self.VEOG_offset)
        epochsource.set_trigpoints(self.VEOGtriggers)
        script = avg_q.Script(self.avg_q_instance)
        script.add_Epochsource(epochsource)
        if self.protect_channels is not None and len(
                self.protect_channels) > 0:
            script.add_transform('''
scale_by -n ?%(protect_channels)s 0
''' % {
                'protect_channels':
                channel_list2arg(self.protect_channels),
            })
        if self.preprocess:
            script.add_transform(self.preprocess)
        # Note that we require the VEOG signal to be "close to baseline" before and after the maximum
        script.add_transform(
            '''
baseline_subtract
push
%(get_VEOG_script)s
trim -x 0 100 400 500
calc abs
reject_bandwidth -m %(max_VEOG_amp_outside_window)f
pop
''' % {
                'get_VEOG_script': self.get_VEOG_script,
                'max_VEOG_amp_outside_window': self.VEOG_minamp
            })
        script.set_collect('average')
        script.add_postprocess('''
query nrofaverages stdout
writeasc -b %(avgEOGfile)s
''' % {'avgEOGfile': self.avgEOGfile})
        rdr = script.runrdr()
        nrofaverages = 0
        for line in rdr:
            nrofaverages = int(line)
        return nrofaverages
Example #8
0
File: EOG.py Project: berndf/avg_q
 def average_EOG(self):
  '''Average EOG events with strict checks for duration and surroundings.
     Returns the number of accepted EOGs.'''
  if os.path.exists(self.avgEOGfile):
   ascfile=avg_q.avg_q_file(self.avgEOGfile)
   return self.avg_q_instance.get_description(ascfile,'nrofaverages')
  self.detect_VEOG()
  epochsource=avg_q.Epochsource(self.Epochsource_list[0].infile,self.VEOG_beforetrig, self.VEOG_aftertrig, offset=self.VEOG_offset)
  epochsource.set_trigpoints(self.VEOGtriggers)
  script=avg_q.Script(self.avg_q_instance)
  script.add_Epochsource(epochsource)
  if self.protect_channels is not None and len(self.protect_channels)>0:
   script.add_transform('''
scale_by -n ?%(protect_channels)s 0
''' % {
   'protect_channels': channel_list2arg(self.protect_channels),
   })
  if self.preprocess:
   script.add_transform(self.preprocess)
  # Note that we require the VEOG signal to be "close to baseline" before and after the maximum
  script.add_transform('''
baseline_subtract
push
%(get_VEOG_script)s
trim -x 0 100 400 500
calc abs
reject_bandwidth -m %(max_VEOG_amp_outside_window)f
pop
''' % {
  'get_VEOG_script': self.get_VEOG_script,
  'max_VEOG_amp_outside_window': self.VEOG_minamp
  })
  script.set_collect('average')
  script.add_postprocess('''
query nrofaverages stdout
writeasc -b %(avgEOGfile)s
''' % {
  'avgEOGfile': self.avgEOGfile})
  rdr=script.runrdr()
  nrofaverages=0
  for line in rdr:
   nrofaverages=int(line)
  return nrofaverages
Example #9
0
 def __init__(self, epochs=None):
     self.epochs = epochs
     self.branch = []
     self.infile = avg_q.avg_q_file()
     # Only used to transfer trigpoints to avg_q.Epochsource
     self.trigpoints = None
Example #10
0
 def __init__(self, epochs=None):
  self.epochs=epochs
  self.branch=[]
  self.infile=avg_q.avg_q_file()
  # Only used to transfer trigpoints to avg_q.Epochsource
  self.trigpoints=None