def add_translation( self, vector: QVector3D, name: str = None, depends_on: Transformation = None, values: Dataset = Dataset(parent_node=None, name="", values=0, type=ValueTypes.DOUBLE), target_pos: int = -1, ) -> Transformation: """ Note, currently assumes translation is in metres :param vector: direction and magnitude of translation as a 3D vector :param name: name of the translation group (Optional) :param depends_on: existing transformation which the new one depends on (otherwise relative to origin) :param values: The translation distance information. :param target_pos: Target position. """ values.parent_node = self unit_vector, _ = _normalise(vector) return self._create_and_add_transform( name, TransformationType.TRANSLATION, 0.0, "m", unit_vector, depends_on, values, target_pos, )
def create_dataset(name: str, dtype: str, val: Any): if np.isscalar(val): return Dataset(parent_node=None, name=name, type=dtype, values=str(val)) return Dataset(parent_node=None, name=name, type=dtype, values=val)
def add_rotation( self, axis: QVector3D, angle: float, name: str = None, depends_on: Transformation = None, values: Dataset = Dataset( parent_node=None, name="", values=0, type=ValueTypes.DOUBLE, ), target_pos: int = -1, ) -> Transformation: """ Note, currently assumes angle is in degrees :param axis: axis :param angle: :param name: Name of the rotation group (Optional) :param depends_on: existing transformation which the new one depends on (otherwise relative to origin) :param values: The translation distance information. :param target_pos: Target position. """ values.parent_node = self return self._create_and_add_transform( name, TransformationType.ROTATION, angle, "degrees", axis, depends_on, values, target_pos, )
def test_dataset_as_dict_contains_expected_keys(): input_name = "test_dataset" test_dataset = Dataset( parent_node=None, name=input_name, type=ValueTypes.STRING, values="the_value" ) dictionary_output = test_dataset.as_dict([]) for expected_key in ("module", "config"): assert expected_key in dictionary_output.keys() assert dictionary_output["config"]["name"] == input_name
def test_GIVEN_attribute_info_WHEN_adding_attributes_THEN_attribute_object_is_created(): key = "units" value = "m" dataset = Dataset(parent_node=None, name="ds", values=123, type=ValueTypes.INT) attributes = Attributes() attributes.set_attribute_value(key, value) dataset.attributes = attributes assert len(dataset.attributes) == 1 assert isinstance(dataset.attributes[0], FieldAttribute) assert dataset.attributes[0].name == key assert dataset.attributes[0].values == value
def test_GIVEN_attributes_info_WHEN_adding_attributes_THEN_attribute_objects_are_created(): key1 = "units" val1 = "m" key2 = "testkey" val2 = "testval" dataset = Dataset(parent_node=None, name="ds", values=123, type=ValueTypes.INT) attributes = Attributes() attributes.set_attribute_value(key1, val1) attributes.set_attribute_value(key2, val2) dataset.attributes = attributes assert len(dataset.attributes) == 2 assert dataset.attributes[0].name == key1 assert dataset.attributes[0].values == val1 assert dataset.attributes[1].name == key2 assert dataset.attributes[1].values == val2
def test_can_set_transform_properties(): initial_name = "slartibartfast" transform = create_transform(initial_name) test_name = "beeblebrox" test_ui_value = 34.0 test_vector = QVector3D(0.0, 0.0, 1.0) test_type = "rotation" test_values = Dataset(parent_node=None, name="valuedataset", values=None) transform.name = test_name transform.ui_value = test_ui_value transform.vector = test_vector transform.transform_type = test_type transform.values = test_values assert ( transform.name == test_name ), "Expected the transform name to match what was in the NeXus file" assert ( transform.ui_value == test_ui_value ), "Expected the transform value to match what was in the NeXus file" assert ( transform.vector == test_vector ), "Expected the transform vector to match what was in the NeXus file" assert ( transform.transform_type == test_type ), "Expected the transform type to match what was in the NeXus file" assert ( transform.values == test_values ), "Expected the transform type to match what was in the NeXus file"
def _create_new_transformation(parent_component, transformation_list, transformation_type, target_pos): values = Dataset(parent_node=parent_component, name="", type=ValueTypes.DOUBLE, values="0.0") if transformation_type == TransformationType.TRANSLATION: new_transformation = parent_component.add_translation( name=generate_unique_name(TransformationType.TRANSLATION, transformation_list), vector=QVector3D(0, 0, 1.0), # default to beam direction values=values, target_pos=target_pos, ) elif transformation_type == TransformationType.ROTATION: new_transformation = parent_component.add_rotation( name=generate_unique_name(TransformationType.ROTATION, transformation_list), axis=QVector3D(1.0, 0, 0), angle=0.0, values=values, target_pos=target_pos, ) else: raise ValueError( f"Unknown transformation type: {transformation_type}") return new_transformation
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_can_get_transform_properties(): test_name = "slartibartfast" test_ui_value = 42 test_vector = QVector3D(1.0, 0.0, 0.0) test_type = "translation" test_values = Dataset(parent_node=None, name="test_dataset", values=None) transform = create_transform( name=test_name, vector=test_vector, ui_value=test_ui_value, values=test_values ) assert ( transform.name == test_name ), "Expected the transform name to match what was in the NeXus file" assert ( transform.ui_value == test_ui_value ), "Expected the transform value to match what was in the NeXus file" assert ( transform.vector == test_vector ), "Expected the transform vector to match what was in the NeXus file" assert ( transform.transform_type == test_type ), "Expected the transform type to match what was in the NeXus file" assert ( transform.values == test_values ), "Expected the transform type to match what was in the NeXus file"
def test_as_dict_method_of_transformation_when_values_is_a_dataset(): name = ":: SOME NAME ::" dataset = Dataset(parent_node=None, name="", values=None, type=ValueTypes.DOUBLE) transform = create_transform(name=name, values=dataset) assert transform.values == dataset return_dict = transform.as_dict([]) assert return_dict[CommonKeys.MODULE] == "dataset" assert return_dict[NodeType.CONFIG][CommonKeys.NAME] == name
def test_GIVEN_dataset_with_string_value_WHEN_adding_dataset_THEN_dataset_object_is_created_with_correct_dtype(): name = "description" values = "a description" parent = Group(name="test") ds = Dataset(parent_node=parent, type=ValueTypes.STRING, values=values, name=name) assert ds.name == name assert ds.values == values assert ds.parent_node == parent assert ds.type == ValueTypes.STRING
def value(self) -> Union[FileWriterModule, None]: dtype = self.value_type_combo.currentText() return_object: FileWriterModule if self.field_type == FieldType.scalar_dataset: val = self.value_line_edit.text() return_object = Dataset( parent_node=self._node_parent, name=self.name, type=dtype, values=val, ) elif self.field_type == FieldType.array_dataset: # Squeeze the array so 1D arrays can exist. Should not affect dimensional arrays. array = np.squeeze(self.table_view.model.array) return_object = Dataset( parent_node=self._node_parent, name=self.name, type=dtype, values=array, ) elif self.field_type == FieldType.kafka_stream: return_object = self.streams_widget.get_stream_module( self._node_parent) elif self.field_type == FieldType.link: return_object = Link( parent_node=self._node_parent, name=self.name, source=self.value_line_edit.text(), ) else: logging.error(f"unknown field type: {self.name}") return None if self.field_type != FieldType.link: for name, value, dtype in self.attrs_dialog.get_attrs(): return_object.attributes.set_attribute_value( attribute_name=name, attribute_value=value, attribute_type=dtype, ) if self.units and self.units is not None: return_object.attributes.set_attribute_value( CommonAttrs.UNITS, self.units) return return_object
def test_if_valid_value_entered_then_converting_to_dict_appends_no_error(): transform = create_transform( values=Dataset(parent_node=None, name="", values="123", type="double"), type=ValueTypes.DOUBLE, ) error_collector = [] transform.as_dict(error_collector) assert not error_collector
def _create_user(self, group: Group, user_data: Dict[str, str]) -> Group: group.name = f"user_{user_data['name'].replace(' ', '')}" group.nx_class = NX_USER for name, value in user_data.items(): group.children.append( Dataset( name=name, parent_node=group, type=ValueTypes.STRING, values=value ) ) return group
def create_transformation(trans_type: TransformationType): t = Transformation( parent_node=None, name="transformation", type=ValueTypes.DOUBLE, values=8 ) t.transform_type = trans_type t.vector = QVector3D(1, 0, 0) t.values = Dataset( parent_node=Transformation, name="", values=0, type=ValueTypes.DOUBLE ) t.units = "m" return t
def test_GIVEN_existing_field_with_attr_WHEN_editing_component_THEN_both_field_and_attrs_are_filled_in_correctly( qtbot, attr_val, field_attributes_dialog ): attr_key = "testattr" ds = Dataset(parent_node=None, name="test", type=ValueTypes.STRING, values="") ds.attributes.set_attribute_value(attr_key, attr_val) field_attributes_dialog.fill_existing_attrs(ds) assert len(field_attributes_dialog.get_attrs()) == 1 assert field_attributes_dialog.get_attrs()[0][1] == str(attr_val)
def set_field_value(self, name: str, value: Any, dtype: str, unit: str = ""): self[name] = Dataset(parent_node=self, name=name, type=dtype, values=value) if unit: self[name].attributes.set_attribute_value(CommonAttrs.UNITS, unit)
def test_GIVEN_existing_field_with_attr_which_is_in_excludelist_WHEN_editing_component_THEN_attr_is_not_filled_in( qtbot, field_attributes_dialog ): attr_key = "units" attr_val = "m" ds = Dataset(parent_node=None, name="test", type=ValueTypes.STRING, values="") ds.attributes.set_attribute_value(attr_key, attr_val) field_attributes_dialog.fill_existing_attrs(ds) assert len(field_attributes_dialog.get_attrs()) == 0
def test_GIVEN_dataset_with_array_value_WHEN_adding_dataset_THEN_dataset_object_is_created_with_numpy_array_as_value(): name = "an_array" values = [1.1, 2.2, 3.3, 4.4] dtype = ValueTypes.FLOAT np_array = np.array(values, dtype=VALUE_TYPE_TO_NP[dtype]) parent = Group(name="test") ds = Dataset(parent_node=parent, type=dtype, values=np_array, name=name) assert ds.name == name assert np.array_equal(ds.values, np_array) assert ds.parent_node == parent assert ds.type == dtype
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 create_corresponding_value_dataset(value: Any): name = "" type = _get_human_readable_type(value) if np.isscalar(value): value = str(value) return Dataset( parent_node=None, name=name, type=type, values=value, )
def _create_transformation_dataset(angle_or_magnitude: float, dtype: str, name: str) -> Dataset: """ Creates the transformation dataset using the angle/magnitude, name, and dtype of the transformation. :param angle_or_magnitude: The angle or magnitude. :param dtype: The data type. :param name: The transformation name. :return: A dataset containing the above information. """ return Dataset( parent_node=None, name=name, type=dtype, values=angle_or_magnitude, )
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 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 create_transform( name="test translation", ui_value=42.0, vector=QVector3D(1.0, 0.0, 0.0), type="translation", values=Dataset(parent_node=None, name="", values=None, type=ValueTypes.DOUBLE), units="m", ): translation = Transformation( name=name, parent_node=None, values=values, type=ValueTypes.STRING, parent_component=None, ) translation.vector = vector translation.transform_type = type translation.ui_value = ui_value translation.units = units return translation
def test_GIVEN_empty_attributes_WHEN_adding_attributes_THEN_returns_nothing(): dataset = Dataset(parent_node=None, name="ds", values=123, type=ValueTypes.INT) assert not dataset.attributes
from copy import copy from typing import Any, Dict, List, Tuple from nexus_constructor.common_attrs import NX_USER, USERS_PLACEHOLDER from nexus_constructor.component_type import ENTRY_CLASS_NAME from nexus_constructor.model.group import Group from nexus_constructor.model.module import Dataset from nexus_constructor.model.value_type import ValueTypes NEXUS_TITLE_NAME = "title" TITLE_PLACEHOLDER_VALUE = "$TITLE$" TITLE_PLACEHOLDER = Dataset( parent_node=None, name=NEXUS_TITLE_NAME, values=TITLE_PLACEHOLDER_VALUE, type=ValueTypes.STRING, ) NEXUS_EXP_ID_NAME = "experiment_identifier" EXP_ID_PLACEHOLDER_VALUE = "$EXP_ID$" EXP_ID_PLACEHOLDER = Dataset( parent_node=None, name=NEXUS_EXP_ID_NAME, values=EXP_ID_PLACEHOLDER_VALUE, type=ValueTypes.STRING, ) class Entry(Group): def __init__(self): super().__init__(name="entry", parent_node=None)
import pytest from PySide2.QtGui import QVector3D from nexus_constructor.model.component import Component from nexus_constructor.model.entry import Entry from nexus_constructor.model.module import Dataset from nexus_constructor.model.value_type import ValueTypes values = Dataset( name="scalar_value", type=ValueTypes.DOUBLE, values=90.0, parent_node=None, ) @pytest.fixture def instrument(): return Entry(parent_node=None) @pytest.mark.skip(reason="old model used") 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