def test_remove_component(nexus_wrapper):
    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)
    under_test = ComponentTreeModel(instrument)
    instrument.create_component("Some name", "some class", "desc")
    component_index = under_test.index(0, 0, QModelIndex())
    assert under_test.rowCount(QModelIndex()) == 1
    under_test.remove_node(component_index)
    assert under_test.rowCount(QModelIndex()) == 0
def test_remove_component_with_transformation(nexus_wrapper):
    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)
    under_test = ComponentTreeModel(instrument)
    instrument.create_component("Some name", "some class", "desc")
    component_index = under_test.index(0, 0, QModelIndex())
    under_test.add_rotation(component_index)
    assert under_test.rowCount(QModelIndex()) == 1
    under_test.remove_node(component_index)
    assert under_test.rowCount(QModelIndex()) == 0, (
        "Expected component to be successfully deleted because it has "
        "a transformation that only has it as a dependent")
def test_remove_transformation(nexus_wrapper):
    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)
    under_test = ComponentTreeModel(instrument)
    instrument.create_component("Some name", "some class", "desc")
    component_index = under_test.index(0, 0, QModelIndex())
    under_test.add_rotation(component_index)
    transformation_list_index = under_test.index(1, 0, component_index)
    transformation_index = under_test.index(0, 0, transformation_list_index)
    assert under_test.rowCount(transformation_list_index) == 1
    under_test.remove_node(transformation_index)
    assert under_test.rowCount(transformation_list_index) == 0
Example #4
0
def test_remove_link():
    wrapper = NexusWrapper("test_remove_link")
    instrument = Instrument(wrapper, NX_CLASS_DEFINITIONS)
    under_test = ComponentTreeModel(instrument)
    instrument.create_component("Some name", "some class", "desc")
    component_index = under_test.index(0, 0, QModelIndex())
    under_test.add_link(component_index)
    transformation_list_index = under_test.index(1, 0, component_index)
    transformation_index = under_test.index(0, 0, transformation_list_index)
    assert under_test.rowCount(transformation_list_index) == 1
    assert len(transformation_list_index.internalPointer()) == 0
    under_test.remove_node(transformation_index)
    assert under_test.rowCount(transformation_list_index) == 0
def test_GIVEN_component_WHEN_adding_component_THEN_components_list_contains_added_component(
    nexus_wrapper, ):
    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)

    component_type = "NXcrystal"
    name = "test_crystal"
    description = "shiny"
    instrument.create_component(name, component_type, description)

    check_if_component_is_in_component_list(component_type,
                                            description,
                                            instrument,
                                            name,
                                            expect_component_present=True)
