def find_best_voxels_epochs(stcs, rois, bands, job_num=1, verbose=True): # Returns the indices of the voxels with maximum power per band in each ROI, in the format roi x band. stc is SourceEstimate, rois is a list of Labels, and bands is a list of length 2 vectors fs = 1. / (stcs[0].times[1] - stcs[0].times[0]) best_voxels = np.empty([len(rois), len(bands)]) best_voxels[:] = np.NaN for idr, roi in enumerate(rois): if verbose: print '\nLooking for best voxel in label ' + str(idr+1) + '/' + str(len(rois)) # in each label, we sum the power over all epochs psd = 0 bad_label = False for stc in stcs: try: roi_activity = stc.in_label(roi) tmp, freqs = dsp.compute_psd(roi_activity.data, fs, n_jobs=job_num) # psd is voxels x freqs psd += tmp except ValueError: print "Oops! No vertices in this label!" bad_label = True # then find the voxel with biggest power in each band. If there are no voxels in the label, replace it by NAN if bad_label: best_voxels[idr, :] = np.NaN else: for idb, band in enumerate(bands): index = np.logical_and(freqs >= band[0], freqs <= band[1]) # taking care of the weird case in which there's only one voxel in the label freq_axis = len(psd.shape) - 1 band_psd = np.mean(psd[:, index], axis=freq_axis) best_voxels[idr, idb] = band_psd.argmax() return best_voxels
def find_best_voxels(stc, rois, bands, job_num=1): # Returns the indices of the voxels with maximum power per band in each ROI, in the format roi x band. stc is SourceEstimate, rois is a list of Labels, and bands is a list of length 2 vectors fs = 1. / stc.tstep best_voxels = np.zeros([len(rois), len(bands)]) for idr, roi in enumerate(rois): roi_activity = stc.in_label(roi) psds, freqs = dsp.compute_psd(roi_activity.data, fs, n_jobs=job_num) for idb, band in enumerate(bands): index = np.logical_and(freqs >= band[0], freqs <= band[1]) band_psd = np.mean(psds[:, index], axis=1) best_voxels[idr, idb] = band_psd.argmax() return best_voxels
events = fg.get_good_events(markers[s], time, window_length) epochs = mne.Epochs(raw, events, None, 0, window_length, preload=True, baseline=None, detrend=0, picks=picks) sols = [np.dot(weights, epoch) for epoch in epochs] # in each ROI, choose the VE with highest power in that band psd = 0 for sol in sols: psd, freqs = dsp.compute_psd(sol, raw.info['sfreq']) psd += psd band_signal = [] for band in bands: index = np.logical_and(freqs >= band[0], freqs <= band[1]) band_psd = np.mean(psd[:, index], axis=1) # generate a list of (n_signals, n_times) epochs, where each signal represents one ROI roi_series = [] for vox in roi_voxels: roi_psd = band_psd[np.array(vox)] # the process of normalization introduces NaNs, so we need to be careful here. If all voxels in the ROI are NaN, return NaNs try: max_vox = np.array(vox)[np.nanargmax(roi_psd)] except: max_vox = vox[0] roi_series.append([sol[max_vox, :] for sol in sols])
# For each subject, we use the weight matrix to compute virtual electrodes for s in subjs: weights = np.load(home + '/sam%s/'%variant + s + '_weights.npz')['weights'] # normalize each beamformer weight by its vector norm normed = np.linalg.norm(weights, axis=1) weights = np.divide(weights.T,normed).T raw_fname = fifs_dir + '/%s_rest_LP100_CP3_DS300_raw.fif'%s raw = mne.fiff.Raw(raw_fname, preload=True, compensation=3) picks = mne.fiff.pick_channels_regexp(raw.info['ch_names'], 'M..-*') data, time = raw[picks, :] events = fg.get_good_events(markers[s], time, window_length) epochs = mne.Epochs(raw, events, None, 0, window_length, preload=True, baseline=None, detrend=0, picks=picks) sols = [np.dot(weights, epoch) for epoch in epochs] # average the power across epochs. Here we need to average instead of adding up because otherwise subjects with higher number of epochs would always have higher power psd = 0 for sol in sols: psd, freqs = dsp.compute_psd(sol, raw.info['sfreq']) psd += psd psd /= len(epochs) # Convert PSDs to dB psd = 10 * np.log10(psd) band_pow = [] for band in bands: index = np.logical_and(freqs >= band[0], freqs <= band[1]) vox_in_band_psd = np.mean(psd[:, index], axis=1) # average the power within rois roi_pow = [np.mean(vox_in_band_psd[np.array(vox)]) for vox in roi_voxels] band_pow.append(roi_pow) np.save(out_dir + s + '_roi_power', band_pow)