Exemple #1
0
def init(addon):
    StreamingPanel.addon = addon
    register()

    StreamingPanel.cm = np.load(op.join(mu.file_fol(), 'color_maps', 'BuPu_YlOrRd.npy'))
    StreamingPanel.fixed_data = fixed_data()
    StreamingPanel.init = True
Exemple #2
0
def init(addon):
    if not bpy.data.objects.get("full_colorbar", None):
        return
    ColorbarPanel.addon = addon
    colorbar_files = glob.glob(op.join(mu.file_fol(), "color_maps", "*.npy"))
    if len(colorbar_files) == 0:
        return None
    files_names = [mu.namebase(fname).replace("_", "-") for fname in colorbar_files]
    colorbar_items = [(c, c, "", ind) for ind, c in enumerate(files_names)]
    bpy.types.Scene.colorbar_files = bpy.props.EnumProperty(
        items=colorbar_items, description="colormaps files", update=colormap_update
    )
    bpy.context.scene.colorbar_files = files_names[0]
    for space in mu.get_3d_spaces():
        if space.lock_object and space.lock_object.name == "full_colorbar":
            space.show_only_render = True
    register()
    ColorbarPanel.init = True
    bpy.context.scene.show_cb_in_render = False
    mu.select_hierarchy("colorbar_camera", False, False)
    if not ColorbarPanel.colorbar_updated:
        bpy.context.scene.colorbar_min = -1
        bpy.context.scene.colorbar_max = 1
        bpy.context.scene.colorbar_title = "MEG"
    bpy.context.scene.colorbar_files = "BuPu-YlOrRd"
    bpy.context.scene.colorbar_y = 0.18
    bpy.context.scene.colorbar_text_y = -1.53
Exemple #3
0
def init(addon):
    cm_fname = op.join(mu.file_fol(), 'color_maps', 'BuPu_YlOrRd.npy')
    if not op.isfile(cm_fname):
        print("Streaming: Can't load without the cm file {}".format(cm_fname))
        return
    if not bpy.data.objects.get('Deep_electrodes'):
        print('Streaming: No electrodes')
        return
    StreamingPanel.addon = addon
    StreamingPanel.is_listening = False
    StreamingPanel.is_streaming = False
    StreamingPanel.first_time = True
    StreamingPanel.electrodes_data = None
    StreamingPanel.data_max, StreamingPanel.data_min = 0, 0

    StreamingPanel.stim_exist = 'STIM' in _addon().settings.sections()
    if StreamingPanel.stim_exist:
        StreamingPanel.config = _addon().settings['STIM']
    if 'STREAM' in _addon().settings.sections():
        stream_con = _addon().settings['STREAM']
        bpy.context.scene.streaming_buffer_size = int(
            stream_con.get('buffer_size', 10))
        bpy.context.scene.streaming_server = stream_con.get(
            'server', 'localhost')
        bpy.context.scene.multicast_group = stream_con.get(
            'multicast_group', '1.1.1.1')
        bpy.context.scene.multicast_group = stream_con.get(
            'multicast_group', '1.1.1.1')
        bpy.context.scene.streaming_server_port = int(
            stream_con.get('port', 222))
        bpy.context.scene.timeout = float(stream_con.get('timeout', 0.1))

    streaming_items = [('udp', 'udp', '', 1)]
    input_fol = op.join(mu.get_user_fol(), 'electrodes', 'streaming')
    streaming_files = glob.glob(op.join(input_fol, '**',
                                        'streaming_data_*.npy'),
                                recursive=True)
    logs_dirs = [logs_dir.split(op.sep)[-1] for logs_dir in glob.glob(op.join(input_fol, '*.*')) if op.isdir(logs_dir) and \
                 len(glob.glob(op.join(input_fol, logs_dir, 'streaming_data_*.npy'))) > 0]
    if len(streaming_files) > 0 and len(logs_dirs) > 0:
        streaming_items.append(('offline', 'Offline recordings', '', 2))
        bpy.types.Scene.logs_folders = bpy.props.EnumProperty(
            items=[(f, f, '', ind + 1) for ind, f in enumerate(logs_dirs)],
            update=logs_folders_update)
        bpy.context.scene.logs_folders = logs_dirs[0]
    bpy.types.Scene.stream_type = bpy.props.EnumProperty(
        items=streaming_items, description='Type of stream listener')
    bpy.context.scene.stream_type = 'udp'
    bpy.context.scene.multicast = False
    register()
    StreamingPanel.cm = np.load(cm_fname)
    # StreamingPanel.fixed_data = fixed_data()
    StreamingPanel.electrodes_objs_names = [
        l.name for l in bpy.data.objects['Deep_electrodes'].children
    ]
    bpy.context.scene.streaming_electrodes_num = len(
        StreamingPanel.electrodes_objs_names)
    StreamingPanel.minmax_vals = []
    StreamingPanel.max_steps = _addon().get_max_time_steps()
    StreamingPanel.init = True
