Exemplo n.º 1
0
def import_electrodes(input_file,
                      electrodes_layer,
                      bipolar='',
                      electrode_size=None,
                      parnet_name='Deep_electrodes'):
    if not electrode_size is None:
        bpy.context.scene.electrodes_radius = electrode_size
    if bipolar != '':
        bpy.context.scene.bipolar = bool(bipolar)
    mu.delete_hierarchy(parnet_name)
    f = np.load(input_file)

    electrode_size = bpy.context.scene.electrodes_radius
    layers_array = [False] * 20
    create_empty_if_doesnt_exists(parnet_name,
                                  _addon().BRAIN_EMPTY_LAYER, layers_array,
                                  parnet_name)

    layers_array = [False] * 20
    layers_array[electrodes_layer] = True

    for (x, y, z), name in zip(f['pos'], f['names']):
        elc_name = name.astype(str)
        if not bpy.data.objects.get(elc_name) is None:
            continue
        print('creating {}: {}'.format(elc_name, (x, y, z)))
        mu.create_sphere((x * 0.1, y * 0.1, z * 0.1), electrode_size,
                         layers_array, elc_name)
        cur_obj = bpy.data.objects[elc_name]
        cur_obj.select = True
        cur_obj.parent = bpy.data.objects[parnet_name]
        mu.create_and_set_material(cur_obj)
Exemplo n.º 2
0
def import_electrodes(input_file, electrodes_layer, bipolar='', electrode_size=None, parnet_name='Deep_electrodes'):
    if not electrode_size is None:
        bpy.context.scene.electrodes_radius = electrode_size
    if bipolar != '':
        bpy.context.scene.bipolar = bool(bipolar)
    mu.delete_hierarchy(parnet_name)
    f = np.load(input_file)

    electrode_size = bpy.context.scene.electrodes_radius
    layers_array = [False] * 20
    create_empty_if_doesnt_exists(parnet_name, _addon().BRAIN_EMPTY_LAYER, layers_array, parnet_name)

    layers_array = [False] * 20
    layers_array[electrodes_layer] = True

    for (x, y, z), name in zip(f['pos'], f['names']):
        elc_name = name.astype(str)
        if not bpy.data.objects.get(elc_name) is None:
            continue
        print('creating {}: {}'.format(elc_name, (x, y, z)))
        mu.create_sphere((x * 0.1, y * 0.1, z * 0.1), electrode_size, layers_array, elc_name)
        cur_obj = bpy.data.objects[elc_name]
        cur_obj.select = True
        cur_obj.parent = bpy.data.objects[parnet_name]
        mu.create_and_set_material(cur_obj)
Exemplo n.º 3
0
def import_electrodes(input_file, bipolar='', electrode_size=None):
    if not electrode_size is None:
        bpy.context.scene.electrodes_radius = electrode_size
    if bipolar != '':
        bpy.context.scene.bipolar = bool(bipolar)
    mu.delete_hierarchy('Deep_electrodes')
    print('Adding deep electrodes')
    f = np.load(input_file)

    deep_electrodes_layer = 1
    electrode_size = bpy.context.scene.electrodes_radius
    layers_array = [False] * 20
    create_empty_if_doesnt_exists('Deep_electrodes', DataMakerPanel.addon.BRAIN_EMPTY_LAYER, layers_array, 'Deep_electrodes')

    # if bpy.data.objects.get("Deep_electrodes") is None:
    #     layers_array[BRAIN_EMPTY_LAYER] = True
    #     bpy.ops.object.empty_add(type='PLAIN_AXES', radius=1, view_align=False, location=(0, 0, 0), layers=layers_array)
    #     bpy.data.objects['Empty'].name = 'Deep_electrodes'

    layers_array = [False] * 20
    layers_array[deep_electrodes_layer] = True

    for (x, y, z), name in zip(f['pos'], f['names']):
        elc_name = name.astype(str)
        if not bpy.data.objects.get(elc_name) is None:
            continue
        print('creating {}: {}'.format(elc_name, (x, y, z)))
        mu.create_sphere((x * 0.1, y * 0.1, z * 0.1), electrode_size, layers_array, elc_name)
        cur_obj = bpy.data.objects[elc_name]
        cur_obj.select = True
        cur_obj.parent = bpy.data.objects['Deep_electrodes']
        # cur_obj.active_material = bpy.data.materials['Deep_electrode_mat']
        mu.create_and_set_material(cur_obj)
