def compute_amps_baseline_before(cnt, square, divide_win_length, marker_def=None): if marker_def is None: marker_def = dict([(str(i), [i]) for i in xrange(1,5)]) trial_start = 0 trial_stop = 4000 win_length_ms = 500 win_length = win_length_ms * cnt.fs / 1000.0 win_stride = win_length_ms * cnt.fs / 1000.0 epo = segment_dat_fast(cnt,marker_def=marker_def, ival=[trial_start,trial_stop]) amplitudes = compute_power_spectra(epo.data.transpose(0,2,1), window_length=win_length, window_stride=win_stride, divide_win_length=divide_win_length,square_amplitude=square) baseline_epo = segment_dat_fast(cnt,marker_def=marker_def, ival=[-500,0]) baseline_amps = compute_power_spectra( baseline_epo.data.transpose(0,2,1), window_length=win_length, window_stride=win_stride, divide_win_length=divide_win_length,square_amplitude=square) # median across trials median_baseline_amp = np.median(baseline_amps, axis=(0)) assert median_baseline_amp.shape[1] == 1, "should only have one timebin" corrected_amps = amplitudes / median_baseline_amp[np.newaxis] all_class_amps = [] for i_class in xrange(len(marker_def)): this_class_amps = corrected_amps[epo.axes[0] == i_class] class_amp = np.log(np.median(this_class_amps, axis=(0,2))) all_class_amps.append(class_amp) all_class_amps = np.array(all_class_amps) return all_class_amps
def determine_clean_trials_and_chans(self): log.info("Cleaning set...") clean_result = self.cleaner.clean(self.signal_processor.cnt) # In case this is not true due to different segment ivals of # cleaner and real data, try to standardize variable name for # segment ival of cleaner, e.g. to segment_ival all_trial_epo = segment_dat_fast(self.signal_processor.cnt, self.signal_processor.marker_def, self.signal_processor.segment_ival) assert np.array_equal( np.union1d(clean_result.clean_trials, clean_result.rejected_trials), range( all_trial_epo.data.shape[0])), ("All trials should " "either be clean or rejected.") assert np.intersect1d(clean_result.clean_trials, clean_result.rejected_trials).size == 0, ( "All " "trials should either be clean or rejected.") self.rejected_chans = clean_result.rejected_chan_names self.rejected_trials = clean_result.rejected_trials self.clean_trials = clean_result.clean_trials # just for later info
def clean(self, cnt, ignore_chans=False): highpassed_cnt = highpass_cnt(cnt, low_cut_off_hz=0.1, filt_order=4) epo = segment_dat_fast(highpassed_cnt, marker_def=self.marker_def, ival=self.segment_ival) if not ignore_chans: max_abs_vals_per_chan = np.max(np.abs(epo.data), axis=(0, 1)) abs_threshold = np.median(max_abs_vals_per_chan) * 4 vars_per_chan = np.mean(np.var(epo.data, axis=1), axis=0) var_threshold = np.median(vars_per_chan) * 4 unwanted_abs_chan = max_abs_vals_per_chan > abs_threshold unwanted_var_chan = vars_per_chan > var_threshold unwanted_chan_mask = unwanted_abs_chan | unwanted_var_chan rejected_chan_names = np.array(epo.axes[2])[unwanted_chan_mask] else: rejected_chan_names = [] clean_trials = range(epo.data.shape[0]) clean_result = CleanResult(rejected_chan_names=rejected_chan_names, rejected_trials=[], clean_trials=clean_trials, rejected_max_min=[], rejected_var=[]) return clean_result
def segment_into_trials(self): assert self.segment_ival is not None # adding the numbers at start to force later sort in segment_dat # to sort them in given order self.epo = segment_dat_fast(self.cnt, ival=self.segment_ival, marker_def=self.marker_def)
def compute_amps_baseline_before(cnt, square, divide_win_length, marker_def=None): if marker_def is None: marker_def = dict([(str(i), [i]) for i in xrange(1, 5)]) trial_start = 0 trial_stop = 4000 win_length_ms = 500 win_length = win_length_ms * cnt.fs / 1000.0 win_stride = win_length_ms * cnt.fs / 1000.0 epo = segment_dat_fast(cnt, marker_def=marker_def, ival=[trial_start, trial_stop]) amplitudes = compute_power_spectra(epo.data.transpose(0, 2, 1), window_length=win_length, window_stride=win_stride, divide_win_length=divide_win_length, square_amplitude=square) baseline_epo = segment_dat_fast(cnt, marker_def=marker_def, ival=[-500, 0]) baseline_amps = compute_power_spectra(baseline_epo.data.transpose(0, 2, 1), window_length=win_length, window_stride=win_stride, divide_win_length=divide_win_length, square_amplitude=square) # median across trials median_baseline_amp = np.median(baseline_amps, axis=(0)) assert median_baseline_amp.shape[1] == 1, "should only have one timebin" corrected_amps = amplitudes / median_baseline_amp[np.newaxis] all_class_amps = [] for i_class in xrange(len(marker_def)): this_class_amps = corrected_amps[epo.axes[0] == i_class] class_amp = np.log(np.median(this_class_amps, axis=(0, 2))) all_class_amps.append(class_amp) all_class_amps = np.array(all_class_amps) return all_class_amps
def run(self): self.init_results() # TODELAY: split apart collecting of features and training lda? # however then you would not get progress output during training # only at very end for bp_nr, filt_band in enumerate(self.filterbands): self.print_filter(bp_nr) bandpassed_cnt = bandpass_cnt(self.cnt, filt_band[0], filt_band[1], filt_order=self.filt_order) if self.standardize_filt_cnt: bandpassed_cnt = exponential_standardize_cnt(bandpassed_cnt) epo = segment_dat_fast(bandpassed_cnt, marker_def=self.marker_def, ival=self.segment_ival) for fold_nr in xrange(len(self.folds)): self.run_fold(epo, bp_nr, fold_nr)
def compute_trial_amplitudes(cnt, square, divide_win_length, marker_def): trial_start = 0 trial_stop = 4000 trial_len = trial_stop - trial_start n_samples_per_trial = trial_len * cnt.fs / 1000.0 epo = segment_dat_fast(cnt,marker_def=marker_def, ival=[trial_start, trial_stop]) assert epo.data.shape[-2] == n_samples_per_trial amplitudes = compute_power_spectra(epo.data.transpose(0,2,1), window_length=n_samples_per_trial, window_stride=n_samples_per_trial, divide_win_length=divide_win_length, square_amplitude=square) assert amplitudes.shape[2] == 1, "should only have one timebin" trial_classes = epo.axes[0] assert len(trial_classes) == len(amplitudes) return amplitudes, trial_classes
def clean(self, cnt, ignore_chans=False): # Segment into trials and take all! :) # Segment just to select markers and kick out out of bounds # trials # chans ignored always anyways... so ignore_chans parameter does not # matter epo = segment_dat_fast(cnt, marker_def=self.marker_def, ival=self.segment_ival) clean_trials = range(epo.data.shape[0]) clean_result = CleanResult(rejected_chan_names=[], rejected_trials=[], clean_trials=clean_trials, rejected_max_min=[], rejected_var=[]) return clean_result
def clean(self, cnt, ignore_chans=False): # Segment into trials and take all! :) # Segment just to select markers and kick out out of bounds # trials # chans ignored always anyways... so ignore_chans parameter does not # matter epo = segment_dat_fast(cnt, marker_def=self.marker_def, ival=self.segment_ival) # max abs over samples and channels trial_max = np.max(np.abs(epo.data), axis=(1,2)) all_trials = range(epo.data.shape[0]) rejected_trials = np.flatnonzero(trial_max > self.threshold) clean_trials = np.sort(np.setdiff1d(all_trials, rejected_trials)) clean_result = CleanResult(rejected_chan_names=[], rejected_trials=rejected_trials, clean_trials=clean_trials, rejected_max_min=rejected_trials, # lets just put it under maxmin rejected_var=[]) return clean_result
def compute_trial_amplitudes(cnt, square, divide_win_length, marker_def): trial_start = 0 trial_stop = 4000 trial_len = trial_stop - trial_start n_samples_per_trial = trial_len * cnt.fs / 1000.0 epo = segment_dat_fast(cnt, marker_def=marker_def, ival=[trial_start, trial_stop]) assert epo.data.shape[-2] == n_samples_per_trial amplitudes = compute_power_spectra(epo.data.transpose(0, 2, 1), window_length=n_samples_per_trial, window_stride=n_samples_per_trial, divide_win_length=divide_win_length, square_amplitude=square) assert amplitudes.shape[2] == 1, "should only have one timebin" trial_classes = epo.axes[0] assert len(trial_classes) == len(amplitudes) return amplitudes, trial_classes
def fill_filterbank_data(self, full_epo_data): for filterband_i in xrange(len(self.filterbands)): low_freq, high_freq= self.filterbands[filterband_i] log.info("Filterband {:d} of {:d}, from {:5.2f} to {:5.2f}".format( filterband_i + 1, len(self.filterbands), low_freq, high_freq)) bandpassed_cnt = bandpass_cnt(self.signal_processor.cnt, low_freq, high_freq, filt_order=3) epo = segment_dat_fast(bandpassed_cnt, marker_def={'1 - Right Hand': [1], '2 - Left Hand': [2], '3 - Rest': [3], '4 - Feet': [4]}, ival=self.signal_processor.segment_ival) epo.data = np.float32(epo.data) epo = select_epochs(epo, self.rejected_trials, invert=True) full_epo_data[:,:,:,filterband_i] = epo.data del epo.data del bandpassed_cnt self.filterband_axes = epo.axes + [self.filterbands.tolist()] self.filterband_names = epo.names + ['filterband'] self.filterband_units = epo.units + ['Hz']
def clean(self, cnt, ignore_chans=False): # Segment into trials and take all! :) # Segment just to select markers and kick out out of bounds # trials # chans ignored always anyways... so ignore_chans parameter does not # matter epo = segment_dat_fast(cnt, marker_def=self.marker_def, ival=self.segment_ival) # max abs over samples and channels trial_max = np.max(np.abs(epo.data), axis=(1, 2)) all_trials = range(epo.data.shape[0]) rejected_trials = np.flatnonzero(trial_max > self.threshold) clean_trials = np.sort(np.setdiff1d(all_trials, rejected_trials)) clean_result = CleanResult( rejected_chan_names=[], rejected_trials=rejected_trials, clean_trials=clean_trials, rejected_max_min=rejected_trials, # lets just put it under maxmin rejected_var=[]) return clean_result
def load_and_preprocess_data(self): # First create eog set for blink rejection self.eog_set.load_signal_and_markers() self.eog_set.segment_into_trials() self.eog_set.remove_continuous_signal() # Then create bandpassed set for variance rejection # in case low or high cut hz is given if self.low_cut_hz is not None and self.high_cut_hz is not None: self.cnt = bandpass_cnt(self.cnt, self.low_cut_hz, self.high_cut_hz, self.filt_order) elif self.low_cut_hz is not None: self.cnt = highpass_cnt(self.cnt, self.low_cut_hz, self.filt_order) elif self.high_cut_hz is not None: self.cnt = lowpass_cnt(self.cnt, self.high_cut_hz, self.filt_order) else: assert self.low_cut_hz is None and self.high_cut_hz is None # Finally create trials self.epo = segment_dat_fast(self.cnt, marker_def=self.marker_def, ival=self.rejection_var_ival) del self.cnt # No longer needed