Exemple #4
0
def init(addon):
    cm_fname = op.join(mu.file_fol(), 'color_maps', 'BuPu_YlOrRd.npy')
    if not op.isfile(cm_fname):
        return
    StreamingPanel.addon = addon
    register()
    StreamingPanel.cm = np.load(cm_fname)
    StreamingPanel.fixed_data = fixed_data()
    StreamingPanel.init = True
def load_colormap():
    colormap_fname = op.join(mu.file_fol(), 'color_maps', '{}.npy'.format(
        bpy.context.scene.colorbar_files))  # .replace('-', '_')
    colormap = np.load(colormap_fname)
    ColorbarPanel.cm = colormap
    for ind in range(colormap.shape[0]):
        cb_obj_name = 'cb.{0:0>3}'.format(ind)
        cb_obj = bpy.data.objects[cb_obj_name]
        cur_mat = cb_obj.active_material
        cur_mat.diffuse_color = colormap[ind]
Exemple #6
0
def load_colormap():
    colormap_fname = op.join(
        mu.file_fol(), "color_maps", "{}.npy".format(bpy.context.scene.colorbar_files.replace("-", "_"))
    )
    colormap = np.load(colormap_fname)
    ColorbarPanel.cm = colormap
    for ind in range(colormap.shape[0]):
        cb_obj_name = "cb.{0:0>3}".format(ind)
        cb_obj = bpy.data.objects[cb_obj_name]
        cur_mat = cb_obj.active_material
        cur_mat.diffuse_color = colormap[ind]
Exemple #7
0
def init(addon):
    try:
        trans_fname = op.join(mu.get_user_fol(), 'orig_trans.npz')
        volumes = glob.glob(
            op.join(mu.get_user_fol(), 'freeview', '*+aseg.npy'))
        luts = glob.glob(
            op.join(mu.get_user_fol(), 'freeview', '*ColorLUT.npz'))
        if op.isfile(trans_fname):
            WhereAmIPanel.subject_orig_trans = mu.Bag(np.load(trans_fname))
        for atlas_vol_fname, atlas_vol_lut_fname in zip(volumes, luts):
            atlas = mu.namebase(atlas_vol_fname)[:-len('+aseg')]
            WhereAmIPanel.vol_atlas[atlas] = np.load(atlas_vol_fname)
            WhereAmIPanel.vol_atlas_lut[atlas] = np.load(atlas_vol_lut_fname)
        subjects_dir = mu.get_link_dir(mu.get_links_dir(), 'subjects')
        annot_files = glob.glob(
            op.join(subjects_dir, mu.get_user(), 'label', 'rh.*.annot'))
        if len(annot_files) > 0:
            files_names = [mu.namebase(fname)[3:] for fname in annot_files]
            items = [(c, c, '', ind) for ind, c in enumerate(files_names)]
            bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(
                items=items)
            bpy.context.scene.subject_annot_files = files_names[0]
        else:
            bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(
                items=[])
            # bpy.context.scene.subject_annot_files = ''

        bpy.context.scene.closest_label_output = ''
        bpy.context.scene.new_label_r = 5

        mri_data_fname = op.join(mu.get_user_fol(), 'freeview',
                                 '{}_data.npz'.format('mri'))
        if op.isfile(mri_data_fname):
            WhereAmIPanel.mri_data = mu.Bag(np.load(mri_data_fname))
        gray_colormap_fname = op.join(mu.file_fol(), 'color_maps', 'gray.npy')
        if op.isfile(gray_colormap_fname):
            WhereAmIPanel.gray_colormap = np.load(gray_colormap_fname)

        WhereAmIPanel.addon = addon
        WhereAmIPanel.init = True
        WhereAmIPanel.run_slices_listener = False
        init_slices()
        if WhereAmIPanel.run_slices_listener:
            start_slicer_server()
        else:
            WhereAmIPanel.slicer_state = slicer.init('mri')
            create_slices('mri', bpy.context.scene.cursor_location)
        save_slices_cursor_pos()
        register()
    except:
        print("Can't init where-am-I panel!")
        print(traceback.format_exc())
