def grab_camera(self=None, do_save=True): RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation = math.degrees( bpy.data.objects['Camera'].rotation_euler.x) bpy.context.scene.Y_rotation = Y_rotation = math.degrees( bpy.data.objects['Camera'].rotation_euler.y) bpy.context.scene.Z_rotation = Z_rotation = math.degrees( bpy.data.objects['Camera'].rotation_euler.z) bpy.context.scene.X_location = X_location = bpy.data.objects[ 'Camera'].location.x bpy.context.scene.Y_location = Y_location = bpy.data.objects[ 'Camera'].location.y bpy.context.scene.Z_location = Z_location = bpy.data.objects[ 'Camera'].location.z if do_save: if op.isdir(bpy.path.abspath(bpy.context.scene.output_path)): camera_fname = op.join( bpy.path.abspath(bpy.context.scene.output_path), 'camera.pkl') mu.save((X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location), camera_fname) print('Camera location was saved to {}'.format(camera_fname)) else: mu.message( self, "Can't find the folder {}".format( bpy.path.abspath(bpy.context.scene.output_path))) RenderFigure.update_camera = True
def get_object_to_filter(self, source_files, data=None, names=None): if data is None: data, names = [], [] for input_file in source_files: try: f = np.load(input_file) data.append(f['data']) names.extend([name.astype(str) for name in f['names']]) except: mu.message(self, "Can't load {}!".format(input_file)) return None, None, None # print('filtering {}-{}'.format(self.filter_from, self.filter_to)) # t_range = range(self.filter_from, self.filter_to + 1) if self.topK > 0: self.topK = min(self.topK, len(names)) # print(self.type_of_func) filter_func = get_func(self.type_of_func) if isinstance(data, list): d = np.vstack((d for d in data)) else: d = data # print('%%%%%%%%%%%%%%%%%%%' + str(len(d[0, :, 0]))) t_range = range(max(self.filter_from, 1), min(self.filter_to, len(d[0, :, 0])) - 1) objects_to_filtter_in, dd = filter_func( d, t_range, self.topK, bpy.context.scene.coloring_lower_threshold) # print(dd[objects_to_filtter_in]) return objects_to_filtter_in, names, dd
def grab_camera(self=None, do_save=True, overwrite=True): RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation = math.degrees( bpy.data.objects['Camera'].rotation_euler.x) bpy.context.scene.Y_rotation = Y_rotation = math.degrees( bpy.data.objects['Camera'].rotation_euler.y) bpy.context.scene.Z_rotation = Z_rotation = math.degrees( bpy.data.objects['Camera'].rotation_euler.z) bpy.context.scene.X_location = X_location = bpy.data.objects[ 'Camera'].location.x bpy.context.scene.Y_location = Y_location = bpy.data.objects[ 'Camera'].location.y bpy.context.scene.Z_location = Z_location = bpy.data.objects[ 'Camera'].location.z if do_save: if op.isdir(op.join(mu.get_user_fol(), 'camera')): camera_fname = op.join(mu.get_user_fol(), 'camera', 'camera.pkl') if not op.isfile(camera_fname) or overwrite: mu.save((X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location), camera_fname) # print('Camera location was saved to {}'.format(camera_fname)) print((X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location)) else: mu.message( self, "Can't find the folder {}".format(mu.get_user_fol(), 'camera')) RenderFigure.update_camera = True
def plot_connections(self, context, d, plot_time, connections_type, condition, threshold, abs_threshold=True): windows_num = d.con_values.shape[1] # xs, ys = get_fcurve_values('RPT3-RAT5') # cond_id = [i for i, cond in enumerate(d.conditions) if cond == condition][0] t = int(plot_time / ConnectionsPanel.addon.get_max_time_steps() * windows_num) if windows_num > 1 else 0 print('plotting connections for t:{}'.format(t)) if t >= d.con_colors.shape[1] and windows_num > 1: mu.message(self, 'time out of bounds! {}'.format(plot_time)) else: # for conn_name, conn_colors in colors.items(): # vals = [] selected_objects, selected_indices = get_all_selected_connections(d) for ind, con_name in zip(selected_indices, selected_objects): # print(con_name, d.con_values[ind, t, 0], d.con_values[ind, t, 1], np.diff(d.con_values[ind, t, :])) cur_obj = bpy.data.objects.get(con_name) color = d.con_colors[ ind, t, :] if d.con_colors.ndim == 3 else d.con_colors[ind, :] con_color = np.hstack((color, [0.])) bpy.context.scene.objects.active = cur_obj mu.create_material('{}_mat'.format(con_name), con_color, 1, False) # vals.append(np.diff(d.con_values[ind, t])[0]) bpy.data.objects[PARENT_OBJ].select = True ConnectionsPanel.addon.show_hide_connections()
def invoke(self, context, event=None): mri_fname = op.join(mu.get_user_fol(), 'freeview', 'orig.mgz') cursor_position = np.array(bpy.context.scene.cursor_location) * 10.0 ret = mu.conn_to_listener.send_command(dict(cmd='open_slice_viewer',data=dict( mri_fname=mri_fname, position=cursor_position))) if not ret: mu.message(self, 'Listener was stopped! Try to restart') return {"FINISHED"}
def add_data_to_brain(base_path='', files_prefix='', objs_prefix=''): if base_path == '': base_path = mu.get_user_fol() source_files = [op.join(base_path, '{}labels_data_lh.npz'.format(files_prefix)), op.join(base_path, '{}labels_data_rh.npz'.format(files_prefix)), op.join(base_path, '{}sub_cortical_activity.npz'.format(files_prefix))] print('Adding data to Brain') number_of_maximal_time_steps = -1 obj_counter = 0 conditions = [] for input_file in source_files: if not op.isfile(input_file): mu.message(None, '{} does not exist!'.format(input_file)) continue f = np.load(input_file) print('{} loaded'.format(input_file)) number_of_maximal_time_steps = max(number_of_maximal_time_steps, len(f['data'][0])) for obj_name, data in zip(f['names'], f['data']): # print('in label loop') obj_name = obj_name.astype(str) if not bpy.context.scene.import_unknown and 'unknown' in obj_name: continue obj_name = '{}{}'.format(objs_prefix, obj_name) print(obj_name) cur_obj = bpy.data.objects[obj_name] # print('cur_obj name = '+cur_obj.name) for cond_ind, cond_str in enumerate(f['conditions']): # cond_str = str(cond_str) # if cond_str[1] == "'": # cond_str = cond_str[2:-1] cond_str = cond_str.astype(str) # Set the values to zeros in the first and last frame for current object(current label) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, 1) mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, 0, len(f['data'][0]) + 2) print('keyframing ' + obj_name + ' object') # For every time point insert keyframe to current object for ind, timepoint in enumerate(data[:, cond_ind]): # print('keyframing '+obj_name+' object') mu.insert_keyframe_to_custom_prop(cur_obj, obj_name + '_' + cond_str, timepoint, ind + 2) # remove the orange keyframe sign in the fcurves window fcurves = bpy.data.objects[obj_name].animation_data.action.fcurves[cond_ind] mod = fcurves.modifiers.new(type='LIMITS') conditions.extend(f['conditions']) try: bpy.ops.graph.previewrange_set() except: pass bpy.types.Scene.maximal_time_steps = number_of_maximal_time_steps for obj in bpy.data.objects: obj.select = False if bpy.data.objects.get(' '): bpy.context.scene.objects.active = bpy.data.objects[' '] selection_panel.set_conditions_enum(conditions) print('Finished keyframing!!')
def invoke(self, context, event=None): try: ret = mu.conn_to_listener.send_command(dict(cmd='plot',data=dict(x=np.arange(10), y=np.arange(10)*3))) if not ret: mu.message(self, 'Listener was stopped! Try to restart') except: print("Can't close connection to listener") print(traceback.format_exc()) return {'PASS_THROUGH'}
def init_listener(self): ret = False tries = 0 while not ret and tries < 3: try: ret = mu.conn_to_listener.init() if ret: mu.conn_to_listener.send_command('Hey!\n') else: mu.message(self, 'Error initialize the listener. Try again') except: print("Can't open connection to listener") print(traceback.format_exc()) tries += 1 return ret
def grab_camera(self=None, do_save=True): RenderFigure.update_camera = False bpy.context.scene.X_rotation = X_rotation = math.degrees(bpy.data.objects['Camera'].rotation_euler.x) bpy.context.scene.Y_rotation = Y_rotation = math.degrees(bpy.data.objects['Camera'].rotation_euler.y) bpy.context.scene.Z_rotation = Z_rotation = math.degrees(bpy.data.objects['Camera'].rotation_euler.z) bpy.context.scene.X_location = X_location = bpy.data.objects['Camera'].location.x bpy.context.scene.Y_location = Y_location = bpy.data.objects['Camera'].location.y bpy.context.scene.Z_location = Z_location = bpy.data.objects['Camera'].location.z if do_save: if op.isdir(op.join(mu.get_user_fol(), 'camera')): camera_fname = op.join(mu.get_user_fol(), 'camera', 'camera.pkl') mu.save((X_rotation, Y_rotation, Z_rotation, X_location, Y_location, Z_location), camera_fname) print('Camera location was saved to {}'.format(camera_fname)) else: mu.message(self, "Can't find the folder {}".format(mu.get_user_fol(), 'camera')) RenderFigure.update_camera = True
def init_listener(): if mu.conn_to_listener.handle_is_open: return True ret = False tries = 0 while not ret and tries < 3: try: ret = mu.conn_to_listener.init() if ret: mu.conn_to_listener.send_command(b'Hey!\n') else: mu.message(None, 'Error initialize the listener. Try again') except: print("Can't open connection to listener") print(traceback.format_exc()) tries += 1 return ret
def get_object_to_filter(self, source_files): data, names = [], [] for input_file in source_files: try: f = np.load(input_file) data.append(f['data']) names.extend([name.astype(str) for name in f['names']]) except: mu.message(self, "Can't load {}!".format(input_file)) return None, None print('filtering {}-{}'.format(self.filter_from, self.filter_to)) t_range = range(self.filter_from, self.filter_to + 1) print(self.type_of_func) d = np.vstack((d for d in data)) print('%%%%%%%%%%%%%%%%%%%' + str(len(d[0, :, 0]))) t_range = range(max(self.filter_from, 1), min(self.filter_to, len(d[0, :, 0])) - 1) if self.type_of_func == 'RMS': dd = np.diff(d[:, t_range, :], axis=2) if dd.shape[2] > 1: dd = np.sum(dd, axis=2) dd = np.squeeze(dd) dd = np.sqrt(np.sum(np.power(dd, 2), 1)) elif self.type_of_func == 'SumAbs': dd = np.sum(abs(d[:, t_range, :]), (1, 2)) elif self.type_of_func == 'threshold': dd = np.max(np.abs(np.squeeze(np.diff(d[:, t_range, :], axis=2))), axis=1) if self.topK > 0: self.topK = min(self.topK, len(names)) else: self.topK = sum(dd > 0) if self.type_of_func == 'threshold': indices = np.where(dd > bpy.context.scene.coloring_threshold)[0] objects_to_filtter_in = sorted(indices, key=lambda i:dd[i])[::-1][:self.topK] # objects_to_filtter_in = np.argsort(dd[indices])[::-1][:self.topK] else: objects_to_filtter_in = np.argsort(dd)[::-1][:self.topK] print(dd[objects_to_filtter_in]) return objects_to_filtter_in, names, dd
def plot_connections(self, context, d, plot_time, connections_type, condition, threshold, abs_threshold=True): windows_num = d.con_values.shape[1] # xs, ys = get_fcurve_values('RPT3-RAT5') # cond_id = [i for i, cond in enumerate(d.conditions) if cond == condition][0] t = int(plot_time / ConnectionsPanel.addon.get_max_time_steps() * windows_num) if windows_num > 1 else 0 print('plotting connections for t:{}'.format(t)) if t >= d.con_colors.shape[1] and windows_num > 1: mu.message(self, 'time out of bounds! {}'.format(plot_time)) else: # for conn_name, conn_colors in colors.items(): # vals = [] selected_objects, selected_indices = get_all_selected_connections(d) for ind, con_name in zip(selected_indices, selected_objects): # print(con_name, d.con_values[ind, t, 0], d.con_values[ind, t, 1], np.diff(d.con_values[ind, t, :])) cur_obj = bpy.data.objects.get(con_name) color = d.con_colors[ind, t, :] if d.con_colors.ndim == 3 else d.con_colors[ind, :] con_color = np.hstack((color, [0.])) bpy.context.scene.objects.active = cur_obj mu.create_material('{}_mat'.format(con_name), con_color, 1, False) # vals.append(np.diff(d.con_values[ind, t])[0]) bpy.data.objects[PARENT_OBJ].select = True ConnectionsPanel.addon.show_connections()