def test_UI_GIVEN_stream_group_as_angle_WHEN_creating_rotation_THEN_ui_is_filled_correctly( qtbot, ): component = Component( name="test", ) x = 0 y = 0 z = 0 transform = component.add_rotation(QVector3D(x, y, z), 0, name="test") topic = "test_topic" source = "source1" type = "double" stream = F142Stream(parent_node=transform, topic=topic, source=source, type=type) transform.values = stream view = EditRotation(parent=None, transformation=transform, model=None) qtbot.addWidget(view) assert view.transformation_frame.x_spinbox.value() == x assert view.transformation_frame.y_spinbox.value() == y assert view.transformation_frame.z_spinbox.value() == z assert view.transformation_frame.value_spinbox.value() == 0.0 assert ( view.transformation_frame.magnitude_widget.field_type == FieldType.kafka_stream ) assert view.transformation_frame.magnitude_widget.value.topic == topic assert view.transformation_frame.magnitude_widget.value.type == type assert view.transformation_frame.magnitude_widget.value.writer_module == "f142" assert view.transformation_frame.magnitude_widget.value.source == source
def test_UI_GIVEN_link_as_rotation_magnitude_WHEN_creating_rotation_view_THEN_ui_is_filled_correctly( qtbot, ): model = Model() component = Component(name="test") x = 0 y = 0 z = 0 path = "/entry" transform = component.add_rotation(QVector3D(x, y, z), 0, name="test") link = Link(parent_node=None, name="test", source=path) transform.values = link view = EditRotation(transformation=transform, model=model, parent=None) qtbot.addWidget(view) assert view.transformation_frame.x_spinbox.value() == x assert view.transformation_frame.y_spinbox.value() == y assert view.transformation_frame.z_spinbox.value() == z assert view.transformation_frame.value_spinbox.value() == 0.0 assert view.transformation_frame.magnitude_widget.field_type == FieldType.link assert view.transformation_frame.magnitude_widget.value.source == path
def test_can_set_off_geometry_properties(): component = Component("test") vertices = [ QVector3D(0.0, 0.0, 1.0), QVector3D(0.0, 1.0, 0.0), QVector3D(0.0, 0.0, 0.0), QVector3D(0.0, 1.0, 1.0), ] faces = [[0, 1, 2, 3]] shape = OFFGeometryNoNexus(vertices, faces) component.set_off_shape(shape) nexus_shape, _ = component.shape vertex_2_x = 0.5 vertex_2_y = -0.5 vertex_2_z = 0 new_vertices = [ QVector3D(-0.5, -0.5, 0), QVector3D(0, 0.5, 0), QVector3D(vertex_2_x, vertex_2_y, vertex_2_z), ] triangle = [0, 1, 2] new_faces = [triangle] nexus_shape.vertices = new_vertices nexus_shape.faces = new_faces assert nexus_shape.faces == new_faces assert nexus_shape.vertices[2].x() == approx(vertex_2_x) assert nexus_shape.vertices[2].y() == approx(vertex_2_y) assert nexus_shape.vertices[2].z() == approx(vertex_2_z)
def fill_existing_entries(self, component_to_edit: Component): """ Populate the pixel fields based on what is already stored in the NeXus file. """ self.reset_pixel_mapping_table() try: component_to_edit.get_field_value(X_PIXEL_OFFSET) self.single_pixel_radio_button.setChecked(True) self.update_pixel_layout_visibility(True, False) self._fill_single_pixel_fields(component_to_edit) return except AttributeError: pass try: component_to_edit.get_field_value(DETECTOR_NUMBER) self.entire_shape_radio_button.setChecked(True) self.update_pixel_layout_visibility(False, True) self._fill_entire_shape_fields(component_to_edit) return except AttributeError: pass self.no_pixels_button.setChecked(True) self.pixel_options_stack.setVisible(False)
def test_can_get_off_geometry_properties(): component = Component("test") vertex_3_x = 0.0 vertex_3_y = 1.0 vertex_3_z = 1.0 vertices = [ QVector3D(0, 0, 1), QVector3D(0, 1, 0), QVector3D(0, 0, 0), QVector3D(vertex_3_x, vertex_3_y, vertex_3_z), ] faces = [[0, 1, 2, 3]] shape = OFFGeometryNoNexus(vertices, faces) component.set_off_shape(shape) nexus_shape, _ = component.shape assert isinstance(nexus_shape, OFFGeometryNexus) assert nexus_shape.faces == faces assert nexus_shape.vertices[3].x() == approx(vertex_3_x) assert nexus_shape.vertices[3].y() == approx(vertex_3_y) assert nexus_shape.vertices[3].z() == approx(vertex_3_z)
def test_axis_direction_must_be_non_zero(): component = Component("test") height = 3 radius = 4 assert not component.set_cylinder_shape(axis_direction=QVector3D(0, 0, 0), height=height, radius=radius, units="m")
def test_end_of_depends_on_chain_of_component_is_linked_to_other_component(): # GIVEN component has depends_on chain with multiple transformations component = Component(name="test_component") values = Dataset(parent_node=None, name="", type=ValueTypes.INT, values=[42]) transforms_2 = component.add_translation( name="transform2", vector=QVector3D(0, 0, 1.0), # default to beam direction values=values, ) transform_1 = component.add_translation( name="transform1", vector=QVector3D(0, 0, 1.0), # default to beam direction values=values, depends_on=transforms_2, ) component.depends_on = transform_1 # WHEN it is linked to another component another_component = Component(name="another_test_component") transform_3 = another_component.add_translation( name="transform3", vector=QVector3D(0, 0, 1.0), # default to beam direction values=values, ) another_component.depends_on = transform_3 component.transforms.link.linked_component = another_component # THEN it is the last component of the depends_on chain which has its depends_on property updated assert transforms_2.depends_on == transform_3
def test_remove_item_removes_single_item_in_list_and_not_everything(): comp1_name = "component1" comp1 = Component(comp1_name) comp2_name = "component2" comp2 = Component(comp2_name) list_of_components = [comp1, comp2] _remove_item(list_of_components, comp1_name) assert list_of_components == [comp2]
def test_component_as_dict_contains_links(): name = "link1" target = "/entry/instrument/something" test_component = Component(name="test") test_component[name] = Link(parent_node=None, name=name, source=target) dictionary_output = test_component.as_dict([]) assert dictionary_output["children"][0]["config"]["name"] == name assert dictionary_output["children"][0]["config"]["source"] == target assert dictionary_output["children"][0]["module"] == "link"
def test_remove_from_beginning_1(instrument): component1 = Component("component1", instrument) rot = component1.add_rotation( name="rotation1", axis=QVector3D(1.0, 0.0, 0.0), angle=values.values, values=values, ) component1.depends_on = rot assert len(rot.dependents) == 1 rot.remove_from_dependee_chain() assert component1.depends_on is None
def replace_pixel_mapping_in_off_component(component: Component, pixel_mapping: PixelMapping, off_geometry: OFFGeometryNexus): """ Change the pixel mapping that is currently stored in a Component. Used to see if everything behaves correctly even for pixel data with 'special' properties. :param component: The component to have its pixel data replaced. :param pixel_mapping: The PixelMapping or PixielGrid object. :param off_geometry: The OffGeometry. """ component.record_pixel_mapping(pixel_mapping) component.set_off_shape(off_geometry, pixel_data=pixel_mapping)
def test_component_as_dict_contains_stream_field(): source = "PVS:pv1" topic = "topic1" name = "stream1" test_component = Component(name="test") test_component[name] = NS10Stream(parent_node=None, source=source, topic=topic) dictionary_output = test_component.as_dict([]) assert dictionary_output["children"][0]["module"] == "ns10" assert dictionary_output["children"][0]["config"]["topic"] == topic assert dictionary_output["children"][0]["config"]["source"] == source
def test_set_item_works_on_list_that_has_components_in(): comp1_name = "component1" comp1 = Component(comp1_name) list_of_components = [comp1] comp2_name = "component2" comp2 = Component(comp2_name) _set_item(None, list_of_components, comp2_name, comp2) assert list_of_components == [comp1, comp2]
def component_with_transformation() -> Component: comp = Component(name="Component") transformation = comp.add_rotation( name="Transformation", angle=90, axis=QVector3D(1, 0, 0), depends_on=None, values=Dataset( parent_node=False, name="test", values=123, type=ValueTypes.DOUBLE ), ) comp.depends_on = transformation return comp
def test_component_set_field_with_scalar_value_correctly_sets_field_value(): comp = Component("comp4") field_name = "testfield" data = 123 dtype = ValueTypes.INT comp.set_field_value(field_name, data, dtype) field_dataset = comp[field_name] assert field_dataset.name == field_name assert field_dataset.values == data assert field_dataset.type == dtype
def test_set_item_with_existing_component_in_overwrites_if_name_is_same(): comp1_name = "component1" comp1 = Component(comp1_name) comp2 = Component(comp1_name) nx_class = "NXaperture" comp2.nx_class = nx_class list_of_components = [comp1] _set_item(None, list_of_components, comp1_name, comp2) assert list_of_components[0].nx_class == nx_class
def test_cylinder_has_property_values_it_was_created_with(): component = Component("test") height = 3 radius = 4 units = "cubits" cylinder = component.set_cylinder_shape(axis_direction=QVector3D(1, 0, 0), height=height, radius=radius, units=units) assert cylinder.radius == approx(radius) assert cylinder.height == approx(height) assert cylinder.units == units
def test_component_set_field_with_numpy_array_correctly_sets_field_value(): comp = Component("comp4") data = [[1], [2]] dtype = ValueTypes.INT field_name = "field1" field_value = np.asarray(data, dtype=int) comp.set_field_value(field_name, field_value, dtype) field_dataset = comp["field1"] assert field_dataset.name == field_name assert np.array_equal(field_dataset.values, field_value) assert field_dataset.type == dtype
def test_remove_from_beginning_3(instrument): component1 = Component("component1", instrument) component2 = Component("component2", instrument) rot1 = component1.add_rotation( name="rotation1", axis=QVector3D(1.0, 0.0, 0.0), angle=values.values, values=values, ) rot2 = component2.add_rotation( name="rotation2", axis=QVector3D(1.0, 0.0, 0.0), angle=values.values, values=values, ) component1.depends_on = rot1 component2.depends_on = rot2 rot1.depends_on = rot2 assert len(rot2.dependents) == 2 rot1.remove_from_dependee_chain() assert len(rot2.dependents) == 2 assert component2 in rot2.dependents assert component1 in rot2.dependents assert component1.depends_on == rot2 assert component1.transforms.link.linked_component == component2
def test_set_two_dependent_components(): transform = create_transform("transform_1") component1 = Component("component1") component2 = Component("component2") transform.register_dependent(component1) transform.register_dependent(component2) set_dependents = transform.dependents assert len(set_dependents) == 2 assert set_dependents[0] == component1 assert set_dependents[1] == component2
def test_box_has_property_values_it_was_created_with(): component = Component("test") length = 1.0 width = 2.0 height = 3.0 units = "m" box = component.set_box_shape(height=height, width=width, length=length, units=units) assert box.size[0] == approx(length) assert box.size[1] == approx(width) assert box.size[2] == approx(height) assert box.units == units
def generate_geometry_model(self, component: Component, pixel_data: PixelData = None): """ Generates a geometry model depending on the type of geometry selected and the current values of the line edits that apply to the particular geometry type. :return: The generated model. """ if self.CylinderRadioButton.isChecked(): geometry = component.set_cylinder_shape( QVector3D( self.cylinderXLineEdit.value(), self.cylinderYLineEdit.value(), self.cylinderZLineEdit.value(), ), self.cylinderHeightLineEdit.value(), self.cylinderRadiusLineEdit.value(), self.unitsLineEdit.text(), pixel_data=pixel_data, ) if not geometry: show_warning_dialog( "3D vector is zero length in cylinder geometry.", "") elif self.boxRadioButton.isChecked(): component.set_box_shape( self.boxLengthLineEdit.value(), self.boxWidthLineEdit.value(), self.boxHeightLineEdit.value(), self.unitsLineEdit.text(), ) elif self.meshRadioButton.isChecked() and self.cad_file_name: mesh_geometry = OFFGeometryNoNexus() geometry_model = load_geometry(self.cad_file_name, self.unitsLineEdit.text(), mesh_geometry) # Units have already been used during loading the file, but we store them and file name # so we can repopulate their fields in the edit component window geometry_model.units = self.unitsLineEdit.text() geometry_model.file_path = self.cad_file_name component.set_off_shape( geometry_model, units=self.unitsLineEdit.text(), filename=self.fileLineEdit.text(), pixel_data=pixel_data, )
def _add_component_to_file(field_name: str, field_value: Any, component_name: str = "test_component"): component = Component(component_name) component.set_field_value( field_name, Dataset( parent_node=None, name=field_name, type=ValueTypes.DOUBLE, size="[1]", values=field_value, ), dtype=ValueTypes.DOUBLE, ) return component
def test_set_one_dependent_component(): transform = create_transform("transform_1") component = Component("test_component") transform.register_dependent(component) set_dependents = transform.dependents assert len(set_dependents) == 1 assert set_dependents[0] == component
def test_remove_item_removes_item_from_list(): name = "component1" test = Component(name) list_of_components = [test] _remove_item(list_of_components, name) assert not list_of_components
def create_tree_structure(): entry = Group("entry") sample = Component("sample", parent_node=entry) instrument = Group("instrument", parent_node=entry) g1 = Group("g1", parent_node=instrument) g2 = Group("g2", parent_node=instrument) g21 = Group("g21", parent_node=g2) c1 = Component("c1", parent_node=instrument) c2 = Group("c2", parent_node=instrument) dataset = Dataset(name="dataset", values=0, parent_node=sample) entry.children = [sample, instrument] sample.children = [g1, g2, dataset] g2.children = [g21] instrument.children = [c1, c2] return entry, sample, instrument
def test_deregister_three_dependent_components(): transform = create_transform("transform_1") component1 = Component("test_component1") component2 = Component("test_component2") component3 = Component("test_component3") transform.register_dependent(component1) transform.register_dependent(component2) transform.register_dependent(component3) transform.deregister_dependent(component1) transform.deregister_dependent(component2) transform.deregister_dependent(component3) set_dependents = transform.dependents assert len(set_dependents) == 0
def test_register_dependent_twice(): transform = create_transform("transform_1") component1 = Component("test_component1") transform.register_dependent(component1) transform.register_dependent(component1) set_dependents = transform.dependents assert len(set_dependents) == 1
def test_get_item_throws_error_when_component_is_not_in_list_then_works_when_item_is_set( ): name = "component1" test = Component(name) list_of_components = [] assert _get_item(list_of_components, name) is None _set_item(None, list_of_components, name, test) assert _get_item(list_of_components, name) == test
def test_remove_from_end(): component1 = Component("component1", instrument) rot1 = component1.add_rotation( name="rotation1", axis=QVector3D(1.0, 0.0, 0.0), angle=values.values, values=values, ) rot2 = component1.add_rotation( name="rotation2", axis=QVector3D(1.0, 0.0, 0.0), angle=values.values, values=values, depends_on=rot1, ) rot3 = component1.add_rotation( name="rotation3", axis=QVector3D(1.0, 0.0, 0.0), angle=values.values, values=values, depends_on=rot2, ) component1.depends_on = rot3 rot1.remove_from_dependee_chain() assert rot1.depends_on is None assert not rot1.dependents assert component1.depends_on == rot3 assert rot2.dependents[0] == rot3 assert len(component1.transforms) == 2