예제 #1
0
def _curves_sep_update(force=False):
    fcurves, data = get_selected_fcurves_and_data()
    if len(fcurves) == 0:
        return
    if data.ndim == 3:
        N, T, C = data.shape
    else:
        N, T = data.shape
        C = 1
    get_data = lambda ind, t, c: data[
        data_ind, t, c] if data.ndim == 3 else data[data_ind, t]
    sep_inds = np.tile(np.arange(0, N), (C, 1)).T.ravel()
    fcurve_ind = 0
    for data_ind in range(N):
        data_mean = np.median(
            data[data_ind]) if bpy.context.scene.curves_sep > 0 else 0
        for c in range(C):
            fcurve = fcurves[fcurve_ind]
            if not force and SelectionMakerPanel.curves_sep.get(mu.get_fcurve_name(fcurve), -1) == \
                    bpy.context.scene.curves_sep:
                fcurve_ind += 1
                continue
            for t in range(T):
                fcurve.keyframe_points[t].co[1] = \
                    get_data(data_ind, t, c) - data_mean + (N / 2 - 0.5 - sep_inds[fcurve_ind]) * bpy.context.scene.curves_sep
            fcurve_ind += 1
            if bpy.context.scene.curves_sep == 0:
                fcurve.keyframe_points[0].co[1] = fcurve.keyframe_points[T].co[
                    1] = 0
    for fcurve in fcurves:
        SelectionMakerPanel.curves_sep[mu.get_fcurve_name(
            fcurve)] = bpy.context.scene.curves_sep
    # mu.change_fcurves_colors(fcurves=fcurves)
    mu.view_all_in_graph_editor()
예제 #2
0
def _clusters_update():
    MEGPanel.current_cluster = cluster = MEGPanel.clusters_lookup[
        bpy.context.scene.meg_clusters]
    # set_cluster_time_series(cluster)
    cluster_name = get_cluster_name(cluster)
    cluster_max_vert_co = mu.get_vert_co(cluster.max_vert, cluster.hemi)
    bpy.context.scene.cursor_location = cluster_max_vert_co
    _addon().set_cursor_pos()
    _addon().set_closest_vertex_and_mesh_to_cursor(
        cluster.max_vert, 'inflated_{}'.format(cluster.hemi))
    _addon().save_cursor_position()
    _addon().create_slices()
    if bpy.context.scene.plot_current_meg_cluster:
        _addon().color_contours([cluster_name], cluster.hemi,
                                MEGPanel.contours,
                                bpy.context.scene.cumulate_meg_cluster, False)
    bpy.data.objects[PARENT_OBJ_NAME].select = True
    # mu.view_all_in_graph_editor()
    fcurves = mu.get_fcurves(PARENT_OBJ_NAME)
    cluster_fcurve_name = get_cluster_fcurve_name(cluster)
    if bpy.context.scene.cumulate_meg_cluster:
        fcurve_ind = [mu.get_fcurve_name(f)
                      for f in fcurves].index(cluster_fcurve_name)
        fcurves[fcurve_ind].hide = False
    else:
        for fcurve in fcurves:
            fcurve_name = mu.get_fcurve_name(fcurve)
            fcurve.hide = fcurve_name != cluster_fcurve_name

    MEGPanel.prev_cluster = bpy.context.scene.meg_clusters
예제 #3
0
def change_graph_all_vals(mat):
    MAX_STEPS = StreamingPanel.max_steps
    T = min(mat.shape[1], MAX_STEPS)
    parent_obj = bpy.data.objects['Deep_electrodes']
    C = len(parent_obj.animation_data.action.fcurves)
    good_electrodes = range(mat.shape[0])
    elecs_cycle = cycle(good_electrodes)
    data_min, data_max = np.min(mat[good_electrodes]), np.max(
        mat[good_electrodes])
    StreamingPanel.data_min = data_min = min(data_min, StreamingPanel.data_min)
    StreamingPanel.data_max = data_max = max(data_max, StreamingPanel.data_max)
    data_abs_minmax = max([abs(data_min), abs(data_max)])
    StreamingPanel.minmax_vals.append(data_abs_minmax)
    if len(StreamingPanel.minmax_vals) > 100:
        StreamingPanel.minmax_vals = StreamingPanel.minmax_vals[-100:]
    StreamingPanel.data_min = data_min = -np.median(StreamingPanel.minmax_vals)
    StreamingPanel.data_max = data_max = np.median(StreamingPanel.minmax_vals)
    colors_ratio = 256 / (data_max - data_min)
    if not _addon().colorbar_values_are_locked():
        _addon().set_colorbar_max_min(data_max, data_min)
    curr_t = bpy.context.scene.frame_current
    for fcurve_ind, fcurve in enumerate(
            parent_obj.animation_data.action.fcurves):
        fcurve_name = mu.get_fcurve_name(fcurve)
        if fcurve_ind == 0:
            max_steps = min([len(fcurve.keyframe_points), MAX_STEPS]) - 2
        elc_ind = next(elecs_cycle)  #fcurve_ind
        if elc_ind >= mat.shape[0]:
            continue
        for ind in range(T):
            t = curr_t + ind
            if t > max_steps:
                t = ind
            fcurve.keyframe_points[t].co[1] = mat[elc_ind, ind] + (
                C / 2 - fcurve_ind) * bpy.context.scene.electrodes_sep
        _addon().color_objects_homogeneously([mat[elc_ind, ind]],
                                             [fcurve_name], None, data_min,
                                             colors_ratio)
        fcurve.keyframe_points[max_steps + 1].co[1] = 0
        fcurve.keyframe_points[0].co[1] = 0

    try:
        StreamingPanel.cycle_data = mat if StreamingPanel.cycle_data == [] else np.hstack(
            (StreamingPanel.cycle_data, mat))
    except:
        StreamingPanel.cycle_data = mat
        # print('asfd')
    bpy.context.scene.frame_current += mat.shape[1]
    if bpy.context.scene.frame_current > MAX_STEPS - 1:
        bpy.context.scene.frame_current = bpy.context.scene.frame_current - MAX_STEPS
        time_diff = (datetime.now() - StreamingPanel.time)
        time_diff_sec = time_diff.seconds + time_diff.microseconds * 1e-6
        print('cycle! ', str(time_diff), time_diff_sec)
        save_cycle()
        max_steps_secs = MAX_STEPS / 1000
        if time_diff_sec < max_steps_secs:
            print('sleep for {}'.format(max_steps_secs - time_diff_sec))
            time.sleep(max_steps_secs - time_diff_sec)
        StreamingPanel.time = datetime.now()
        StreamingPanel.cycle_data = []
