def _set_transformation_values(self): if isinstance(self.values, StreamModule): nx_log = Group(name=self.name, parent_node=self) # type: ignore nx_log.nx_class = LOG_CLASS_NAME self.values.parent_node = nx_log nx_log.children.append(self.values) self.values = nx_log
def get_transforms_group(self) -> Group: if self[TRANSFORMS_GROUP_NAME] is not None: return self[TRANSFORMS_GROUP_NAME] self[TRANSFORMS_GROUP_NAME] = Group(TRANSFORMS_GROUP_NAME, parent_node=self) self[TRANSFORMS_GROUP_NAME].nx_class = "NXtransformations" return self[TRANSFORMS_GROUP_NAME]
def test_get_flags_transformation_list(): test_component_tree_model, test_instrument = create_component_tree_model( [get_component()]) transformation_group = Group(name="transformations") transformation_group.nx_class = NX_TRANSFORMATIONS index = test_component_tree_model.createIndex(0, 0, transformation_group) assert (test_component_tree_model.flags(index) == Qt.ItemIsEnabled | Qt.ItemIsSelectable)
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 test_group_as_dict_contains_expected_keys(): input_name = "test_group" test_group = Group("test_group") dictionary_output = test_group.as_dict([]) for expected_key in ( "name", "type", "children", ): # Attributes should not be included by default if there are none, unlike children assert expected_key in dictionary_output.keys() assert dictionary_output["name"] == input_name
def __init__( self, length: float, width: float, height: float, name: str = "", units: str = "m", ): Group.__init__(self, name) self._size = [length, width, height] self._units = units self.nx_class = GEOMETRY_NX_CLASS self._create_datasets_and_add_to_shape_group()
def __init__( self, vertices: List[QVector3D] = None, faces: List[List[int]] = None, name: str = "", colors: List[List[int]] = [], ): """ :param vertices: list of Vector objects used as corners of polygons in the geometry :param faces: list of integer lists. Each sublist is a winding path around the corners of a polygon. Each sublist item is an index into the vertices list to identify a specific point in 3D space """ Group.__init__(self, name) OFFGeometry.__init__(self) self.name = name self._vertices = vertices self._faces = faces self._colors = colors
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 _create_datasets_and_add_to_shape_group(self): group = Group(name=SHAPE_GROUP_NAME) group.nx_class = SHAPE_NX_CLASS group.parent_node = self new_child = create_fw_module_object( WriterModules.DATASET.value, self._get_dataset_config(self._size, SIZE), group, ) new_child.type = ValueTypes.DOUBLE attributes = Attributes() attributes.set_attribute_value(CommonAttrs.UNITS, self._units) new_child.attributes = attributes new_child.parent_node = group group.children.append(new_child) group[SHAPE_GROUP_NAME] = create_fw_module_object( WriterModules.DATASET.value, self._get_dataset_config(NX_BOX, SHAPE_GROUP_NAME), group, ) self[SHAPE_GROUP_NAME] = group
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 test_get_absolute_path_works_if_component_with_parents(): name1 = "thing1" node1 = Group(name=name1, parent_node=None) name2 = "thing2" node2 = Group(name=name2, parent_node=node1) assert node2.absolute_path == f"/{name1}/{name2}"
def test_get_absolute_path_works_with_no_parent(): name = "test" node = Group(name=name, parent_node=None) assert node.absolute_path == f"/{name}"
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_get_field_value_throws_if_field_does_not_exist(): group = Group("test_group") with pytest.raises(AttributeError): group.get_field_value("nonexistentfield")
def _read_json_object(self, json_object: Dict, parent_node: Group = None): """ Tries to create a component based on the contents of the JSON file. :param json_object: A component from the JSON dictionary. :param parent_name: The name of the parent object. Used for warning messages if something goes wrong. """ nexus_object: Union[Group, FileWriterModule] = None use_placeholder = False if isinstance(json_object, str) and json_object in PLACEHOLDER_WITH_NX_CLASSES: json_object = self._replace_placeholder(json_object) if not json_object: return use_placeholder = True if (CommonKeys.TYPE in json_object and json_object[CommonKeys.TYPE] == NodeType.GROUP): try: name = json_object[CommonKeys.NAME] except KeyError: self._add_object_warning(CommonKeys.NAME, parent_node) return None nx_class = _find_nx_class(json_object.get(CommonKeys.ATTRIBUTES)) if nx_class == SAMPLE_CLASS_NAME: self.sample_name = name if not self._validate_nx_class(name, nx_class): self._add_object_warning(f"valid Nexus class {nx_class}", parent_node) if nx_class in COMPONENT_TYPES: nexus_object = Component(name=name, parent_node=parent_node) children_dict = json_object[CommonKeys.CHILDREN] self._add_transform_and_shape_to_component( nexus_object, children_dict) self.model.append_component(nexus_object) else: nexus_object = Group(name=name, parent_node=parent_node) nexus_object.nx_class = nx_class if CommonKeys.CHILDREN in json_object: for child in json_object[CommonKeys.CHILDREN]: node = self._read_json_object(child, nexus_object) if node and isinstance(node, StreamModule): nexus_object.children.append(node) elif node and node.name not in nexus_object: nexus_object[node.name] = node elif CommonKeys.MODULE in json_object and NodeType.CONFIG in json_object: module_type = json_object[CommonKeys.MODULE] if (module_type == WriterModules.DATASET.value and json_object[NodeType.CONFIG][CommonKeys.NAME] == CommonAttrs.DEPENDS_ON): nexus_object = None elif module_type in [x.value for x in WriterModules]: nexus_object = create_fw_module_object( module_type, json_object[NodeType.CONFIG], parent_node) nexus_object.parent_node = parent_node else: self._add_object_warning("valid module type", parent_node) return None elif json_object == USERS_PLACEHOLDER: self.model.entry.users_placeholder = True return None else: self._add_object_warning( f"valid {CommonKeys.TYPE} or {CommonKeys.MODULE}", parent_node) # Add attributes to nexus_object. if nexus_object: json_attrs = json_object.get(CommonKeys.ATTRIBUTES) if json_attrs: attributes = Attributes() for json_attr in json_attrs: if not json_attr[CommonKeys.VALUES]: self._add_object_warning( f"values in attribute {json_attr[CommonKeys.NAME]}", parent_node, ) elif CommonKeys.DATA_TYPE in json_attr: attributes.set_attribute_value( json_attr[CommonKeys.NAME], json_attr[CommonKeys.VALUES], json_attr[CommonKeys.DATA_TYPE], ) elif CommonKeys.NAME in json_attr: attributes.set_attribute_value( json_attr[CommonKeys.NAME], json_attr[CommonKeys.VALUES]) nexus_object.attributes = attributes if (parent_node and isinstance(nexus_object, Dataset) and parent_node.nx_class == ENTRY_CLASS_NAME): self.model.entry[nexus_object.name] = nexus_object if isinstance(nexus_object, Group) and not nexus_object.nx_class: self._add_object_warning( f"valid {CommonAttrs.NX_CLASS}", parent_node, ) elif isinstance(nexus_object, Group) and nexus_object.nx_class == "NXuser": self.model.entry[nexus_object.name] = nexus_object if isinstance(nexus_object, Group): nexus_object.group_placeholder = use_placeholder return nexus_object
def users(self, users: List[Dict[str, str]]): self._clear_all_users() for user in users: group = Group(name="temporary name", parent_node=self) group = self._create_user(group, user) self[group.name] = group