def create_movie(): output_file = op.join(bpy.context.scene.output_path, 'combine_images_cmd.txt') if op.isfile(output_file): os.remove(output_file) flags = '--fol {} --copy_files 1 --frame_rate {}'.format( bpy.context.scene.output_path, bpy.context.scene.frames_num) mu.run_mmvt_func('src.utils.movies_utils', 'combine_images', flags=flags)
def load_surf_files(nii_fname, run_fmri_preproc=True, user_fol='', debug=True): fmri_file_template = '' if user_fol == '': user_fol = mu.get_user_fol() nii_fol = mu.get_fname_folder(nii_fname) if debug: print('load_surf_files: nii_fol: {}'.format(nii_fol)) hemi, fmri_hemis = mu.get_hemi_from_full_fname(nii_fname) if debug: print('load_surf_files: hemi, fmri_hemis: {}, {}'.format( hemi, fmri_hemis)) if hemi == '': hemi = mu.find_hemi_using_vertices_num(nii_fname) if hemi == '': return '' # fmri_hemis = mu.get_both_hemis_files(nii_fname) local_fname = build_local_fname(nii_fname, user_fol) if debug: print('load_surf_files: local_fname: {}'.format(local_fname)) mu.make_dir(op.join(user_fol, 'fmri')) if nii_fol != op.join(user_fol, 'fmri'): mu.make_link(nii_fname, local_fname, True) other_hemi = mu.other_hemi(hemi) if debug: print('load_surf_files: other_hemi: {}'.format(other_hemi)) other_hemi_fname = fmri_hemis[other_hemi] if other_hemi_fname == '': other_hemi_fname = local_fname.replace(hemi, other_hemi) if debug: print('load_surf_files: other_hemi_fname: {}'.format(other_hemi_fname)) # todo: if the other hemi file doens't exist, just create an empty one output_fname_template = '' if op.isfile(other_hemi_fname): local_other_hemi_fname = build_local_fname(other_hemi_fname, user_fol) if nii_fol != op.join(user_fol, 'fmri'): mu.make_link(other_hemi_fname, local_other_hemi_fname, True) fmri_file_template = mu.get_template_hemi_label_name( mu.namebase_with_ext(local_fname)) if run_fmri_preproc: mu.add_mmvt_code_root_to_path() from src.preproc import fMRI importlib.reload(fMRI) vertices_num = mu.get_vertices_num() ret, npy_output_fname_template = fMRI.load_surf_files( mu.get_user(), fmri_hemis, vertices_num=vertices_num) output_fname_template = op.join( mu.get_parent_fol(npy_output_fname_template), mu.namebase_with_ext(npy_output_fname_template)[len('fmri_'):]) else: mu.run_mmvt_func( 'src.preproc.fMRI', 'load_surf_files', flags='--fmri_file_template "{}"'.format(fmri_file_template)) # todo: find what should be the output_fname_template else: print( "Couldn't find the other hemi file! ({})".format(other_hemi_fname)) return output_fname_template #, hemi, other_hemi
def grow_a_label(): closest_mesh_name, vertex_ind, _ = \ _addon().find_closest_vertex_index_and_mesh(use_shape_keys=True) hemi = closest_mesh_name[len('infalted_'):] if _addon().is_inflated() else closest_mesh_name subject, atlas = mu.get_user(), bpy.context.scene.subject_annot_files label_name, label_r = bpy.context.scene.new_label_name, bpy.context.scene.new_label_r flags = '-a {} --vertice_indice {} --hemi {} --label_name {} --label_r {}'.format( atlas, vertex_ind, hemi, label_name, label_r) mu.run_mmvt_func('src.preproc.anatomy', 'grow_label', flags=flags)
def invoke(self, context, event=None): electrodes_pos_fname = op.join( mu.get_user_fol(), 'electrodes', 'electrodes{}_positions.npz'.format( '_bipolar' if bpy.context.scene.bipolar else '')) flags = '-a {} -b {} --electrodes_pos_fname {} --ignore_missing 1'.format( bpy.context.scene.atlas, bpy.context.scene.bipolar, electrodes_pos_fname) mu.run_mmvt_func('src.preproc.freeview', flags=flags) bpy.context.scene.freeview_messages = 'Preparing...' return {"FINISHED"}
def init(modality, modality_data=None, colormap=None, subject='', mmvt_dir=''): if subject == '': subject = mu.get_user() if mmvt_dir == '': mmvt_dir = mu.file_fol() if modality_data is None: if modality == 'mri': fname = op.join(mmvt_dir, subject, 'freeview', 'mri_data.npz') elif modality == 't2': fname = op.join(mmvt_dir, subject, 'freeview', 't2_data.npz') elif modality == 'ct': fname = op.join(mmvt_dir, subject, 'ct', 'ct_data.npz'.format(modality)) if op.isfile(fname): modality_data = mu.Bag(np.load(fname)) else: print('To see the slices the following command is being called:'.format(modality)) print('python -m src.preproc.anatomy -s {} -f save_images_data_and_header'.format(mu.get_user())) mu.run_mmvt_func('src.preproc.anatom', 'save_subject_orig_trans,save_images_data_and_header') # cmd = '{} -m src.preproc.anatomy -s {} -f save_subject_orig_trans,save_images_data_and_header --ignore_missing 1'.format( # bpy.context.scene.python_cmd, mu.get_user()) # mu.run_command_in_new_thread(cmd, False, cwd=mu.get_mmvt_code_root()) return None if colormap is None: if op.isfile(op.join(mmvt_dir, 'color_maps', 'gray.npy')): colormap_fname = op.join(mmvt_dir, 'color_maps', 'gray.npy') print("Can't find The gray colormap!") else: colormap_fname = glob.glob(op.join(mmvt_dir, 'color_maps', '*.npy'))[0] colormap = np.load(colormap_fname) affine = np.array(modality_data.affine, float) data = modality_data.data clim = modality_data.precentiles if IN_BLENDER: bpy.context.scene.slices_x_min, bpy.context.scene.slices_x_max = clim colors_ratio = modality_data.colors_ratio codes = axcodes2ornt(aff2axcodes(affine)) order = np.argsort([c[0] for c in codes]) print(modality, order) flips = np.array([c[1] < 0 for c in codes])[order] flips[0] = not flips[0] sizes = [data.shape[order] for order in order] scalers = voxel_sizes(affine) r = [scalers[order[2]] / scalers[order[1]], scalers[order[2]] / scalers[order[0]], scalers[order[1]] / scalers[order[0]]] extras = [0] * 3 pial_vol_mask_fname = op.join(mmvt_dir, subject, 'freeview', 'pial_vol_mask.npy') pial_vol_mask = np.load(pial_vol_mask_fname) if op.isfile(pial_vol_mask_fname) else None self = mu.Bag(dict( data=data, affine=affine, order=order, sizes=sizes, flips=flips, clim=clim, r=r, colors_ratio=colors_ratio, colormap=colormap, coordinates=[], modality=modality, extras=extras, pial_vol_mask=pial_vol_mask)) return self
def add_colorbar_to_image(image_fname, cb_ticks_num=None, cb_ticks_font_size=None): if cb_ticks_num is None: cb_ticks_num = bpy.context.scene.cb_ticks_num if cb_ticks_font_size is None: cb_ticks_font_size = bpy.context.scene.cb_ticks_font_size data_max, data_min = _addon().get_colorbar_max_min() cb_ticks = ','.join(_addon().get_colorbar_ticks(cb_ticks_num)) cb_ticks = cb_ticks.replace('-', '*') # Bug in argparse. todo: change the , to space flags = '--figure_fname "{}" --data_max "{}" --data_min "{}" --colors_map {} --background_color {} '.format( image_fname, data_max, data_min, _addon().get_colormap(), get_background_rgb_string()) + \ '--cb_title "{}" --cb_ticks "{}" --cb_ticks_font_size {} --perc {}'.format( _addon().get_colorbar_title(), cb_ticks, cb_ticks_font_size, _addon().get_colorbar_prec()) mu.run_mmvt_func('src.utils.figures_utils', 'add_colorbar_to_image', flags=flags)
def save_colorbar(min_val=None, max_val=None, color_map=None, ticks_num=None, ticks_font_size=None, prec=None, title=None, background_color_name=None, colorbar_name='', fol=''): org_colorbar_min, org_colorbar_max = (get_colorbar_min(), get_colorbar_max()) org_background_color = _addon().get_panels_background_color() if min_val is None: min_val = get_colorbar_min() if max_val is None: max_val = get_colorbar_max() if color_map is None: color_map = get_colormap_name() if ticks_num is None: ticks_num = get_colorbar_ticks_num() if ticks_font_size is None: ticks_font_size = get_cb_ticks_font_size() if prec is None: prec = get_colorbar_prec() if title is None: title = get_colorbar_title() if background_color_name is None: background_color_rgb = _addon().get_background_rgb_string() else: background_color_rgb = ','.join( [str(x) for x in cu.name_to_rgb(background_color_name)]) if colorbar_name == '': colorbar_name = '{}_colorbar.jpg'.format(color_map) else: if not colorbar_name.endswith('.jpg'): colorbar_name = '{}.jpg'.format(colorbar_name) if fol == '' or not op.isdir(fol): fol = _addon().get_output_path() cb_ticks = ','.join(get_colorbar_ticks(ticks_num, prec, min_val, max_val)) cb_ticks = cb_ticks.replace( '-', '*') # Bug in argparse. todo: change the , to space flags = '--colorbar_name "{}" --data_max "{}" --data_min "{}" --colors_map {} --background_color {} '.format( colorbar_name, max_val, min_val, color_map, background_color_rgb) + \ '--cb_title "{}" --cb_ticks "{}" --cb_ticks_font_size {} --fol "{}"'.format( title, cb_ticks, ticks_font_size, fol) mu.run_mmvt_func('src.utils.figures_utils', 'plot_color_bar', flags=flags) set_colorbar_max_min(org_colorbar_max, org_colorbar_min, force_update=True) _addon().set_panels_background_color(org_background_color)
def combine_two_images_and_add_colorbar(lh_figure_fname, rh_figure_fname, new_image_fname, cb_ticks_num=None, cb_ticks_font_size=None): if cb_ticks_num is None: cb_ticks_num = bpy.context.scene.cb_ticks_num if cb_ticks_font_size is None: cb_ticks_font_size = bpy.context.scene.cb_ticks_font_size data_max, data_min = _addon().get_colorbar_max_min() cb_ticks = ','.join(_addon().get_colorbar_ticks(cb_ticks_num)) cb_ticks = cb_ticks.replace('-', '*') # Bug in argparse flags = '--lh_figure_fname "{}" --rh_figure_fname "{}" '.format(lh_figure_fname, rh_figure_fname) + \ '--new_image_fname "{}" --data_max "{}" --data_min "{}" '.format(new_image_fname, data_max, data_min) + \ '--colors_map {} --background_color {} '.format(_addon().get_colormap(), get_background_rgb_string()) + \ '--add_cb {} --cb_title "{}" --cb_ticks "{}" --cb_ticks_font_size {} '.format( bpy.context.scene.save_views_with_cb, _addon().get_colorbar_title(), cb_ticks, cb_ticks_font_size) + \ '--crop_figures 1 --remove_original_figures 1' print('Combining {} and {}'.format(lh_figure_fname, rh_figure_fname)) mu.run_mmvt_func( 'src.utils.figures_utils', 'combine_two_images_and_add_colorbar', flags=flags)
def load_surf_files(nii_fname, run_fmri_preproc=True, user_fol=''): fmri_file_template = '' if user_fol == '': user_fol = mu.get_user_fol() nii_fol = mu.get_fname_folder(nii_fname) # hemi = mu.get_hemi_from_fname(mu.namebase(nii_fname)) # if hemi == '': hemi, fmri_hemis = mu.get_hemi_from_full_fname(nii_fname) if hemi == '': hemi = mu.find_hemi_using_vertices_num(nii_fname) if hemi == '': return '', '' # fmri_hemis = mu.get_both_hemis_files(nii_fname) local_fname = build_local_fname(nii_fname, user_fol) mu.make_dir(op.join(user_fol, 'fmri')) if nii_fol != op.join(user_fol, 'fmri'): mu.make_link(nii_fname, local_fname, True) other_hemi = mu.other_hemi(hemi) other_hemi_fname = fmri_hemis[other_hemi] # todo: if the other hemi file doens't exist, just create an empty one if op.isfile(other_hemi_fname): local_other_hemi_fname = build_local_fname(other_hemi_fname, user_fol) if nii_fol != op.join(user_fol, 'fmri'): mu.make_link(other_hemi_fname, local_other_hemi_fname, True) fmri_file_template = mu.get_template_hemi_label_name( mu.namebase_with_ext(local_fname)) if run_fmri_preproc: mu.run_mmvt_func( 'src.preproc.fMRI', 'load_surf_files', flags='--fmri_file_template "{}"'.format(fmri_file_template)) # cmd = '{} -m -s {} -f --fmri_file_template "{}" --ignore_missing 1'.format( # bpy.context.scene.python_cmd, mu.get_user(), fmri_file_template) # mu.run_command_in_new_thread(cmd, False, cwd=mu.get_mmvt_code_root()) else: print( "Couldn't find the other hemi file! ({})".format(other_hemi_fname)) return fmri_file_template, hemi, other_hemi