예제 #4
0
def filter_graph(context, d, condition, threshold, threshold_type, connections_type, stat=STAT_DIFF):
    mu.show_hide_hierarchy(False, PARENT_OBJ)
    masked_con_names = calc_masked_con_names(d, threshold, threshold_type, connections_type, condition, stat)
    parent_obj = bpy.data.objects[PARENT_OBJ]
    for con_name in d.con_names:
        cur_obj = bpy.data.objects.get(con_name)
        if cur_obj:
            cur_obj.hide = con_name not in masked_con_names
            cur_obj.hide_render = con_name not in masked_con_names
            if bpy.context.scene.selection_type == 'conds':
                cur_obj.select = not cur_obj.hide
    if parent_obj.animation_data is None:
        return
    now = time.time()
    fcurves_num = len(parent_obj.animation_data.action.fcurves)
    for fcurve_index, fcurve in enumerate(parent_obj.animation_data.action.fcurves):
        # mu.time_to_go(now, fcurve_index, fcurves_num, runs_num_to_print=10)
        con_name = mu.get_fcurve_name(fcurve)
        # cur_obj = bpy.data.objects[con_name]
        # cur_obj.hide = con_name not in masked_con_names
        # cur_obj.hide_render = con_name not in masked_con_names
        if bpy.context.scene.selection_type != 'conds':
            fcurve.hide = con_name not in masked_con_names
            fcurve.select = not fcurve.hide

    parent_obj.select = True
    mu.view_all_in_graph_editor(context)
예제 #5
0
    def filter_rois(self, current_file_to_upload):
        print('filter_ROIs')
        source_files = [
            op.join(self.current_activity_path,
                    current_file_to_upload.format(hemi=hemi))
            for hemi in mu.HEMIS
        ]
        objects_indices, names, Filtering.filter_values = self.get_object_to_filter(
            source_files)
        Filtering.objects_indices, Filtering.filter_objects = objects_indices, names
        deselect_all_objects()

        filter_obj_names = [names[ind] for ind in objects_indices]
        if bpy.context.scene.selection_type == 'diff':
            brain_obj = bpy.data.objects['Brain']
            self.objs_colors = []
            for fcurve in brain_obj.animation_data.action.fcurves:
                con_name = mu.get_fcurve_name(fcurve)
                fcurve.hide = con_name not in filter_obj_names
                fcurve.select = not fcurve.hide
                if fcurve.select:
                    self.objs_colors.append(fcurve.color)
            brain_obj.select = True
            _addon().color_contours(filter_obj_names,
                                    specific_colors=self.objs_colors,
                                    move_cursor=False)
        else:
            for roi_name in filter_obj_names:
                bpy.data.objects[roi_name].select = True
            self.objs_colors = mu.change_selected_fcurves_colors(
                mu.OBJ_TYPES_ROIS)
            _addon().color_contours(filter_obj_names,
                                    specific_colors=self.objs_colors,
                                    move_cursor=False)
