def calc_stc_zvals(subject, modality, ictal_clips, inverse_method, overwrite=False, n_jobs=4): from_index, to_index = None, None use_abs = False baseline_stc_fnames = glob.glob(op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'baseline-{}-stcs'.format(inverse_method), '{}-epilepsy-{}-{}-*-rh.stc'.format(subject, inverse_method, modality))) if len(baseline_stc_fnames) == 0: print('No baseline!') return False fol = utils.make_dir(op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'ictal-{}-zvals-stcs'.format(inverse_method))) params = [(subject, clip_fname, baseline_stc_fnames, modality, inverse_method, fol, from_index, to_index, use_abs, overwrite) for clip_fname in ictal_clips] utils.run_parallel(_calc_zvals_parallel, params, n_jobs)
def calc_rois_connectivity( subject, clips, modality, atlas, inverse_method, min_order=1, max_order=20, crop_times=(-0.5, 1), onset_time=2, windows_length=0.1, windows_shift=0.05, overwrite=False, n_jobs=4): windows_length *= 1000 windows_shift *= 1000 params = [] clusters_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality), 'clusters') fwd_usingMEG, fwd_usingEEG = meg.get_fwd_flags(modality) bands = {'all': [None, None]} crop_times = [t + onset_time for t in crop_times] use_functional_rois_atlas = False conds = [utils.namebase(clip_fname) for clip_fname in clips['ictal']] conds.extend(['{}_baseline'.format(utils.namebase(clip_fname)) for clip_fname in clips['baseline']]) if not use_functional_rois_atlas: labels = lu.read_labels(subject, SUBJECTS_DIR, atlas) func_atlas = con_indentifer = atlas for clip_fname, cond in zip(clips['ictal'] + clips['baseline'], conds): if use_functional_rois_atlas: check_connectivity_labels(clips['ictal'], modality, inverse_method, n_jobs=n_jobs) labels_fol = op.join( clusters_fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format( subject, inverse_method, modality, utils.namebase(clip_fname))) labels = lu.read_labels_files(subject, labels_fol, n_jobs=n_jobs) # for connectivity we need shorter names labels = epi_utils.shorten_labels_names(labels) func_atlas = utils.namebase(clip_fname) con_indentifer = 'func_rois' params.append(( subject, clip_fname, utils.namebase(clip_fname), func_atlas, labels, inverse_method, fwd_usingMEG, fwd_usingEEG, crop_times, bands, min_order, max_order, windows_length, windows_shift, con_indentifer, overwrite, 1)) utils.run_parallel(_calc_clip_rois_connectivity_parallel, params, n_jobs)
def _calc_zvals_parallel(p): subject, clip_fname, baseline_fnames, modality, inverse_method, fol, from_index, to_index, use_abs, overwrite = p stc_zvals_fname = op.join( fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format( subject, inverse_method, modality, utils.namebase(clip_fname))) if utils.both_hemi_files_exist('{}-{}.stc'.format( stc_zvals_fname, '{hemi}')) and not overwrite: return True stc_fname = op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'ictal-{}-stcs'.format(inverse_method), '{}-epilepsy-{}-{}-{}'.format(subject, inverse_method, modality, utils.namebase(clip_fname))) if not utils.both_hemi_files_exist('{}-{}.stc'.format(stc_fname, '{hemi}')): print('Error finding {}!'.format(stc_fname)) return False return meg.calc_stc_zvals( subject, '{}-rh.stc'.format(stc_fname), baseline_fnames, stc_zvals_fname, # '{}-rh.stc'.format(baseline_fname) use_abs, from_index, to_index, True, overwrite)
def calc_baseline_threshold(subject, modality, inverse_method, overwrite=False): baseline_threshold_fname = op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'baseline-{}-stcs'.format(inverse_method), 'averaged_threhold.npy') if op.isfile(baseline_threshold_fname) and not overwrite: return np.load(baseline_threshold_fname) baseline_stc_fnames = glob.glob(op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'baseline-{}-stcs'.format(inverse_method), '{}-epilepsy-{}-{}-*-rh.stc'.format(subject, inverse_method, modality))) baseline_data = np.array([mne.read_source_estimate(baseline_fname).data for baseline_fname in baseline_stc_fnames]) N, V, T = baseline_data.shape baseline_data = np.reshape(baseline_data, (V, N * T)) baseline_threshold = np.median(np.max(baseline_data, axis=0).squeeze()) print('baseline threshold: {}'.format(baseline_threshold)) np.save(baseline_threshold_fname, baseline_threshold) return baseline_threshold
def calc_accumulate_stc( subject, ictal_clips, modality, seizure_times, mean_baseline, inverse_method, reverse, set_t_as_val, n_jobs): modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality)) stcs_fol = op.join(modality_fol, 'ictal-{}-zvals-stcs'.format(inverse_method)) params = [(subject, clip_fname, inverse_method, modality, seizure_times, mean_baseline, stcs_fol, reverse, set_t_as_val, n_jobs) for clip_fname in ictal_clips] ictals = utils.run_parallel(_calc_ictal_and_baseline_parallel, params, n_jobs) return ictals
def calc_func_rois_vertives_lookup( subject, ictal_clips, modality, inverse_method, seizure_times, windows_length, windows_shift): modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality)) windows = calc_windows(seizure_times, windows_length, windows_shift) labels_fols = [] for ictal_fname in ictal_clips: for from_t, to_t in windows: labels_fol = op.join(modality_fol, 'clusters', '{}-epilepsy-{}-{}-{}-amplitude-zvals-{:.2f}-{:.2f}'.format( subject, inverse_method, modality, utils.namebase(ictal_fname), from_t, to_t)) labels_fols.append(labels_fol) utils.run_parallel(_calc_func_rois_vertives_lookup_parallel, labels_fols, n_jobs)
def find_functional_rois(subject, ictal_clips, modality, seizure_times, atlas, min_cluster_size, inverse_method, overwrite=False, n_jobs=4): fwd_usingMEG, fwd_usingEEG = meg.get_fwd_flags(modality) modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality)) stcs_fol = op.join(modality_fol, 'ictal-{}-zvals-stcs'.format(inverse_method)) ictlas_fname = op.join( modality_fol, '{}-epilepsy-{}-{}-amplitude-zvals-ictals.pkl'.format( subject, inverse_method, modality)) # Make sure we have a morph map, and if not, create it here, and not in the parallel function mne.surface.read_morph_map(subject, subject, subjects_dir=SUBJECTS_DIR) connectivity = anat.load_connectivity(subject) if overwrite: utils.delete_folder_files( op.join(MMVT_DIR, subject, modality_fol, 'clusters')) if op.isfile(ictlas_fname): ictals = utils.load(ictlas_fname) else: params = [(subject, clip_fname, inverse_method, modality, seizure_times, stcs_fol, n_jobs) for clip_fname in ictal_clips] ictals = utils.run_parallel(_calc_ictal_and_baseline_parallel, params, n_jobs) utils.save(ictals, ictlas_fname) for stc_name, ictal_stc, mean_baseline in ictals: max_ictal = ictal_stc.data.max() if max_ictal < mean_baseline: print('max ictal ({}) < mean baseline ({})!'.format( max_ictal, mean_baseline)) continue meg.find_functional_rois_in_stc(subject, subject, atlas, utils.namebase(stc_name), mean_baseline, threshold_is_precentile=False, extract_time_series_for_clusters=False, time_index=0, min_cluster_size=min_cluster_size, min_cluster_max=mean_baseline, fwd_usingMEG=fwd_usingMEG, fwd_usingEEG=fwd_usingEEG, stc_t_smooth=ictal_stc, modality=modality, connectivity=connectivity, n_jobs=n_jobs)
def check_connectivity_labels(ictal_clips, modality, inverse_method, n_jobs=1): # Check if can calc the labels info (if not, we want to know now...) for clip_fname in ictal_clips: print('Checking {} labels'.format(utils.namebase(clip_fname))) labels_fol = op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'clusters', '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format( subject, inverse_method, modality, utils.namebase(clip_fname))) labels = lu.read_labels_files(subject, labels_fol, n_jobs=n_jobs) if len(labels) == 0: raise Exception('No labels!') else: print('{} labels'.format(len(labels))) connectivity.calc_lables_info(subject, labels=labels)
def find_functional_rois( subject, ictal_clips, modality, seizure_times, atlas, min_cluster_size, inverse_method, mean_baseline, windows_length=0.1, windows_shift=0.05, overwrite=False, n_jobs=4): modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality)) # Make sure we have a morph map, and if not, create it here, and not in the parallel function mne.surface.read_morph_map(subject, subject, subjects_dir=SUBJECTS_DIR) connectivity = anat.load_connectivity(subject) if overwrite: utils.delete_folder_files(op.join(MMVT_DIR, subject, modality_fol, 'clusters')) windows = calc_windows(seizure_times, windows_length, windows_shift) params = [] for ictal_clip in ictal_clips: for from_t, to_t in windows: params.append((subject, modality, ictal_clip, atlas, inverse_method, mean_baseline, from_t, to_t, min_cluster_size, connectivity)) utils.run_parallel(_find_functional_rois_parallel, params, n_jobs)
def calc_clip_rois_connectivity(p): (subject, clip_fname, baseline_epochs, modality, inverse_method, min_order, max_order, crop_times, onset_time, windows_length, windows_shift, overwrite, n_jobs) = p clusters_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality), 'clusters') fwd_usingMEG, fwd_usingEEG = meg.get_fwd_flags(modality) bands = {'all': [None, None]} crop_times = [t + onset_time for t in crop_times] labels_fol = op.join( clusters_fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format( subject, inverse_method, modality, utils.namebase(clip_fname))) labels = lu.read_labels_files(subject, labels_fol, n_jobs=n_jobs) # for connectivity we need shorter names labels = epi_utils.shorten_labels_names(labels) ictal_evoked = mne.read_evokeds(clip_fname)[0] atlas = utils.namebase(clip_fname) for clip, cond in zip([ictal_evoked, baseline_epochs], [ utils.namebase(clip_fname), '{}_baseline'.format( utils.namebase(clip_fname)) ]): meg.calc_labels_connectivity(subject, atlas, {cond: 1}, subjects_dir=SUBJECTS_DIR, mmvt_dir=MMVT_DIR, inverse_method=inverse_method, pick_ori='normal', fwd_usingMEG=fwd_usingMEG, fwd_usingEEG=fwd_usingEEG, con_method='gc', overwrite_connectivity=overwrite, crops_times=crop_times, epochs=clip, bands=bands, con_indentifer='func_rois', labels=labels, min_order=min_order, max_order=max_order, downsample=2, windows_length=windows_length, windows_shift=windows_shift, n_jobs=n_jobs)
def _calc_ictal_and_baseline_parallel(p): (subject, clip_fname, inverse_method, modality, seizure_times, mean_baseline, stcs_fol, reverse, set_t_as_val,\ n_jobs) = p # print('Calculating accumulate_stc for {}'.format(utils.namebase(clip_fname))) stc_name = op.join(stcs_fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format( subject, inverse_method, modality, utils.namebase(clip_fname))) if not stc_exist(stc_name): raise Exception('Cannot find stc_name!') stc = mne.read_source_estimate('{}-rh.stc'.format(stc_name)) # t_from, t_to = stc.time_as_index(seizure_times[0])[0], stc.time_as_index(seizure_times[1])[0] t_from, t_to = [stc.time_as_index(seizure_times[k])[0] for k in range(2)] # mean_baseline_quick = np.median(np.max(stc.data[:, :stc.time_as_index(0)[0]], axis=0).squeeze()) # mean_baseline = calc_baseline_mean(subject, stc, n_jobs) modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality)) labels_fol = op.join(modality_fol, 'clusters', '{}-epilepsy-{}-{}-{}-amplitude-zvals-{:.2f}-{:.2f}'.format( subject, inverse_method, modality, utils.namebase(clip_fname), seizure_times[0], seizure_times[1])) lookup_fname = op.join(labels_fol, 'vertices_lookup.pkl') if set_t_as_val and not op.isfile(lookup_fname): raise Exception('Can\'t find labels lookup! {}'.format(lookup_fname)) ictal_stc, labels_times = meg.accumulate_stc( subject, stc, t_from, t_to, mean_baseline, lookup_fname, reverse=reverse, set_t_as_val=set_t_as_val, n_jobs=n_jobs) return clip_fname, stc_name, ictal_stc, mean_baseline, labels_times,
def calc_accumulate_stc_as_time(subject, ictal_clips, modality, seizure_times, windows_length, windows_shift, mean_baseline, inverse_method, n_jobs): modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality)) stcs_fol = utils.make_dir(op.join(modality_fol, 'time_accumulate')) windows = calc_windows(seizure_times, windows_length, windows_shift) for ictal_clip in ictal_clips: output_fname = op.join(stcs_fol, '{}_labels_times.txt'.format(utils.namebase(ictal_clip))) output_str = '{}:\n'.format(utils.namebase(ictal_clip)) for from_t, to_t in windows: ictals = calc_accumulate_stc( subject, [ictal_clip], modality, (from_t, to_t), mean_baseline, inverse_method, False, True, n_jobs) ictal_fname, stc_name, ictal_stc, mean_baseline, labels_times = ictals[0] utils.save(labels_times, op.join(stcs_fol, '{}_{:.2f}_{:.2f}_labels_times.pkl'.format( utils.namebase(stc_name), from_t, to_t))) for label_name, label_time in labels_times.items(): output_str += '{}: {:.4f}\n'.format('_'.join(label_name.split('_')[-2:]), label_time) # stc_output_fname = op.join(stcs_fol, '{}-time-acc'.format(utils.namebase(stc_name))) # print('Saving accumulate stc: {}'.format(stc_output_fname)) # ictal_stc.save(stc_output_fname) print('Saving {}'.format(output_fname)) with open(output_fname, 'w') as output_file: print(output_str, file=output_file)
def calc_rois_connectivity(subject, clips, modality, inverse_method, min_order=1, max_order=20, crop_times=(-0.5, 1), onset_time=2, windows_length=100, windows_shift=10, overwrite=False, n_jobs=4): check_connectivity_labels(clips['ictal'], modality, inverse_method) baseline_epochs_fname = op.join(MMVT_DIR, subject, meg.modality_fol(modality), 'baseline-epo.fif') baseline_epochs = epi_utils.combine_windows_into_epochs( clips['baseline'], baseline_epochs_fname) params = [(subject, clip_fname, baseline_epochs, modality, inverse_method, min_order, max_order, crop_times, onset_time, windows_length, windows_shift, overwrite, n_jobs) for clip_fname in clips['ictal']] utils.run_parallel(calc_clip_rois_connectivity, params, 1)