def load_connections_file(): d, vertices, vertices_lookup = None, None, None conn_file_name = bpy.context.scene.connectivity_files.replace(' ', '_') connectivity_file = op.join(mu.get_user_fol(), 'connectivity', '{}.npz'.format(conn_file_name)) vertices_file = op.join(mu.get_user_fol(), 'connectivity', '{}_vertices.pkl'.format( conn_file_name.replace('_static', ''))) if op.isfile(connectivity_file): print('loading connectivity: {}'.format(connectivity_file)) d = mu.Bag(np.load(connectivity_file)) d.labels = [l.astype(str) for l in d.labels] d.hemis = [l.astype(str) for l in d.hemis] d.con_names = np.array([l.astype(str) for l in d.con_names], dtype=np.str) d.conditions = [l.astype(str) for l in d.conditions] if d.con_values.ndim == 2: d.con_values = d.con_values[:, :, np.newaxis] conditions_items = [(cond, cond, '', cond_ind) for cond_ind, cond in enumerate(d.conditions)] if len(d.conditions) > 1: diff_cond = '{}-{} difference'.format(d.conditions[0], d.conditions[1]) conditions_items.append((diff_cond, diff_cond, '', len(d.conditions))) bpy.types.Scene.conditions = bpy.props.EnumProperty(items=conditions_items, description="Conditions") # bpy.context.scene.connections_max, bpy.context.scene.connections_min = d['data_max'], d['data_min'] else: print('No connections file! {}'.format(connectivity_file)) if op.isfile(vertices_file): vertices, vertices_lookup = mu.load(vertices_file) else: name_parts = mu.namebase(vertices_file).split('_') vertices_files = glob.glob( op.join(mu.get_parent_fol(vertices_file), '{}*_{}.pkl'.format(name_parts[0], name_parts[-1]))) if len(vertices_files) == 1: vertices, vertices_lookup = mu.load(vertices_files[0]) else: print('No vertices file! ({})'.format(vertices_file)) ConnectionsPanel.d, ConnectionsPanel.vertices, ConnectionsPanel.vertices_lookup = d, vertices, vertices_lookup
def init_electrodes_labeling(addon): #todo: this panel should work also without labeling file labeling_fname = '{}_{}_electrodes_cigar_r_*_l_*{}*.pkl'.format( mu.get_user(), bpy.context.scene.atlas, '_bipolar' if bpy.context.scene.bipolar else '') labling_files = glob.glob( op.join(mu.get_user_fol(), 'electrodes', labeling_fname)) if not bpy.context.scene.bipolar: labling_files = [ fname for fname in labling_files if 'bipolar' not in fname ] if len(labling_files) == 0: print("!!! Can't find any electrodes labeling file in {} !!!".format( op.join(mu.get_user_fol(), 'electrodes', labeling_fname))) else: files_names = [ mu.namebase(fname) for fname in labling_files if mu.load(fname) ] labeling_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_labeling_files = bpy.props.EnumProperty( items=labeling_items, description='Labeling files', update=electrodes_labeling_files_update) bpy.context.scene.electrodes_labeling_files = files_names[0] # ElecsPanel.electrodes_locs = mu.load(labling_files[0]) # ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.electrodes) ElecsPanel.faces_verts = addon.get_faces_verts()
def init_electrodes_labeling(addon): ela_code_fol = op.join(mu.get_parent_fol(mu.get_mmvt_code_root()), 'electrodes_rois') ElecsPanel.ela_code_exist = op.isfile( op.join(ela_code_fol, 'find_rois', 'find_rois.py')) ElecsPanel.labling_files = labling_files = find_elecrode_labeling_files() if len(labling_files) > 0: files_names = [ mu.namebase(fname) for fname in labling_files if mu.load(fname) ] labeling_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_labeling_files = bpy.props.EnumProperty( items=labeling_items, description='Labeling files', update=electrodes_labeling_files_update) bpy.context.scene.electrodes_labeling_files = files_names[0] # ElecsPanel.electrodes_locs = mu.load(labling_files[0]) # ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.electrodes) ElecsPanel.faces_verts = addon.get_faces_verts() atlases = mu.get_annot_files() if len(atlases) > 0: ElecsPanel.atlases_exist = True atlases_items = [(c, c, '', ind) for ind, c in enumerate(atlases)] bpy.types.Scene.ela_atlas = bpy.props.EnumProperty( items=atlases_items, description='atlases') bpy.context.scene.ela_atlas = atlases[0] bpy.context.scene.ela_bipolar = bpy.context.scene.bipolar return len(labling_files) > 0
def load_camera(camera_fname=''): if camera_fname == '': camera_fname = op.join(mu.get_user_fol(), 'camera', '{}.pkl'.format(bpy.context.scene.camera_files)) if op.isfile(camera_fname): camera_name = mu.namebase(camera_fname) for hemi in mu.HEMIS: if hemi in camera_name: _addon().show_hide_hemi(False, hemi) _addon().show_hide_hemi(True, mu.other_hemi(hemi)) X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location = mu.load( camera_fname) RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation bpy.context.scene.Y_rotation = Y_rotation bpy.context.scene.Z_rotation = Z_rotation bpy.context.scene.X_location = X_location bpy.context.scene.Y_location = Y_location bpy.context.scene.Z_location = Z_location # print('Camera loaded: rotation: {},{},{} locatioin: {},{},{}'.format( # X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location)) # print('Camera loaded: {}'.format(camera_fname)) RenderFigure.update_camera = True update_camera() else: pass
def _electrodes_labeling_files_update(): labeling_fname = op.join( mu.get_user_fol(), 'electrodes', '{}.pkl'.format(bpy.context.scene.electrodes_labeling_files)) ElecsPanel.electrodes_locs = mu.load(labeling_fname) ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.all_electrodes)
def electrodes_labeling_files_update(self, context): # if ElecsPanel.init: # todo: How to get the other file names? # list(bpy.types.Scene.electrodes_labeling_files[1].items())[3][1] labeling_fname = op.join(mu.get_user_fol(), 'electrodes', '{}.pkl'.format( bpy.context.scene.electrodes_labeling_files)) ElecsPanel.electrodes_locs = mu.load(labeling_fname) ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.all_electrodes)
def load_clusters_file(clusters_name=''): if clusters_name == '': clusters_name = bpy.context.scene.meg_clusters_labels_files fname = MEGPanel.clusters_labels_fnames[clusters_name] c = mu.Bag(mu.load(fname)) for ind in range(len(c.values)): c.values[ind] = mu.Bag(c.values[ind]) return c
def init(addon): user_fol = mu.get_user_fol() # clusters_labels_files = glob.glob(op.join(user_fol, 'fmri', 'clusters_labels_*.pkl')) # old code was saving those files as npy instead of pkl # clusters_labels_files.extend(glob.glob(op.join(user_fol, 'fmri', 'clusters_labels_*.npy'))) # fmri_blobs = glob.glob(op.join(user_fol, 'fmri', 'blobs_*_rh.npy')) fMRIPanel.addon = addon fMRIPanel.lookup, fMRIPanel.clusters_labels = {}, {} fMRIPanel.cluster_labels = {} files_names, clusters_labels_files, clusters_labels_items = get_clusters_files( user_fol) fMRIPanel.fMRI_clusters_files_exist = len( files_names) > 0 # and len(fmri_blobs) > 0 if not fMRIPanel.fMRI_clusters_files_exist: register() fMRIPanel.init = True return # files_names = [mu.namebase(fname)[len('clusters_labels_'):] for fname in clusters_labels_files] fMRIPanel.clusters_labels_file_names = files_names bpy.types.Scene.fmri_clusters_labels_files = bpy.props.EnumProperty( items=clusters_labels_items, description="fMRI files", update=fmri_clusters_labels_files_update) bpy.context.scene.fmri_clusters_labels_files = files_names[0] bpy.context.scene.plot_fmri_cluster_contours = False for file_name, clusters_labels_file in zip(files_names, clusters_labels_files): # Check if the constrast files exist if all( [get_contrast_fname(file_name, hemi) != '' for hemi in mu.HEMIS]): key = file_name fMRIPanel.clusters_labels[key] = c = mu.Bag( mu.load(clusters_labels_file)) for ind in range(len(c.values)): c.values[ind] = mu.Bag(c.values[ind]) fMRIPanel.lookup[key] = create_lookup_table( fMRIPanel.clusters_labels[key]) # bpy.context.scene.fmri_cluster_val_threshold = 2 # bpy.context.scene.fmri_cluster_size_threshold = 20 bpy.context.scene.search_closest_cluster_only_in_filtered = True bpy.context.scene.fmri_what_to_plot = 'blob' bpy.context.scene.fmri_how_to_sort = 'tval' bpy.context.scene.plot_current_cluster = True update_clusters() fMRIPanel.blobs_activity, _ = calc_blobs_activity( fMRIPanel.constrast, fMRIPanel.clusters_labels_filtered, fMRIPanel.colors_in_hemis) bpy.context.scene.plot_fmri_cluster_per_click = False fMRIPanel.dont_show_clusters_info = True # addon.clear_cortex() register() fMRIPanel.init = True
def init(addon): import shutil ElecsPanel.addon = addon ElecsPanel.parent = bpy.data.objects.get('Deep_electrodes') if ElecsPanel.parent is None or len(ElecsPanel.parent.children) == 0: print("Can't register electrodes panel, no Deep_electrodes object") return mu.make_dir(op.join(mu.get_user_fol(), 'electrodes')) sorted_groups_fname = op.join(mu.get_user_fol(), 'electrodes', 'sorted_groups.pkl') if not op.isfile(sorted_groups_fname): # Try to get the file from the subject's root folder if op.isfile(op.join(mu.get_user_fol(), 'sorted_groups.pkl')): shutil.move(op.join(mu.get_user_fol(), 'sorted_groups.pkl'), sorted_groups_fname) else: print("Can't register electrodes panel, no sorted groups file") return # show_hide_electrodes(True) ElecsPanel.sorted_groups = mu.load(sorted_groups_fname) ElecsPanel.groups_hemi = create_groups_hemi_lookup( ElecsPanel.sorted_groups) ElecsPanel.all_electrodes = [el.name for el in ElecsPanel.parent.children] ElecsPanel.groups = create_groups_lookup_table(ElecsPanel.all_electrodes) ElecsPanel.groups_first_electrode = find_first_electrode_per_group( ElecsPanel.all_electrodes) ElecsPanel.groups_electrodes = create_groups_electrodes_lookup( ElecsPanel.all_electrodes) init_leads_list() init_electrodes_list() ret = init_electrodes_labeling(addon) if not ret: print('No electrodes labeling files.') return # addon.clear_colors_from_parent_childrens('Deep_electrodes') # addon.clear_cortex() bpy.context.scene.show_only_lead = False bpy.context.scene.listen_to_keyboard = False bpy.context.scene.listener_is_running = False bpy.context.scene.show_lh_electrodes = True bpy.context.scene.show_rh_electrodes = True _electrodes_labeling_files_update() if not ElecsPanel.electrodes_locs or not ElecsPanel.lookup: if not ElecsPanel.electrodes_locs: print( "!!! Can't find electrodes labeling files in user/electrdes!") if not ElecsPanel.lookup: print('No electrodes lookup table!') print("!!! Can't plot electrodes' probabilties !!!") if not ElecsPanel.groups or not ElecsPanel.groups_first_electrode or not ElecsPanel.sorted_groups or \ not ElecsPanel.groups_hemi or not ElecsPanel.groups_electrodes: print('Error in electrodes panel init!') else: register() ElecsPanel.init = True
def init(addon): from random import shuffle ColoringMakerPanel.addon = addon user_fol = mu.get_user_fol() ColoringMakerPanel.faces_verts = None labels_vertices_fname = op.join(user_fol, 'labels_vertices_{}.pkl'.format(bpy.context.scene.atlas)) if not op.isfile(labels_vertices_fname): print("!!! Can't find {}!".format('labels_vertices_{}.pkl'.format(bpy.context.scene.atlas))) return None labels_names, labels_vertices = mu.load(labels_vertices_fname) ColoringMakerPanel.labels_vertices = dict(labels_names=labels_names, labels_vertices=labels_vertices) fmri_files = glob.glob(op.join(user_fol, 'fmri', 'fmri_*_lh.npy')) if len(fmri_files) > 0: files_names = [mu.namebase(fname)[5:-3] for fname in fmri_files] clusters_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.fmri_files = bpy.props.EnumProperty( items=clusters_items, description="fMRI files", update=fmri_files_update) bpy.context.scene.fmri_files = files_names[0] for hemi in mu.HEMIS: ColoringMakerPanel.fMRI[hemi] = np.load('{}_{}.npy'.format(fmri_files[0][:-7], hemi)) electrodes_source_files = glob.glob(op.join(user_fol, 'electrodes', '*_labels_*-rh.npz')) if len(electrodes_source_files) > 0: files_names = [mu.namebase(fname)[:-len('-rh')] for fname in electrodes_source_files] items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_sources_files = bpy.props.EnumProperty( items=items, description="electrodes sources", update=electrodes_sources_files_update) bpy.context.scene.electrodes_sources_files = files_names[0] mu.make_dir(op.join(user_fol, 'coloring')) manually_color_files = glob.glob(op.join(user_fol, 'coloring', '*.csv')) if len(manually_color_files) > 0: files_names = [mu.namebase(fname) for fname in manually_color_files] coloring_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.coloring_files = bpy.props.EnumProperty(items=coloring_items, description="Coloring files") bpy.context.scene.coloring_files = files_names[0] vol_color_files = glob.glob(op.join(user_fol, 'coloring', 'volumetric', '*.csv')) if len(vol_color_files) > 0: files_names = [mu.namebase(fname) for fname in vol_color_files] coloring_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.vol_coloring_files = bpy.props.EnumProperty( items=coloring_items, description="Volumetric Coloring files") bpy.context.scene.vol_coloring_files = files_names[0] ColoringMakerPanel.colors = list(set(list(cu.NAMES_TO_HEX.keys())) - set(['black'])) shuffle(ColoringMakerPanel.colors) ColoringMakerPanel.labels_groups = read_groups_labels(ColoringMakerPanel.colors) if len(ColoringMakerPanel.labels_groups) > 0: groups_items = [(gr, gr, '', ind) for ind, gr in enumerate(list(ColoringMakerPanel.labels_groups.keys()))] bpy.types.Scene.labels_groups = bpy.props.EnumProperty( items=groups_items, description="Groups") ColoringMakerPanel.faces_verts = load_faces_verts() register()
def electrodes_labeling_files_update(self, context): # if ElecsPanel.init: # todo: How to get the other file names? # list(bpy.types.Scene.electrodes_labeling_files[1].items())[3][1] if ElecsPanel.init: labeling_fname = op.join( mu.get_user_fol(), 'electrodes', '{}.pkl'.format(bpy.context.scene.electrodes_labeling_files)) ElecsPanel.electrodes_locs = mu.load(labeling_fname) ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.all_electrodes)
def init_sorted_groups(): import shutil sorted_groups_fname = op.join(mu.get_user_fol(), 'electrodes', 'sorted_groups.pkl') if not op.isfile(sorted_groups_fname): # Try to get the file from the subject's root folder if op.isfile(op.join(mu.get_user_fol(), 'sorted_groups.pkl')): shutil.move(op.join(mu.get_user_fol(), 'sorted_groups.pkl'), sorted_groups_fname) else: print("electrodes_panel: no sorted groups file") # return if op.isfile(sorted_groups_fname): ElecsPanel.sorted_groups = mu.load(sorted_groups_fname)
def fmri_files_update(self, context): #todo: there are two frmi files list (the other one in fMRI panel) user_fol = mu.get_user_fol() for hemi in mu.HEMIS: fname = op.join(user_fol, 'fmri', 'fmri_{}_{}.npy'.format(bpy.context.scene.fmri_files, hemi)) ColoringMakerPanel.fMRI[hemi] = np.load(fname) fmri_data_maxmin_fname = op.join(mu.get_user_fol(), 'fmri', 'fmri_activity_map_minmax_{}.pkl'.format( bpy.context.scene.fmri_files)) if op.isfile(fmri_data_maxmin_fname): data_min, data_max = mu.load(fmri_data_maxmin_fname) ColoringMakerPanel.fmri_activity_colors_ratio = 256 / (data_max - data_min) ColoringMakerPanel.fmri_activity_data_min = data_min ColoringMakerPanel.fmri_activity_data_max = data_max
def plot_pathway(self, context, layers_dti, pathway_name, pathway_type): if pathway_type == TRACULA: pkl_fname = op.join(SUBJECT_DTI_FOL, TRACULA, '{}{}.pkl'.format(pathway_name, TRACULA_POSTFIX)) mu.create_empty_if_doesnt_exists(pathway_name, DTIPanel.addon.CONNECTIONS_LAYER, None, PARENT_OBJ) parent_obj = bpy.data.objects[pathway_name] tracks = mu.load(pkl_fname) N = len(tracks) now = time.time() for ind, track in enumerate(tracks[:1000]): mu.time_to_go(now, ind, N, 100) track = track * 0.1 # pprint(track) cur_obj = mu.create_spline(track, layers_dti, bevel_depth=0.01) # cur_obj.scale = [0.1] * 3 cur_obj.name = '{}_{}'.format(pathway_name, ind) cur_obj.parent = parent_obj
def load_camera(camera_fname=''): if camera_fname == '': camera_fname = op.join(bpy.path.abspath(bpy.context.scene.output_path), 'camera.pkl') if op.isfile(camera_fname): X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location = mu.load(camera_fname) RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation bpy.context.scene.Y_rotation = Y_rotation bpy.context.scene.Z_rotation = Z_rotation bpy.context.scene.X_location = X_location bpy.context.scene.Y_location = Y_location bpy.context.scene.Z_location = Z_location RenderFigure.update_camera = True update_camera() else: print('No camera file was found in {}!'.format(camera_fname))
def init_electrodes_labeling(addon): #todo: this panel should work also without labeling file labling_files = find_elecrode_labeling_files() if len(labling_files) > 0: files_names = [ mu.namebase(fname) for fname in labling_files if mu.load(fname) ] labeling_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_labeling_files = bpy.props.EnumProperty( items=labeling_items, description='Labeling files', update=electrodes_labeling_files_update) bpy.context.scene.electrodes_labeling_files = files_names[0] # ElecsPanel.electrodes_locs = mu.load(labling_files[0]) # ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.electrodes) ElecsPanel.faces_verts = addon.get_faces_verts() return len(labling_files) > 0
def init(addon): import shutil ElecsPanel.addon = addon ElecsPanel.parent = bpy.data.objects.get('Deep_electrodes') if ElecsPanel.parent is None or len(ElecsPanel.parent.children) == 0: print("!!!! Can't register electrodes panel, no Deep_electrodes object!!!!") return mu.make_dir(op.join(mu.get_user_fol(), 'electrodes')) sorted_groups_fname = op.join(mu.get_user_fol(), 'electrodes', 'sorted_groups.pkl') if not op.isfile(sorted_groups_fname): # Try to get the file from the subject's root folder if op.isfile(op.join(mu.get_user_fol(), 'sorted_groups.pkl')): shutil.move(op.join(mu.get_user_fol(), 'sorted_groups.pkl'), sorted_groups_fname) else: print("!!!! Can't register electrodes panel, no sorted groups file!!!!") return ElecsPanel.sorted_groups = mu.load(sorted_groups_fname) ElecsPanel.groups_hemi = create_groups_hemi_lookup(ElecsPanel.sorted_groups) ElecsPanel.all_electrodes = [el.name for el in ElecsPanel.parent.children] ElecsPanel.groups = create_groups_lookup_table(ElecsPanel.all_electrodes) ElecsPanel.groups_first_electrode = find_first_electrode_per_group(ElecsPanel.all_electrodes) ElecsPanel.groups_electrodes = create_groups_electrodes_lookup(ElecsPanel.all_electrodes) init_leads_list() init_electrodes_list() init_electrodes_labeling(addon) addon.clear_colors_from_parent_childrens('Deep_electrodes') addon.clear_cortex() bpy.context.scene.show_only_lead = False bpy.context.scene.listen_to_keyboard = False bpy.context.scene.listener_is_running = False bpy.context.scene.show_lh_electrodes = True bpy.context.scene.show_rh_electrodes = True _electrodes_labeling_files_update() if not ElecsPanel.electrodes_locs or not ElecsPanel.lookup: if not ElecsPanel.electrodes_locs: print("!!! Can't find electrodes labeling files in user/electrdes!") if not ElecsPanel.lookup: print('No electrodes lookup table!') print("!!! Can't plot electrodes' probabilties !!!") if not ElecsPanel.groups or not ElecsPanel.groups_first_electrode or not ElecsPanel.sorted_groups or \ not ElecsPanel.groups_hemi or not ElecsPanel.groups_electrodes: print('Error in electrodes panel init!') else: register() ElecsPanel.init = True
def load_camera(camera_fname=''): if camera_fname == '': camera_fname = op.join(bpy.path.abspath(bpy.context.scene.output_path), 'camera.pkl') if op.isfile(camera_fname): X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location = mu.load( camera_fname) RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation bpy.context.scene.Y_rotation = Y_rotation bpy.context.scene.Z_rotation = Z_rotation bpy.context.scene.X_location = X_location bpy.context.scene.Y_location = Y_location bpy.context.scene.Z_location = Z_location RenderFigure.update_camera = True update_camera() else: print('No camera file was found in {}!'.format(camera_fname))
def init_electrodes_labeling(addon): #todo: this panel should work also without labeling file labeling_fname = '{}_{}_electrodes_cigar_r_*_l_*{}*.pkl'.format(mu.get_user(), bpy.context.scene.atlas, '_bipolar' if bpy.context.scene.bipolar else '') labling_files = glob.glob(op.join(mu.get_user_fol(), 'electrodes', labeling_fname)) if not bpy.context.scene.bipolar: labling_files = [fname for fname in labling_files if 'bipolar' not in fname] if len(labling_files) == 0: print("!!! Can't find any electrodes labeling file in {} !!!".format( op.join(mu.get_user_fol(), 'electrodes', labeling_fname))) else: files_names = [mu.namebase(fname) for fname in labling_files if mu.load(fname)] labeling_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_labeling_files = bpy.props.EnumProperty( items=labeling_items, description='Labeling files', update=electrodes_labeling_files_update) bpy.context.scene.electrodes_labeling_files = files_names[0] # ElecsPanel.electrodes_locs = mu.load(labling_files[0]) # ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.electrodes) ElecsPanel.faces_verts = addon.get_faces_verts()
def find_elecrode_labeling_files(): files = [] blender_electrodes_names = set([o.name for o in bpy.data.objects['Deep_electrodes'].children]) # labeling_template = '{}_{}_electrodes_cigar_r_*_l_*.pkl'.format(mu.get_user(), bpy.context.scene.atlas) labeling_template = '{}_*_electrodes_cigar_r_*_l_*.pkl'.format(mu.get_user(), bpy.context.scene.atlas) labling_files = glob.glob(op.join(mu.get_user_fol(), 'electrodes', labeling_template)) for labling_file in labling_files: try: d = mu.load(labling_file) electrodes_names = set([e['name'] for e in d]) # todo: find what is the problem... (mg96) # if len(electrodes_names - blender_electrodes_names) == 0: files.append(labling_file) except: print('Error reading {}'.format(labling_file)) continue if len(files) == 0: print("Can't find any labeling file that match the electrodes names in{}!".format( op.join(mu.get_user_fol(), 'electrodes'))) return files
def plot_tracks(): tracks_name = bpy.context.scene.dti_tracks tracks_fname = op.join(SUBJECT_DTI_FOL, '{}_tracks.npy'.format(tracks_name)) tracks_header = op.join(SUBJECT_DTI_FOL, '{}_header.pkl'.format(tracks_name)) world_matrix = mu.get_matrix_world() tracks = np.load(tracks_fname) * 0.1 header = mu.load(tracks_header) layers_dti = [False] * 20 dti_layer = DTIPanel.addon.CONNECTIONS_LAYER layers_dti[dti_layer] = True mu.create_empty_if_doesnt_exists(tracks_name, DTIPanel.addon.CONNECTIONS_LAYER, None, PARENT_OBJ) parent_obj = bpy.data.objects[tracks_name] # N = len(tracks) # now = time.time() # for ind, track in enumerate(tracks): # mu.time_to_go(now, ind, N, 100) cur_obj = mu.create_spline(tracks, layers_dti, bevel_depth=0.01) cur_obj.name = tracks_name cur_obj.parent = parent_obj
def load_camera(camera_fname=''): if camera_fname == '': camera_fname = op.join(mu.get_user_fol(), 'camera', '{}.pkl'.format(bpy.context.scene.camera_files)) if op.isfile(camera_fname): camera_name = mu.namebase(camera_fname) for hemi in mu.HEMIS: if hemi in camera_name: _addon().show_hide_hemi(False, hemi) _addon().show_hide_hemi(True, mu.other_hemi(hemi)) X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location = mu.load(camera_fname) RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation bpy.context.scene.Y_rotation = Y_rotation bpy.context.scene.Z_rotation = Z_rotation bpy.context.scene.X_location = X_location bpy.context.scene.Y_location = Y_location bpy.context.scene.Z_location = Z_location RenderFigure.update_camera = True update_camera() else: print('No camera file was found in {}!'.format(camera_fname))
def find_point_thickness(): vertex_ind, closest_mesh_name = _addon().snap_cursor() vertex_co = bpy.data.objects['inner_skull'].data.vertices[vertex_ind].co distances = np.load(op.join(mu.get_user_fol(), 'skull', 'ray_casts.npy')) rays_info = mu.load( op.join(mu.get_user_fol(), 'skull', 'ray_casts_info.pkl')) # closest_mesh_name, vertex_ind, vertex_co, _ = _addon().find_vertex_index_and_mesh_closest_to_cursor( # objects_names=['inner_skull']) # distance = np.linalg.norm(f.intersections[vertex_ind, 0] - f.intersections[vertex_ind, 1]) distance = distances[vertex_ind][0] (hit, loc, norm, index) = rays_info[vertex_ind] # bpy.context.scene.cursor_location = vertex_co print(closest_mesh_name, vertex_ind, vertex_co, distance, loc) SkullPanel.vertex_skull_thickness = distance if not bpy.context.scene.thickness_arrows and bpy.context.scene.show_point_arrow: if SkullPanel.prev_vertex_arrow is not None: SkullPanel.prev_vertex_arrow.hide = True vertex_arrow = bpy.data.objects.get('mt_{}'.format(vertex_ind), None) if vertex_arrow is not None: vertex_arrow.hide = False SkullPanel.prev_vertex_arrow = vertex_arrow
def init(addon): from random import shuffle ColoringMakerPanel.addon = addon user_fol = mu.get_user_fol() ColoringMakerPanel.faces_verts = None labels_vertices_fname = op.join(user_fol, 'labels_vertices_{}.pkl'.format(bpy.context.scene.atlas)) if not op.isfile(labels_vertices_fname): print("!!! Can't find {}!".format('labels_vertices_{}.pkl'.format(bpy.context.scene.atlas))) return None labels_names, labels_vertices = mu.load(labels_vertices_fname) ColoringMakerPanel.labels_vertices = dict(labels_names=labels_names, labels_vertices=labels_vertices) ColoringMakerPanel.max_labels_vertices_num = {} meg_files_exist = mu.hemi_files_exists(op.join(user_fol, 'activity_map_{hemi}', 't0.npy')) meg_data_maxmin_fname = op.join(mu.get_user_fol(), 'meg_activity_map_minmax.pkl') if meg_files_exist and op.isfile(meg_data_maxmin_fname): data_min, data_max = mu.load(meg_data_maxmin_fname) ColoringMakerPanel.meg_activity_colors_ratio = 256 / (data_max - data_min) ColoringMakerPanel.meg_activity_data_min = data_min ColoringMakerPanel.meg_activity_data_max = data_max print('data meg: {}-{}'.format(data_min, data_max)) _addon().set_colorbar_max_min(data_max, data_min, True) _addon().set_colorbar_title('MEG') fmri_files = glob.glob(op.join(user_fol, 'fmri', 'fmri_*_lh.npy')) if len(fmri_files) > 0: files_names = [mu.namebase(fname)[5:-3] for fname in fmri_files] clusters_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.fmri_files = bpy.props.EnumProperty( items=clusters_items, description="fMRI files", update=fmri_files_update) bpy.context.scene.fmri_files = files_names[0] for hemi in mu.HEMIS: ColoringMakerPanel.fMRI[hemi] = np.load('{}_{}.npy'.format(fmri_files[0][:-7], hemi)) # Check separately for each contrast fmri_data_maxmin_fname = op.join(mu.get_user_fol(), 'fmri', 'fmri_activity_map_minmax.pkl') if op.isfile(fmri_data_maxmin_fname): data_min, data_max = mu.load(fmri_data_maxmin_fname) ColoringMakerPanel.fmri_activity_colors_ratio = 256 / (data_max - data_min) ColoringMakerPanel.fmri_activity_data_min = data_min ColoringMakerPanel.fmri_activity_data_max = data_max electrodes_source_files = glob.glob(op.join(user_fol, 'electrodes', '*_labels_*-rh.npz')) if len(electrodes_source_files) > 0: files_names = [mu.namebase(fname)[:-len('-rh')] for fname in electrodes_source_files] items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_sources_files = bpy.props.EnumProperty( items=items, description="electrodes sources", update=electrodes_sources_files_update) bpy.context.scene.electrodes_sources_files = files_names[0] mu.make_dir(op.join(user_fol, 'coloring')) manually_color_files = glob.glob(op.join(user_fol, 'coloring', '*.csv')) if len(manually_color_files) > 0: files_names = [mu.namebase(fname) for fname in manually_color_files] coloring_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.coloring_files = bpy.props.EnumProperty(items=coloring_items, description="Coloring files") bpy.context.scene.coloring_files = files_names[0] vol_color_files = glob.glob(op.join(user_fol, 'coloring', 'volumetric', '*.csv')) if len(vol_color_files) > 0: files_names = [mu.namebase(fname) for fname in vol_color_files] coloring_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.vol_coloring_files = bpy.props.EnumProperty( items=coloring_items, description="Volumetric Coloring files") bpy.context.scene.vol_coloring_files = files_names[0] ColoringMakerPanel.colors = list(set(list(cu.NAMES_TO_HEX.keys())) - set(['black'])) shuffle(ColoringMakerPanel.colors) ColoringMakerPanel.labels_groups = read_groups_labels(ColoringMakerPanel.colors) if len(ColoringMakerPanel.labels_groups) > 0: groups_items = [(gr, gr, '', ind) for ind, gr in enumerate(list(ColoringMakerPanel.labels_groups.keys()))] bpy.types.Scene.labels_groups = bpy.props.EnumProperty( items=groups_items, description="Groups") ColoringMakerPanel.faces_verts = load_faces_verts() bpy.context.scene.coloring_meg_subcorticals = False # ColoringMakerPanel.cm = np.load(op.join(mu.file_fol(), 'color_maps', 'BuPu_YlOrRd.npy')) register()
def init(addon): from random import shuffle ColoringMakerPanel.addon = addon user_fol = mu.get_user_fol() ColoringMakerPanel.faces_verts = None labels_vertices_fname = op.join(user_fol, 'labels_vertices_{}.pkl'.format(bpy.context.scene.atlas)) if not op.isfile(labels_vertices_fname): print("!!! Can't find {}!".format('labels_vertices_{}.pkl'.format(bpy.context.scene.atlas))) return None labels_names, labels_vertices = mu.load(labels_vertices_fname) ColoringMakerPanel.labels_vertices = dict(labels_names=labels_names, labels_vertices=labels_vertices) meg_files_exist = mu.hemi_files_exists(op.join(user_fol, 'activity_map_{hemi}', 't0.npy')) if meg_files_exist: data_min, data_max = mu.load(op.join(mu.get_user_fol(), 'meg_activity_map_minmax.pkl')) ColoringMakerPanel.meg_activity_colors_ratio = 256 / (data_max - data_min) ColoringMakerPanel.meg_activity_data_min = data_min print('data meg: {}-{}'.format(data_min, data_max)) fmri_files = glob.glob(op.join(user_fol, 'fmri', 'fmri_*_lh.npy')) if len(fmri_files) > 0: files_names = [mu.namebase(fname)[5:-3] for fname in fmri_files] clusters_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.fmri_files = bpy.props.EnumProperty( items=clusters_items, description="fMRI files", update=fmri_files_update) bpy.context.scene.fmri_files = files_names[0] for hemi in mu.HEMIS: ColoringMakerPanel.fMRI[hemi] = np.load('{}_{}.npy'.format(fmri_files[0][:-7], hemi)) electrodes_source_files = glob.glob(op.join(user_fol, 'electrodes', '*_labels_*-rh.npz')) if len(electrodes_source_files) > 0: files_names = [mu.namebase(fname)[:-len('-rh')] for fname in electrodes_source_files] items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.electrodes_sources_files = bpy.props.EnumProperty( items=items, description="electrodes sources", update=electrodes_sources_files_update) bpy.context.scene.electrodes_sources_files = files_names[0] mu.make_dir(op.join(user_fol, 'coloring')) manually_color_files = glob.glob(op.join(user_fol, 'coloring', '*.csv')) if len(manually_color_files) > 0: files_names = [mu.namebase(fname) for fname in manually_color_files] coloring_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.coloring_files = bpy.props.EnumProperty(items=coloring_items, description="Coloring files") bpy.context.scene.coloring_files = files_names[0] vol_color_files = glob.glob(op.join(user_fol, 'coloring', 'volumetric', '*.csv')) if len(vol_color_files) > 0: files_names = [mu.namebase(fname) for fname in vol_color_files] coloring_items = [(c, c, '', ind) for ind, c in enumerate(files_names)] bpy.types.Scene.vol_coloring_files = bpy.props.EnumProperty( items=coloring_items, description="Volumetric Coloring files") bpy.context.scene.vol_coloring_files = files_names[0] ColoringMakerPanel.colors = list(set(list(cu.NAMES_TO_HEX.keys())) - set(['black'])) shuffle(ColoringMakerPanel.colors) ColoringMakerPanel.labels_groups = read_groups_labels(ColoringMakerPanel.colors) if len(ColoringMakerPanel.labels_groups) > 0: groups_items = [(gr, gr, '', ind) for ind, gr in enumerate(list(ColoringMakerPanel.labels_groups.keys()))] bpy.types.Scene.labels_groups = bpy.props.EnumProperty( items=groups_items, description="Groups") ColoringMakerPanel.faces_verts = load_faces_verts() ColoringMakerPanel.cm = np.load(op.join(mu.file_fol(), 'color_maps', 'BuPu_YlOrRd.npy')) register()
def _electrodes_labeling_files_update(): labeling_fname = op.join(mu.get_user_fol(), 'electrodes', '{}.pkl'.format( bpy.context.scene.electrodes_labeling_files)) ElecsPanel.electrodes_locs = mu.load(labeling_fname) ElecsPanel.lookup = create_lookup_table(ElecsPanel.electrodes_locs, ElecsPanel.all_electrodes)