예제 #6
0
    def filter_electrodes_or_sensors(self, parent_name, data, meta):
        # source_files = [op.join(self.current_activity_path, current_file_to_upload)]
        objects_indices, names, Filtering.filter_values = self.get_object_to_filter(
            [], data, meta['names'])
        names = [mu.to_str(e) for e in meta['names']]
        Filtering.objects_indices, Filtering.filter_objects = objects_indices, names
        if objects_indices is None:
            return
        for obj in bpy.data.objects:
            obj.select = False
        parent_obj = bpy.data.objects[parent_name]
        for obj in parent_obj.children:
            obj.active_material.node_tree.nodes["Layer Weight"].inputs[
                0].default_value = 1

        if bpy.context.scene.selection_type == 'diff' and not parent_obj.animation_data is None:
            filter_obj_names = [names[ind] for ind in objects_indices]
            for fcurve in parent_obj.animation_data.action.fcurves:
                con_name = mu.get_fcurve_name(fcurve)
                fcurve.hide = con_name not in filter_obj_names
                fcurve.select = not fcurve.hide
            parent_obj.select = True
        else:
            parent_obj.select = False

        for ind in range(min(self.topK, len(objects_indices)) - 1, -1, -1):
            if bpy.data.objects.get(names[objects_indices[ind]]):
                orig_name = bpy.data.objects[names[objects_indices[ind]]].name
                filter_electrode_or_sensor(orig_name)
            else:
                print("Can't find {}!".format(names[objects_indices[ind]]))
예제 #7
0
def filter_graph(context,
                 d,
                 condition,
                 threshold,
                 threshold_type,
                 connections_type,
                 stat=STAT_DIFF):
    mu.show_hide_hierarchy(False, PARENT_OBJ)
    masked_con_names = calc_masked_con_names(d, threshold, threshold_type,
                                             connections_type, condition, stat)
    parent_obj = bpy.data.objects[PARENT_OBJ]
    for con_name in d.con_names:
        cur_obj = bpy.data.objects.get(con_name)
        if cur_obj:
            cur_obj.hide = con_name not in masked_con_names
            cur_obj.hide_render = con_name not in masked_con_names
            if bpy.context.scene.selection_type == 'conds':
                cur_obj.select = not cur_obj.hide
    if parent_obj.animation_data is None:
        return
    now = time.time()
    fcurves_num = len(parent_obj.animation_data.action.fcurves)
    for fcurve_index, fcurve in enumerate(
            parent_obj.animation_data.action.fcurves):
        # mu.time_to_go(now, fcurve_index, fcurves_num, runs_num_to_print=10)
        con_name = mu.get_fcurve_name(fcurve)
        # cur_obj = bpy.data.objects[con_name]
        # cur_obj.hide = con_name not in masked_con_names
        # cur_obj.hide_render = con_name not in masked_con_names
        if bpy.context.scene.selection_type != 'conds':
            fcurve.hide = con_name not in masked_con_names
            fcurve.select = not fcurve.hide

    parent_obj.select = True
    mu.view_all_in_graph_editor(context)
예제 #8
0
    def filter_rois(self, current_file_to_upload):
        print('filter_ROIs')
        source_files = [
            op.join(self.current_activity_path,
                    current_file_to_upload.format(hemi=hemi))
            for hemi in mu.HEMIS
        ]
        objects_indices, names, self.filter_values = self.get_object_to_filter(
            source_files)
        Filtering.objects_indices, Filtering.filter_objects = objects_indices, names
        deselect_all_objects()

        if bpy.context.scene.selection_type == 'diff':
            filter_obj_names = [names[ind] for ind in objects_indices]
            brain_obj = bpy.data.objects['Brain']
            for fcurve in brain_obj.animation_data.action.fcurves:
                con_name = mu.get_fcurve_name(fcurve)
                fcurve.hide = con_name not in filter_obj_names
                fcurve.select = not fcurve.hide
            brain_obj.select = True

        if bpy.context.scene.filter_items_one_by_one:
            _addon().show_rois()
        else:
            objects_names, objects_colors, objects_data = self.get_objects_to_color(
                names, objects_indices)
            _addon().color_objects(objects_names, objects_colors, objects_data)
예제 #9
0
def _curves_sep_update(force=False):
    # if len(bpy.context.selected_objects) == 1:
    #     return
    fcurves, data = get_selected_fcurves_and_data()
    if len(fcurves) == 0:
        return
    data = data.squeeze()
    if data.ndim == 3:
        N, T, C = data.shape
        get_data = lambda ind, t, c: data[data_ind, t, c]
    elif data.ndim == 2:
        N, T = data.shape
        if N - 10 < len(fcurves[0].keyframe_points) < N + 10:
            N, T = T, N
            # switch dims
            data = data.T
        C = 1
        get_data = lambda ind, t, c: data[data_ind, t]
    elif data.ndim == 1:
        T = len(data)
        N, C = 1, 1
        get_data = lambda ind, t, c: data[t]
    else:
        print('_curves_sep_update: Wrong number dims!')
    sep_inds = np.tile(np.arange(0, N), (C, 1)).T.ravel()
    for data_ind in range(N):
        fcurve_ind = 0
        data_mean = np.median(data[data_ind]) if bpy.context.scene.curves_sep > 0 else 0
        for c in range(C):
            fcurve = fcurves[fcurve_ind]
            fcurve_T = min(T, len(fcurve.keyframe_points)) - 1
            if not force and SelectionMakerPanel.curves_sep.get(mu.get_fcurve_name(fcurve), -1) == \
                    bpy.context.scene.curves_sep:
                fcurve_ind += 1
                continue
            for t in range(fcurve_T):
                fcurve.keyframe_points[t].co[1] = \
                    get_data(data_ind, t, c) - data_mean + (N / 2 - 0.5 - sep_inds[fcurve_ind]) * bpy.context.scene.curves_sep
            fcurve_ind += 1
            if bpy.context.scene.curves_sep == 0:
                fcurve.keyframe_points[0].co[1] = fcurve.keyframe_points[fcurve_T].co[1] = 0
    for fcurve in fcurves:
        SelectionMakerPanel.curves_sep[mu.get_fcurve_name(fcurve)] = bpy.context.scene.curves_sep
    # mu.change_fcurves_colors(fcurves=fcurves)
    mu.view_all_in_graph_editor()