Exemple #8
0
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
Exemple #9
0
def init(modality, modality_data=None, colormap=None):
    if modality_data is None:
        fname = op.join(mu.get_user_fol(), 'freeview',
                        '{}_data.npz'.format(modality))
        if op.isfile(fname):
            modality_data = mu.Bag(np.load(fname))
        else:
            print(
                'To see the slices you need to create the file {}_data.npz by calling:'
                .format(modality))
            print(
                'python -m src.preproc.anatomy -s {} -f save_images_data_and_header'
                .format(mu.get_user()))
            return None
    if colormap is None:
        colormap_fname = op.join(mu.file_fol(), 'color_maps', 'gray.npy')
        colormap = np.load(colormap_fname)
    affine = np.array(modality_data.affine, float)
    data = modality_data.data
    clim = modality_data.precentiles
    colors_ratio = modality_data.colors_ratio
    codes = axcodes2ornt(aff2axcodes(affine))
    order = np.argsort([c[0] for c in codes])
    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]]
    ]
    self = mu.Bag(
        dict(data=data,
             affine=affine,
             order=order,
             sizes=sizes,
             flips=flips,
             clim=clim,
             r=r,
             colors_ratio=colors_ratio,
             colormap=colormap))
    return self
Exemple #10
0
def init(addon):
    try:
        t1_trans_fname = op.join(mu.get_user_fol(), 't1_trans.npz')
        if not op.isfile(t1_trans_fname):
            # backward compatibility
            t1_trans_fname = op.join(mu.get_user_fol(), 'orig_trans.npz')
        t2_trans_fname = op.join(mu.get_user_fol(), 't2_trans.npz')
        ct_trans_fname = op.join(mu.get_user_fol(), 'ct', 'ct_trans.npz')
        volumes = glob.glob(op.join(mu.get_user_fol(), 'freeview', '*+aseg.npy'))
        luts = glob.glob(op.join(mu.get_user_fol(), 'freeview', '*ColorLUT.npz'))
        if op.isfile(t1_trans_fname):
            WhereAmIPanel.subject_t1_trans = mu.Bag(np.load(t1_trans_fname))
        if op.isfile(t2_trans_fname):
            WhereAmIPanel.subject_t2_trans = mu.Bag(np.load(t2_trans_fname))
        if op.isfile(ct_trans_fname):
            WhereAmIPanel.subject_ct_trans = mu.Bag(np.load(ct_trans_fname))
        for atlas_vol_fname, atlas_vol_lut_fname in zip(volumes, luts):
            atlas = mu.namebase(atlas_vol_fname)[:-len('+aseg')]
            WhereAmIPanel.vol_atlas[atlas] = np.load(atlas_vol_fname)
            WhereAmIPanel.vol_atlas_lut[atlas] = np.load(atlas_vol_lut_fname)
        try:
            subjects_dir = mu.get_link_dir(mu.get_links_dir(), 'subjects')
            annot_files = [mu.namebase(fname)[3:] for fname in glob.glob(
                op.join(subjects_dir, mu.get_user(), 'label', 'rh.*.annot'))]
            annot_files += [mu.namebase(fname)[3:] for fname in glob.glob(
                op.join(mu.get_user_fol(), 'labels', 'rh.*.annot'))]
            WhereAmIPanel.annot_files = annot_files = list(set(annot_files))
            if len(annot_files) > 0:
                items = [(c, c, '', ind) for ind, c in enumerate(annot_files)]
                bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(
                    items=items, update=subject_annot_files_update, description='List of different atlases.\n\nCurrent atlas')
                ind = mu.index_in_list(bpy.context.scene.atlas, annot_files, 0)
                bpy.context.scene.subject_annot_files = annot_files[ind]
            else:
                bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(items=[])
                # bpy.context.scene.subject_annot_files = ''
        except:
            bpy.types.Scene.subject_annot_files = bpy.props.EnumProperty(items=[])
        bpy.context.scene.closest_label_output = ''
        bpy.context.scene.new_label_r = 5
        bpy.context.scene.find_closest_label_on_click = False
        bpy.context.scene.plot_closest_label_contour = False

        mri_data_fname = op.join(mu.get_user_fol(), 'freeview', 'mri_data.npz')
        if op.isfile(mri_data_fname):
            WhereAmIPanel.mri_data = mu.Bag(np.load(mri_data_fname))
        gray_colormap_fname = op.join(mu.file_fol(), 'color_maps', 'gray.npy')
        if op.isfile(gray_colormap_fname):
            WhereAmIPanel.gray_colormap = np.load(gray_colormap_fname)

        WhereAmIPanel.addon = addon
        WhereAmIPanel.init = True
        WhereAmIPanel.run_slices_listener = False
        init_slices()
        if WhereAmIPanel.run_slices_listener:
            start_slicer_server()
        else:
            WhereAmIPanel.slicer_state = mu.Bag({})
            WhereAmIPanel.slicer_state['mri'] = slicer.init(_addon(), 'mri')
            create_slices(None, bpy.context.scene.cursor_location)
        save_slices_cursor_pos()
        register()
    except:
        print("Can't init where-am-I panel!")
        print(traceback.format_exc())
Exemple #11
0
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()