def create_bipolar_derivation(raw, rows, prefix="bipolar"): """Convenience function for creating bipolar channels. Parameters ---------- raw : instance of Raw Raw instance containing traces for which bipolar derivation is taken. rows : list List of lists containing electrode indices, each list is treated separately. Bipolar channels are create between adjacent electrode entries. prefix : string Prefix for creating channel names. """ counter = 0 nr_channels = len(raw.ch_names) nr_bipolar_channels = len(rows) * (len(rows[0]) - 1) elecs = np.zeros((nr_bipolar_channels, 2), dtype="int") filters = np.zeros((nr_channels, nr_bipolar_channels)) for row in rows: for i in range(len(row) - 1): filters[row[i], counter] = 1 filters[row[i + 1], counter] = -1 elecs[counter] = [row[i], row[i + 1]] counter += 1 raw_bipolar = ssd.apply_filters(raw, filters, prefix=prefix) return raw_bipolar, filters, elecs
plt.ion() # -- load continuous data exp_id = "motor_basic" participant = "ug" file_name = "../working/%s_%s_raw.fif" % (participant, exp_id) raw = mne.io.read_raw_fif(file_name, preload=True) raw.pick_types(ecog=True) # -- get common average referenced activity nr_channels = len(raw.ch_names) filters_car = np.zeros((nr_channels, nr_channels)) - 1 / nr_channels for i in range(nr_channels): filters_car[i, i] = 1 raw_car = ssd.apply_filters(raw, filters_car, prefix="car") # -- compute SSD bin_width = 1.2 peak = 9.46 filters, patterns = ssd.run_ssd(raw, peak, bin_width) patterns_car = filters_car.T @ patterns # -- get SSS referenced signal nr_components = 3 raw_ssd = ssd.apply_filters(raw, filters[:, :nr_components]) # -- detect bursts with bycycle picks = [np.argsort(np.abs(patterns_car[:, 0]))[-1]] nr_trials = 15 raw_car.pick(picks)
exp_id = "motor_basic" participant = "ug" # -- load continuous data file_name = "../working/%s_%s_raw.fif" % (participant, exp_id) raw = mne.io.read_raw_fif(file_name, preload=True) raw.pick_types(ecog=True) raw_org = raw.copy() # -- apply SSD and compute SNR for all components bin_width = 1.2 peak = 9.46 nr_components = 3 filters, patterns = ssd.run_ssd(raw_org, peak, bin_width) raw_ssd = ssd.apply_filters(raw_org, filters) # -- SSD is polarity invariant, align to electrode signals by visual inspection signs = [1, -1, -1] for i_s, sign in enumerate(signs): patterns[:, i_s] *= sign raw_ssd._data[i_s] *= sign SNR_ssd = helper.get_SNR(raw_ssd, freq=[peak - 2, peak + 2]) raw_ssd.pick(range(nr_components)) # -- select electrodes and compute SNR for the selected peak frequency ch_names = ["ecog16", "ecog10", "ecog20"] picks = mne.pick_channels(raw.ch_names, ch_names, ordered=True) raw.pick(picks) SNR_electrodes = helper.get_SNR(raw, freq=[peak - 2, peak + 2])
picks1 = mne.pick_channels(raw2.ch_names, ch_names_leads, ordered=True) plot_seeg_3dbrain(raw) # -- apply SSD peak1 = 4.0 bin_width1 = 1.25 filters1, patterns = ssd.run_ssd(raw, peak1, bin_width1) peak2 = 8.15 bin_width2 = 2.5 filters2, patterns2 = ssd.run_ssd(raw, peak2, bin_width2) # combine top-SNR filters for each peak frequency into one matrix filters1[:, 1] = filters2[:, 0] patterns[:, 1] = patterns2[:, 0] raw_ssd = ssd.apply_filters(raw, filters1[:, :2]) raw_ssd.filter(1, None) picks = range(8) ch_names = ["ecog%i" % i for i in picks] lead1 = raw.copy().pick_channels(ch_names) lead1.set_eeg_reference("average") picks = range(8, 14) ch_names = ["ecog%i" % i for i in picks] lead2 = raw.copy().pick_channels(ch_names) lead2.set_eeg_reference("average") picks = range(14, 19) ch_names = ["ecog%i" % i for i in picks] lead3 = raw.copy().pick_channels(ch_names)
plt.ion() # -- load continuous data participant_id = "wc_fixation_pwrlaw" raw = mne.io.read_raw_fif("../working/%s_raw.fif" % participant_id) raw.load_data() raw.pick_types(ecog=True) # -- compute & apply SSD spatial filters peak = 8.15 bin_width = 2 filters, patterns = ssd.run_ssd(raw, peak, bin_width) nr_components = 2 raw_ssd = ssd.apply_filters(raw, filters[:, :nr_components]) picks = [np.argmax(np.abs(patterns[:, 0])), np.argmax(np.abs(patterns[:, 1]))] raw2 = raw.copy().pick(picks) raw2.add_channels([raw_ssd]) # -- compute spectrum psd, freq = mne.time_frequency.psd_welch(raw2, fmin=1, fmax=45, n_fft=3000) # -- create plot fig = plt.figure() outer_grid = gridspec.GridSpec(2, 1, height_ratios=[1, 2]) top_cell = outer_grid[0, :] gs = gridspec.GridSpecFromSubplotSpec(1, 3, top_cell, width_ratios=[1, 2, 1]) # -- plot time domain ax1 = plt.subplot(gs[0, 1], zorder=10)
dfs = [] participant_id = "%s_%s" % (participant, exp) raw_file = "../working/%s_raw.fif" % participant_id raw = mne.io.read_raw_fif(raw_file) raw.load_data() raw.pick_types(ecog=True) raw = helper.reject_channels(raw, participant, exp) # get individual peaks peaks = helper.get_participant_peaks(participant, exp) for peak1 in peaks: patterns, filters = helper.load_ssd(participant_id, peak1) raw_ssd = ssd.apply_filters(raw, filters[:, :2]) bandwidth = (float(peak1) - freq_width, float(peak1) + freq_width) i_comp = 0 # compute features for SSD component df = compute_features( raw_ssd._data[i_comp], raw.info["sfreq"], f_range=bandwidth, burst_detection_kwargs=osc_param, center_extrema="T", ) # save mean burst features df = df[df.is_burst] nr_bursts = len(df)