예제 #10
0
def find_connections_closest_to_target_value(closet_object_name, closest_curve_name, target):
    parent_obj = bpy.data.objects[PARENT_OBJ]
    if bpy.context.scene.selection_type == 'conds':
        for cur_obj in parent_obj.children:
            if not cur_obj.animation_data:
                continue
            for fcurve in cur_obj.animation_data.action.fcurves:
                if cur_obj.name == closet_object_name:
                    fcurve_name = mu.get_fcurve_name(fcurve)
                    fcurve.select = fcurve_name == closest_curve_name
                    fcurve.hide = fcurve_name != closest_curve_name
                else:
                    fcurve.select = False
                    fcurve.hide = True
    else:  # diff
        # todo: implement this part
        for fcurve in parent_obj.animation_data.action.fcurves:
            conn_name = mu.get_fcurve_name(conn_name)
예제 #11
0
def find_connections_closest_to_target_value(closet_object_name,
                                             closest_curve_name, target):
    parent_obj = bpy.data.objects[PARENT_OBJ]
    if bpy.context.scene.selection_type == 'conds':
        for cur_obj in parent_obj.children:
            if not cur_obj.animation_data:
                continue
            for fcurve in cur_obj.animation_data.action.fcurves:
                if cur_obj.name == closet_object_name:
                    fcurve_name = mu.get_fcurve_name(fcurve)
                    fcurve.select = fcurve_name == closest_curve_name
                    fcurve.hide = fcurve_name != closest_curve_name
                else:
                    fcurve.select = False
                    fcurve.hide = True
    else:  # diff
        # todo: implement this part
        for fcurve in parent_obj.animation_data.action.fcurves:
            conn_name = mu.get_fcurve_name(conn_name)
예제 #12
0
def change_graph(next_val):
    obj_name = 'LMF6'
    fcurve_name = 'LMF6_interference'
    bpy.data.objects[obj_name].select = True
    parent_obj = bpy.data.objects[obj_name]
    curves = [c for c in parent_obj.animation_data.action.fcurves if mu.get_fcurve_name(c) == fcurve_name]
    for fcurve in curves:
        N = len(fcurve.keyframe_points)
        for ind in range(N - 1, 0, -1):
            fcurve.keyframe_points[ind].co[1] = fcurve.keyframe_points[ind - 1].co[1]
        fcurve.keyframe_points[0].co[1] = next_val
    return next_val
예제 #13
0
def filter_graph(context,
                 d,
                 condition,
                 threshold,
                 threshold_type,
                 connections_type,
                 stat=STAT_DIFF):
    parent_obj_name = get_connections_parent_name()
    mu.show_hide_hierarchy(False, parent_obj_name)
    masked_con_names = calc_masked_con_names(d, threshold, threshold_type,
                                             connections_type, condition, stat)
    parent_obj = bpy.data.objects[parent_obj_name]
    bpy.context.scene.connections_num = min(len(masked_con_names),
                                            len(parent_obj.children) - 1)
    conn_show = 0
    selected_objects, selected_indices = [], []
    for con_ind, con_name in enumerate(d.con_names):
        cur_obj = bpy.data.objects.get(con_name)
        if cur_obj:
            if con_name in masked_con_names:
                selected_objects.append(cur_obj)
                selected_indices.append(con_ind)
                conn_show += 1
            cur_obj.hide = con_name not in masked_con_names
            cur_obj.hide_render = con_name not in masked_con_names
            if bpy.context.scene.selection_type == 'conds':
                cur_obj.select = not cur_obj.hide
    ConnectionsPanel.selected_objects = selected_objects
    ConnectionsPanel.selected_indices = selected_indices
    print('Showing {} connections after filtering'.format(conn_show))
    if conn_show > 0:
        bpy.context.scene.connections_min = np.min(
            d.con_values[selected_indices])
        bpy.context.scene.connections_max = np.max(
            d.con_values[selected_indices])
    if parent_obj.animation_data is None:
        return
    now = time.time()
    fcurves_num = len(parent_obj.animation_data.action.fcurves)
    for fcurve_index, fcurve in enumerate(
            parent_obj.animation_data.action.fcurves):
        # mu.time_to_go(now, fcurve_index, fcurves_num, runs_num_to_print=10)
        con_name = mu.get_fcurve_name(fcurve)
        # cur_obj = bpy.data.objects[con_name]
        # cur_obj.hide = con_name not in masked_con_names
        # cur_obj.hide_render = con_name not in masked_con_names
        if bpy.context.scene.selection_type != 'conds':
            fcurve.hide = con_name not in masked_con_names
            fcurve.select = not fcurve.hide

    parent_obj.select = True
    mu.view_all_in_graph_editor(context)