Exemplo n.º 4
0
def create_keyframes(d, threshold, threshold_type, radius=.1, stat=STAT_DIFF, verts_color='pink'):
    layers_rods = [False] * 20
    rods_layer = _addon().CONNECTIONS_LAYER
    layers_rods[rods_layer] = True
    mu.delete_hierarchy(get_connections_parent_name())
    mu.create_empty_if_doesnt_exists(get_connections_parent_name(), _addon().BRAIN_EMPTY_LAYER, None, 'Functional maps')
    if d.con_values.ndim >= 2:
        windows_num = d.con_values.shape[1]
    else:
        windows_num = 1
    T = ConnectionsPanel.addon.get_max_time_steps()
    if T == 0:
        T = windows_num
    norm_fac = T / windows_num
    if d.con_values.ndim > 2:
        stat_data = calc_stat_data(d.con_values, stat)
    else:
        stat_data = d.con_values
    mask = calc_mask(stat_data, threshold, threshold_type, windows_num)
    indices = np.where(mask)[0]
    parent_obj = bpy.data.objects[get_connections_parent_name()]
    parent_obj.animation_data_clear()
    N = len(indices)
    print('{} connections are above the threshold'.format(N))
    create_vertices(d, mask, verts_color)
    create_conncection_per_condition(d, layers_rods, indices, mask, windows_num, norm_fac, T, radius)
    print('Create connections for the conditions {}'.format('difference' if stat == STAT_DIFF else 'mean'))
    create_keyframes_for_parent_obj(d, indices, mask, windows_num, norm_fac, T, stat)
    print('finish keyframing!')
Exemplo n.º 5
0
def create_keyframes(d, threshold, threshold_type, radius=.1, stat=STAT_DIFF):
    layers_rods = [False] * 20
    rods_layer = CONNECTIONS_LAYER
    layers_rods[rods_layer] = True
    mu.delete_hierarchy(PARENT_OBJ)
    mu.create_empty_if_doesnt_exists(PARENT_OBJ, BRAIN_EMPTY_LAYER, None,
                                     'Functional maps')
    # cond_id = [i for i, cond in enumerate(d.conditions) if cond == condition][0]
    if d.con_colors.ndim == 3:
        windows_num = d.con_colors.shape[1]
    else:
        windows_num = 1
    T = ConnectionsPanel.addon.get_max_time_steps()
    if T == 0:
        T = windows_num
    norm_fac = T / windows_num
    # todo: Check if we really want to let the user create connection according to the first option
    # if bpy.context.scene.selection_type == 'conds':
    #     # Takes all the connections that at least one condition pass the threshold
    #     # Ex: np.array([True, False, False]) | np.array([False, True, False]) = array([ True,  True, False], dtype=bool)
    #     mask1 = np.max(d.con_values[:, :, 0], axis=1) > threshold
    #     mask2 = np.max(d.con_values[:, :, 1], axis=1) > threshold
    #     mask = mask1 | mask2
    # else:
    if d.con_values.ndim > 2:
        stat_data = calc_stat_data(d.con_values, stat)
    else:
        stat_data = d.con_values
    mask = calc_mask(stat_data, threshold, threshold_type, windows_num)
    indices = np.where(mask)[0]
    parent_obj = bpy.data.objects[PARENT_OBJ]
    parent_obj.animation_data_clear()
    N = len(indices)
    print('{} connections are above the threshold'.format(N))
    create_conncection_for_both_conditions(d, layers_rods, indices, mask,
                                           windows_num, norm_fac, T, radius)
    print('Create connections for the conditions {}'.format(
        'difference' if stat == STAT_DIFF else 'mean'))
    create_keyframes_for_parent_obj(d, indices, mask, windows_num, norm_fac, T,
                                    stat)
    print('finish keyframing!')
