Пример #1
0
def test_GIVEN_group_with_nx_class_as_str_WHEN_getting_nx_class_THEN_returns_nx_class_as_str(
    file, ):

    entry = file.create_group("entry")
    nx_class = "NXentry"
    entry.attrs["NX_class"] = nx_class
    assert get_nx_class(entry) == nx_class
Пример #2
0
def test_GIVEN_group_with_nx_class_as_bytes_WHEN_getting_nx_class_THEN_returns_nx_class_as_str(
):
    with InMemoryFile("test_file") as file:
        entry = file.create_group("entry")
        nx_class = b"NXentry"
        entry.attrs["NX_class"] = nx_class
        assert get_nx_class(entry) == str(nx_class, encoding="utf-8")
 def find_streams(_, node):
     nonlocal pv_names
     nonlocal stream_list
     if (isinstance(node, h5py.Group)
             and get_nx_class(node) == CommonAttrs.NC_STREAM
             and node["writer_module"][()] in FORWARDER_SCHEMAS):
         writer_module = node["writer_module"][()]
         pv_name = node["source"][()]
         if pv_name not in pv_names.keys():
             stream_list.append({
                 "channel":
                 pv_name,
                 "converter":
                 get_converter(node, writer_module),
                 "channel_provider_type":
                 provider_type,
             })
             pv_names[pv_name] = len(stream_list)
         else:
             duplicated_stream = stream_list[pv_names[pv_name] - 1]
             new_stream_converter = get_converter(node, writer_module)
             if isinstance(duplicated_stream["converter"], list):
                 duplicated_stream["converter"].append(new_stream_converter)
             elif isinstance(duplicated_stream["converter"], dict):
                 duplicated_stream["converter"] = [
                     duplicated_stream["converter"],
                     new_stream_converter,
                 ]
Пример #4
0
def create_transformation(wrapper: nx.NexusWrapper,
                          node: h5Node) -> Transformation:
    """
    Factory for creating different types of transform.
    If it is an NXlog group then the magnitude and units fields will be different to a normal transformation dataset.
    """
    if get_nx_class(node) == "NXlog":
        return NXLogTransformation(wrapper, node)
    return Transformation(wrapper, node)
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)
Пример #6
0
def _check_nx_class(group: h5py.Group, nx_class: str, problems: List):

    group_nx_class = get_nx_class(group)

    if not group_nx_class:
        problems.append(f"Expected {group.name} to have an NX_class attribute")
    elif group_nx_class != nx_class:
        problems.append(
            f"Expected {group.name} to have NX_class attribute of {nx_class} but it was {group.attrs['NX_class']}"
        )
Пример #7
0
 def refresh_depends_on(_, node):
     """
     Refresh the depends_on attribute of each transformation, which also results in registering dependents
     """
     if isinstance(node, h5py.Group):
         if get_nx_class(node) == "NXtransformations":
             for transformation_name, transformation_node in node.items(
             ):
                 transform = create_transformation(
                     self.nexus, node[transformation_name])
                 transform.depends_on = transform.depends_on
def get_shape_from_component(
    component_group: h5py.Group, nexus_file: nx.NexusWrapper,
    shape_group_name: str
) -> Union[OFFGeometry, CylindricalGeometry, NoShapeGeometry]:
    if shape_group_name in component_group:
        shape_group = component_group[shape_group_name]
        nx_class = get_nx_class(shape_group)
        if nx_class == CYLINDRICAL_GEOMETRY_NEXUS_NAME:
            return CylindricalGeometry(nexus_file, shape_group)
        if nx_class == OFF_GEOMETRY_NEXUS_NAME:
            return OFFGeometryNexus(nexus_file, shape_group)

    # else return a placeholder to indicate the component's position
    return NoShapeGeometry()
Пример #9
0
def find_field_type(item: h5Node) -> Callable:
    if (
        isinstance(item, h5py.Dataset)
        and get_name_of_node(item) not in INVALID_FIELD_NAMES
    ):
        if np.isscalar(item[()]):
            return update_existing_scalar_field
        else:
            return update_existing_array_field

    elif isinstance(item, h5py.Group):
        if isinstance(item.parent.get(item.name, getlink=True), h5py.SoftLink):
            return update_existing_link_field
        elif get_nx_class(item) == CommonAttrs.NC_STREAM:
            return update_existing_stream_field
    logging.debug(
        f"Object {get_name_of_node(item)} not handled as field - could be used for other parts of UI instead"
    )
    def _handle_group(self, root: h5py.Group):
        """
        Generate JSON dict for a h5py group.
        :param root: h5py group to generate dict from.
        :return: generated dict of group and children.
        """
        root_dict = {"type": "group", "name": get_name_of_node(root), "children": []}
        # Add the entries
        if get_nx_class(root) == CommonAttrs.NC_STREAM:
            self._handle_stream(root, root_dict)
            return root_dict

        for entry in root.values():
            # Check if there are SoftLinks in the group
            if isinstance(root.get(name=entry.name, getlink=True), h5py.SoftLink):
                self._handle_link(entry, root, root_dict)
            root_dict["children"].append(self._root_to_dict(entry))

        return root_dict
Пример #11
0
def test_GIVEN_group_without_nx_class_WHEN_getting_nx_class_THEN_returns_none(
):
    with InMemoryFile("test_file") as file:
        entry = file.create_group("entry")
        assert get_nx_class(entry) is None
Пример #12
0
 def nx_class(self):
     return get_nx_class(self.group)
Пример #13
0
 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))
Пример #14
0
def test_GIVEN_group_without_nx_class_WHEN_getting_nx_class_THEN_returns_none(
        file):
    entry = file.create_group("entry")
    assert get_nx_class(entry) is None
Пример #15
0
 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))