예제 #14
0
    def filter_rois(self, current_file_to_upload):
        print('filter_ROIs')
        _addon().show_rois()
        source_files = [
            op.join(self.current_activity_path,
                    current_file_to_upload.format(hemi=hemi))
            for hemi in mu.HEMIS
        ]
        objects_indices, names, self.filter_values = self.get_object_to_filter(
            source_files)
        Filtering.objects_indices, Filtering.filter_objects = objects_indices, names
        deselect_all_objects()

        if bpy.context.scene.selection_type == 'diff':
            filter_obj_names = [names[ind] for ind in objects_indices]
            brain_obj = bpy.data.objects['Brain']
            for fcurve in brain_obj.animation_data.action.fcurves:
                con_name = mu.get_fcurve_name(fcurve)
                fcurve.hide = con_name not in filter_obj_names
                fcurve.select = not fcurve.hide
            brain_obj.select = True

        curves_num = 0
        for ind in range(min(self.topK, len(objects_indices))):
            orig_name = names[objects_indices[ind]]
            if 'unknown' not in orig_name:
                obj = bpy.data.objects.get(orig_name)
                if not obj is None and not obj.animation_data is None:
                    curves_num += len(obj.animation_data.action.fcurves)

        colors = cu.get_distinct_colors(curves_num)
        objects_names, objects_colors, objects_data = defaultdict(
            list), defaultdict(list), defaultdict(list)
        for ind in range(min(self.topK, len(objects_indices)) - 1, -1, -1):
            if bpy.data.objects.get(names[objects_indices[ind]]):
                orig_name = names[objects_indices[ind]]
                obj_type = mu.check_obj_type(orig_name)
                objects_names[obj_type].append(orig_name)
                objects_colors[obj_type].append(cu.name_to_rgb('green'))
                objects_data[obj_type].append(1.0)
                if 'unknown' not in orig_name:
                    filter_roi_func(orig_name)
                    for fcurve in bpy.data.objects[
                            orig_name].animation_data.action.fcurves:
                        fcurve.color_mode = 'CUSTOM'
                        fcurve.color = tuple(next(colors))
            else:
                print("Can't find {}!".format(names[objects_indices[ind]]))

        _addon().color_objects(objects_names, objects_colors, objects_data)
예제 #15
0
def select_all_clusters():
    parent_obj = bpy.data.objects.get(PARENT_OBJ_NAME, None)
    if parent_obj is not None:
        parent_obj.select = True

    fcurves = mu.get_fcurves(PARENT_OBJ_NAME)
    filtered_fcurves_names = [
        get_cluster_fcurve_name(c) for c in MEGPanel.clusters_labels_filtered
    ]
    for fcurve in fcurves:
        fcurve_name = mu.get_fcurve_name(fcurve)
        fcurve.hide = fcurve_name not in filtered_fcurves_names
    plot_all_clusters()
    mu.view_all_in_graph_editor()
예제 #16
0
def get_all_selected_connections(d):
    objs, inds = [], []
    parent_obj = bpy.data.objects[PARENT_OBJ]
    if bpy.context.scene.selection_type == 'conds' or parent_obj.animation_data is None:
        for ind, con_name in enumerate(d.con_names):
            cur_obj = bpy.data.objects.get(con_name)
            if cur_obj and not cur_obj.hide:
                objs.append(cur_obj.name)
                inds.append(ind)
    else:
        for fcurve in parent_obj.animation_data.action.fcurves:
            con_name = mu.get_fcurve_name(fcurve)
            if fcurve.select and not fcurve.hide:
                objs.append(con_name)
                ind = np.where(d.con_names == con_name)[0][0]
                inds.append(ind)
    return objs, inds
예제 #17
0
def change_graph(index):
    obj_name = 'LMF6'
    fcurve_name = 'LMF6_interference'
    bpy.data.objects[obj_name].select = True
    parent_obj = bpy.data.objects[obj_name]
    T = 2500
    next_val = next(StreamingPanel.fixed_data)
    for fcurve in parent_obj.animation_data.action.fcurves:
        if mu.get_fcurve_name(fcurve) == fcurve_name:
            # for kp in fcurve.keyframe_points:
                # kp.co[1] = next(StreamingPanel.fixed_data)#[kp.co[0] + index * 100]
                # kp.co[1] = np.sin(2 * np.pi * (kp.co[0] / T * 4 - 0.1 * index))
            N = len(fcurve.keyframe_points)
            for ind in range(N - 1, 0, -1):
                fcurve.keyframe_points[ind].co[1] = fcurve.keyframe_points[ind - 1].co[1]
            fcurve.keyframe_points[0].co[1] = next_val
    return next_val
