def test_picking_edge_produces_properly_configured_composite(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) model_display_item = document_model.get_display_item_for_data_item( model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].pick_action() self.__run_until_complete(document_controller) self.assertEqual(2, len(document_model.data_items)) eels_data_item = document_model.data_items[1] self.assertEqual(model_display_item.graphics[0], eels_data_item.source) self.assertEqual(eels_data_item, document_model.data_structures[1].source) self.assertEqual("elemental_mapping_edge_ref", document_model.data_structures[1].structure_type) self.assertEqual( document_model.data_structures[0], document_model.data_structures[1].get_referenced_object( "edge"))
def test_multiprofile_of_two_maps_builds_two_line_profiles(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].map_action() self.__run_until_complete(document_controller) edge_bundle[1].map_action() self.__run_until_complete(document_controller) self.assertEqual(3, len(document_model.data_items)) elemental_mapping_controller.build_multiprofile( document_controller) self.assertEqual(5, len(document_model.data_items)) self.assertEqual(6, len(document_model.display_items)) composite_display_item = document_model.display_items[3] line_profile1_data_item = document_model.data_items[3] line_profile2_data_item = document_model.data_items[4] self.assertIn(line_profile1_data_item, composite_display_item.data_items) self.assertIn(line_profile2_data_item, composite_display_item.data_items)
def test_deleting_pick_region_also_deletes_pick_composition(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) model_display_item = document_model.get_display_item_for_data_item( model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].pick_action() self.__run_until_complete(document_controller) pick_region = model_display_item.graphics[0] eels_data_item = document_model.data_items[1] self.assertEqual(1, len(document_model.computations)) self.assertEqual(2, len(document_model.data_items)) self.assertEqual(2, len(document_model.data_structures)) model_display_item.remove_graphic(pick_region) self.assertEqual(0, len(document_model.computations)) self.assertEqual(1, len(document_model.data_items)) self.assertEqual(1, len(document_model.data_structures))
def test_deleting_pick_region_also_deletes_pick_composition(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) model_display_item = document_model.get_display_item_for_data_item( model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].pick_action() self.__run_until_complete(document_controller) pick_region = model_display_item.graphics[0] eels_data_item = document_model.data_items[1] self.assertEqual(1, len(document_model.computations)) self.assertEqual(2, len(document_model.data_items)) self.assertEqual(2, len(document_model.data_structures)) model_display_item.remove_graphic(pick_region) self.assertEqual(0, len(document_model.computations)) self.assertEqual(1, len(document_model.data_items)) self.assertEqual(1, len(document_model.data_structures))
def test_multiprofile_of_two_maps_builds_two_line_profiles(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].map_action() self.__run_until_complete(document_controller) edge_bundle[1].map_action() self.__run_until_complete(document_controller) self.assertEqual(3, len(document_model.data_items)) elemental_mapping_controller.build_multiprofile( document_controller) self.assertEqual(5, len(document_model.data_items)) self.assertEqual(6, len(document_model.display_items)) composite_display_item = document_model.display_items[3] line_profile1_data_item = document_model.data_items[3] line_profile2_data_item = document_model.data_items[4] self.assertIn(line_profile1_data_item, composite_display_item.data_items) self.assertIn(line_profile2_data_item, composite_display_item.data_items)
def test_multiprofile_of_two_maps_connects_line_profiles(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].map_action() self.__run_until_complete(document_controller) edge_bundle[1].map_action() self.__run_until_complete(document_controller) elemental_mapping_controller.build_multiprofile( document_controller) map1_display_item = document_model.get_display_item_for_data_item( document_model.data_items[1]) map2_display_item = document_model.get_display_item_for_data_item( document_model.data_items[2]) # composite_data_item = document_model.data_items[3] # line_profile1_data_item = document_model.data_items[4] # line_profile2_data_item = document_model.data_items[5] line_region1 = map1_display_item.graphics[0] line_region2 = map2_display_item.graphics[0] self.assertEqual(line_region1.vector, line_region2.vector) self.assertEqual(line_region1.width, line_region2.width) line_region1.vector = (0.11, 0.12), (0.21, 0.22) self.assertEqual(line_region1.vector, line_region2.vector) self.assertEqual(line_region1.width, line_region2.width)
def test_mapping_edge_produces_properly_configured_map(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].map_action() self.__run_until_complete(document_controller) self.assertEqual(2, len(document_model.data_items)) mapped_data_item = document_model.data_items[1] self.assertEqual(model_data_item, mapped_data_item.source) self.assertEqual(1, len(document_model.computations)) self.assertEqual("eels.mapping", document_model.computations[0].processing_id) self.assertEqual(mapped_data_item.dimensional_calibrations, model_data_item.dimensional_calibrations[0:2])
def __create_edge(self, model_data_item: DataItem.DataItem, electron_shell: PeriodicTable.ElectronShell): binding_energy_eV = PeriodicTable.PeriodicTable( ).nominal_binding_energy_ev(electron_shell) signal_interval_eV = binding_energy_eV, binding_energy_eV * 1.10 fit_interval_eV = binding_energy_eV * 0.93, binding_energy_eV * 0.98 dimensional_shape = model_data_item.dimensional_shape dimensional_calibrations = model_data_item.dimensional_calibrations if dimensional_shape is not None and dimensional_calibrations is not None and len( dimensional_calibrations) > 0: calibration = dimensional_calibrations[-1] if calibration.units == "eV": fit_region_start = calibration.convert_from_calibrated_value( fit_interval_eV[0]) / dimensional_shape[-1] fit_region_end = calibration.convert_from_calibrated_value( fit_interval_eV[1]) / dimensional_shape[-1] signal_region_start = calibration.convert_from_calibrated_value( signal_interval_eV[0]) / dimensional_shape[-1] signal_region_end = calibration.convert_from_calibrated_value( signal_interval_eV[1]) / dimensional_shape[-1] fit_interval = fit_region_start, fit_region_end signal_interval = signal_region_start, signal_region_end return ElementalMappingController.ElementalMappingEdge( electron_shell=electron_shell, fit_interval=fit_interval, signal_interval=signal_interval) return None
def test_adding_multiple_edges(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K self.assertEqual(2, len(document_model.data_structures))
def test_explore_adds_edge(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) document_controller.event_loop.create_task( elemental_mapping_controller.explore_edges( document_controller)) document_controller.periodic() # start tasks document_model.recompute_all() document_controller.periodic() # finish tasks explorer_data_item = document_model.data_items[1] explorer_display_item = document_model.get_display_item_for_data_item( explorer_data_item) elemental_mapping_controller.set_current_data_item( explorer_data_item) self.assertIsNotNone(elemental_mapping_controller.model_data_item) energy_calibration = explorer_data_item.dimensional_calibrations[ -1] explorer_display_item.graphics[ -1].interval = energy_calibration.convert_from_calibrated_value( 1200 ) / 1024, energy_calibration.convert_from_calibrated_value( 1226) / 1024 for _ in range(3): # there is something funny about how async works; recent versions of Swift are faster # and have revealed some race condition about how items get added to the async queue. # to avoid that problem, do periodic over a period of a few ms. document_controller.periodic() # update explorer interval time.sleep(0.01) edges = PeriodicTable.PeriodicTable( ).find_edges_in_energy_interval( elemental_mapping_controller.explorer_interval) elemental_mapping_controller.add_edge(edges[0]) self.assertEqual(1, len(document_model.data_structures)) edge_data_struct = document_model.data_structures[0] self.assertEqual("elemental_mapping_edge", edge_data_struct.structure_type) self.assertEqual(model_data_item, edge_data_struct.source) self.assertEqual( 32, edge_data_struct.get_property_value("atomic_number")) self.assertEqual( 2, edge_data_struct.get_property_value("shell_number")) self.assertEqual( 3, edge_data_struct.get_property_value("subshell_index"))
def test_adding_multiple_edges(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K self.assertEqual(2, len(document_model.data_structures))
def test_explore_adds_edge(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) document_controller.event_loop.create_task( elemental_mapping_controller.explore_edges( document_controller)) document_controller.periodic() # start tasks document_model.recompute_all() document_controller.periodic() # finish tasks explorer_data_item = document_model.data_items[1] explorer_display_item = document_model.get_display_item_for_data_item( explorer_data_item) elemental_mapping_controller.set_current_data_item( explorer_data_item) self.assertIsNotNone(elemental_mapping_controller.model_data_item) energy_calibration = explorer_data_item.dimensional_calibrations[ -1] explorer_display_item.graphics[ -1].interval = energy_calibration.convert_from_calibrated_value( 1200 ) / 1024, energy_calibration.convert_from_calibrated_value( 1226) / 1024 document_controller.periodic() # update explorer interval edges = PeriodicTable.PeriodicTable( ).find_edges_in_energy_interval( elemental_mapping_controller.explorer_interval) elemental_mapping_controller.add_edge(edges[0]) self.assertEqual(1, len(document_model.data_structures)) edge_data_struct = document_model.data_structures[0] self.assertEqual("elemental_mapping_edge", edge_data_struct.structure_type) self.assertEqual(model_data_item, edge_data_struct.source) self.assertEqual( 32, edge_data_struct.get_property_value("atomic_number")) self.assertEqual( 2, edge_data_struct.get_property_value("shell_number")) self.assertEqual( 3, edge_data_struct.get_property_value("subshell_index"))
def test_removing_edges(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) ge_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L si_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K self.assertEqual(2, len(document_model.data_structures)) elemental_mapping_controller.remove_edge(ge_edge) self.assertEqual(1, len(document_model.data_structures)) self.assertEqual( 14, document_model.data_structures[0].get_property_value( "atomic_number")) elemental_mapping_controller.remove_edge(si_edge) self.assertEqual(0, len(document_model.data_structures))
def test_selecting_composite_updates_edge_value(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) si_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].pick_action() self.__run_until_complete(document_controller) self.assertIsNone(elemental_mapping_controller.edge) eels_data_item = document_model.data_items[1] elemental_mapping_controller.set_current_data_item(eels_data_item) self.assertEqual(model_data_item, elemental_mapping_controller.model_data_item) self.assertEqual(si_edge.data_structure, elemental_mapping_controller.edge.data_structure)
def test_controller_has_proper_edge_bundles_when_explorer_selected(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K document_controller.event_loop.create_task( elemental_mapping_controller.explore_edges( document_controller)) self.__run_until_complete(document_controller) explorer_data_item = document_model.data_items[1] elemental_mapping_controller.set_current_data_item( explorer_data_item) self.assertIsNotNone(elemental_mapping_controller.model_data_item) edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) self.assertEqual(1, len(edge_bundle))
def test_explore_creates_initial_line_plot(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) data_item = self.__create_spectrum_image() document_model.append_data_item(data_item) elemental_mapping_controller.set_current_data_item(data_item) document_controller.event_loop.create_task( elemental_mapping_controller.explore_edges( document_controller)) document_controller.periodic() # start tasks document_model.recompute_all() document_controller.periodic() # finish tasks self.assertEqual(2, len(document_model.data_items)) explorer_data_item = document_model.data_items[1] explorer_display_item = document_model.get_display_item_for_data_item( explorer_data_item) self.assertEqual(1, len(explorer_data_item.dimensional_shape)) self.assertEqual("explore", explorer_display_item.graphics[-1].graphic_id)
def test_background_subtraction_computation_functions_reasonably(self): document_model = DocumentModel.DocumentModel() self.app._set_document_model( document_model) # required to allow API to find document model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) si_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K spectrum_data_item = self.__create_spectrum() document_model.append_data_item(spectrum_data_item) spectrum_display_item = document_model.get_display_item_for_data_item( spectrum_data_item) spectrum_display_data_channel = spectrum_display_item.get_display_data_channel_for_data_item( spectrum_data_item) computation = document_model.create_computation() computation.create_object( "eels_spectrum_xdata", document_model.get_object_specifier( spectrum_display_data_channel, "display_xdata")) computation.create_input( "fit_interval", document_model.get_object_specifier(si_edge.data_structure), "fit_interval") computation.create_input( "signal_interval", document_model.get_object_specifier(si_edge.data_structure), "signal_interval") computation.processing_id = "eels.background_subtraction" document_model.append_computation(computation) document_model.recompute_all() document_controller.periodic() self.assertEqual(2, len(document_model.data_items))
def test_mapping_edge_produces_properly_configured_map(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].map_action() self.__run_until_complete(document_controller) self.assertEqual(2, len(document_model.data_items)) mapped_data_item = document_model.data_items[1] self.assertEqual(model_data_item, mapped_data_item.source) self.assertEqual(1, len(document_model.computations)) self.assertEqual("eels.mapping", document_model.computations[0].processing_id) self.assertEqual(mapped_data_item.dimensional_calibrations, model_data_item.dimensional_calibrations[0:2])
def test_removing_edges(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) ge_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L si_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K self.assertEqual(2, len(document_model.data_structures)) elemental_mapping_controller.remove_edge(ge_edge) self.assertEqual(1, len(document_model.data_structures)) self.assertEqual( 14, document_model.data_structures[0].get_property_value( "atomic_number")) elemental_mapping_controller.remove_edge(si_edge) self.assertEqual(0, len(document_model.data_structures))
def test_background_subtraction_computation_functions_reasonably(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller_with_application( ) document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) si_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K spectrum_data_item = self.__create_spectrum() document_model.append_data_item(spectrum_data_item) spectrum_display_item = document_model.get_display_item_for_data_item( spectrum_data_item) spectrum_display_data_channel = spectrum_display_item.get_display_data_channel_for_data_item( spectrum_data_item) computation = document_model.create_computation() computation.create_input_item( "eels_spectrum_xdata", Symbolic.make_item(spectrum_display_data_channel, type="display_xdata")) computation.create_input_item("fit_interval", Symbolic.make_item( si_edge.data_structure), property_name="fit_interval") computation.create_input_item("signal_interval", Symbolic.make_item( si_edge.data_structure), property_name="signal_interval") computation.processing_id = "eels.background_subtraction" document_model.append_computation(computation) document_model.recompute_all() document_controller.periodic() self.assertEqual(2, len(document_model.data_items))
def test_controller_has_proper_edge_bundles_when_explorer_selected(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K document_controller.event_loop.create_task( elemental_mapping_controller.explore_edges( document_controller)) self.__run_until_complete(document_controller) explorer_data_item = document_model.data_items[1] elemental_mapping_controller.set_current_data_item( explorer_data_item) self.assertIsNotNone(elemental_mapping_controller.model_data_item) edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) self.assertEqual(1, len(edge_bundle))
def test_selecting_composite_updates_edge_value(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) si_edge = elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].pick_action() self.__run_until_complete(document_controller) self.assertIsNone(elemental_mapping_controller.edge) eels_data_item = document_model.data_items[1] elemental_mapping_controller.set_current_data_item(eels_data_item) self.assertEqual(model_data_item, elemental_mapping_controller.model_data_item) self.assertEqual(si_edge.data_structure, elemental_mapping_controller.edge.data_structure)
def test_explore_creates_initial_line_plot(self): document_model = DocumentModel.DocumentModel() elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller), contextlib.closing( elemental_mapping_controller): data_item = self.__create_spectrum_image() document_model.append_data_item(data_item) elemental_mapping_controller.set_current_data_item(data_item) document_controller.event_loop.create_task( elemental_mapping_controller.explore_edges( document_controller)) document_controller.periodic() # start tasks document_model.recompute_all() document_controller.periodic() # finish tasks self.assertEqual(2, len(document_model.data_items)) explorer_data_item = document_model.data_items[1] explorer_display_item = document_model.get_display_item_for_data_item( explorer_data_item) self.assertEqual(1, len(explorer_data_item.dimensional_shape)) self.assertEqual("explore", explorer_display_item.graphics[-1].graphic_id)
def __init__(self, document_controller, panel_id, properties): super().__init__(document_controller, panel_id, _("Elemental Mappings")) document_model = document_controller.document_model self.__elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) ui = document_controller.ui self.__button_group = None column = ui.create_column_widget() edge_column = ui.create_column_widget() explore_column = ui.create_column_widget() add_edge_column = ui.create_column_widget() auto_edge_column = ui.create_column_widget() column.add(edge_column) column.add_spacing(12) column.add(explore_column) column.add_spacing(12) column.add(add_edge_column) column.add_spacing(12) column.add(auto_edge_column) column.add_spacing(12) column.add_stretch() self.widget = column explore_row = ui.create_row_widget() explore_button_widget = ui.create_push_button_widget(_("Explore")) multiprofile_button_widget = ui.create_push_button_widget( _("Multiprofile")) explore_row.add(explore_button_widget) explore_row.add_spacing(8) explore_row.add(multiprofile_button_widget) explore_row.add_stretch() explore_column.add(explore_row) def data_item_changed(data_item) -> None: self.__elemental_mapping_controller.set_current_data_item( data_item) current_data_item = data_item model_data_item = self.__elemental_mapping_controller.model_data_item edge = self.__elemental_mapping_controller.edge edge_column.remove_all() add_edge_column.remove_all() if self.__button_group: self.__button_group.close() self.__button_group = None if model_data_item: def explore_pressed(): document_controller.event_loop.create_task( self.__elemental_mapping_controller.explore_edges( document_controller)) explore_button_widget.on_clicked = explore_pressed multiprofile_button_widget.on_clicked = functools.partial( self.__elemental_mapping_controller.build_multiprofile, document_controller) self.__button_group = ui.create_button_group() for index, edge_bundle in enumerate( self.__elemental_mapping_controller.build_edge_bundles( document_controller)): def delete_pressed(): edge_bundle.delete_action() data_item_changed(current_data_item ) # TODO: this should be automatic row = ui.create_row_widget() radio_button = None label = None if edge: radio_button = ui.create_radio_button_widget( edge_bundle.electron_shell_str) self.__button_group.add_button(radio_button, index) radio_button.checked = edge_bundle.selected radio_button.on_clicked = edge_bundle.select_action else: label = ui.create_label_widget( edge_bundle.electron_shell_str) delete_button = ui.create_push_button_widget(_("Delete")) pick_button = ui.create_push_button_widget(_("Pick")) map_button = ui.create_push_button_widget(_("Map")) delete_button.on_clicked = delete_pressed pick_button.on_clicked = edge_bundle.pick_action map_button.on_clicked = edge_bundle.map_action row.add_spacing(20) if radio_button: row.add(radio_button) row.add_spacing(4) elif label: row.add(label) row.add_spacing(12) row.add(pick_button) row.add_spacing(12) row.add(map_button) row.add_stretch() row.add(delete_button) row.add_spacing(12) edge_column.add(row) atomic_number_widget = ui.create_combo_box_widget( items=PeriodicTable.PeriodicTable().get_elements_list(), item_getter=operator.itemgetter(1)) edge_widget = ui.create_combo_box_widget( items=PeriodicTable.PeriodicTable().get_edges_list(1), item_getter=operator.itemgetter(1)) add_button_widget = ui.create_push_button_widget(_("Add Edge")) atomic_number_row = ui.create_row_widget() atomic_number_row.add_spacing(20) atomic_number_row.add(ui.create_label_widget(_("Element"))) atomic_number_row.add_spacing(8) atomic_number_row.add(atomic_number_widget) atomic_number_row.add_spacing(8) atomic_number_row.add_stretch() edge_row = ui.create_row_widget() edge_row.add_spacing(20) edge_row.add(ui.create_label_widget(_("Edge"))) edge_row.add_spacing(8) edge_row.add(edge_widget) edge_row.add_spacing(8) edge_row.add_stretch() add_button_row = ui.create_row_widget() add_button_row.add_spacing(20) add_button_row.add(add_button_widget) add_button_row.add_spacing(8) add_button_row.add_stretch() add_edge_column.add(atomic_number_row) add_edge_column.add(edge_row) add_edge_column.add(add_button_row) def add_edge_current(): self.__elemental_mapping_controller.add_edge( edge_widget.current_item[0]) data_item_changed(model_data_item) data_item_changed(data_item) add_button_widget.on_clicked = add_edge_current def atomic_number_changed(item): edge_widget.items = PeriodicTable.PeriodicTable( ).get_edges_list(item[0]) atomic_number_widget.on_current_item_changed = atomic_number_changed add_row = ui.create_row_widget() add_column = ui.create_column_widget() refresh_row = ui.create_row_widget() add_grid = ui.create_row_widget() col1 = ui.create_column_widget() col2 = ui.create_column_widget() add_grid.add(col1) add_grid.add(col2) add_grid.add_stretch() def update_add_buttons(): col1.remove_all() col2.remove_all() explore_interval = self.__elemental_mapping_controller.explorer_interval if explore_interval is not None: edges = PeriodicTable.PeriodicTable( ).find_edges_in_energy_interval(explore_interval) for i, edge in enumerate(edges[0:4]): button = ui.create_push_button_widget( edge.to_long_str()) def add_edge(model_data_item, edge, data_item): self.__elemental_mapping_controller.add_edge( edge) data_item_changed(model_data_item) data_item_changed(data_item) button.on_clicked = functools.partial( add_edge, model_data_item, edge, data_item) col = col1 if i % 2 == 0 else col2 col.add(button) col1.add_stretch() col2.add_stretch() refresh_widget = ui.create_push_button_widget("\u21BB") refresh_widget.on_clicked = lambda: data_item_changed( current_data_item) # TODO: re-layout in Qt is awful update_add_buttons() refresh_row.add(refresh_widget) refresh_row.add_stretch() add_column.add(refresh_row) add_column.add(add_grid) add_row.add_spacing(20) add_row.add(add_column) add_row.add_stretch() add_edge_column.add(add_row) def display_item_changed(display_item): data_item = display_item.data_item if display_item else None data_item_changed(data_item) self.__focused_display_item_changed_event_listener = document_controller.focused_display_item_changed_event.listen( display_item_changed) selected_data_item = document_controller.selected_data_item data_item_changed(selected_data_item)
def test_changing_edge_configures_other_items_correctly(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model elemental_mapping_controller = ElementalMappingController.ElementalMappingController( document_model) model_data_item = self.__create_spectrum_image() document_model.append_data_item(model_data_item) model_display_item = document_model.get_display_item_for_data_item( model_data_item) elemental_mapping_controller.set_current_data_item(model_data_item) elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(14, 1, 1)) # Si-K elemental_mapping_controller.add_edge( PeriodicTable.ElectronShell(32, 2, 3)) # Ge-L edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) edge_bundle[0].pick_action() self.__run_until_complete(document_controller) eels_data_item = document_model.data_items[1] eels_display_item = document_model.get_display_item_for_data_item( eels_data_item) elemental_mapping_controller.set_current_data_item(eels_data_item) edge_bundle = elemental_mapping_controller.build_edge_bundles( document_controller) # apply the change to the other edge edge_bundle[1].select_action() self.__run_until_complete(document_controller) computation = document_model.computations[0] old_edge_data_structure = document_model.data_structures[0] new_edge_data_structure = document_model.data_structures[1] edge_ref_data_structure = document_model.data_structures[2] pick_region = model_display_item.graphics[0] # check the titles self.assertEqual("Pick Ge-L3", pick_region.label) self.assertEqual("Pick Ge-L3 EELS Data of Untitled", eels_data_item.title) # check the old intervals are disconnected and the new are connected old_fit_interval = eels_display_item.graphics[0].interval old_signal_interval = eels_display_item.graphics[1].interval new_fit_interval = (0.6, 0.7) new_signal_interval = (0.7, 0.8) # ensure changing old edge doesn't affect any connections old_edge_data_structure.set_property_value("fit_interval", new_fit_interval) old_edge_data_structure.set_property_value("signal_interval", new_signal_interval) self.assertEqual(old_fit_interval, eels_display_item.graphics[0].interval) self.assertEqual(old_signal_interval, eels_display_item.graphics[1].interval) self.assertEqual(old_fit_interval, computation.get_input("fit_interval")) self.assertEqual(old_signal_interval, computation.get_input("signal_interval")) # ensure changing new edge affects all connections new_edge_data_structure.set_property_value("fit_interval", new_fit_interval) new_edge_data_structure.set_property_value("signal_interval", new_signal_interval) self.assertEqual(new_fit_interval, eels_display_item.graphics[0].interval) self.assertEqual(new_signal_interval, eels_display_item.graphics[1].interval) self.assertEqual(new_fit_interval, computation.get_input("fit_interval")) self.assertEqual(new_signal_interval, computation.get_input("signal_interval")) # and the edge reference self.assertEqual( new_edge_data_structure, edge_ref_data_structure.get_referenced_object("edge"))