def __init__(self, raw, events, event_id, tmin, tmax, baseline=(None, 0), picks=None, name='Unknown', keep_comp=False, dest_comp=0, preload=False, reject=None, flat=None, proj=True): self.raw = raw self.event_id = event_id self.tmin = tmin self.tmax = tmax self.picks = picks self.name = name self.keep_comp = keep_comp self.dest_comp = dest_comp self.baseline = baseline self.preload = preload self.reject = reject self.flat = flat self._bad_dropped = False # Handle measurement info self.info = copy.deepcopy(raw.info) if picks is not None: self.info['chs'] = [self.info['chs'][k] for k in picks] self.info['ch_names'] = [self.info['ch_names'][k] for k in picks] self.info['nchan'] = len(picks) if picks is None: picks = range(len(raw.info['ch_names'])) self.ch_names = raw.info['ch_names'] else: self.ch_names = [raw.info['ch_names'][k] for k in picks] if len(picks) == 0: raise ValueError("Picks cannot be empty.") # Set up projection if self.info['projs'] is None or not proj: print 'No projector specified for these data' self.proj = None else: # Activate the projection items self.info['projs'] = activate_proj(self.info['projs'], copy=False) # Add EEG ref reference proj print "Adding average EEG reference projection." eeg_sel = pick_types(self.info, meg=False, eeg=True) if len(eeg_sel) > 0: eeg_proj = make_eeg_average_ref_proj(self.info) self.info['projs'].append(eeg_proj) # Create the projector proj, nproj = fiff.proj.make_projector_info(self.info) if nproj == 0: print 'The projection vectors do not apply to these channels' self.proj = None else: print ('Created an SSP operator (subspace dimension = %d)' % nproj) self.proj = proj # Set up the CTF compensator current_comp = fiff.get_current_comp(self.info) if current_comp > 0: print 'Current compensation grade : %d' % current_comp if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw['comp'] = fiff.raw.make_compensator(raw.info, current_comp, dest_comp) print 'Appropriate compensator added to change to grade %d.' % ( dest_comp) # Select the desired events self.events = events if event_id is not None: selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) self.events = self.events[selected] n_events = len(self.events) if n_events > 0: print '%d matching events found' % n_events else: raise ValueError('No desired events found.') # Handle times assert tmin < tmax sfreq = raw.info['sfreq'] n_times_min = int(round(tmin * float(sfreq))) n_times_max = int(round(tmax * float(sfreq))) self.times = np.arange(n_times_min, n_times_max + 1, dtype=np.float) / sfreq # setup epoch rejection self._reject_setup() if self.preload: self._data, good_events = self._get_data_from_disk() self.events = np.atleast_2d(self.events[good_events, :]) self._bad_dropped = True
def __init__(self, raw, events, event_id, tmin, tmax, baseline=(None, 0), picks=None, name='Unknown', keep_comp=False, dest_comp=0, preload=False, reject=None, flat=None, proj=True): self.raw = raw self.event_id = event_id self.tmin = tmin self.tmax = tmax self.picks = picks self.name = name self.keep_comp = keep_comp self.dest_comp = dest_comp self.baseline = baseline self.preload = preload self.reject = reject self.flat = flat # Handle measurement info self.info = copy.copy(raw.info) if picks is not None: self.info['chs'] = [self.info['chs'][k] for k in picks] self.info['ch_names'] = [self.info['ch_names'][k] for k in picks] self.info['nchan'] = len(picks) if picks is None: picks = range(len(raw.info['ch_names'])) self.ch_names = raw.info['ch_names'] else: self.ch_names = [raw.info['ch_names'][k] for k in picks] if len(picks) == 0: raise ValueError("Picks cannot be empty.") # Set up projection if self.info['projs'] is None or not proj: print 'No projector specified for these data' self.proj = None else: # Activate the projection items for proj in self.info['projs']: proj['active'] = True print '%d projection items activated' % len(self.info['projs']) # Create the projector proj, nproj = fiff.proj.make_projector_info(self.info) if nproj == 0: print 'The projection vectors do not apply to these channels' self.proj = None else: print ('Created an SSP operator (subspace dimension = %d)' % nproj) self.proj = proj # Set up the CTF compensator current_comp = fiff.get_current_comp(self.info) if current_comp > 0: print 'Current compensation grade : %d' % current_comp if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw['comp'] = fiff.raw.make_compensator(raw.info, current_comp, dest_comp) print 'Appropriate compensator added to change to grade %d.' % ( dest_comp) # Select the desired events selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) self.events = events[selected] n_events = len(self.events) if n_events > 0: print '%d matching events found' % n_events else: raise ValueError('No desired events found.') # Handle times sfreq = raw.info['sfreq'] self.times = np.arange(int(round(tmin * sfreq)), int(round(tmax * sfreq)), dtype=np.float) / sfreq # setup epoch rejection self._reject_setup() if self.preload: self._data = self._get_data_from_disk()
def __init__(self, raw, events, event_id, tmin, tmax, baseline=(None, 0), picks=None, name='Unknown', keep_comp=False, dest_comp=0, preload=False, reject=None, flat=None, proj=True, verbose=None): self.raw = raw self.verbose = raw.verbose if verbose is None else verbose self.event_id = event_id self.tmin = tmin self.tmax = tmax self.name = name self.keep_comp = keep_comp self.dest_comp = dest_comp self.baseline = baseline self.preload = preload self.reject = reject self.flat = flat self.proj = proj self._bad_dropped = False self.drop_log = None # Handle measurement info self.info = cp.deepcopy(raw.info) if picks is not None: self.info['chs'] = [self.info['chs'][k] for k in picks] self.info['ch_names'] = [self.info['ch_names'][k] for k in picks] self.info['nchan'] = len(picks) if picks is None: picks = range(len(raw.info['ch_names'])) self.ch_names = raw.info['ch_names'] else: self.ch_names = [raw.info['ch_names'][k] for k in picks] self.picks = picks if len(picks) == 0: raise ValueError("Picks cannot be empty.") self._projector, self.info = setup_proj(self.info) # Set up the CTF compensator current_comp = fiff.get_current_comp(self.info) if current_comp > 0: logger.info('Current compensation grade : %d' % current_comp) if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw['comp'] = fiff.raw.make_compensator(raw.info, current_comp, dest_comp) logger.info('Appropriate compensator added to change to ' 'grade %d.' % (dest_comp)) # Select the desired events self.events = events if event_id is not None: selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) self.events = self.events[selected] n_events = len(self.events) if n_events > 0: logger.info('%d matching events found' % n_events) else: raise ValueError('No desired events found.') # Handle times assert tmin < tmax sfreq = raw.info['sfreq'] n_times_min = int(round(tmin * float(sfreq))) n_times_max = int(round(tmax * float(sfreq))) self.times = np.arange(n_times_min, n_times_max + 1, dtype=np.float) / sfreq # setup epoch rejection self._reject_setup() if self.preload: self._data = self._get_data_from_disk()
def read_epochs(raw, events, event_id, tmin, tmax, picks=None, keep_comp=False, dest_comp=0, baseline=None): """Read epochs from a raw dataset Parameters ---------- raw : Raw object Returned by the setup_read_raw function events : array, of shape [n_events, 3] Returned by the read_events function event_id : int The id of the event to consider tmin : float Start time before event tmax : float End time after event keep_comp : boolean Apply CTF gradient compensation baseline: None (default) or tuple of length 2 The time interval to apply baseline correction. If None do not apply it. If baseline is (a, b) the interval is between "a (s)" and "b (s)". If a is None the beginning of the data is used and if b is None then b is set to the end of the interval. If baseline is equal ot (None, None) all the time interval is used. Returns ------- data : list of epochs An epoch is a dict with key: epoch the epoch, channel by channel event event # tmin starting time in the raw data file (initial skip omitted) tmax ending stime in the raw data file (initial skip omitted) times : array The time points of the samples, in seconds ch_names : list of strings Names of the channels included Notes ----- NOTE 1: The purpose of this function is to demonstrate the raw data reading routines. You may need to modify this for your purposes NOTE 2: You need to run mne_process_raw once as mne_process_raw --raw <fname> --projoff to create the fif-format event file (or open the file in mne_browse_raw). """ ch_names = [raw['info']['ch_names'][k] for k in picks] sfreq = raw['info']['sfreq'] # Set up projection if raw['info']['projs'] is None: print 'No projector specified for these data' raw['proj'] = [] else: # Activate the projection items for proj in raw['info']['projs']: proj['active'] = True print '%d projection items activated' % len(raw['info']['projs']) # Create the projector proj, nproj = fiff.proj.make_projector_info(raw['info']) if nproj == 0: print 'The projection vectors do not apply to these channels' raw['proj'] = None else: print 'Created an SSP operator (subspace dimension = %d)' % nproj raw['proj'] = proj # Set up the CTF compensator current_comp = fiff.get_current_comp(raw['info']) if current_comp > 0: print 'Current compensation grade : %d' % current_comp if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw.comp = fiff.raw.make_compensator(raw['info'], current_comp, dest_comp) print 'Appropriate compensator added to change to grade %d.' % ( dest_comp) # # Read the events # if event_fname is None: # if fname.endswith('.fif'): # event_name = '%s-eve.fif' % fname[:-4] # else: # raise ValueError, 'Raw file name does not end properly' # # events = fiff.read_events(event_name) # print 'Events read from %s' % event_name # else: # # Binary file # if event_name.endswith('-eve.fif'): # events = fiff.read_events(event_name) # print 'Binary event file %s read' % event_name # else: # # Text file # events = np.loadtxt(event_name) # if events.shape[0] < 1: # raise ValueError, 'No data in the event file' # # # Convert time to samples if sample number is negative # events[events[:,0] < 0,0] = events[:,1] * sfreq # # Select the columns of interest (convert to integers) # events = np.array(events[:,[0, 2, 3]], dtype=np.int32) # # New format? # if events[0,1] == 0 and events[0,2] == 0: # print 'The text event file %s is in the new format' % event_name # if events[0,0] != raw['first_samp']: # raise ValueError, ('This new format event file is not compatible' # ' with the raw data') # else: # print 'The text event file %s is in the old format' % event_name # # Offset with first sample # events[:,0] += raw['first_samp'] # Select the desired events selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) n_events = np.sum(selected) if n_events > 0: print '%d matching events found' % n_events else: raise ValueError, 'No desired events found.' data = list() for p, event_samp in enumerate(events[selected, 0]): # Read a data segment start = event_samp + tmin*sfreq stop = event_samp + tmax*sfreq epoch, _ = raw[picks, start:stop] if p == 0: times = np.arange(start - event_samp, stop - event_samp, dtype=np.float) / sfreq # Run baseline correction if baseline is not None: print "Applying baseline correction ..." bmin = baseline[0] bmax = baseline[1] if bmin is None: imin = 0 else: imin = int(np.where(times >= bmin)[0][0]) if bmax is None: imax = len(times) else: imax = int(np.where(times <= bmax)[0][-1]) + 1 epoch -= np.mean(epoch[:, imin:imax], axis=1)[:,None] else: print "No baseline correction applied..." d = dict() d['epoch'] = epoch d['event'] = event_id d['tmin'] = (float(start) - float(raw['first_samp'])) / sfreq d['tmax'] = (float(stop) - float(raw['first_samp'])) / sfreq data.append(d) print 'Read %d epochs, %d samples each.' % (len(data), data[0]['epoch'].shape[1]) # Remember to close the file descriptor # raw['fid'].close() # print 'File closed.' return data, times, ch_names
def __init__( self, raw, events, event_id, tmin, tmax, baseline=(None, 0), picks=None, name="Unknown", keep_comp=False, dest_comp=0, preload=False, reject=None, flat=None, proj=True, ): self.raw = raw self.event_id = event_id self.tmin = tmin self.tmax = tmax self.picks = picks self.name = name self.keep_comp = keep_comp self.dest_comp = dest_comp self.baseline = baseline self.preload = preload self.reject = reject self.flat = flat # Handle measurement info self.info = copy.deepcopy(raw.info) if picks is not None: self.info["chs"] = [self.info["chs"][k] for k in picks] self.info["ch_names"] = [self.info["ch_names"][k] for k in picks] self.info["nchan"] = len(picks) if picks is None: picks = range(len(raw.info["ch_names"])) self.ch_names = raw.info["ch_names"] else: self.ch_names = [raw.info["ch_names"][k] for k in picks] if len(picks) == 0: raise ValueError("Picks cannot be empty.") # Set up projection if self.info["projs"] is None or not proj: print "No projector specified for these data" self.proj = None else: # Activate the projection items for proj in self.info["projs"]: proj["active"] = True print "%d projection items activated" % len(self.info["projs"]) # Add EEG ref reference proj print "Adding average EEG reference projection." eeg_sel = pick_types(self.info, meg=False, eeg=True) eeg_names = [self.ch_names[k] for k in eeg_sel] n_eeg = len(eeg_sel) if n_eeg > 0: vec = np.ones((1, n_eeg)) / n_eeg eeg_proj_data = dict(col_names=eeg_names, row_names=None, data=vec, nrow=1, ncol=n_eeg) eeg_proj = dict(active=True, data=eeg_proj_data, desc="Average EEG reference", kind=1) self.info["projs"].append(eeg_proj) # Create the projector proj, nproj = fiff.proj.make_projector_info(self.info) if nproj == 0: print "The projection vectors do not apply to these channels" self.proj = None else: print ("Created an SSP operator (subspace dimension = %d)" % nproj) self.proj = proj # Set up the CTF compensator current_comp = fiff.get_current_comp(self.info) if current_comp > 0: print "Current compensation grade : %d" % current_comp if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw["comp"] = fiff.raw.make_compensator(raw.info, current_comp, dest_comp) print "Appropriate compensator added to change to grade %d." % (dest_comp) # Select the desired events selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) self.events = events[selected] n_events = len(self.events) if n_events > 0: print "%d matching events found" % n_events else: raise ValueError("No desired events found.") # Handle times assert tmin < tmax sfreq = raw.info["sfreq"] n_times_min = int(round(tmin * float(sfreq))) n_times_max = int(round(tmax * float(sfreq))) self.times = np.arange(n_times_min, n_times_max + 1, dtype=np.float) / sfreq # setup epoch rejection self._reject_setup() if self.preload: self._data = self._get_data_from_disk()
def __init__(self, raw, events, event_id, tmin, tmax, baseline=(None, 0), picks=None, name='Unknown', keep_comp=False, dest_comp=0, preload=False, reject=None, flat=None, proj=True): self.raw = raw self.event_id = event_id self.tmin = tmin self.tmax = tmax self.picks = picks self.name = name self.keep_comp = keep_comp self.dest_comp = dest_comp self.baseline = baseline self.preload = preload self.reject = reject self.flat = flat # Handle measurement info self.info = copy.copy(raw.info) if picks is not None: self.info['chs'] = [self.info['chs'][k] for k in picks] self.info['ch_names'] = [self.info['ch_names'][k] for k in picks] self.info['nchan'] = len(picks) if picks is None: picks = range(len(raw.info['ch_names'])) self.ch_names = raw.info['ch_names'] else: self.ch_names = [raw.info['ch_names'][k] for k in picks] if len(picks) == 0: raise ValueError("Picks cannot be empty.") # Set up projection if self.info['projs'] is None or not proj: print 'No projector specified for these data' self.proj = None else: # Activate the projection items for proj in self.info['projs']: proj['active'] = True print '%d projection items activated' % len(self.info['projs']) # Create the projector proj, nproj = fiff.proj.make_projector_info(self.info) if nproj == 0: print 'The projection vectors do not apply to these channels' self.proj = None else: print('Created an SSP operator (subspace dimension = %d)' % nproj) self.proj = proj # Set up the CTF compensator current_comp = fiff.get_current_comp(self.info) if current_comp > 0: print 'Current compensation grade : %d' % current_comp if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw['comp'] = fiff.raw.make_compensator(raw.info, current_comp, dest_comp) print 'Appropriate compensator added to change to grade %d.' % ( dest_comp) # Select the desired events selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) self.events = events[selected] n_events = len(self.events) if n_events > 0: print '%d matching events found' % n_events else: raise ValueError('No desired events found.') # Handle times sfreq = raw.info['sfreq'] self.times = np.arange(int(round(tmin * sfreq)), int(round(tmax * sfreq)), dtype=np.float) / sfreq # setup epoch rejection self._reject_setup() if self.preload: self._data = self._get_data_from_disk()
def __init__( self, raw, events, event_id, tmin, tmax, baseline=(None, 0), picks=None, name="Unknown", keep_comp=False, dest_comp=0, preload=False, reject=None, flat=None, proj=True, verbose=None, ): self.raw = raw self.verbose = raw.verbose if verbose is None else verbose self.event_id = event_id self.tmin = tmin self.tmax = tmax self.name = name self.keep_comp = keep_comp self.dest_comp = dest_comp self.baseline = baseline self.preload = preload self.reject = reject self.flat = flat self.proj = proj self._bad_dropped = False self.drop_log = None # Handle measurement info self.info = cp.deepcopy(raw.info) if picks is not None: self.info["chs"] = [self.info["chs"][k] for k in picks] self.info["ch_names"] = [self.info["ch_names"][k] for k in picks] self.info["nchan"] = len(picks) if picks is None: picks = range(len(raw.info["ch_names"])) self.ch_names = raw.info["ch_names"] else: self.ch_names = [raw.info["ch_names"][k] for k in picks] self.picks = picks if len(picks) == 0: raise ValueError("Picks cannot be empty.") self._projector, self.info = setup_proj(self.info) # Set up the CTF compensator current_comp = fiff.get_current_comp(self.info) if current_comp > 0: logger.info("Current compensation grade : %d" % current_comp) if keep_comp: dest_comp = current_comp if current_comp != dest_comp: raw["comp"] = fiff.raw.make_compensator(raw.info, current_comp, dest_comp) logger.info("Appropriate compensator added to change to " "grade %d." % (dest_comp)) # Select the desired events self.events = events if event_id is not None: selected = np.logical_and(events[:, 1] == 0, events[:, 2] == event_id) self.events = self.events[selected] n_events = len(self.events) if n_events > 0: logger.info("%d matching events found" % n_events) else: raise ValueError("No desired events found.") # Handle times assert tmin < tmax sfreq = raw.info["sfreq"] n_times_min = int(round(tmin * float(sfreq))) n_times_max = int(round(tmax * float(sfreq))) self.times = np.arange(n_times_min, n_times_max + 1, dtype=np.float) / sfreq # setup epoch rejection self._reject_setup() if self.preload: self._data = self._get_data_from_disk()