예제 #18
0
def get_all_selected_connections(d):
    objs, inds = [], []
    parent_obj = bpy.data.objects[PARENT_OBJ]
    if bpy.context.scene.selection_type == 'conds' or parent_obj.animation_data is None:
        for ind, con_name in enumerate(d.con_names):
            cur_obj = bpy.data.objects.get(con_name)
            if cur_obj and not cur_obj.hide:
                objs.append(cur_obj.name)
                inds.append(ind)
    else:
        for fcurve in parent_obj.animation_data.action.fcurves:
            con_name = mu.get_fcurve_name(fcurve)
            if fcurve.select and not fcurve.hide:
                objs.append(con_name)
                ind = np.where(d.con_names == con_name)[0][0]
                inds.append(ind)
    return objs, inds
예제 #19
0
def vertices_selected(vertice_name):
    label_name = vertice_name[:-len('_vertice')]
    if _addon().both_conditions():
        for conn_name in ConnectionsPanel.vertices_lookup[label_name]:
            conn_obj = bpy.data.objects.get(conn_name, None)
            if conn_obj and not conn_obj.hide:
                conn_obj.select = True
    elif _addon().conditions_diff():
        parent_obj = bpy.context.scene.objects[get_connections_parent_name()]
        if parent_obj.animation_data is None:
            return
        parent_obj.select = True
        for fcurve in parent_obj.animation_data.action.fcurves:
            con_name = mu.get_fcurve_name(fcurve)
            fcurve.select = con_name in ConnectionsPanel.vertices_lookup[label_name]
            fcurve.hide = not con_name in ConnectionsPanel.vertices_lookup[label_name]
    mu.change_fcurves_colors(bpy.data.objects[get_connections_parent_name()].children)
    _addon().fit_selection()
예제 #20
0
def filter_electrodes_via_connections(context, do_filter):
    display_conds = bpy.context.scene.selection_type == 'conds'
    for elc_name in ConnectionsPanel.addon.play_panel.PlayPanel.electrodes_names:
        cur_obj = bpy.data.objects.get(elc_name)
        if cur_obj:
            cur_obj.hide = do_filter
            cur_obj.hide_render = do_filter
            cur_obj.select = not do_filter and display_conds
            for fcurve in cur_obj.animation_data.action.fcurves:
                fcurve.hide = do_filter
                fcurve.select = not do_filter

    elecs_parent_obj = bpy.data.objects['Deep_electrodes']
    elecs_parent_obj.select = not display_conds
    for fcurve in elecs_parent_obj.animation_data.action.fcurves:
        fcurve.hide = do_filter
        fcurve.select = not do_filter

    if do_filter:
        selected_electrodes = set()
        for ind, con_name in enumerate(ConnectionsPanel.d.con_names):
            cur_obj = bpy.data.objects.get(con_name)
            if not cur_obj or cur_obj.hide:
                continue
            electrodes = con_name.split('-')
            for elc in electrodes:
                cur_elc = bpy.data.objects.get(elc)
                if not cur_elc or elc in selected_electrodes:
                    continue
                selected_electrodes.add(elc)
                # if bpy.context.scene.selection_type == 'conds':
                cur_elc.hide = False
                cur_elc.hide_render = False
                cur_elc.select = display_conds
                for fcurve in cur_elc.animation_data.action.fcurves:
                    fcurve.hide = False
                    fcurve.select = True
        for fcurve in elecs_parent_obj.animation_data.action.fcurves:
            elc_name = mu.get_fcurve_name(fcurve)
            if elc_name in selected_electrodes:
                fcurve.hide = False
                fcurve.select = True

    mu.view_all_in_graph_editor()
예제 #21
0
def filter_electrodes_via_connections(context, do_filter):
    display_conds = bpy.context.scene.selection_type == 'conds'
    for elc_name in ConnectionsPanel.addon.play_panel.PlayPanel.electrodes_names:
        cur_obj = bpy.data.objects.get(elc_name)
        if cur_obj:
            cur_obj.hide = do_filter
            cur_obj.hide_render = do_filter
            cur_obj.select = not do_filter and display_conds
            for fcurve in cur_obj.animation_data.action.fcurves:
                fcurve.hide = do_filter
                fcurve.select = not do_filter

    elecs_parent_obj = bpy.data.objects['Deep_electrodes']
    elecs_parent_obj.select = not display_conds
    for fcurve in elecs_parent_obj.animation_data.action.fcurves:
        fcurve.hide = do_filter
        fcurve.select = not do_filter

    if do_filter:
        selected_electrodes = set()
        for ind, con_name in enumerate(ConnectionsPanel.d.con_names):
            cur_obj = bpy.data.objects.get(con_name)
            if not cur_obj or cur_obj.hide:
                continue
            electrodes = con_name.split('-')
            for elc in electrodes:
                cur_elc = bpy.data.objects.get(elc)
                if not cur_elc or elc in selected_electrodes:
                    continue
                selected_electrodes.add(elc)
                # if bpy.context.scene.selection_type == 'conds':
                cur_elc.hide = False
                cur_elc.hide_render = False
                cur_elc.select = display_conds
                for fcurve in cur_elc.animation_data.action.fcurves:
                    fcurve.hide = False
                    fcurve.select = True
        for fcurve in elecs_parent_obj.animation_data.action.fcurves:
            elc_name = mu.get_fcurve_name(fcurve)
            if elc_name in selected_electrodes:
                fcurve.hide = False
                fcurve.select = True

    mu.view_all_in_graph_editor(context)
