Beispiel #1
0
def test_dependents_list_is_created_by_instrument(file):  # noqa: F811
    """
    The dependents list for transforms is stored in the "dependent_of" attribute,
    which is not part of the NeXus standard,
    we therefore cannot rely on it being present and correct in a file we load.
    This test makes sure that instrument generates this information in the wrapped NeXus file it is given.
    """

    # Create minimal test file with some transformations but no "dependent_of" attributes
    entry_group = file.create_group("entry")
    entry_group.attrs["NX_class"] = "NXentry"
    instrument_group = entry_group.create_group("instrument")
    instrument_group.attrs["NX_class"] = "NXinstrument"
    transforms_group = instrument_group.create_group("transformations")
    transforms_group.attrs["NX_class"] = "NXtransformations"
    transform_1 = transforms_group.create_dataset("transform_1", data=42)
    transform_2 = transforms_group.create_dataset("transform_2", data=42)
    transform_3 = transforms_group.create_dataset("transform_3", data=42)
    transform_4 = transforms_group.create_dataset("transform_4", data=42)
    transform_2.attrs["depends_on"] = transform_1.name
    transform_3.attrs["depends_on"] = transform_2.name
    transform_4.attrs["depends_on"] = transform_2.name

    nexus_wrapper = NexusWrapper("test_file_with_transforms")
    nexus_wrapper.load_file(entry_group, file)
    Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)

    transform_1_loaded = Transformation(nexus_wrapper, transform_1)
    assert (
        len(transform_1_loaded.get_dependents()) == 1
    ), "Expected transform 1 to have a registered dependent (transform 2)"

    transform_2_loaded = Transformation(nexus_wrapper, transform_2)
    assert (
        len(transform_2_loaded.get_dependents()) == 2
    ), "Expected transform 2 to have 2 registered dependents (transforms 3 and 4)"
    def remove_transformation(self, transform: Transformation):
        if not self._transform_is_in_this_component(transform):
            raise PermissionError(
                "Transform is not in this component, do not have permission to delete"
            )

        dependents = transform.get_dependents()
        if dependents:
            raise DependencyError(
                f"Cannot delete transformation, it is a dependency of {dependents}"
            )

        # Remove whole transformations group if this is the only transformation in it
        if len(transform._dataset.parent.keys()) == 1:
            self.file.delete_node(transform._dataset.parent)
        # Otherwise just remove the transformation from the group
        else:
            self.file.delete_node(transform._dataset)