async def map_new_edge(document_controller, model_data_item, edge) -> None: document_model = document_controller.document_model project = document_model.get_project_for_item(model_data_item) map_data_item = DataItem.new_data_item() map_data_item.title = "{} of {}".format(_("Map"), str(edge.electron_shell)) map_data_item.category = model_data_item.category map_data_item.source = model_data_item document_model.append_data_item(map_data_item, project=project) computation = document_model.create_computation() computation.source = map_data_item computation.create_input_item( "spectrum_image_xdata", Symbolic.make_item(model_data_item, type="xdata")) computation.create_input_item("fit_interval", Symbolic.make_item(edge.data_structure), property_name="fit_interval") computation.create_input_item("signal_interval", Symbolic.make_item(edge.data_structure), property_name="signal_interval") computation.processing_id = "eels.mapping" computation.create_output_item("map", Symbolic.make_item(map_data_item)) document_model.append_computation(computation, project=project) await document_model.compute_immediate(document_controller.event_loop, computation) map_display_item = document_model.get_display_item_for_data_item( map_data_item) document_controller.show_display_item(map_display_item)
def test_cursor_over_2d_data_sequence_displays_correct_ordering_of_indices( self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): display_panel = document_controller.selected_display_panel data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((20, 100, 100), numpy.float64), data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_item.display_data_channels[0].sequence_index = 4 display_item.calibration_style_id = "pixels-top-left" display_panel.set_display_panel_display_item(display_item) header_height = display_panel.header_canvas_item.header_height info_panel = document_controller.find_dock_panel("info-panel") display_panel.root_container.layout_immediate( (1000 + header_height, 1000)) display_panel.display_canvas_item.mouse_entered() document_controller.periodic() display_panel.display_canvas_item.mouse_position_changed( 500, 500, Graphics.NullModifiers()) document_controller.periodic() self.assertEqual(info_panel.label_row_1.text, "Position: 50.0, 50.0, 4.0") self.assertEqual(info_panel.label_row_2.text, "Value: 1") self.assertIsNone(info_panel.label_row_3.text, None) display_panel.display_canvas_item.mouse_exited()
def test_dimension_used_for_scale_marker_on_4d_diffraction_image_is_correct( self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): display_panel = document_controller.selected_display_panel calibrations = [ Calibration.Calibration(units="y"), Calibration.Calibration(units="x"), Calibration.Calibration(units="a"), Calibration.Calibration(units="b") ] data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((10, 10, 50, 50)), dimensional_calibrations=calibrations, data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) header_height = display_panel.header_canvas_item.header_height display_panel.root_container.layout_immediate( (1000 + header_height, 1000)) # run test self.assertEqual( display_panel.display_canvas_item. _info_overlay_canvas_item_for_test. _dimension_calibration_for_test.units, "b")
def test_changing_sequence_index_updates_display_range(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data = numpy.zeros((3, 8, 8)) data[1, ...] = 1 data[2, ...] = 2 xdata = DataAndMetadata.new_data_and_metadata( data, data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2)) data_item = DataItem.new_data_item(xdata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] self.assertEqual( display_data_channel.get_calculated_display_values( True).display_range, (0, 0)) self.assertEqual( display_data_channel.get_calculated_display_values( True).data_range, (0, 0)) display_data_channel.sequence_index = 1 self.assertEqual( display_data_channel.get_calculated_display_values( True).display_range, (1, 1)) self.assertEqual( display_data_channel.get_calculated_display_values( True).data_range, (1, 1))
def test_exception_during_calculate_display_values_recovers_gracefully( self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model d = numpy.random.randn(4, 4, 3, 3) data_and_metadata = DataAndMetadata.new_data_and_metadata( d, data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) def next_calculated_display_values(): pass display_data_channel = display_item.display_data_channels[0] listener = display_data_channel.add_calculated_display_values_listener( next_calculated_display_values) with contextlib.closing(listener): display_data = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data # now run the test display_data_channel.collection_index = 2, 2 # should trigger the thread display_data = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data display_data_channel.collection_index = 2, 2 display_data = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data self.assertTrue(numpy.array_equal(display_data, d[2, 2, ...]))
def test_mapping_widget_to_image_on_3d_spectrum_image_uses_collection_dimensions( self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): display_panel = document_controller.selected_display_panel document_controller.selected_display_panel.change_display_panel_content( {"type": "image"}) data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((10, 10, 50)), data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 1)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) header_height = display_panel.header_canvas_item.header_height display_panel.root_container.layout_immediate( (100 + header_height, 100)) # run test document_controller.tool_mode = "line-profile" display_panel.display_canvas_item.simulate_drag((20, 25), (65, 85)) self.assertEqual(display_item.graphics[0].vector, ((0.2, 0.25), (0.65, 0.85)))
def test_line_plot_calculates_calibrated_vs_uncalibrated_display_y_values( self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model data_item = DataItem.new_data_item( DataAndMetadata.new_data_and_metadata( numpy.ones((8, )), intensity_calibration=Calibration.Calibration(offset=0, scale=10, units="nm"))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_item.display_type = "line_plot" display_panel = document_controller.selected_display_panel display_panel.set_display_panel_display_item(display_item) display_panel.display_canvas_item.layout_immediate((640, 480)) self.assertTrue( numpy.array_equal( display_panel.display_canvas_item.line_graph_canvas_item. calibrated_xdata.data, numpy.full((8, ), 10))) display_item.calibration_style_id = "pixels-top-left" display_panel.display_canvas_item.layout_immediate((640, 480)) self.assertTrue( numpy.array_equal( display_panel.display_canvas_item.line_graph_canvas_item. calibrated_xdata.data, numpy.ones((8, ))))
def test_line_plot_handles_calibrated_vs_uncalibrated_display(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model data_item = DataItem.new_data_item( DataAndMetadata.new_data_and_metadata( numpy.ones((8, )), dimensional_calibrations=[ Calibration.Calibration(offset=0, scale=10, units="nm") ])) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_item.display_type = "line_plot" display_panel = document_controller.selected_display_panel display_panel.set_display_panel_display_item(display_item) display_panel.display_canvas_item.layout_immediate((640, 480)) self.assertEqual( display_panel.display_canvas_item.line_graph_canvas_item. calibrated_xdata.dimensional_calibrations[-1].units, "nm") display_item.calibration_style_id = "pixels-top-left" display_panel.display_canvas_item.layout_immediate((640, 480)) self.assertFalse( display_panel.display_canvas_item.line_graph_canvas_item. calibrated_xdata.dimensional_calibrations[-1].units)
def test_dimension_used_for_scale_marker_on_2d_data_stack_is_correct(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model display_panel = document_controller.selected_display_panel calibrations = [ Calibration.Calibration(units="s"), Calibration.Calibration(units="y"), Calibration.Calibration(units="x") ] data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((50, 10, 10)), dimensional_calibrations=calibrations, data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) header_height = display_panel.header_canvas_item.header_height display_panel.root_container.layout_immediate( (1000 + header_height, 1000)) # run test self.assertEqual( display_panel.display_canvas_item. _info_overlay_canvas_item_for_test. _dimension_calibration_for_test.units, "x")
def test_display_data_is_1d_for_collection_of_1d_datum(self): # this tests the changes of 1d collections of 1d data. see #529. with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((2, 8), float), data_descriptor=DataAndMetadata.DataDescriptor(False, 1, 1)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] self.assertEqual( (8, ), display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_shape) self.assertEqual( float, display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_dtype) self.assertEqual((8, ), display_data_channel.display_data_shape) self.assertEqual( 1, len( display_data_channel.get_datum_calibrations( data_item.dimensional_calibrations)))
def test_collection_index_validates_when_data_changes(self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): d = numpy.random.randn(4, 4, 3, 3) data_and_metadata = DataAndMetadata.new_data_and_metadata( d, data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] display_data_channel.collection_index = 3, 3 display_data = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data self.assertTrue(numpy.array_equal(display_data, d[3, 3, ...])) d2 = numpy.random.randn(2, 2, 3, 3) data_and_metadata2 = DataAndMetadata.new_data_and_metadata( d2, data_descriptor=DataAndMetadata.DataDescriptor(False, 2, 2)) display_item.data_item.set_xdata(data_and_metadata2) display_data2 = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data self.assertTrue(numpy.array_equal(display_data2, d2[1, 1, ...]))
def test_cursor_over_1d_image_without_exception_x(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_item = DataItem.new_data_item(DataAndMetadata.new_data_and_metadata(numpy.zeros((4, 25)), data_descriptor=DataAndMetadata.DataDescriptor(False, 1, 1))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item(data_item) display_item.display_type = "image" p, v = display_item.get_value_and_position_text(display_item.display_data_channel, (2, 20)) self.assertEqual(p, "20.0, 2.0") self.assertEqual(v, "0")
def test_cursor_over_1d_multiple_data_but_2_datum_dimensions_displays_without_exception(self): document_model = DocumentModel.DocumentModel() with contextlib.closing(document_model): data_and_metadata = DataAndMetadata.new_data_and_metadata(numpy.zeros((4, 1000), numpy.float64), data_descriptor=DataAndMetadata.DataDescriptor(False, 0, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item(data_item) display_item.calibration_style_id = "pixels-top-left" p, v = display_item.get_value_and_position_text(display_item.display_data_channel, (500,)) self.assertEqual(p, "500.0, 0.0") self.assertEqual(v, "0")
def test_cursor_over_1d_sequence_data_displays_without_exception(self): with TestContext.create_memory_context() as test_context: document_model = test_context.create_document_model() data_and_metadata = DataAndMetadata.new_data_and_metadata(numpy.zeros((4, 1000), numpy.float64), data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 1)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item(data_item) display_item.calibration_style_id = "pixels-top-left" p, v = display_item.get_value_and_position_text(display_item.display_data_channel, (500,)) self.assertEqual(p, "500.0, 0.0") self.assertEqual(v, "0")
def __create_spectrum(self) -> DataItem.DataItem: data = numpy.random.uniform(10, 1000, 1024).astype(numpy.float32) intensity_calibration = Calibration.Calibration(units="~") dimensional_calibrations = [ Calibration.Calibration(scale=2.0, units="eV") ] data_descriptor = DataAndMetadata.DataDescriptor( is_sequence=False, collection_dimension_count=0, datum_dimension_count=1) xdata = DataAndMetadata.new_data_and_metadata( data, intensity_calibration=intensity_calibration, dimensional_calibrations=dimensional_calibrations, data_descriptor=data_descriptor) return DataItem.new_data_item(xdata)
def test_display_data_is_2d_for_collection_of_1d_datum(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((2, 8), numpy.float64), data_descriptor=DataAndMetadata.DataDescriptor(False, 1, 1)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_shape, (2, 8)) self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_dtype, numpy.float64) self.assertEqual(display_data_channel.display_data_shape, (2, 8))
def test_create_rgba_sequence_should_work(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model data = (numpy.random.rand(4, 64, 64, 3) * 255).astype(numpy.uint8) data_item = DataItem.new_data_item( DataAndMetadata.new_data_and_metadata( data, data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_dtype, numpy.uint8) self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_shape[-1], 3)
def test_display_data_is_2d_for_collection_of_1d_datum(self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((2, 8), numpy.float64), data_descriptor=DataAndMetadata.DataDescriptor(False, 1, 1)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_shape, (2, 8)) self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_dtype, numpy.float64) self.assertEqual(display_data_channel.display_data_shape, (2, 8))
def test_create_rgba_sequence_should_work(self): document_model = DocumentModel.DocumentModel() document_controller = DocumentController.DocumentController( self.app.ui, document_model, workspace_id="library") with contextlib.closing(document_controller): data = (numpy.random.rand(4, 64, 64, 3) * 255).astype(numpy.uint8) data_item = DataItem.new_data_item( DataAndMetadata.new_data_and_metadata( data, data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2))) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_dtype, numpy.uint8) self.assertEqual( display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data_shape[-1], 3)
async def map_new_edge(document_controller, model_data_item, edge) -> None: document_model = document_controller.document_model map_data_item = DataItem.new_data_item() map_data_item.title = "{} of {}".format(_("Map"), str(edge.electron_shell)) map_data_item.category = model_data_item.category map_data_item.source = model_data_item document_model.append_data_item(map_data_item) computation = document_model.create_computation() computation.source = map_data_item computation.create_object("spectrum_image_xdata", document_model.get_object_specifier(model_data_item, "xdata")) 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.mapping" computation.create_result("map", document_model.get_object_specifier(map_data_item)) document_model.append_computation(computation) await document_model.compute_immediate(document_controller.event_loop, computation) map_display_item = document_model.get_display_item_for_data_item(map_data_item) document_controller.show_display_item(map_display_item)
def test_sequence_index_validates_when_data_changes(self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model d = numpy.random.randn(4, 3, 3) data_and_metadata = DataAndMetadata.new_data_and_metadata( d, data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_data_channel = display_item.display_data_channels[0] display_data_channel.sequence_index = 3 display_data = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data self.assertTrue(numpy.array_equal(display_data, d[3, ...])) d2 = numpy.random.randn(2, 3, 3) data_and_metadata2 = DataAndMetadata.new_data_and_metadata( d2, data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2)) display_item.data_item.set_xdata(data_and_metadata2) display_data2 = display_data_channel.get_calculated_display_values( True).display_data_and_metadata.data self.assertTrue(numpy.array_equal(display_data2, d2[1, ...]))
def test_mapping_widget_to_image_on_2d_data_stack_uses_signal_dimensions( self): with TestContext.create_memory_context() as test_context: document_controller = test_context.create_document_controller() document_model = document_controller.document_model display_panel = document_controller.selected_display_panel document_controller.selected_display_panel.change_display_panel_content( {"type": "image"}) data_and_metadata = DataAndMetadata.new_data_and_metadata( numpy.ones((50, 10, 10)), data_descriptor=DataAndMetadata.DataDescriptor(True, 0, 2)) data_item = DataItem.new_data_item(data_and_metadata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) display_panel.set_display_panel_display_item(display_item) header_height = display_panel.header_canvas_item.header_height display_panel.root_container.layout_immediate( (100 + header_height, 100)) # run test document_controller.tool_mode = "line-profile" display_panel.display_canvas_item.simulate_drag((20, 25), (65, 85)) self.assertEqual(display_item.graphics[0].vector, ((0.2, 0.25), (0.65, 0.85)))
def test_calibrate_spectrum_for_single_spectrum(self): with TestContext.create_memory_context() as profile_context: document_controller = profile_context.create_document_controller_with_application( ) document_model = document_controller.document_model data = numpy.zeros((100, ), dtype=numpy.float32) data[10] = 1 intensity_calibration = Calibration.Calibration(units="~") dimensional_calibrations = [ Calibration.Calibration(scale=1.0, units="eV") ] data_descriptor = DataAndMetadata.DataDescriptor( is_sequence=False, collection_dimension_count=0, datum_dimension_count=1) xdata = DataAndMetadata.new_data_and_metadata( data, intensity_calibration=intensity_calibration, dimensional_calibrations=dimensional_calibrations, data_descriptor=data_descriptor) data_item = DataItem.new_data_item(xdata) document_model.append_data_item(data_item) display_item = document_model.get_display_item_for_data_item( data_item) document_controller.select_display_items_in_data_panel( [display_item]) document_controller.data_panel_focused() api = Facade.get_api("~1.0", "~1.0") dialog = AlignZLP.calibrate_spectrum( api, api.application.document_windows[0]) self.assertEqual(1, len(document_model.data_items)) self.assertEqual(1, len(document_model.display_items)) self.assertEqual(1, len(api.library.data_items)) # calibrate_spectrum should create two graphics, offset and scale self.assertEqual(2, len(api.library.data_items[0].graphics)) # It should find the peak at 10 and set the calibration offset accordingly self.assertAlmostEqual( data_item.dimensional_calibrations[0].offset, -10.5) # Move the scale graphic and check that the calibration changed accordingly offset_graphic = api.library.data_items[0].graphics[0] scale_graphic = api.library.data_items[0].graphics[1] position_diff = scale_graphic.position - offset_graphic.position scale_graphic.position = offset_graphic.position + 0.5 * position_diff self.assertAlmostEqual(data_item.dimensional_calibrations[0].scale, 2.0) # Move the offset graphic and check that the calibration offset and the scale graphic have moved position_diff = scale_graphic.position - offset_graphic.position offset_graphic.position += 0.1 self.assertAlmostEqual( position_diff, scale_graphic.position - offset_graphic.position) self.assertAlmostEqual( data_item.dimensional_calibrations[0].offset, -41) # Closing the dialog should remove the graphics dialog.request_close() self.assertEqual(0, len(api.library.data_items[0].graphics)) # Test cleanup document_model.remove_data_item(data_item) self.assertEqual(0, len(document_model.data_items)) self.assertEqual(0, len(document_model.display_items)) self.assertEqual(0, len(document_model.data_structures))
def __create_spectrum_image(self) -> DataItem.DataItem: return DataItem.new_data_item(self.__create_spectrum_image_xdata())