예제 #22
0
def get_all_selected_connections(d):
    objs, inds = [], []
    parent_obj_name = get_connections_parent_name()
    parent_obj = bpy.data.objects[parent_obj_name]
    # con_objs_names = [obj.name for obj in bpy.data.objects[get_connections_parent_name()].children if obj.name != 'connections_vertices']
    # all_con_set = set(d.con_names.tolist())
    # indices = [np.where(d.con_names == obj_name)[0][0] for obj_name in con_objs_names]
    if bpy.context.scene.selection_type == 'conds' or parent_obj.animation_data is None:
        for ind, con_name in enumerate(d.con_names):
            cur_obj = bpy.data.objects.get(con_name)
            if cur_obj and not cur_obj.hide:
                objs.append(cur_obj.name)
                inds.append(ind)
    else:
        for fcurve in parent_obj.animation_data.action.fcurves:
            con_name = mu.get_fcurve_name(fcurve)
            if fcurve.select and not fcurve.hide:
                objs.append(con_name)
                ind = np.where(d.con_names == con_name)[0][0]
                inds.append(ind)
    return objs, inds
예제 #23
0
def get_selected_clusters_data():
    names = []
    data = None
    fcurves = mu.get_fcurves(PARENT_OBJ_NAME)
    filtered_fcurves_names = [
        get_cluster_fcurve_name(c) for c in MEGPanel.clusters_labels_filtered
    ]
    filtered_clusters = {
        fcurve_name: cluster
        for fcurve_name, cluster in zip(filtered_fcurves_names,
                                        MEGPanel.clusters_labels_filtered)
    }
    data_ind = 0
    for fcurve in fcurves:
        fcurve_name = mu.get_fcurve_name(fcurve)
        if fcurve_name in filtered_fcurves_names:
            x = filtered_clusters[fcurve_name].label_data
            if data is None:
                data = np.zeros((len(filtered_fcurves_names), len(x)))
            data[data_ind] = x
            names.append(fcurve_name)
            data_ind += 1
    return data, names, ['all']
예제 #24
0
def select_roi(roi_name, change_selected_fcurves_colors=True):
    roi = bpy.data.objects.get(roi_name)
    if roi is None:
        return

    # check if MEG data is loaded and attahced to the obj
    if mu.count_fcurves(roi) > 0:
        roi.select = True
        labels_selection_coloring(roi_name)
        # mu.change_fcurves_colors(roi)
    else:
        # Check if dynamic fMRI data is loaded
        fmri_parent_obj = bpy.data.objects.get('fMRI')
        fcurves = mu.get_fcurves(fmri_parent_obj)
        for fcurve in fcurves:
            if mu.get_fcurve_name(fcurve) == roi_name:
                fcurve.hide = False
                fmri_parent_obj.select = True
            else:
                fcurve.hide = True
    if change_selected_fcurves_colors:
        mu.change_selected_fcurves_colors(mu.OBJ_TYPES_ROIS)
    mu.view_all_in_graph_editor()
예제 #25
0
def get_selected_fcurves_and_data():
    # todo: support more than one type in selection
    all_fcurves, all_data = [], []
    for selction_type, parent_obj in zip([SEL_ELECTRODES, SEL_ROIS, SEL_FUNC_ROIS],
                                         [_addon().electrodes_panel_parent, '', _addon().meg_panel_parent]):
        if selction_type not in SelectionMakerPanel.get_data:
            continue
        # fcurves = mu.get_fcurves(parent_obj, recursive=True, only_selected=True)
        fcurves = SelectionMakerPanel.fcurves[selction_type]()
        if fcurves is None or len(fcurves) == 0:
            continue
        data, names, conditions = SelectionMakerPanel.get_data[selction_type]()
        # try:
        #     conditions = mu.get_animation_conditions(bpy.data.objects[names[0]])
        # except:
        #     conditions = None
        # if conditions is None:
        #     conditions = _conditions
        if data is None:
            continue
        fcurves_names = set([mu.get_fcurve_name(f) for f in fcurves])
        # todo: fix that for rois!
        # if mu.if_cond_is_diff(parent_obj):
        #     if data.ndim == 3:
        #         data = np.diff(data, axis=2)
        #     selected_indices = [ind for ind, name in enumerate(names) if name in fcurves_names]
        # else:
        if selction_type != SEL_FUNC_ROIS:
            selected_indices = [ind for ind, name in enumerate(names)
                                if any(['{}_{}'.format(name, cond) in fcurves_names for cond in conditions])]
            if len(selected_indices) == 0:
                continue
            data = data[selected_indices]
        all_fcurves.extend(fcurves)
        all_data = data if all_data == [] else np.concatenate((all_data, data))
    return all_fcurves, all_data
