def test_GIVEN_an_nx_group_with_shape_WHEN_calling_create_component_THEN_component_returns_OFFGeometry( nexus_wrapper, ): monitor_group = nexus_wrapper.create_nx_group("monitor", "NXmonitor", nexus_wrapper.instrument) new_component = create_component(nexus_wrapper, monitor_group) shape_group = nexus_wrapper.create_nx_group("shape", "NXoff_geometry", new_component.group) vertices_dataset = nexus_wrapper.set_field_value( shape_group, "vertices", np.array([[0, 2, -2], [-1, -1, 1], [1, -1, 1]]), dtype=np.float, ) nexus_wrapper.set_field_value(shape_group, "winding_order", np.array([0, 1, 2]), dtype=np.int32) nexus_wrapper.set_field_value(shape_group, "faces", np.array([0]), dtype=np.int32) nexus_wrapper.set_attribute_value(vertices_dataset, "units", "m") assert isinstance(new_component.shape[0], OFFGeometryNexus)
def create_new_component( self, component_name: str, description: str, nx_class: str, pixel_data: PixelData, ): """ Creates a new component. :param component_name: The name of the component. :param description: The component description. :param nx_class: The component class. :param pixel_data: The PixelData for the component. Will be None if it was not given of if the component type doesn't have pixel-related fields. :return: The geometry object. """ component = self.instrument.create_component( component_name, nx_class, description ) self.generate_geometry_model(component, pixel_data) self.write_pixel_data_to_component(component, nx_class, pixel_data) add_fields_to_component(component, self.fieldsListWidget) self.component_model.add_component(component) component_with_geometry = create_component( self.instrument.nexus, component.group ) return component_with_geometry.shape
def test_GIVEN_an_NXdisk_chopper_group_WHEN_calling_create_component_THEN_component_returns_OFFGeometry_of_chopper( nexus_wrapper, ): chopper_group = nexus_wrapper.create_nx_group("chopper", "NXdisk_chopper", nexus_wrapper.instrument) add_dataset( chopper_group, "slit_edges", np.array([ 83.71, 94.7, 140.49, 155.79, 193.26, 212.56, 242.32, 265.33, 287.91, 314.37, 330.3, 360.0, ]) + 15.0, attributes={"units": "deg"}, ) add_dataset(chopper_group, "slits", 6) add_dataset(chopper_group, "slit_height", 130.0, attributes={"units": "mm"}) add_dataset(chopper_group, "radius", 300.0, attributes={"units": "mm"}) new_component = create_component(nexus_wrapper, chopper_group) assert isinstance(new_component._shape, ChopperShape) assert isinstance(new_component.shape[0], OFFGeometryNoNexus) assert ( len(new_component.shape[0].vertices) > 8 ), "Expect chopper geometry with many vertices, not just a placeholder cube with 8 vertices"
def test_GIVEN_an_NXdetector_group_with_pixel_shape_WHEN_calling_create_component_THEN_component_has_a_PixelShape(): wrapper = nx.NexusWrapper("file_with_detector") detector_group = wrapper.create_nx_group( "detector", "NXdetector", wrapper.instrument ) wrapper.create_nx_group("pixel_shape", "NXoff_geometry", detector_group) new_component = create_component(wrapper, detector_group) assert isinstance(new_component._shape, PixelShape)
def test_GIVEN_an_NXdetector_group_with_pixel_shape_WHEN_calling_create_component_THEN_component_has_a_PixelShape( nexus_wrapper, ): detector_group = nexus_wrapper.create_nx_group("detector", "NXdetector", nexus_wrapper.instrument) nexus_wrapper.create_nx_group("pixel_shape", "NXoff_geometry", detector_group) new_component = create_component(nexus_wrapper, detector_group) assert isinstance(new_component._shape, PixelShape)
def test_GIVEN_an_NXdisk_chopper_group_WHEN_calling_create_component_THEN_component_has_a_ChopperShape( nexus_wrapper, ): chopper_group = nexus_wrapper.create_nx_group("chopper", "NXdisk_chopper", nexus_wrapper.instrument) new_component = create_component(nexus_wrapper, chopper_group) assert isinstance(new_component._shape, ChopperShape) assert isinstance( new_component.shape[0], NoShapeGeometry ), "Expect chopper component to return NoShapeGeometry as it has insufficient details to create a mesh of the disk shape"
def create_component(self, name: str, nx_class: str, description: str) -> Component: """ Creates a component group in a NeXus file :param name: Name of the component group to create :param nx_class: NX_class of the component group to create :param description: Description of the component :return Wrapper for added component """ name = _convert_name_with_spaces(name) parent_group = self.nexus.instrument if nx_class in COMPONENTS_IN_ENTRY: parent_group = self.nexus.entry component_group = self.nexus.create_nx_group(name, nx_class, parent_group) component = create_component(self.nexus, component_group) component.description = description return component
def edit_existing_component( self, component_name: str, description: str, nx_class: str, pixel_data: PixelData, ): """ Edits an existing component. :param component_name: The component name. :param description: The component description. :param nx_class: The component class. :param pixel_data: The component PixelData. Can be None. :return: The geometry object. """ # remove the previous object from the qt3d view if not isinstance(self.component_to_edit.shape[0], NoShapeGeometry): self.parent().sceneWidget.delete_component(self.component_to_edit.name) # remove previous fields for field_group in self.component_to_edit.group.values(): if get_name_of_node(field_group) not in INVALID_FIELD_NAMES: del self.instrument.nexus.nexus_file[field_group.name] self.component_to_edit.name = component_name self.component_to_edit.nx_class = nx_class self.component_to_edit.description = description self.write_pixel_data_to_component(self.component_to_edit, nx_class, pixel_data) add_fields_to_component(self.component_to_edit, self.fieldsListWidget) self.generate_geometry_model(self.component_to_edit, pixel_data) component_with_geometry = create_component( self.instrument.nexus, self.component_to_edit.group ) return component_with_geometry.shape
def test_GIVEN_an_nx_group_with_no_shape_WHEN_calling_create_component_THEN_component_has_a_ComponentShape(): wrapper = nx.NexusWrapper("file_with_component_with_no_shape") monitor_group = wrapper.create_nx_group("monitor", "NXmonitor", wrapper.instrument) new_component = create_component(wrapper, monitor_group) assert isinstance(new_component._shape, ComponentShape)
def find_components(_, node): if isinstance(node, h5py.Group): if CommonAttrs.NX_CLASS in node.attrs.keys(): nx_class = get_nx_class(node) if nx_class and nx_class in self.nx_component_classes: component_list.append(create_component(self.nexus, node))
def test_GIVEN_an_nx_group_with_no_shape_WHEN_calling_create_component_THEN_component_has_a_ComponentShape( nexus_wrapper, ): monitor_group = nexus_wrapper.create_nx_group("monitor", "NXmonitor", nexus_wrapper.instrument) new_component = create_component(nexus_wrapper, monitor_group) assert isinstance(new_component._shape, ComponentShape)
def find_components(_, node): if isinstance(node, h5py.Group): if get_nx_class(node) in self.nx_component_classes: component_list.append(create_component(self.nexus, node))