def plot_topomaps(subject, modality, windows, bad_channels, parallel=True): figs_fol = utils.make_dir( op.join(MMVT_DIR, subject, 'epilepsy-figures', 'topomaps')) params = [(subject, modality, window_fname, bad_channels, figs_fol) for window_fname in windows] utils.run_parallel(_plot_topomaps_parallel, params, len(windows) if parallel else 1)
def run_function_in_parallel(func, all_params, n_jobs, split_jobs=True, verbose=True): # Filter our items that should not run (all input files should exist, and all output files shoud not) params = [ p for p in all_params if all(_run_func_in_parallel((func, [p], True, 0, verbose))) ] print('*** Run {}/{} records, {} jobs ***'.format(len(params), len(all_params), n_jobs)) ret = input('ok? (y/n) ') if not au.is_true(ret): return if split_jobs: chunks_indices = np.array_split(np.arange(len(params)), n_jobs) chunk_params = [ (func, [params[ind] for ind in chunk_indices], False, thread_ind, verbose) for thread_ind, chunk_indices in enumerate(chunks_indices) ] else: chunk_params = [(func, [p], False, thread_ind, verbose) for thread_ind, p in enumerate(params)] utils.run_parallel(_run_func_in_parallel, chunk_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 solve_labels_collision(subject, subjects_dir, atlas, backup_atlas, n_jobs=1): now = time.time() print('Read labels') labels = utils.read_labels_parallel(subject, subjects_dir, atlas, n_jobs) backup_labels_fol = op.join(subjects_dir, subject, 'label', backup_atlas) labels_fol = op.join(subjects_dir, subject, 'label', atlas) if op.isdir(backup_labels_fol): shutil.rmtree(backup_labels_fol) os.rename(labels_fol, backup_labels_fol) utils.make_dir(labels_fol) hemis_verts, labels_hemi, pia_verts = {}, {}, {} print('Read surface ({:.2f}s)'.format(time.time() - now)) for hemi in HEMIS: surf_fname = op.join(subjects_dir, subject, 'surf', '{}.pial'.format(hemi)) hemis_verts[hemi], _ = mne.surface.read_surface(surf_fname) labels_hemi[hemi] = [l for l in labels if l.hemi == hemi] print('Calc centroids ({:.2f}s)'.format(time.time() - now)) centroids = calc_labels_centroids(labels_hemi, hemis_verts) for hemi in HEMIS: print('Calc vertices labeling for {} ({:.2f}s)'.format(hemi, time.time() - now)) hemi_centroids_dist = cdist(hemis_verts[hemi], centroids[hemi]) vertices_labels_indices = np.argmin(hemi_centroids_dist, axis=1) labels_hemi_chunks = utils.chunks(list(enumerate(labels_hemi[hemi])), len(labels_hemi[hemi]) / n_jobs) params = [(labels_hemi_chunk, atlas, vertices_labels_indices, hemis_verts, labels_fol) for labels_hemi_chunk in labels_hemi_chunks] print('Save labels for {} ({:.2f}s)'.format(hemi, time.time() - now)) utils.run_parallel(_save_new_labels_parallel, params, n_jobs)
def morph_electrodes(electrodes, template_system, subjects_dir, mmvt_dir, overwrite=False, print_only=False, n_jobs=4): subject_to = 'fsaverage' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system output_fname = op.join(mmvt_dir, '{subject}', 'electrodes', 'electrodes_morph_to_{}.txt'.format(subject_to)) subjects = [ s for s in list(electrodes.keys()) if overwrite or not op.isfile(output_fname.format(subject=s)) ] if len(subjects) > 0: indices = np.array_split(np.arange(len(subjects)), n_jobs) chunks = [([subjects[ind] for ind in chunk_indices], subject_to, subjects_dir, mmvt_dir, output_fname, overwrite, print_only) for chunk_indices in indices] utils.run_parallel(_morph_electrodes_parallel, chunks, n_jobs) bad_subjects, good_subjects = [], [] for subject_from in list(electrodes.keys()): ret = op.isfile(output_fname.format(subject=subject_from)) if not ret: bad_subjects.append(subject_from) else: good_subjects.append(subject_from) print('morph_electrodes:') print('good subjects: {}'.format(good_subjects)) print('bad subjects: {}'.format(bad_subjects)) print('{}/{} good subjects'.format(len(good_subjects), len(list(electrodes.keys()))))
def create_movie(time_range, xticks, fol, dpi, fps, video_fname, cb_data_type, data_to_show_in_graph, cb_title='', cb_min_max_eq=True, cb_norm_percs=None, color_map='jet', bitrate=5000, fol2='', cb2_data_type='', cb2_title='', cb2_min_max_eq=True, color_map2='jet', ylim=(), ylabels=(), xticklabels=(), xlabel='Time (ms)', pics_type='png', show_first_pic=False, show_animation=False, overwrite=True, n_jobs=1): images1 = get_pics(fol, pics_type)[:len(time_range)] images1_chunks = utils.chunks(images1, len(images1) / n_jobs) if fol2 != '': images2 = get_pics(fol2, pics_type) if len(images2) != len(images1): raise Exception('fol and fol2 have different number of pictures!') images2_chunks = utils.chunks(images2, int(len(images2) / n_jobs)) else: images2_chunks = [''] * int(len(images1) / n_jobs) params = [(images1_chunk, images2_chunk, time_range, xticks, dpi, fps, video_fname, cb_data_type, data_to_show_in_graph, cb_title, cb_min_max_eq, cb_norm_percs, color_map, bitrate, ylim, ylabels, xticklabels, xlabel, show_first_pic, fol, fol2, cb2_data_type, cb2_title, cb2_min_max_eq, color_map2, run, show_animation, overwrite) for \ run, (images1_chunk, images2_chunk) in enumerate(zip(images1_chunks, images2_chunks))] n_jobs = utils.get_n_jobs(n_jobs) if n_jobs > 1: utils.run_parallel(_create_movie_parallel, params, n_jobs) video_name, video_type = op.splitext(video_fname) mu.combine_movies(fol, video_name, video_type[1:]) else: for p in params: _create_movie_parallel(p)
def solve_labels_collision(subject, subjects_dir, atlas, backup_atlas, n_jobs=1): now = time.time() print('Read labels') # utils.read_labels_parallel(subject, subjects_dir, atlas, labels_fol='', n_jobs=n_jobs) labels = read_labels(subject, subjects_dir, atlas, n_jobs=n_jobs) backup_labels_fol = op.join(subjects_dir, subject, 'label', backup_atlas) labels_fol = op.join(subjects_dir, subject, 'label', atlas) if op.isdir(backup_labels_fol): shutil.rmtree(backup_labels_fol) os.rename(labels_fol, backup_labels_fol) utils.make_dir(labels_fol) hemis_verts, labels_hemi, pia_verts = {}, {}, {} print('Read surface ({:.2f}s)'.format(time.time() - now)) for hemi in HEMIS: surf_fname = op.join(subjects_dir, subject, 'surf', '{}.pial'.format(hemi)) hemis_verts[hemi], _ = mne.surface.read_surface(surf_fname) labels_hemi[hemi] = [l for l in labels if l.hemi == hemi] print('Calc centroids ({:.2f}s)'.format(time.time() - now)) centroids = calc_labels_centroids(labels_hemi, hemis_verts) for hemi in HEMIS: print('Calc vertices labeling for {} ({:.2f}s)'.format(hemi, time.time() - now)) hemi_centroids_dist = cdist(hemis_verts[hemi], centroids[hemi]) vertices_labels_indices = np.argmin(hemi_centroids_dist, axis=1) labels_hemi_chunks = utils.chunks(list(enumerate(labels_hemi[hemi])), len(labels_hemi[hemi]) / n_jobs) params = [(labels_hemi_chunk, atlas, vertices_labels_indices, hemis_verts, labels_fol) for labels_hemi_chunk in labels_hemi_chunks] print('Save labels for {} ({:.2f}s)'.format(hemi, time.time() - now)) utils.run_parallel(_save_new_labels_parallel, params, n_jobs)
def calc_amplitude(subject, modality, run_num, windows_fnames, inverse_method='dSPM', overwrite=False, n_jobs=4): params = [(subject, window_fname, modality, run_num, windows_fnames, inverse_method, overwrite) for window_fname in windows_fnames] utils.run_parallel(_calc_amplitude_parallel, params, n_jobs)
def plot_max_powers(subject, windows_fnames, modality, inverse_method='dSPM', overwrite=False, parallel=True): params = [(subject, window_fname, modality, inverse_method, overwrite) for window_fname in windows_fnames] utils.run_parallel(_plot_max_powers_parllel, params, len(windows_fnames) if parallel else 1)
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 calc_amplitude_zvals(subject, windows_fnames, baseline_name, modality, from_index=None, to_index=None, inverse_method='dSPM', parallel=True, overwrite=False): params = [(subject, modality, window_fname, baseline_name, from_index, to_index, inverse_method, overwrite) for window_fname in windows_fnames] utils.run_parallel(_calc_amplitude_zvals_parallel, params, len(windows_fnames) if parallel else 1)
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 plot_stcs_files(subject, modality, n_jobs=4): modality_fol = op.join(MMVT_DIR, subject, 'eeg' if modality == 'eeg' else 'meg') stc_files = [ f for f in glob.glob(op.join(modality_fol, '*-lh.stc')) if '-epilepsy-' in utils.namebase(f) and '-zvals-' in utils.namebase(f) and '-{}-'.format(modality) in utils.namebase(f) ] print('{} files for {}'.format(len(stc_files), modality)) figures_fol = utils.make_dir( op.join(MMVT_DIR, subject, 'epilepsy-figures', 'all_stcs', modality)) utils.run_parallel(_plot_stcs_files_parallel, [(stc_fname, figures_fol) for stc_fname in stc_files], n_jobs)
def plot_modalities(subject, windows, modalities, bands, inverse_method, max_t=0, overwrite=False, n_jobs=4): from itertools import product figures_fol = utils.make_dir( op.join(MMVT_DIR, subject, 'epilepsy-figures', 'modalities')) params = [(subject, modalities, inverse_method, figures_fol, band, window_fname, max_t, overwrite) for (band, window_fname) in product(bands, windows)] utils.run_parallel(_plot_modalities_parallel, params, n_jobs)
def morph_electrodes(electrodes, template_system, subjects_dir, mmvt_dir, overwrite=False, print_only=False, n_jobs=4): subject_to = 'fsaverage5' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system subjects = list(electrodes.keys()) indices = np.array_split(np.arange(len(subjects)), n_jobs) chunks = [([subjects[ind] for ind in chunk_indices], subject_to, subjects_dir, overwrite, print_only) for chunk_indices in indices] utils.run_parallel(_morph_electrodes_parallel, chunks, n_jobs)
def morph_labels_from_fsaverage(subject, subjects_dir, mmvt_dir, aparc_name='aparc250', fs_labels_fol='', sub_labels_fol='', n_jobs=6, fsaverage='fsaverage', overwrite=False): fsaverage = find_template_brain_with_annot_file(aparc_name, fsaverage, subjects_dir) if fsaverage == '': return False if subject == fsaverage: return True subject_dir = op.join(subjects_dir, subject) labels_fol = op.join(subjects_dir, fsaverage, 'label', aparc_name) if fs_labels_fol == '' else fs_labels_fol sub_labels_fol = op.join( subject_dir, 'label', aparc_name) if sub_labels_fol == '' else sub_labels_fol if not op.isdir(sub_labels_fol): os.makedirs(sub_labels_fol) labels = read_labels(fsaverage, subjects_dir, aparc_name, n_jobs=n_jobs) verts = utils.load_surf(subject, mmvt_dir, subjects_dir) # 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, fsaverage, subjects_dir=subjects_dir) indices = np.array_split(np.arange(len(labels)), n_jobs) chunks = [([labels[ind] for ind in chunk_indices], subject, fsaverage, labels_fol, sub_labels_fol, verts, subjects_dir, overwrite) for chunk_indices in indices] results = utils.run_parallel(_morph_labels_parallel, chunks, n_jobs) return all(results)
def read_labels_parallel(subject, subjects_dir, atlas, hemi='', labels_fol='', n_jobs=1): try: labels_fol = op.join(subjects_dir, subject, 'label', atlas) if labels_fol == '' else labels_fol if hemi != '': labels_files = glob.glob( op.join(labels_fol, '*{}.label'.format(hemi))) labels_files.extend( glob.glob(op.join(labels_fol, '{}.*label'.format(hemi)))) else: labels_files = glob.glob(op.join(labels_fol, '*.label')) files_chunks = utils.chunks(labels_files, len(labels_files) / n_jobs) results = utils.run_parallel(_read_labels_parallel, files_chunks, njobs=n_jobs) labels = [] for labels_chunk in results: labels.extend(labels_chunk) return labels except: print(traceback.format_exc()) return []
def save_labels_from_vertices_lookup(subject, atlas, subjects_dir, mmvt_dir, surf_type='pial', read_labels_from_fol='', overwrite_vertices_labels_lookup=False, n_jobs=6): lookup = create_vertices_labels_lookup( subject, atlas, read_labels_from_fol=read_labels_from_fol, overwrite=overwrite_vertices_labels_lookup) labels_fol = op.join(subjects_dir, subject, 'label', atlas) surf = utils.load_surf(subject, mmvt_dir, subjects_dir) utils.delete_folder_files(labels_fol) ok = True for hemi in utils.HEMIS: labels_vertices = defaultdict(list) # surf_fname = op.join(subjects_dir, subject, 'surf', '{}.{}'.format(hemi, surf_type)) # surf, _ = mne.surface.read_surface(surf_fname) for vertice, label in lookup[hemi].items(): labels_vertices[label].append(vertice) chunks_indices = np.array_split(np.arange(len(labels_vertices)), n_jobs) labels_vertices_items = list(labels_vertices.items()) chunks = [([labels_vertices_items[ind] for ind in chunk_indices], subject, labels_vertices, surf, hemi, labels_fol) for chunk_indices in chunks_indices] results = utils.run_parallel(_save_labels_from_vertices_lookup_hemi, chunks, n_jobs) ok = ok and all(results) return ok
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 plot_baseline(subject, baseline_name): stc_fnames = glob.glob( op.join(MMVT_DIR, subject, 'meg', '{}-epilepsy-*-{}_*.stc'.format(subject, baseline_name))) + \ glob.glob(op.join(MMVT_DIR, subject, 'eeg', 'non-zvals', '{}-epilepsy-*-{}_*.stc'.format(subject, baseline_name))) if len(stc_fnames) == 0: stc_fnames = glob.glob( op.join(MMVT_DIR, subject, 'meg', 'non-zvals', '{}-epilepsy-*-{}_*.stc'.format(subject, baseline_name))) + \ glob.glob(op.join(MMVT_DIR, subject, 'eeg', 'non-zvals', '{}-epilepsy-*-{}_*.stc'.format(subject, baseline_name))) if len(stc_fnames) == 0: print('No baselines stc files were found!') return figures_fol = utils.make_dir( op.join(MMVT_DIR, subject, 'epilepsy-figures', 'baseline')) utils.run_parallel(_plot_stcs_files_parallel, [(stc_fname, figures_fol) for stc_fname in stc_fnames], n_jobs)
def cvs_register_to_template(subjects, template_system, subjects_dir, overwrite=False, print_only=False, n_jobs=1): subject_to = 'fsaverage5' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system subjects = [ s for s in subjects if s != subject_to and (overwrite or not op.isfile( op.join(subjects_dir, s, f'mri_cvs_register_to_{subject_to}', f'final_CVSmorph_to{subject_to}.m3z'))) ] indices = np.array_split(np.arange(len(subjects)), n_jobs) chunks = [([subjects[ind] for ind in chunk_indices], subject_to, subjects_dir, overwrite, print_only) for chunk_indices in indices] utils.run_parallel(_mri_cvs_register_parallel, chunks, n_jobs)
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 read_labels_parallel(subject, subjects_dir, atlas, n_jobs): labels_files = glob.glob(op.join(subjects_dir, subject, 'label', atlas, '*.label')) files_chunks = utils.chunks(labels_files, len(labels_files) / n_jobs) results = utils.run_parallel(_read_labels_parallel, files_chunks, n_jobs) labels = [] for labels_chunk in results: labels.extend(labels_chunk) return labels
def read_labels_parallel(subject, subjects_dir, atlas, labels_fol='', n_jobs=1): labels_fol = op.join(subjects_dir, subject, 'label', atlas) if labels_fol == '' else labels_fol labels_files = glob.glob(op.join(labels_fol, '*.label')) files_chunks = utils.chunks(labels_files, len(labels_files) / n_jobs) results = utils.run_parallel(_read_labels_parallel, files_chunks, n_jobs) labels = [] for labels_chunk in results: labels.extend(labels_chunk) return labels
def cvs_register_to_template(electrodes, template_system, subjects_dir, overwrite=False, print_only=False, n_jobs=1, openmp=8): subject_to = 'fsaverage' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system if subject_to == 'fsaverage': output_fname = op.join( subjects_dir, '{subject}', 'mri_cvs_register_to_mni', 'combined_tocvs_avg35_inMNI152_elreg_afteraseg-norm.tm3d') else: output_fname = op.join( subjects_dir, '{subject}', 'mri_cvs_register_to_{}'.format(subject_to), 'combined_to{}_elreg_afteraseg-norm.tm3d'.format(subject_to)) subjects = list(electrodes.keys()) for subject in subjects: if op.isfile(output_fname.format(subject=subject)): print('{} was already morphed to {}'.format(subject, subject_to)) no_morph_subjects = [] for subject in subjects: if not op.isfile(output_fname.format(subject=subject)): print('{} has to be morphed to {}'.format(subject, subject_to)) no_morph_subjects.append(subject) print('Need to morph {}/{} subjecs'.format(len(no_morph_subjects), len(subjects))) subjects = [ s for s in subjects if s != subject_to and ( overwrite or not op.isfile(output_fname.format(subject=s))) ] if len(subjects) == 0: return print('subject to morph: ') print(subjects) indices = np.array_split(np.arange(len(subjects)), n_jobs) chunks = [([subjects[ind] for ind in chunk_indices], subject_to, subjects_dir, overwrite, print_only, openmp) for chunk_indices in indices] utils.run_parallel(_mri_cvs_register_parallel, chunks, n_jobs) return subjects
def create_movie(time_range, xticks, fol, dpi, fps, video_fname, cb_data_type, data_to_show_in_graph, cb_title='', cb_min_max_eq=True, color_map='jet', bitrate=5000, fol2='', ylim=(), ylabels=(), xticklabels=(), xlabel='Time (ms)', pics_type='png', show_first_pic=False, n_jobs=1): images1 = get_pics(fol, pics_type) images1_chunks = utils.chunks(images1, len(images1) / n_jobs) if fol2 != '': images2 = get_pics(fol2, pics_type) if len(images2) != len(images1): raise Exception('fol and fol2 have different number of pictures!') images2_chunks = utils.chunks(images2, int(len(images2) / n_jobs)) else: images2_chunks = [''] * int(len(images1) / n_jobs) params = [(images1_chunk, images2_chunk, time_range, xticks, dpi, fps, video_fname, cb_data_type, data_to_show_in_graph, cb_title, cb_min_max_eq, color_map, bitrate, ylim, ylabels, xticklabels, xlabel, show_first_pic, fol, fol2, run) for \ run, (images1_chunk, images2_chunk) in enumerate(zip(images1_chunks, images2_chunks))] utils.run_parallel(_create_movie_parallel, params, n_jobs) video_name, video_type = op.splitext(video_fname) mu.combine_movies(fol, video_name, video_type[1:])
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 analyze_graph_data(con, n_jobs): T = con.shape[2] con[con < np.percentile(con, 80)] = 0 indices = np.array_split(np.arange(T), n_jobs) chunks = [(con, indices_chunk, graph_func) for indices_chunk in indices] results = utils.run_parallel(_calc_graph_func, chunks, n_jobs) first = True for vals_chunk, times_chunk in results: if first: values = np.zeros((len(vals_chunk[0]), T)) first = False values[:, times_chunk] = vals_chunk.T return values
def plot_norm_powers_per_label(subject, windows_fnames, baseline_window, modality, inverse_method='dSPM', calc_also_non_norm_powers=False, overwrite=False, n_jobs=4): root_dir = op.join(EEG_DIR if modality == 'eeg' else MEG_DIR, subject) baseline_fol = op.join( root_dir, '{}-epilepsy-{}-{}-{}-induced_power'.format( subject, inverse_method, modality, utils.namebase(baseline_window))) baseline_labels = glob.glob( op.join(baseline_fol, 'epilepsy_*_induced_power.npy')) indices = np.array_split(np.arange(len(baseline_labels)), n_jobs) chunks = [([baseline_labels[ind] for ind in chunk_indices ], subject, windows_fnames, inverse_method, modality, baseline_window, calc_also_non_norm_powers, overwrite) for chunk_indices in indices] utils.run_parallel(_plot_norm_powers_per_label_parallel, chunks, n_jobs)
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)
def _calc_source_ttest(subject): fol = op.join(MMVT_DIR, subject, 'meg') output_fname = op.join(fol, 'dSPM_mean_flip_vertices_power_spectrum_stat') if utils.both_hemi_files_exist('{}-{}.stc'.format( output_fname, '{hemi}')) and not args.overwrite: print('{} already exist'.format(output_fname)) return True file_name = '{cond}_dSPM_mean_flip_vertices_power_spectrum.pkl' if not all([ op.isfile(op.join(fol, file_name.format(cond=cond.lower()))) for cond in MSIT_CONDS ]): print('No stc files for both conditions!') return False vertices_data = {} try: for cond in MSIT_CONDS: vertices_data[cond], freqs = utils.load( op.join(fol, file_name.format(cond=cond.lower()))) except: print('Can\'t read {}'.format(file_name.format(cond=cond.lower()))) return False pvals, vertno = {}, {} for hemi in utils.HEMIS: vertices_inds = {} pvals[hemi] = np.zeros( (len(vertices_data[MSIT_CONDS[0]][hemi].keys()), len(freqs))) for cond in MSIT_CONDS: vertices_inds[cond] = np.array( sorted(list(vertices_data[cond][hemi].keys()))) if not np.all( vertices_inds[MSIT_CONDS[0]] == vertices_inds[MSIT_CONDS[1]]): raise Exception('Not the same vertices!') vertno[hemi] = vertices_inds[MSIT_CONDS[0]] params = [(vert_ind, vertices_data[MSIT_CONDS[0]][hemi][vert], vertices_data[MSIT_CONDS[1]][hemi][vert], len(freqs)) \ for vert_ind, vert in enumerate(vertices_data[MSIT_CONDS[0]][hemi].keys())] results = utils.run_parallel(calc_vert_pvals, params, args.n_jobs) for vert_pvals, vert_ind in results: pvals[hemi][vert_ind, :] = vert_pvals data = np.concatenate([pvals['lh'], pvals['rh']]) vertices = [vertno['lh'], vertno['rh']] stc_pvals = mne.SourceEstimate(data, vertices, freqs[0], freqs[1] - freqs[0], subject=subject) print('Writing to {}'.format(output_fname)) stc_pvals.save(output_fname)
def morph_electrodes(subjects_electrodes, subject_to='colin27', atlas='aparc.DKTatlas', annotation_template='fsaverage', overwrite=False, n_jobs=1): subjects = list(subjects_electrodes.keys()) indices = np.array_split(np.arange(len(subjects)), n_jobs) chunks = [([subjects[ind] for ind in chunk_indices], atlas, subject_to, subjects_electrodes, annotation_template, overwrite) for chunk_indices in indices] results = utils.run_parallel(_morph_electrodes_parallel, chunks, n_jobs) for bad_subjects in results: for bad_subject in bad_subjects: print(bad_subject)
def calc_measures(subject, con_name, func_name, n_jobs=4): fol = op.join(MMVT_DIR, subject, 'connectivity') print('Loading {}'.format(op.join(fol, '{}.npy'.format(con_name)))) con = np.load(op.join(fol, '{}.npy'.format(con_name))).squeeze() # names = np.load(op.join(fol, 'labels_names.npy')) T = con.shape[2] con[con < np.percentile(con, 99)] = 0 indices = np.array_split(np.arange(T), n_jobs) chunks = [(con, indices_chunk) for indices_chunk in indices] results = utils.run_parallel(calc_closeness_centrality, chunks, n_jobs) first = True for vals_chunk, times_chunk in results: if first: values = np.zeros((len(vals_chunk[0]), T)) first = False values[:, times_chunk] = vals_chunk.T print('{}: min={}, max={}, mean={}'.format(con_name, np.min(values), np.max(values), np.mean(values))) np.save(op.join(fol, '{}_{}.npy'.format(con_name, func_name)), values)