def off_component_with_no_pixel_data( nexus_wrapper, nx_off_geometry_group, off_geometry_file ): component = Component(nexus_wrapper, nx_off_geometry_group) component.set_off_shape(off_geometry_file) return component
def cylindrical_component_with_pixel_mapping( nexus_wrapper, nx_cylindrical_geometry_group, pixel_mapping_with_single_pixel ): component = Component(nexus_wrapper, nx_cylindrical_geometry_group) component.record_pixel_mapping(pixel_mapping_with_single_pixel) return component
def create_component(nexus_wrapper: NexusWrapper, component_group: h5py.Group) -> Component: nx_class = get_nx_class(component_group) if nx_class == CHOPPER_CLASS_NAME: return Component(nexus_wrapper, component_group, ChopperShape(nexus_wrapper, component_group)) if nx_class in PIXEL_COMPONENT_TYPES and "pixel_shape" in component_group: return Component(nexus_wrapper, component_group, PixelShape(nexus_wrapper, component_group)) return Component(nexus_wrapper, component_group)
def create_component(nexus_wrapper: NexusWrapper, component_group: h5py.Group) -> Component: if (nexus_wrapper.get_attribute_value( component_group, CommonAttrs.NX_CLASS) == CHOPPER_CLASS_NAME): return Component(nexus_wrapper, component_group, ChopperShape(nexus_wrapper, component_group)) if (nexus_wrapper.get_attribute_value( component_group, CommonAttrs.NX_CLASS) in PIXEL_COMPONENT_TYPES and "pixel_shape" in component_group): return Component(nexus_wrapper, component_group, PixelShape(nexus_wrapper, component_group)) return Component(nexus_wrapper, component_group)
def replace_pixel_grid_in_off_component( component: Component, pixel_grid: PixelGrid, off_geometry: OFFGeometryNexus ): """ Change the pixel grid 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_grid: The PixelGrid object. :param off_geometry: The OffGeometry. """ component.record_pixel_grid(pixel_grid) component.set_off_shape(off_geometry, pixel_data=pixel_grid)
def test_transform_dependents_depends_on_are_updated_when_transformation_name_is_changed( nexus_wrapper, ): test_name = "slartibartfast" test_value = 42 test_vector = QVector3D(1.0, 0.0, 0.0) test_type = "Translation" transform_dataset = _add_transform_to_file(nexus_wrapper, test_name, test_value, test_vector, test_type) component = nexus_wrapper.create_nx_group("test", "NXaperture", nexus_wrapper.nexus_file) component.create_dataset("depends_on", data=transform_dataset.name) transform = Transformation(nexus_wrapper, transform_dataset) transform.register_dependent(Component(nexus_wrapper, component)) new_name = test_name + "1" transform.name = new_name assert transform.name == new_name assert str(component["depends_on"][()], encoding="UTF-8") == transform.dataset.name
def test_no_link_2(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") rot = component1.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) component1.depends_on = rot new_component = Component(component1.file, component1.group) assert not new_component.transforms.has_link
def test_GIVEN_single_scalar_field_and_string_WHEN_adding_fields_to_component_THEN_field_appears_in_component_fields_with_correct_name_and_value(): file = NexusWrapper("test_fields_2") component_group = file.nexus_file.create_group("test_component") component = Component(file, component_group) field_name = "test_field" field_value = np.string_(b"some_value") field = DummyField(field_name, field_value, field_value.dtype) list_widget = DummyListWidget() list_widget.addItem(field) add_fields_to_component(component, list_widget) assert component.get_field(field_name) assert bytes(component.get_field(field_name), encoding="ASCII") == field_value
def test_has_one_transformation_2(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") rot1 = component1.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) component1.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) component1.depends_on = rot1 new_component = Component(component1.file, component1.group) assert len(new_component.transforms) == 1
def test_linked_component_via_component_1(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") component2 = add_component_to_file(nexus_wrapper, "field", 42, "component2") rot = component2.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) component2.depends_on = rot component1.transforms.link.linked_component = component2 new_component = Component(component1.file, component1.group) assert new_component.transforms.link.linked_component == component2
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_list() if component_to_edit.get_field("x_pixel_offset") is not None: self.single_pixel_radio_button.setChecked(True) self.update_pixel_layout_visibility(True, False) self._fill_single_pixel_fields(component_to_edit) elif component_to_edit.get_field("detector_number") is not None: self.entire_shape_radio_button.setChecked(True) self.update_pixel_layout_visibility(False, True) self._fill_entire_shape_fields(component_to_edit) else: self.no_pixels_button.setChecked(True) self.pixel_options_stack.setVisible(False)
def add_component_to_file( nexus_wrapper: NexusWrapper, field_name: str = "test_field", field_value: Any = 42, component_name: str = "test_component", ) -> Component: component_group = nexus_wrapper.nexus_file.create_group(component_name) component_group.create_dataset(field_name, data=field_value) component = Component(nexus_wrapper, component_group) return component
def _find_linked_component(self) -> Optional[Component]: for transformation in self.parent: if self.parent._transform_has_external_link(transformation): component_path = transformation.depends_on.absolute_path[ : transformation.depends_on.absolute_path.find(TRANSFORM_STR) ] return Component( self.parent.parent_component.file, self.parent.parent_component.file.nexus_file[component_path], ) return None
def test_GIVEN_single_scalar_field_and_float_WHEN_adding_fields_to_component_THEN_field_appears_in_component_fields_with_correct_name_and_value( nexus_wrapper, ): component_group = nexus_wrapper.nexus_file.create_group("test_component") component = Component(nexus_wrapper, component_group) field_name = "test_field" field_dtype = np.float32 field_value = field_dtype(32.123) field = DummyField(field_name, field_value, field_dtype) list_widget = DummyListWidget() list_widget.addItem(field) add_fields_to_component(component, list_widget) assert component.get_field(field_name) assert component.get_field(field_name).dtype == field_dtype assert component.get_field(field_name)[...] == field_value
def off_component_with_pixel_grid( pixel_grid, nexus_wrapper, nx_off_geometry_group, off_geometry_file ): component = Component(nexus_wrapper, nx_off_geometry_group) component.record_pixel_grid(pixel_grid) component.set_off_shape(off_geometry_file, pixel_data=pixel_grid) return component
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(): 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, ) elif self.meshRadioButton.isChecked(): 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 linked_component(self) -> Optional[Component]: if not self.parent.has_link: return None if self._has_direct_component_link(): component_path = self.parent.parent_component.depends_on.absolute_path[ : self.parent.parent_component.depends_on.absolute_path.find( TRANSFORM_STR ) ] return Component( self.parent.parent_component.file, self.parent.parent_component.file.nexus_file[component_path], ) return self._find_linked_component()
def _fill_single_pixel_fields(self, component_to_edit: Component): """ Fill the "single pixel" fields of a component that's being edited and contains pixel information. :param component_to_edit: The component that's being edited. """ # Retrieve the pixel offsets and detector number from the component x_pixel_offset = component_to_edit.get_field("x_pixel_offset") y_pixel_offset = component_to_edit.get_field("y_pixel_offset") detector_numbers = component_to_edit.get_field("detector_number") # Check that x offset is more than one value if data_is_an_array_with_more_than_one_element(x_pixel_offset): # Set the number of rows and the row height n_rows, row_height = self._get_row_information(y_pixel_offset) self.row_count_spin_box.setValue(n_rows) self.row_height_spin_box.setValue(row_height) # Set the number of columns and the column width n_cols, col_width = self._get_column_information(x_pixel_offset) self.column_count_spin_box.setValue(n_cols) self.column_width_spin_box.setValue(col_width) # Set the first ID, start counting option, and the count direction option ( first_id, start_counting_text, count_along_text, ) = self._get_detector_number_information(detector_numbers) self.first_id_spin_box.setValue(first_id) self.start_counting_combo_box.setCurrentText(start_counting_text) self.count_first_combo_box.setCurrentText(count_along_text) else: # If the pixel offset information represents a single pixel pass
def test_transforms_has_no_link(nexus_wrapper): component1 = add_component_to_file( nexus_wrapper, "some_field", 42, "component_name" ) component2 = add_component_to_file( nexus_wrapper, "some_field", 42, "other_component_name" ) transform1 = component1.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) transform2 = component2.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) component1.depends_on = transform1 component2.depends_on = transform2 new_component = Component(component1.file, component1.group) assert not new_component.transforms.has_link
def off_component_with_pixel_mapping( nexus_wrapper, nx_off_geometry_group, pixel_mapping_with_six_pixels, off_geometry_file, ): component = Component(nexus_wrapper, nx_off_geometry_group) component.record_pixel_mapping(pixel_mapping_with_six_pixels) component.set_off_shape(off_geometry_file, pixel_data=pixel_mapping_with_six_pixels) return component
def write_pixel_data_to_component( component: Component, nx_class: str, pixel_data: PixelData ): """ Writes the detector number/pixel grid data to a component. :param component: The component to modify. :param nx_class: The NXclass of the component. :param pixel_data: The pixel data. """ component.clear_pixel_data() if pixel_data is None or nx_class not in PIXEL_COMPONENT_TYPES: return if isinstance(pixel_data, PixelMapping): component.record_pixel_mapping(pixel_data) if isinstance(pixel_data, PixelGrid): component.record_pixel_grid(pixel_data)
def add_component(self, name: str, nx_class: str, description: str): nexus_wrapper = NexusWrapper(str(uuid1())) component_group = _add_component_to_file(nexus_wrapper, name, 42, "component_name") return Component(nexus_wrapper, component_group)
def test_does_not_have_transformations_2(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") component1.add_rotation(QVector3D(1.0, 0.0, 0.0), 90.0) new_component = Component(component1.file, component1.group) assert len(new_component.transforms) == 0
def component(nexus_wrapper, component_group): return Component(nexus_wrapper, component_group)
def test_does_not_have_transformations_1(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") new_component = Component(component1.file, component1.group) assert len(new_component.transforms) == 0
def test_no_link_1(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") new_component = Component(component1.file, component1.group) assert not new_component.transforms.has_link
def get_component(): nexus_wrapper = NexusWrapper(str(uuid1())) component_group = _add_component_to_file(nexus_wrapper, "some_field", 42, "component_name") return Component(nexus_wrapper, component_group)
def test_linked_component_is_none_3(nexus_wrapper): component1 = add_component_to_file(nexus_wrapper, "field", 42, "component1") new_component = Component(component1.file, component1.group) assert new_component.transforms.link.linked_component is None