def build_multiprofile(self, document_controller): document_model = document_controller.document_model model_data_item = self.__model_data_item if not model_data_item: return project = document_model._project multiprofile_display_item = None line_profile_regions = list() colors = ("rgba(0, 0, 255, 0.5)", "rgba(255, 0, 0, 0.5)", "rgba(0, 255, 0, 0.5)") for index, dependent_data_item in enumerate( document_model.get_dependent_data_items(model_data_item)): if self.__is_calibrated_map(dependent_data_item): dependent_display_item = document_model.get_display_item_for_data_item( dependent_data_item) if not multiprofile_display_item: multiprofile_display_item = DisplayItem.DisplayItem() multiprofile_display_item.title = _("Multi-profile") multiprofile_display_item.display_type = "line_plot" multiprofile_display_item.set_display_property( "legend_position", "top-right") document_model.append_display_item( multiprofile_display_item) line_profile_data_item = document_model.get_line_profile_new( dependent_display_item, dependent_display_item.data_item) line_profile_display_item = document_model.get_display_item_for_data_item( line_profile_data_item) line_profile_display_data_channel = line_profile_display_item.get_display_data_channel_for_data_item( line_profile_data_item) line_profile_region = dependent_display_item.graphics[0] line_profile_region.vector = (0.5, 0.2), (0.5, 0.8) line_profile_regions.append(line_profile_region) multiprofile_display_item.append_display_data_channel_for_data_item( line_profile_data_item) layer_label = dependent_data_item.title[dependent_data_item. title.index(" of ") + 4:] multiprofile_display_item._set_display_layer_properties( index, label=layer_label, fill_color=colors[index % len(colors)]) if multiprofile_display_item: for line_profile_region in line_profile_regions[1:]: document_model.append_connection( Connection.PropertyConnection( line_profile_regions[0], "vector", line_profile_region, "vector", parent=multiprofile_display_item)) document_model.append_connection( Connection.PropertyConnection( line_profile_regions[0], "width", line_profile_region, "width", parent=multiprofile_display_item)) document_controller.show_display_item(multiprofile_display_item)
def test_connection_establishes_transaction_on_parallel_source_connection( self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item_src = DataItem.DataItem(numpy.zeros((1000, ))) data_item_dst1 = DataItem.DataItem(numpy.zeros((1000, ))) data_item_dst2 = DataItem.DataItem(numpy.zeros((1000, ))) document_model.append_data_item(data_item_src) document_model.append_data_item(data_item_dst1) document_model.append_data_item(data_item_dst2) display_item_src = document_model.get_display_item_for_data_item( data_item_src) display_item_dst1 = document_model.get_display_item_for_data_item( data_item_dst1) display_item_dst2 = document_model.get_display_item_for_data_item( data_item_dst2) interval_src = Graphics.IntervalGraphic() interval_dst1 = Graphics.IntervalGraphic() interval_dst2 = Graphics.IntervalGraphic() display_item_src.add_graphic(interval_src) display_item_dst1.add_graphic(interval_dst1) display_item_dst2.add_graphic(interval_dst2) connection1 = Connection.PropertyConnection(interval_src, "interval", interval_dst1, "interval", parent=data_item_dst1) connection2 = Connection.PropertyConnection(interval_src, "interval", interval_dst2, "interval", parent=data_item_dst2) document_model.append_connection(connection1) document_model.append_connection(connection2) # check dependencies with document_model.item_transaction(data_item_dst1): self.assertTrue( document_model.is_in_transaction_state(data_item_dst1)) self.assertTrue( document_model.is_in_transaction_state(interval_dst1)) self.assertTrue( document_model.is_in_transaction_state(interval_src)) self.assertTrue( document_model.is_in_transaction_state( display_item_dst2)) # from graphic self.assertTrue( document_model.is_in_transaction_state(interval_dst2)) self.assertTrue( document_model.is_in_transaction_state( display_item_src)) # from graphic self.assertEqual(0, document_model.transaction_count)
def test_connection_establishes_transaction_on_target_data_structure(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item1 = DataItem.DataItem(numpy.zeros((2, 2))) data_item2 = DataItem.DataItem(numpy.zeros((2, 2))) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) interval1 = Graphics.IntervalGraphic() interval2 = Graphics.IntervalGraphic() display_item1.add_graphic(interval1) display_item2.add_graphic(interval2) data_struct1 = document_model.create_data_structure() data_struct2 = document_model.create_data_structure() data_struct1.set_property_value("x_interval", (0.5, 0.1)) data_struct2.set_property_value("x_interval", (0.5, 0.2)) document_model.append_data_structure(data_struct1) document_model.append_data_structure(data_struct2) connection1 = Connection.PropertyConnection(data_struct1, "x_interval", interval1, "interval", parent=data_item1) connection2 = Connection.PropertyConnection(interval2, "interval", data_struct2, "x_interval", parent=data_item2) document_model.append_connection(connection1) document_model.append_connection(connection2) with document_model.item_transaction(data_item1): self.assertTrue( document_model.is_in_transaction_state(data_item1)) self.assertTrue( document_model.is_in_transaction_state(display_item1)) self.assertTrue( document_model.is_in_transaction_state(data_struct1)) with document_model.item_transaction(data_item2): self.assertTrue( document_model.is_in_transaction_state(data_item2)) self.assertTrue( document_model.is_in_transaction_state(display_item2)) self.assertTrue( document_model.is_in_transaction_state(data_struct2)) self.assertEqual(0, document_model.transaction_count)
def test_removing_graphic_as_connection_source_results_in_consistent_state( self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_item1 = DataItem.DataItem(numpy.zeros((20, ))) data_item2 = DataItem.DataItem(numpy.zeros((20, ))) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item1.add_graphic(Graphics.IntervalGraphic()) display_item2.add_graphic(Graphics.IntervalGraphic()) graphic1 = display_item1.graphics[0] graphic2 = display_item2.graphics[0] document_model.append_connection( Connection.PropertyConnection(graphic1, "interval", graphic2, "interval", parent=data_item1)) display_item1.remove_graphic(graphic1) # the document must have a consistent state for item transaction to work with document_model.item_transaction(data_item1): pass with document_model.item_transaction(data_item2): pass
def test_connection_updates_source_when_target_changes(self): # setup document model with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item_3d = DataItem.DataItem( numpy.zeros((8, 8, 32), numpy.uint32)) data_item_1d = DataItem.DataItem(numpy.zeros((32, ), numpy.uint32)) document_model.append_data_item(data_item_3d) document_model.append_data_item(data_item_1d) display_item_1d = document_model.get_display_item_for_data_item( data_item_1d) display_item_3d = document_model.get_display_item_for_data_item( data_item_1d) interval = Graphics.IntervalGraphic() display_item_1d.add_graphic(interval) display_data_channel_3d = display_item_3d.display_data_channels[0] connection = Connection.PropertyConnection(display_data_channel_3d, "slice_center", interval, "start", parent=data_item_1d) document_model.append_connection(connection) # test to see if connection updates target when source changes interval.start = 9 self.assertEqual(display_data_channel_3d.slice_center, 9)
def test_connection_to_graphic_puts_data_item_under_transaction(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item = DataItem.DataItem(numpy.zeros((20, ))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) interval = Graphics.IntervalGraphic() display_item.add_graphic(interval) data_struct = document_model.create_data_structure() data_struct.set_property_value("x_interval", (0.5, 0.1)) document_model.append_data_structure(data_struct) connection = Connection.PropertyConnection(data_struct, "x_interval", interval, "interval", parent=data_item) document_model.append_connection(connection) with document_model.item_transaction(data_struct): self.assertTrue( document_model.is_in_transaction_state(data_struct)) self.assertTrue( document_model.is_in_transaction_state(interval)) self.assertTrue( document_model.is_in_transaction_state(display_item))
def test_removing_graphic_as_connection_target_results_in_consistent_state( self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item1 = DataItem.DataItem(numpy.zeros((20, ))) data_item2 = DataItem.DataItem(numpy.zeros((20, ))) document_model.append_data_item(data_item1) document_model.append_data_item(data_item2) display_item1 = document_model.get_display_item_for_data_item( data_item1) display_item2 = document_model.get_display_item_for_data_item( data_item2) display_item1.add_graphic(Graphics.IntervalGraphic()) display_item2.add_graphic(Graphics.IntervalGraphic()) graphic1 = display_item1.graphics[0] graphic2 = display_item2.graphics[0] document_model.append_connection( Connection.PropertyConnection(graphic1, "interval", graphic2, "interval", parent=data_item1)) display_item1.remove_graphic(graphic2).close() # the document must have a consistent state for item transaction to work with document_model.item_transaction(data_item1): pass with document_model.item_transaction(data_item2): pass
def test_connection_updates_target_when_source_changes(self): # setup document model document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_item_3d = DataItem.DataItem( numpy.zeros((8, 8, 32), numpy.uint32)) data_item_1d = DataItem.DataItem(numpy.zeros((32, ), numpy.uint32)) document_model.append_data_item(data_item_3d) document_model.append_data_item(data_item_1d) display_item_1d = document_model.get_display_item_for_data_item( data_item_1d) display_item_3d = document_model.get_display_item_for_data_item( data_item_1d) interval = Graphics.IntervalGraphic() display_item_1d.add_graphic(interval) connection = Connection.PropertyConnection( display_item_3d.display_data_channels[0], "slice_center", interval, "start", parent=data_item_1d) document_model.append_connection(connection) # test to see if connection updates target when source changes display_item_3d.display_data_channels[0].slice_center = 12 self.assertEqual(interval.start, 12)
def test_connection_establishes_transaction_on_source(self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_item_src = DataItem.DataItem(numpy.zeros((1000, ))) data_item_dst = DataItem.DataItem(numpy.zeros((1000, ))) document_model.append_data_item(data_item_src) document_model.append_data_item(data_item_dst) display_item_src = document_model.get_display_item_for_data_item( data_item_src) display_item_dst = document_model.get_display_item_for_data_item( data_item_dst) interval_src = Graphics.IntervalGraphic() interval_dst = Graphics.IntervalGraphic() display_item_src.add_graphic(interval_src) display_item_dst.add_graphic(interval_dst) connection = Connection.PropertyConnection(interval_src, "interval", interval_dst, "interval", parent=data_item_dst) document_model.append_connection(connection) # check dependencies with document_model.item_transaction(data_item_dst): self.assertTrue( document_model.is_in_transaction_state(data_item_dst)) self.assertTrue( document_model.is_in_transaction_state(display_item_dst)) self.assertTrue( document_model.is_in_transaction_state(interval_dst)) self.assertTrue( document_model.is_in_transaction_state(interval_src)) self.assertTrue( document_model.is_in_transaction_state(display_item_src)) self.assertEqual(0, document_model.transaction_count)
def read_project(self) -> None: properties = self._raw_properties self.__project_version = properties.get("version", None) if not self._raw_properties: self.__project_state = "missing" elif self.__project_version is not None and self.__project_version in ( FileStorageSystem.PROJECT_VERSION, 2): for item_d in properties.get("data_items", list()): data_item = DataItem.DataItem() data_item.begin_reading() data_item.read_from_dict(item_d) data_item.finish_reading() if not self.get_item_by_uuid("data_items", data_item.uuid): self.load_item("data_items", len(self.data_items), data_item) for item_d in properties.get("display_items", list()): display_item = DisplayItem.DisplayItem() display_item.begin_reading() display_item.read_from_dict(item_d) display_item.finish_reading() if not self.get_item_by_uuid("display_items", display_item.uuid): self.load_item("display_items", len(self.display_items), display_item) for item_d in properties.get("data_structures", list()): data_structure = DataStructure.DataStructure() data_structure.begin_reading() data_structure.read_from_dict(item_d) data_structure.finish_reading() if not self.get_item_by_uuid("data_structures", data_structure.uuid): self.load_item("data_structures", len(self.data_structures), data_structure) for item_d in properties.get("computations", list()): computation = Symbolic.Computation() computation.begin_reading() computation.read_from_dict(item_d) computation.finish_reading() if not self.get_item_by_uuid("computations", computation.uuid): self.load_item("computations", len(self.computations), computation) # TODO: handle update script and bind after reload in document model computation.update_script( self.container.container.container. _processing_descriptions) for item_d in properties.get("connections", list()): connection = Connection.connection_factory(item_d.get) connection.begin_reading() connection.read_from_dict(item_d) connection.finish_reading() if not self.get_item_by_uuid("connections", connection.uuid): self.load_item("connections", len(self.connections), connection) self.__project_state = "loaded" elif self.__project_version is not None: self.__project_state = "needs_upgrade" else: self.__project_state = "missing"
def test_connections_between_items_with_duplicated_uuids_are_connected_per_project( self): with create_memory_profile_context() as profile_context: profile = profile_context.create_profile() profile.add_project_memory() document_model = DocumentModel.DocumentModel(profile=profile) with contextlib.closing(document_model): data_item1 = DataItem.DataItem(numpy.ones((4, 4))) document_model.append_data_item(data_item1, project=profile.projects[0]) display_item1 = document_model.get_display_item_for_data_item( data_item1) data_item2 = DataItem.DataItem(numpy.ones((4, 4))) document_model.append_data_item(data_item2, project=profile.projects[0]) display_item2 = document_model.get_display_item_for_data_item( data_item2) connection = Connection.PropertyConnection(display_item1, "title", display_item2, "title", parent=data_item1) document_model.append_connection(connection) project_keys = list(profile_context.x_data_properties_map.keys()) profile_context.x_data_properties_map[ project_keys[1]] = copy.deepcopy( profile_context.x_data_properties_map[project_keys[0]]) profile_context.x_data_map[project_keys[1]] = copy.deepcopy( profile_context.x_data_map[project_keys[0]]) profile_context.x_project_properties[ project_keys[1]] = copy.deepcopy( profile_context.x_project_properties[project_keys[0]]) profile_context.x_project_properties[ project_keys[1]]["uuid"] = str(project_keys[1]) document_model = DocumentModel.DocumentModel( profile=profile_context.create_profile()) with contextlib.closing(document_model): project0 = document_model.profile.projects[0] project1 = document_model.profile.projects[1] self.assertEqual(4, len(document_model.data_items)) self.assertEqual(2, len(document_model.connections)) self.assertEqual(1, len(project0.connections)) self.assertEqual(1, len(project1.connections)) self.assertEqual(4, len(document_model.display_items)) self.assertEqual(2, len(project0.display_items)) self.assertEqual(2, len(project1.display_items)) for item in project0.connections[0].connected_items: self.assertEqual(project0, Project.get_project_for_item(item)) for item in project1.connections[0].connected_items: self.assertEqual(project1, Project.get_project_for_item(item))
def test_connection_saves_and_restores(self): # setup document with create_memory_profile_context() as profile_context: document_model = profile_context.create_document_model( auto_close=False) with document_model.ref(): data_item_3d = DataItem.DataItem( numpy.zeros((8, 8, 32), numpy.uint32)) data_item_1d = DataItem.DataItem( numpy.zeros((32, ), numpy.uint32)) document_model.append_data_item(data_item_3d) document_model.append_data_item(data_item_1d) display_item_1d = document_model.get_display_item_for_data_item( data_item_1d) display_item_3d = document_model.get_display_item_for_data_item( data_item_1d) interval = Graphics.IntervalGraphic() display_item_1d.add_graphic(interval) connection = Connection.PropertyConnection( display_item_3d.display_data_channels[0], "slice_center", interval, "start", parent=data_item_1d) document_model.append_connection(connection) # read it back document_model = profile_context.create_document_model( auto_close=False) with document_model.ref(): # verify it read back data_item_3d = document_model.data_items[0] data_item_1d = document_model.data_items[1] display_item_1d = document_model.get_display_item_for_data_item( data_item_1d) display_item_3d = document_model.get_display_item_for_data_item( data_item_1d) interval = display_item_1d.graphics[0] self.assertEqual(1, len(document_model.connections)) # verify connection is working in both directions display_data_channel_3d = display_item_3d.display_data_channels[ 0] display_data_channel_3d.slice_center = 11 self.assertEqual(interval.start, 11) interval.start = 7 self.assertEqual(display_data_channel_3d.slice_center, 7)
def test_connection_establishes_transaction_on_target_data_structure_dependent( self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_item = DataItem.DataItem(numpy.zeros((20, ))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) interval = Graphics.IntervalGraphic() display_item.add_graphic(interval) data_struct = document_model.create_data_structure() data_struct.set_property_value("x_interval", (0.5, 0.1)) document_model.append_data_structure(data_struct) connection = Connection.PropertyConnection(data_struct, "x_interval", interval, "interval", parent=data_item) document_model.append_connection(connection) computed_data_item = DataItem.DataItem(numpy.zeros((100, ))) document_model.append_data_item(computed_data_item) computation = document_model.create_computation( Symbolic.xdata_expression("a.xdata")) computation.create_object( "a", document_model.get_object_specifier(data_item)) computation.create_object( "d", document_model.get_object_specifier(data_struct)) document_model.set_data_item_computation(computed_data_item, computation) with document_model.item_transaction(data_item): self.assertTrue( document_model.is_in_transaction_state(data_item)) self.assertTrue( document_model.is_in_transaction_state(display_item)) self.assertTrue( document_model.is_in_transaction_state(data_struct)) self.assertTrue( document_model.is_in_transaction_state(computed_data_item)) self.assertEqual(0, document_model.transaction_count)
def test_connection_between_data_structures(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item = DataItem.DataItem(numpy.zeros((2, 2))) document_model.append_data_item(data_item) data_struct1 = document_model.create_data_structure() data_struct2 = document_model.create_data_structure() data_struct1.set_property_value("title", "t1") data_struct2.set_property_value("title", "t2") document_model.append_data_structure(data_struct1) document_model.append_data_structure(data_struct2) connection = Connection.PropertyConnection(data_struct1, "title", data_struct2, "title", parent=data_item) document_model.append_connection(connection) data_struct1.set_property_value("title", "T1") self.assertEqual("T1", data_struct1.get_property_value("title")) self.assertEqual("T1", data_struct2.get_property_value("title")) data_struct2.set_property_value("title", "T2") self.assertEqual("T2", data_struct1.get_property_value("title")) self.assertEqual("T2", data_struct2.get_property_value("title"))
def test_connection_closed_when_data_item_removed_from_model(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item_3d = DataItem.DataItem( numpy.zeros((32, 8, 8), numpy.uint32)) data_item_1d = DataItem.DataItem(numpy.zeros((32, ), numpy.uint32)) document_model.append_data_item(data_item_3d) document_model.append_data_item(data_item_1d) display_item_1d = document_model.get_display_item_for_data_item( data_item_1d) display_item_3d = document_model.get_display_item_for_data_item( data_item_1d) interval = Graphics.IntervalGraphic() display_item_1d.add_graphic(interval) connection = Connection.PropertyConnection( display_item_3d.display_data_channels[0], "slice_center", interval, "start", parent=data_item_1d) document_model.append_connection(connection) self.assertFalse(connection._closed) document_model.remove_data_item(data_item_1d) self.assertTrue(connection._closed)
def read_project(self) -> None: if callable(self.handle_start_read): self.handle_start_read() properties = self._raw_properties if properties: project_version = properties.get("version", None) if project_version is not None and project_version == FileStorageSystem.PROJECT_VERSION: for item_d in properties.get("data_items", list()): data_item = DataItem.DataItem() data_item.begin_reading() data_item.read_from_dict(item_d) data_item.finish_reading() if not self.get_item_by_uuid("data_items", data_item.uuid): self.load_item("data_items", len(self.data_items), data_item) else: data_item.close() for item_d in properties.get("display_items", list()): display_item = DisplayItem.DisplayItem() display_item.begin_reading() display_item.read_from_dict(item_d) display_item.finish_reading() if not self.get_item_by_uuid("display_items", display_item.uuid): self.load_item("display_items", len(self.display_items), display_item) else: display_item.close() for item_d in properties.get("data_structures", list()): data_structure = DataStructure.DataStructure() data_structure.begin_reading() data_structure.read_from_dict(item_d) data_structure.finish_reading() if not self.get_item_by_uuid("data_structures", data_structure.uuid): self.load_item("data_structures", len(self.data_structures), data_structure) else: data_structure.close() for item_d in properties.get("computations", list()): computation = Symbolic.Computation() computation.begin_reading() computation.read_from_dict(item_d) computation.finish_reading() if not self.get_item_by_uuid("computations", computation.uuid): self.load_item("computations", len(self.computations), computation) # TODO: handle update script and bind after reload in document model computation.update_script( Project._processing_descriptions) computation.reset() else: computation.close() for item_d in properties.get("connections", list()): connection = Connection.connection_factory(item_d.get) connection.begin_reading() connection.read_from_dict(item_d) connection.finish_reading() if not self.get_item_by_uuid("connections", connection.uuid): self.load_item("connections", len(self.connections), connection) else: connection.close() for item_d in properties.get("data_groups", list()): data_group = DataGroup.data_group_factory(item_d.get) data_group.begin_reading() data_group.read_from_dict(item_d) data_group.finish_reading() if not self.get_item_by_uuid("data_groups", data_group.uuid): self.load_item("data_groups", len(self.data_groups), data_group) else: data_group.close() for item_d in properties.get("workspaces", list()): workspace = WorkspaceLayout.factory(item_d.get) workspace.begin_reading() workspace.read_from_dict(item_d) workspace.finish_reading() if not self.get_item_by_uuid("workspaces", workspace.uuid): self.load_item("workspaces", len(self.workspaces), workspace) else: workspace.close() workspace_uuid_str = properties.get("workspace_uuid", None) if workspace_uuid_str: self._set_persistent_property_value( "workspace_uuid", uuid.UUID(workspace_uuid_str)) self._set_persistent_property_value( "data_item_references", properties.get("data_item_references", dict())) self._set_persistent_property_value( "mapped_items", properties.get("mapped_items", list())) self.__has_been_read = True if callable(self.handle_finish_read): self.handle_finish_read()
def test_connection_added_to_same_project_as_inputs(self): with create_memory_profile_context() as profile_context: profile = profile_context.create_profile() profile.add_project_memory() document_model = DocumentModel.DocumentModel(profile=profile) with contextlib.closing(document_model): # make 1st connection between two data items data_item1 = DataItem.DataItem(numpy.ones((4, 4))) document_model.append_data_item(data_item1, project=profile.projects[0]) display_item1 = document_model.get_display_item_for_data_item( data_item1) data_item2 = DataItem.DataItem(numpy.ones((4, 4))) document_model.append_data_item(data_item2, project=profile.projects[0]) display_item2 = document_model.get_display_item_for_data_item( data_item2) connection = Connection.PropertyConnection(display_item1, "title", display_item2, "title", parent=data_item1) document_model.append_connection(connection) # make 2nd connection between two data items data_item1 = DataItem.DataItem(numpy.ones((4, 4))) document_model.append_data_item(data_item1, project=profile.projects[1]) display_item1 = document_model.get_display_item_for_data_item( data_item1) data_item2 = DataItem.DataItem(numpy.ones((4, 4))) document_model.append_data_item(data_item2, project=profile.projects[1]) display_item2 = document_model.get_display_item_for_data_item( data_item2) connection = Connection.PropertyConnection(display_item1, "title", display_item2, "title", parent=data_item1) document_model.append_connection(connection) # check assumptions self.assertEqual(4, len(document_model.data_items)) self.assertEqual(2, len(document_model.connections)) self.assertEqual( 1, len(document_model.profile.projects[0].connections)) self.assertEqual( 1, len(document_model.profile.projects[1].connections)) self.assertEqual(4, len(document_model.display_items)) self.assertEqual( 2, len(document_model.profile.projects[0].display_items)) self.assertEqual( 2, len(document_model.profile.projects[1].display_items)) document_model = DocumentModel.DocumentModel( profile=profile_context.create_profile()) with contextlib.closing(document_model): # check after reload self.assertEqual(4, len(document_model.data_items)) self.assertEqual(2, len(document_model.connections)) self.assertEqual( 1, len(document_model.profile.projects[0].connections)) self.assertEqual( 1, len(document_model.profile.projects[1].connections)) self.assertEqual(4, len(document_model.display_items)) self.assertEqual( 2, len(document_model.profile.projects[0].display_items)) self.assertEqual( 2, len(document_model.profile.projects[1].display_items))
async def pick_new_edge(document_controller, model_data_item, edge) -> None: """Set up a new edge pick from the model data item and the given edge. The library will have the following new components and connections: - a pick region on the model data item - a pick data item with a fit/signal connected to the edge data structure - a background subtraction computation with model data item, and edge intervals as inputs - a background data item, computed by the background subtraction computation - a subtracted data item, computed by the background subtraction computation - a eels line plot with pick, background, and subtracted data items as components - an edge reference, owned by eels line plot, with reference to edge - the edge reference is used to recognize the eels line plot as associated with the referenced edge """ document_model = document_controller.document_model model_display_item = document_model.get_display_item_for_data_item(model_data_item) pick_region = Graphics.RectangleGraphic() pick_region.size = min(1 / 16, 16 / model_data_item.dimensional_shape[0]), min(1 / 16, 16 / model_data_item.dimensional_shape[1]) pick_region.label = "{} {}".format(_("Pick"), str(edge.electron_shell)) model_display_item.add_graphic(pick_region) # set up the computation for this edge. eels_data_item = DataItem.DataItem() document_model.append_data_item(eels_data_item) eels_data_item.title = "{} EELS Data of {}".format(pick_region.label, model_data_item.title) eels_data_item.source = pick_region eels_display_item = document_model.get_display_item_for_data_item(eels_data_item) eels_display_item.display_type = "line_plot" eels_display_item.display_layers = [ {"label": "Signal", "data_index": 0, "data_row": 2, "fill_color": "#0F0"}, {"label": "Background", "data_index": 0, "data_row": 1, "fill_color": "rgba(255, 0, 0, 0.3)"}, {"label": "Data", "data_index": 0, "data_row": 0, "fill_color": "#1E90FF"}, ] eels_display_item.set_display_property("legend_position", "top-right") fit_region = Graphics.IntervalGraphic() fit_region.label = _("Fit") fit_region.graphic_id = "fit" fit_region.interval = edge.fit_interval eels_display_item.add_graphic(fit_region) signal_region = Graphics.IntervalGraphic() signal_region.label = _("Signal") signal_region.graphic_id = "signal" signal_region.interval = edge.signal_interval eels_display_item.add_graphic(signal_region) document_model.append_connection(Connection.PropertyConnection(edge.data_structure, "fit_interval", fit_region, "interval", parent=eels_data_item)) document_model.append_connection(Connection.PropertyConnection(edge.data_structure, "signal_interval", signal_region, "interval", parent=eels_data_item)) computation = document_model.create_computation() computation.create_object("eels_xdata", document_model.get_object_specifier(model_data_item, "xdata")) computation.create_object("region", document_model.get_object_specifier(pick_region)) computation.create_input("fit_interval", document_model.get_object_specifier(edge.data_structure), "fit_interval") computation.create_input("signal_interval", document_model.get_object_specifier(edge.data_structure), "signal_interval") computation.processing_id = "eels.background_subtraction11" computation.create_result("data", document_model.get_object_specifier(eels_data_item)) document_model.append_computation(computation) # the eels item will need the initial computation results to display properly (view to intervals) await document_model.compute_immediate(document_controller.event_loop, computation) # ensure computation is deleted when eels is deleted computation.source = eels_data_item # create an elemental_mapping_edge_ref data structure, owned by the eels data item, with a referenced # object pointing to the edge. used for recognizing the eels data item as such. data_structure = document_model.create_data_structure(structure_type="elemental_mapping_edge_ref", source=eels_data_item) data_structure.set_referenced_object("spectrum_image", model_data_item) data_structure.set_referenced_object("edge", edge.data_structure) data_structure.set_referenced_object("data", eels_data_item) data_structure.set_referenced_object("pick_region", pick_region) document_model.append_data_structure(data_structure) # display it eels_display_item.view_to_intervals(eels_data_item.xdata, [edge.data_structure.fit_interval, edge.data_structure.signal_interval]) document_controller.show_display_item(eels_display_item)
async def change_edge(document_controller: DocumentController.DocumentController, model_data_item: DataItem.DataItem, eels_data_item: DataItem.DataItem, edge: "ElementalMappingEdge") -> None: """Change the eels data item and associated items to display new edge. The library will be changed in the following way: - the pick region will be renamed - the pick data item will connect fit/signal regions to new edge data structure - the background subtraction computation will use edge intervals from new edge - the pick, background, subtracted, and eels line plot data items will be renamed - the eels line plot will connect fit/signal regions to new edge data structure - the edge reference will reference the new edge """ document_model = document_controller.document_model computation = None # type: Symbolic.Computation for computation_ in document_model.computations: if computation_.source == eels_data_item and computation_.processing_id == "eels.background_subtraction11": computation = computation_ break edge_ref_data_structure = None old_edge_data_structure = None for data_structure in document_model.data_structures: if data_structure.source == eels_data_item and data_structure.structure_type == "elemental_mapping_edge_ref": edge_ref_data_structure = data_structure old_edge_data_structure = data_structure.get_referenced_object("edge") break if not computation or not edge_ref_data_structure or not old_edge_data_structure: return pick_region = edge_ref_data_structure.get_referenced_object("pick_region") if not eels_data_item or not pick_region: return pick_region.label = "{} {}".format(_("Pick"), str(edge.electron_shell)) for connection in copy.copy(document_model.connections): if connection.parent == eels_data_item and connection.source_property in ("fit_interval", "signal_interval"): source_property = connection.source_property target_property = connection.target_property target = connection._target document_model.remove_connection(connection) new_connection = Connection.PropertyConnection(edge.data_structure, source_property, target, target_property, parent=eels_data_item) document_model.append_connection(new_connection) for computation_variable in computation.variables: if computation_variable.name in ("fit_interval", "signal_interval"): computation_variable.specifier = document_model.get_object_specifier(edge.data_structure) eels_data_item.title = "{} EELS Data of {}".format(pick_region.label, model_data_item.title) for connection in copy.copy(document_model.connections): if connection.parent == eels_data_item and connection.source_property in ("fit_interval", "signal_interval"): source_property = connection.source_property target_property = connection.target_property target = connection._target document_model.remove_connection(connection) new_connection = Connection.PropertyConnection(edge.data_structure, source_property, target, target_property, parent=eels_data_item) document_model.append_connection(new_connection) edge_ref_data_structure.remove_referenced_object("edge") edge_ref_data_structure.set_referenced_object("edge", edge.data_structure) # the eels item will need the initial computation results to display properly (view to intervals) await document_model.compute_immediate(document_controller.event_loop, computation) eels_display_item = document_model.get_display_item_for_data_item(eels_data_item) eels_display_item.view_to_intervals(eels_data_item.xdata, [edge.fit_interval, edge.signal_interval])