def load_fmri_vol_data(mmvt): fmri_vol_fnames = glob.glob( op.join(mu.get_user_fol(), 'fmri', '{}.*'.format(mmvt.coloring.get_fmri_vol_fname()))) fmri_vol = None if len(fmri_vol_fnames) > 0: fmri_vol_fname = fmri_vol_fnames[0] if mu.file_type(fmri_vol_fname) in ('mgz', 'nii', 'nii.gz'): fmri_vol = nib.load(fmri_vol_fname).get_data() elif mu.file_type(fmri_vol_fname) == 'npy': fmri_vol = np.load(fmri_vol_fname) return fmri_vol
def _load_labels_data(labels_data_fname): labels_data_type = mu.file_type(labels_data_fname) if labels_data_type == 'npz': d = mu.load_npz_to_bag(labels_data_fname) elif labels_data_type == 'mat': d = mu.load_mat_to_bag(labels_data_fname) d.names = mu.matlab_cell_str_to_list(d.names) d.atlas = d.atlas[0] if not isinstance(d.atlas, str) else d.atlas #d.cmap = d.cmap[0] if not isinstance(d.cmap, str) else d.cmap else: print('Currently we support only mat and npz files') return False labels, data = d.names, d.data if 'atlas' not in d: atlas = mu.check_atlas_by_labels_names(labels) if atlas == '': print('The labeling file must contains an atlas field!') return False else: atlas = str(d.atlas) LabelsPanel.labels_data_atlas = atlas labels = [l.replace('.label', '') for l in labels] cb_title = str(d.get('title', '')) labels_min = d.get('data_min', np.min(data)) labels_max = d.get('data_max', np.max(data)) cmap = str(d.get('cmap', None)) return d, labels, data, atlas, cb_title, labels_max, labels_min, cmap
def get_slices_names(ind=None): slices_name_type = set([mu.file_type(img.name) for img in bpy.data.images if img.name != 'Render Result']) if len(slices_name_type) > 1: print('get_slices_names: different images types! {}'.format(slices_name_type)) return [] slice_file_type = list(slices_name_type)[0] return ['{}{}.{}'.format(pres, '_{}'.format(ind) if ind is not None else '', slice_file_type) for pres in get_perspectives()]
def clean_nii_temp_files(fmri_file_template, user_fol=''): if user_fol == '': user_fol = mu.get_user_fol() file_type = mu.file_type(fmri_file_template) file_temp = op.join( user_fol, 'fmri', '{}'.format( fmri_file_template.replace('{hemi}', '?h')[:-len(file_type)])) mu.delete_files('{}{}'.format(file_temp, file_type)) mu.delete_files('{}{}'.format(file_temp, 'mgz'))
def _load_labels_data(labels_data_fname): labels_data_type = mu.file_type(labels_data_fname) if labels_data_type == 'npz': d = mu.load_npz_to_bag(labels_data_fname) elif labels_data_type == 'mat': d = mu.load_mat_to_bag(labels_data_fname) d.names = mu.matlab_cell_str_to_list(d.names) d.atlas = d.atlas[0] if not isinstance(d.atlas, str) else d.atlas #d.cmap = d.cmap[0] if not isinstance(d.cmap, str) else d.cmap else: print('Currently we support only mat and npz files') return False labels, data = d.names, d.data if len(labels) == 0: return False if isinstance(labels[0], mne.Label): labels = [l.name for l in labels] if data.ndim == 2: if data.shape[0] == len(labels): data = data[:, bpy.context.scene.frame_current] elif data.shape[1] == len(labels): data = data[bpy.context.scene.frame_current] else: data = data.mean( axis=1) if data.shape[0] == len(labels) else data.mean(axis=0) if 'atlas' not in d: atlas = mu.check_atlas_by_labels_names(labels) if atlas == '': print('The labeling file must contains an atlas field!') return False else: atlas = str(d.atlas) LabelsPanel.atlas_in_annot_files = atlas in _addon( ).where_am_i.get_annot_files() LabelsPanel.labels_data_atlas = atlas labels = [l.replace('.label', '') for l in labels] cb_title = str(d.get('title', '')) labels_min = d.get('data_min', np.min(data)) labels_max = d.get('data_max', np.max(data)) cmap = str(d.get('cmap', None)) return d, labels, data, atlas, cb_title, labels_max, labels_min, cmap
def init(mmvt, 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.anatomy', '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') else: colormap_fname = glob.glob(op.join(mmvt_dir, 'color_maps', '*.npy'))[0] print("Can't find The gray colormap!") 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 dural_vol_mask_fname = op.join(mmvt_dir, subject, 'freeview', 'dural_vol_mask.npy') dural_vol_mask = np.load(dural_vol_mask_fname) if op.isfile( dural_vol_mask_fname) else None fmri_vol_fnames = glob.glob( op.join(mu.get_user_fol(), 'fmri', '{}.*'.format(mmvt.coloring.get_fmri_vol_fname()))) fmri_vol = None if len(fmri_vol_fnames) > 0: fmri_vol_fname = fmri_vol_fnames[0] if mu.file_type(fmri_vol_fname) in ('mgz', 'nii', 'nii.gz'): fmri_vol = nib.load(fmri_vol_fname).get_data() elif mu.file_type(fmri_vol_fname) == 'npy': fmri_vol = np.load(fmri_vol_fname) 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, dural_vol_mask=dural_vol_mask, fmri_vol=fmri_vol)) return self