Example #1
0
def test_GIVEN_stream_group_that_has_ev42_advanced_option_WHEN_filling_in_existing_field_widget_THEN_ev42_group_box_is_shown(
        file, qtbot):
    group = file.create_group("stream2")
    group.attrs["NX_class"] = "NCstream"

    vlen_str = h5py.special_dtype(vlen=str)
    group.create_dataset("writer_module", dtype=vlen_str, data="ev42")
    group.create_dataset("topic", dtype=vlen_str, data="topic1")
    group.create_dataset("source", dtype=vlen_str, data="source1")
    group.create_dataset(ADC_PULSE_DEBUG, dtype=bool, data=True)

    wrapper = NexusWrapper()
    wrapper.load_file(file, file)

    instrument = Instrument(wrapper, {})

    widget = FieldWidget(instrument=instrument)
    qtbot.addWidget(widget)

    update_existing_stream_field(group, widget)

    # this would usually be done outside of the update_existing_stream_field
    widget.name = get_name_of_node(group)

    assert widget.streams_widget.ev42_advanced_group_box.isEnabled()

    generated_group = widget.streams_widget.get_stream_group()
    assert generated_group["writer_module"][()] == group["writer_module"][()]
    assert generated_group["topic"][()] == group["topic"][()]
    assert generated_group["source"][()] == group["source"][()]
    assert generated_group[ADC_PULSE_DEBUG][()] == group[ADC_PULSE_DEBUG][()]
Example #2
0
def test_GIVEN_stream_group_that_has_f142_advanced_option_WHEN_filling_in_existing_field_widget_THEN_f142_group_box_is_shown(
        file, qtbot, nexus_wrapper):
    group = file.create_group("stream1")
    group.attrs["NX_class"] = "NCstream"

    vlen_str = h5py.special_dtype(vlen=str)
    group.create_dataset("writer_module", dtype=vlen_str, data="f142")
    group.create_dataset("type", dtype=vlen_str, data="byte")
    group.create_dataset("topic", dtype=vlen_str, data="topic1")
    group.create_dataset("source", dtype=vlen_str, data="source1")
    group.create_dataset(NEXUS_INDICES_INDEX_EVERY_MB, dtype=int, data=1)

    wrapper = nexus_wrapper
    wrapper.load_file(file, file)

    instrument = Instrument(wrapper, {})

    widget = FieldWidget(instrument=instrument)
    qtbot.addWidget(widget)

    update_existing_stream_field(group, widget)

    # this would usually be done outside of the update_existing_stream_field
    widget.name = get_name_of_node(group)

    assert widget.streams_widget.f142_advanced_group_box.isEnabled()

    generated_group = widget.streams_widget.get_stream_group()
    assert generated_group["writer_module"][()] == group["writer_module"][()]
    assert generated_group["topic"][()] == group["topic"][()]
    assert generated_group["type"][()] == group["type"][()]
    assert generated_group["source"][()] == group["source"][()]
    assert (generated_group[NEXUS_INDICES_INDEX_EVERY_MB][(
    )] == group[NEXUS_INDICES_INDEX_EVERY_MB][()])
 def _get_transform(
     self,
     depends_on: str,
     transforms: List[Transformation],
     local_only: bool = False,
 ):
     """
     Recursive function, appends each transform in depends_on chain to transforms list
     :param depends_on: The next depends_on string to find the next transformation in the chain
     :param transforms: The list to populate with transformations
     :param local_only: If True then only add transformations which are stored within this component
     """
     if depends_on is not None and depends_on != ".":
         if (
             transforms
             and depends_on
             == self.file.get_attribute_value(
                 transforms[-1].dataset, CommonAttrs.DEPENDS_ON
             )
             and depends_on
             in [get_name_of_node(x) for x in transforms[-1].dataset.parent.values()]
         ):
             # depends_on is recursive, ie one transformation in this group depends on another transformation in the group, and it is also relative
             transform_dataset = self.file.nexus_file[
                 f"{transforms[-1].dataset.parent.name}/{depends_on}"
             ]
         elif f"{self.group.name}/{depends_on}" in self.file.nexus_file:
             transform_dataset = self.file.nexus_file[
                 f"{self.group.name}/{depends_on}"
             ]
         else:
             transform_dataset = self.file.nexus_file[depends_on]
         if (
             local_only
             and transform_dataset.parent.parent.name != self.absolute_path
         ):
             # We're done, the next transformation is not stored in this component
             return
         new_transform = create_transformation(self.file, transform_dataset)
         new_transform.parent = transforms
         transforms.append(new_transform)
         if (
             self.file.get_attribute_value(transform_dataset, CommonAttrs.DEPENDS_ON)
             is not None
         ):
             self._get_transform(
                 self.file.get_attribute_value(
                     transform_dataset, CommonAttrs.DEPENDS_ON
                 ),
                 transforms,
                 local_only,
             )
 def _handle_link(entry: NexusObject, root: h5py.Group, root_dict: Dict):
     """
     Create link specific fields in the JSON when a softlink is found.
     :param entry: The entry (dataset or group) that is to be linked
     :param root: the group containing the link object
     :param root_dict: the output dictionary for the JSON writer
     """
     root_dict["children"].append(
         {
             "type": "link",
             "name": get_name_of_node(entry),
             "target": root.get(name=entry.name, getlink=True).path,
         }
     )
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_dataset(root: Union[h5py.Dataset, h5py.SoftLink]):
        """
        Generate JSON dict for a h5py dataset.
        :param root: h5py dataset to generate dict from.
        :return: generated dictionary of dataset values and attrs.
        """
        data, dataset_type, size = get_data_and_type(root)

        root_dict = {
            "type": "dataset",
            "name": get_name_of_node(root),
            "dataset": {"type": dataset_type},
            "values": data,
        }
        if size != 1:
            root_dict["dataset"]["size"] = size

        return root_dict
    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
Example #8
0
def find_field_type(item):
    if (
        isinstance(item, h5py.Dataset)
        and get_name_of_node(item) not in INVALID_FIELD_NAMES
    ):
        if np.isscalar(item[()]):
            return item, update_existing_scalar_field
        else:
            return item, update_existing_array_field

    elif isinstance(item, h5py.Group):
        if isinstance(item.parent.get(item.name, getlink=True), h5py.SoftLink):
            return item, update_existing_link_field
        elif (
            CommonAttrs.NX_CLASS in item.attrs.keys()
            and item.attrs[CommonAttrs.NX_CLASS] == CommonAttrs.NC_STREAM
        ):
            return item, 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"
    )
    return item, None
    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
Example #10
0
 def name(self):
     return nx.get_name_of_node(self.group)
 def create_new_ui_field(self, field):
     new_ui_field = self.add_field()
     new_ui_field.name = get_name_of_node(field)
     return new_ui_field
Example #12
0
 def name(self):
     return nx.get_name_of_node(self.dataset)