Exemplo n.º 6
0
def create_keyframes(d, threshold, threshold_type, radius=.1, stat=STAT_DIFF, verts_color='pink'):
    layers_rods = [False] * 20
    rods_layer = _addon().CONNECTIONS_LAYER
    layers_rods[rods_layer] = True
    mu.delete_hierarchy(PARENT_OBJ)
    mu.create_empty_if_doesnt_exists(PARENT_OBJ, _addon().BRAIN_EMPTY_LAYER, None, 'Functional maps')
    # cond_id = [i for i, cond in enumerate(d.conditions) if cond == condition][0]
    if d.con_colors.ndim == 3:
        windows_num = d.con_colors.shape[1]
    else:
        windows_num = 1
    T = ConnectionsPanel.addon.get_max_time_steps()
    if T == 0:
        T = windows_num
    norm_fac = T / windows_num
    # todo: Check if we really want to let the user create connection according to the first option
    # if bpy.context.scene.selection_type == 'conds':
    #     # Takes all the connections that at least one condition pass the threshold
    #     # Ex: np.array([True, False, False]) | np.array([False, True, False]) = array([ True,  True, False], dtype=bool)
    #     mask1 = np.max(d.con_values[:, :, 0], axis=1) > threshold
    #     mask2 = np.max(d.con_values[:, :, 1], axis=1) > threshold
    #     mask = mask1 | mask2
    # else:
    if d.con_values.ndim > 2:
        stat_data = calc_stat_data(d.con_values, stat)
    else:
        stat_data = d.con_values
    mask = calc_mask(stat_data, threshold, threshold_type, windows_num)
    indices = np.where(mask)[0]
    parent_obj = bpy.data.objects[PARENT_OBJ]
    parent_obj.animation_data_clear()
    N = len(indices)
    print('{} connections are above the threshold'.format(N))
    create_conncection_for_both_conditions(d, layers_rods, indices, mask, windows_num, norm_fac, T, radius)
    create_vertices(d, mask, verts_color)
    print('Create connections for the conditions {}'.format('difference' if stat == STAT_DIFF else 'mean'))
    create_keyframes_for_parent_obj(d, indices, mask, windows_num, norm_fac, T, stat)
    print('finish keyframing!')
Exemplo n.º 7
0
 def invoke(self, context, event=None):
     mu.delete_hierarchy(PARENT_OBJ)
     return {"FINISHED"}
Exemplo n.º 8
0
def delete_electrodes():
    mu.delete_hierarchy('Deep_electrodes')
Exemplo n.º 9
0
 def invoke(self, context, event=None):
     mu.delete_hierarchy(get_connections_parent_name())
     return {"FINISHED"}
Exemplo n.º 10
0
def get_plane_values(direction_vert=None,
                     plot_arrows=False,
                     plot_directional_arrow=False):
    context = bpy.context
    scene = context.scene
    layers_array = bpy.context.scene.layers
    skull_thickness = np.load(
        op.join(mu.get_user_fol(), 'skull', 'ray_casts_from_outer.npy'))
    emptys_name = 'plane_arrows'
    _addon().create_empty_if_doesnt_exists(emptys_name,
                                           _addon().SKULL_LAYER, layers_array,
                                           'Skull')

    skull = bpy.data.objects['outer_skull']
    plane = bpy.data.objects['skull_plane']

    if direction_vert is None:
        _, vertex_ind, vertex_co = find_point_thickness(
            plane.location, skull_type='outer_skull')
        direction_vert = skull.data.vertices[vertex_ind]

    omw = plane.matrix_world
    factor = np.linalg.inv(omw)[0, 0]
    omwi = omw.inverted()
    imw = skull.matrix_world
    mat = omwi * imw

    o = mat * direction_vert.co
    n = mat * (direction_vert.co + direction_vert.normal * -1) - o
    hit, loc, norm, index = plane.ray_cast(o, n)
    if hit:
        if plot_directional_arrow:
            draw_empty_arrow(scene, emptys_name, 'direction', omw * loc,
                             omw * o - omw * loc)
        dir_vec_length = abs((omw * loc - omw * o).length * factor)
    else:
        print('No hit from directional vertex!')
        return

    plane_thikness, hits = [], []
    vertices = skull.data.vertices
    for vert_ind, vert in enumerate(vertices):
        o = mat * vert.co
        n = mat * (vert.co + direction_vert.normal * -1) - o
        hit, loc, norm, index = plane.ray_cast(o, n)
        if hit:
            length = abs((omw * loc - omw * o).length * factor)
            if length > dir_vec_length * 2:
                continue
            # else:
            #     print(length, dir_vec_length)
            plane_thikness.append(skull_thickness[vert_ind])
            hits.append((vert_ind, o, loc))

    if len(plane_thikness) > 0:
        plane_thikness = np.array(plane_thikness).squeeze()
        SkullPanel.plane_thickness = (np.min(plane_thikness),
                                      np.max(plane_thikness),
                                      np.mean(plane_thikness))
        if plot_arrows:
            mu.delete_hierarchy(emptys_name)
            _addon().create_empty_if_doesnt_exists(emptys_name,
                                                   _addon().SKULL_LAYER,
                                                   layers_array, 'Skull')
            for vert_ind, hit, o in hits:  # [hits[np.argmax(plane_thikness)], hits[np.argmin(plane_thikness)]]:
                draw_empty_arrow(scene, emptys_name, vert_ind, omw * o,
                                 omw * hit - omw * o)
    else:
        print('No hits from outer skull to the plane!')
Exemplo n.º 11
0
 def invoke(self, context, event=None):
     mu.delete_hierarchy(PARENT_OBJ)
     return {"FINISHED"}