def test_UI_GIVEN_array_dataset_as_magnitude_WHEN_creating_translation_THEN_ui_is_filled_correctly(
    qtbot, file  # noqa:F811
):
    wrapper = NexusWrapper()
    instrument = Instrument(wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    array = np.array([1, 2, 3, 4])

    x = 1
    y = 0
    z = 0
    transform = component.add_translation(QVector3D(x, y, z), name="test")

    transform.dataset = file.create_dataset("test", data=array)

    view = EditTranslation(parent=None, transformation=transform, instrument=instrument)
    qtbot.addWidget(view)

    assert view.transformation_frame.x_spinbox.value() == x
    assert view.transformation_frame.y_spinbox.value() == y
    assert view.transformation_frame.z_spinbox.value() == z
    assert np.allclose(view.transformation.dataset[...], array)
    assert (
        view.transformation_frame.magnitude_widget.field_type == FieldType.array_dataset
    )
def test_UI_GIVEN_scalar_angle_WHEN_creating_rotation_view_THEN_ui_is_filled_correctly(
    qtbot,
):
    wrapper = NexusWrapper()
    instrument = Instrument(wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 1
    y = 2
    z = 3
    angle = 90

    transform = component.add_rotation(angle=angle, axis=QVector3D(x, y, z))

    view = EditRotation(parent=None, transformation=transform, instrument=instrument)
    qtbot.addWidget(view)

    assert view.transformation_frame.x_spinbox.value() == x
    assert view.transformation_frame.y_spinbox.value() == y
    assert view.transformation_frame.z_spinbox.value() == z
    assert view.transformation_frame.value_spinbox.value() == angle
    assert view.transformation_frame.magnitude_widget.value[()] == angle
    assert (
        view.transformation_frame.magnitude_widget.field_type
        == FieldType.scalar_dataset
    )
def test_GIVEN_component_with_cylindrical_shape_information_WHEN_duplicating_component_THEN_shape_information_is_stored_in_nexus_file(
    nexus_wrapper, ):

    instrument = Instrument(nexus_wrapper, NX_CLASS_DEFINITIONS)

    first_component_name = "component1"
    first_component_nx_class = "NXdetector"
    description = "desc"
    first_component = instrument.create_component(first_component_name,
                                                  first_component_nx_class,
                                                  description)

    axis_direction = QVector3D(1, 0, 0)
    height = 2
    radius = 3
    units = "cm"
    first_component.set_cylinder_shape(axis_direction=axis_direction,
                                       height=height,
                                       radius=radius,
                                       units=units)
    tree_model = ComponentTreeModel(instrument)

    first_component_index = tree_model.index(0, 0, QModelIndex())
    tree_model.duplicate_node(first_component_index)

    assert tree_model.rowCount(QModelIndex()) == 3
    second_component_index = tree_model.index(2, 0, QModelIndex())
    second_component = second_component_index.internalPointer()
    second_shape, _ = second_component.shape
    assert second_shape.axis_direction == axis_direction
    assert second_shape.height == height
    assert second_shape.units == units
def test_UI_GIVEN_link_as_rotation_magnitude_WHEN_creating_rotation_view_THEN_ui_is_filled_correctly(
    qtbot,
):
    wrapper = NexusWrapper(filename="asdfgsdfh")
    instrument = Instrument(wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 0
    y = 0
    z = 0
    path = "/entry"

    transform = component.add_rotation(QVector3D(x, y, z), 0, name="test")
    link = wrapper.instrument["asdfgh"] = h5py.SoftLink(path)

    transform.dataset = link

    view = EditRotation(parent=None, transformation=transform, instrument=instrument)
    qtbot.addWidget(view)

    assert view.transformation_frame.x_spinbox.value() == x
    assert view.transformation_frame.y_spinbox.value() == y
    assert view.transformation_frame.z_spinbox.value() == z
    assert view.transformation_frame.value_spinbox.value() == 0.0
    assert view.transformation_frame.magnitude_widget.field_type == FieldType.link
    assert view.transformation_frame.magnitude_widget.value.path == path
def test_UI_GIVEN_new_values_are_provided_WHEN_save_changes_is_called_THEN_transformation_changed_signal_is_called_to_update_3d_view(
    qtbot,
):
    wrapper = NexusWrapper()
    instrument = Instrument(wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 1
    y = 2
    z = 3
    angle = 90

    transform = component.add_rotation(angle=angle, axis=QVector3D(x, y, z))

    view = EditRotation(parent=None, transformation=transform, instrument=instrument)
    instrument.nexus.transformation_changed = Mock()
    qtbot.addWidget(view)

    new_x = 4

    view.transformation_frame.x_spinbox.setValue(new_x)
    view.saveChanges()
    instrument.nexus.transformation_changed.emit.assert_called_once()
    assert transform.vector == QVector3D(new_x, y, z)
def test_UI_GIVEN_vector_updated_WHEN_saving_view_changes_THEN_model_is_updated(qtbot):
    wrapper = NexusWrapper()
    instrument = Instrument(wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 1
    y = 2
    z = 3
    angle = 90

    transform = component.add_rotation(angle=angle, axis=QVector3D(x, y, z))

    view = EditRotation(parent=None, transformation=transform, instrument=instrument)
    qtbot.addWidget(view)

    new_x = 4
    new_y = 5
    new_z = 6

    view.transformation_frame.x_spinbox.setValue(new_x)
    view.transformation_frame.y_spinbox.setValue(new_y)
    view.transformation_frame.z_spinbox.setValue(new_z)

    view.saveChanges()

    assert transform.vector == QVector3D(new_x, new_y, new_z)
Example #12
0
def test_GIVEN_instrument_containing_component_WHEN_generating_json_THEN_file_is_written_containing_components(
):
    file = io.StringIO(newline=None)
    wrapper = NexusWrapper("test.nxs")
    data = Instrument(wrapper, NX_CLASS_DEFINITIONS)

    component_name = "pinhole"
    component_nx_class = "NXpinhole"

    dataset_name = "depends_on"
    dataset_value = "something_else"

    component = data.create_component(component_name, component_nx_class, "")
    component.set_field(dataset_name, value=dataset_value, dtype=str)

    write_nexus_structure_to_json(data, file)

    output_file_dict = json.loads(file.getvalue())

    component = output_file_dict["children"][0]["children"][0]["children"][0]

    assert component["name"].lstrip("/entry/instrument/") == component_name
    assert (component["children"][0]["name"].lstrip(
        f"/entry/instrument/{component_name}/") == dataset_name)
    assert component["children"][0]["type"] == "dataset"
    assert component["children"][0]["values"] == dataset_value
    assert component["children"][0]["dataset"]["type"] == "string"
Example #13
0
def test_GIVEN_instrument_with_component_WHEN_component_is_removed_THEN_components_list_does_not_contain_component(
):
    wrapper = NexusWrapper("test_components_list")
    instrument = Instrument(wrapper, NX_CLASS_DEFINITIONS)

    component_type = "NXcrystal"
    name = "test_crystal"
    description = "shiny"
    test_component = instrument.create_component(name, component_type,
                                                 description)

    # Test component should be in list
    check_if_component_is_in_component_list(component_type,
                                            description,
                                            instrument,
                                            name,
                                            expect_component_present=True)

    instrument.remove_component(test_component)

    # Test component should no longer be in list
    check_if_component_is_in_component_list(component_type,
                                            description,
                                            instrument,
                                            name,
                                            expect_component_present=False)
Example #14
0
def test_GIVEN_component_with_off_shape_information_WHEN_duplicating_component_THEN_shape_information_is_stored_in_nexus_file(
):
    wrapper = NexusWrapper("test_duplicate_off_shape")
    instrument = Instrument(wrapper, NX_CLASS_DEFINITIONS)

    first_component_name = "component1"
    first_component_nx_class = "NXdetector"
    description = "desc"
    first_component = instrument.create_component(first_component_name,
                                                  first_component_nx_class,
                                                  description)

    vertices = [
        QVector3D(-0.5, -0.5, 0.5),
        QVector3D(0.5, -0.5, 0.5),
        QVector3D(-0.5, 0.5, 0.5),
        QVector3D(0.5, 0.5, 0.5),
        QVector3D(-0.5, 0.5, -0.5),
        QVector3D(0.5, 0.5, -0.5),
        QVector3D(-0.5, -0.5, -0.5),
        QVector3D(0.5, -0.5, -0.5),
    ]

    faces = [
        [0, 1, 3, 2],
        [2, 3, 5, 4],
        [4, 5, 7, 6],
        [6, 7, 1, 0],
        [1, 7, 5, 3],
        [6, 0, 2, 4],
    ]

    first_component.set_off_shape(
        OFFGeometryNoNexus(vertices=vertices, faces=faces))

    tree_model = ComponentTreeModel(instrument)

    first_component_index = tree_model.index(0, 0, QModelIndex())
    tree_model.duplicate_node(first_component_index)

    assert tree_model.rowCount(QModelIndex()) == 3
    second_component_index = tree_model.index(2, 0, QModelIndex())
    second_component = second_component_index.internalPointer()
    second_shape, _ = second_component.shape

    assert second_shape.vertices == vertices
    assert second_shape.faces == faces
Example #15
0
def test_UI_GIVEN_stream_group_as_angle_WHEN_creating_rotation_THEN_ui_is_filled_correctly(
        qtbot, file, nexus_wrapper):
    instrument = Instrument(nexus_wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 0
    y = 0
    z = 0

    transform = component.add_rotation(QVector3D(x, y, z), 0, name="test")

    stream_group = file.create_group("stream_group")
    stream_group.attrs["NX_class"] = "NCstream"
    topic = "test_topic"
    writer_module = "ev42"
    source = "source1"
    stream_group.create_dataset("topic",
                                dtype=h5py.special_dtype(vlen=str),
                                data=topic)
    stream_group.create_dataset("writer_module", data=writer_module)
    stream_group.create_dataset("source", data=source)

    transform.dataset = stream_group

    view = EditRotation(parent=None,
                        transformation=transform,
                        instrument=instrument)
    qtbot.addWidget(view)

    assert view.transformation_frame.x_spinbox.value() == x
    assert view.transformation_frame.y_spinbox.value() == y
    assert view.transformation_frame.z_spinbox.value() == z
    assert view.transformation_frame.value_spinbox.value() == 0.0
    assert (view.transformation_frame.magnitude_widget.field_type ==
            FieldType.kafka_stream)
    assert view.transformation_frame.magnitude_widget.value["topic"][(
    )] == topic
    assert (view.transformation_frame.magnitude_widget.value["writer_module"][(
    )] == writer_module)
    assert view.transformation_frame.magnitude_widget.value["source"][(
    )] == source
def test_UI_GIVEN_view_loses_focus_WHEN_transformation_view_exists_THEN_spinboxes_are_disabled(
    qtbot,
):
    wrapper = NexusWrapper()
    instrument = Instrument(wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 1
    y = 2
    z = 3
    angle = 90

    transform = component.add_rotation(angle=angle, axis=QVector3D(x, y, z))

    view = EditRotation(parent=None, transformation=transform, instrument=instrument)
    qtbot.addWidget(view)

    view.disable()

    assert not view.transformation_frame.x_spinbox.isEnabled()
    assert not view.transformation_frame.y_spinbox.isEnabled()
    assert not view.transformation_frame.z_spinbox.isEnabled()
    assert not view.transformation_frame.name_line_edit.isEnabled()
Example #17
0
def test_UI_GIVEN_scalar_vector_WHEN_creating_translation_view_THEN_ui_is_filled_correctly(
        qtbot, nexus_wrapper):

    instrument = Instrument(nexus_wrapper, {})

    component = instrument.create_component("test", "NXaperture", "")

    x = 1
    y = 0
    z = 0
    transform = component.add_translation(QVector3D(x, y, z), name="test")

    view = EditTranslation(parent=None,
                           transformation=transform,
                           instrument=instrument)
    qtbot.addWidget(view)

    assert view.transformation_frame.x_spinbox.value() == x
    assert view.transformation_frame.y_spinbox.value() == y
    assert view.transformation_frame.z_spinbox.value() == z
    assert view.transformation_frame.value_spinbox.value() == 1
    assert view.transformation_frame.magnitude_widget.value[()] == 1
    assert (view.transformation_frame.magnitude_widget.field_type ==
            FieldType.scalar_dataset)