예제 #26
0
def change_graph_all_vals(mat, channels_names=(), stim_channels=(), stim_length=50):
    MAX_STEPS = StreamingPanel.max_steps
    T = min(mat.shape[1], MAX_STEPS)
    parent_obj = bpy.data.objects['Deep_electrodes']
    C = len(parent_obj.animation_data.action.fcurves)
    good_electrodes = range(mat.shape[0])
    elecs_cycle = cycle(good_electrodes)
    no_zeros_data = mat[good_electrodes]
    no_zeros_data = no_zeros_data[np.where(no_zeros_data)]
    if len(no_zeros_data) == 0:
        data_min, data_max = 0, 0
        colors_ratio = 256
    else:
        data_min, data_max = np.min(no_zeros_data), np.max(no_zeros_data)
        colors_ratio = 256 / (data_max - data_min)
    StreamingPanel.data_min = data_min = min(data_min, StreamingPanel.data_min)
    StreamingPanel.data_max = data_max = max(data_max, StreamingPanel.data_max)
    data_abs_minmax = max([abs(data_min), abs(data_max)])
    StreamingPanel.minmax_vals.append(data_abs_minmax)
    # if len(StreamingPanel.minmax_vals) > 100:
    #     StreamingPanel.minmax_vals = StreamingPanel.minmax_vals[-100:]
    # StreamingPanel.data_min = data_min = -np.median(StreamingPanel.minmax_vals)
    # StreamingPanel.data_max = data_max = np.median(StreamingPanel.minmax_vals)
    if not _addon().colorbar_values_are_locked():
        _addon().set_colorbar_max_min(data_max, data_min)
    curr_t = bpy.context.scene.frame_current
    first_curve = True

    # stim
    stim_ch_indices = [channels_names.index(s) for s in stim_channels if s in channels_names]
    for stim_ch_indice in stim_ch_indices:
        if len(np.unique(mat[stim_ch_indice])) == 1:
            mat[stim_ch_indice] = 0
        else:
            stim_indices = np.where(np.diff(mat[stim_ch_indice]) == 1)[0] + 1
            stim_ch = np.zeros(T)
            for stim_indice in stim_indices:
                # mat[stim_ch_indice][(np.arange(T) < stim_indice) | (np.arange(T) > stim_indice + stim_length)] = 0
                stim_ch[(np.arange(T) >= stim_indice) & (np.arange(T) < stim_indice + stim_length)] = 1
            mat[stim_ch_indice] = stim_ch

    for fcurve_ind, fcurve in enumerate(parent_obj.animation_data.action.fcurves):
        fcurve_name = mu.get_fcurve_name(fcurve)
        if len(channels_names) > 0 and fcurve_name not in channels_names and fcurve_name not in stim_channels:
            bpy.data.objects[fcurve_name].hide = bpy.context.scene.stream_show_only_good_electrodes
            fcurve.hide = True
            continue
        if first_curve:
            max_steps = min([len(fcurve.keyframe_points), MAX_STEPS]) - 2
            first_curve = False
        elc_ind = next(elecs_cycle) #fcurve_ind
        if elc_ind >= mat.shape[0]:
            continue
        for ind in range(T):
            t = curr_t + ind
            if t > max_steps:
                t = ind
            fcurve.keyframe_points[t].co[1] = mat[elc_ind, ind] + (C / 2 - fcurve_ind) * bpy.context.scene.electrodes_sep
            # if fcurve.keyframe_points[t].co[1] != 0:
            #     print(elc_ind, ind, fcurve.keyframe_points[t].co[1])
        _addon().color_objects_homogeneously([mat[elc_ind, ind]], [fcurve_name], None, data_min, colors_ratio)
        # fcurve.keyframe_points[max_steps + 1].co[1] = 0
        # fcurve.keyframe_points[0].co[1] = 0

    try:
        StreamingPanel.cycle_data = mat if StreamingPanel.cycle_data == [] else np.hstack((StreamingPanel.cycle_data, mat))
    except:
        StreamingPanel.cycle_data = mat
        # print('asfd')
    bpy.context.scene.frame_current += mat.shape[1]
    if bpy.context.scene.frame_current > MAX_STEPS - 1:
        bpy.context.scene.frame_current = bpy.context.scene.frame_current - MAX_STEPS
        time_diff = (datetime.now() - StreamingPanel.time)
        time_diff_sec = time_diff.seconds + time_diff.microseconds * 1e-6
        print('cycle! ', str(time_diff), time_diff_sec)
        save_cycle()
        max_steps_secs = MAX_STEPS / 1000
        if time_diff_sec < max_steps_secs:
            print('sleep for {}'.format(max_steps_secs - time_diff_sec))
            time.sleep(max_steps_secs - time_diff_sec)
        StreamingPanel.time = datetime.now()
        StreamingPanel.cycle_data = []