Example #1
0
def test_multiple_relative_transform_paths_are_converted_to_absolute_path_in_dependee_of_field(
        file, nexus_wrapper):
    component_name = "component_1"

    component1 = add_component_to_file(nexus_wrapper,
                                       component_name=component_name)
    # make depends_on point to relative transformations group
    component1.group["depends_on"] = "transformations/transform1"

    transformations_group = component1.group.create_group("transformations")

    transform1_name = "transform1"
    transform1_dataset = transformations_group.create_dataset(transform1_name,
                                                              data=1)
    transform1_dataset.attrs[CommonAttrs.VECTOR] = qvector3d_to_numpy_array(
        QVector3D(1, 0, 0))
    transform1_dataset.attrs[
        CommonAttrs.TRANSFORMATION_TYPE] = TransformationType.TRANSLATION

    transform2_name = "transform2"

    # make transform1 depends_on point to relative transform in same directory
    transform1_dataset.attrs["depends_on"] = transform2_name

    transform2_dataset = transformations_group.create_dataset(transform2_name,
                                                              data=2)
    transform2_dataset.attrs[CommonAttrs.VECTOR] = qvector3d_to_numpy_array(
        QVector3D(1, 1, 0))
    transform2_dataset.attrs[
        CommonAttrs.TRANSFORMATION_TYPE] = TransformationType.TRANSLATION

    # make sure the depends_on points to the absolute path of the transform it depends on in the file
    assert (Transformation(
        nexus_wrapper,
        transform1_dataset).depends_on.dataset.name == transform2_dataset.name)
Example #2
0
def test_transform_dependents_depends_on_are_updated_when_transformation_name_is_changed(
    nexus_wrapper, ):

    test_name = "slartibartfast"
    test_value = 42
    test_vector = QVector3D(1.0, 0.0, 0.0)
    test_type = "Translation"

    transform_dataset = _add_transform_to_file(nexus_wrapper, test_name,
                                               test_value, test_vector,
                                               test_type)

    component = nexus_wrapper.create_nx_group("test", "NXaperture",
                                              nexus_wrapper.nexus_file)

    component.create_dataset("depends_on", data=transform_dataset.name)

    transform = Transformation(nexus_wrapper, transform_dataset)
    transform.register_dependent(Component(nexus_wrapper, component))

    new_name = test_name + "1"

    transform.name = new_name

    assert transform.name == new_name
    assert str(component["depends_on"][()],
               encoding="UTF-8") == transform.dataset.name
Example #3
0
def test_can_get_transform_properties(nexus_wrapper):

    test_name = "slartibartfast"
    test_value = 42
    test_vector = QVector3D(1.0, 0.0, 0.0)
    test_type = "Translation"

    transform_dataset = _add_transform_to_file(nexus_wrapper, test_name,
                                               test_value, test_vector,
                                               test_type)

    transform = Transformation(nexus_wrapper, transform_dataset)

    assert (
        transform.name == test_name
    ), "Expected the transform name to match what was in the NeXus file"
    assert (
        transform.ui_value == test_value
    ), "Expected the transform value to match what was in the NeXus file"
    assert (
        transform.vector == test_vector
    ), "Expected the transform vector to match what was in the NeXus file"
    assert (
        transform.type == test_type
    ), "Expected the transform type to match what was in the NeXus file"
Example #4
0
def test_can_get_rotation_as_4_by_4_matrix(nexus_wrapper):

    test_value = 45.0  # degrees
    test_vector = QVector3D(0.0, 1.0, 0.0)  # around y-axis
    test_type = "Rotation"
    dataset = _add_transform_to_file(nexus_wrapper, "test_transform",
                                     test_value, test_vector, test_type)
    transformation = Transformation(nexus_wrapper, dataset)

    test_matrix = transformation.qmatrix
    # for a rotation around the y-axis:
    test_value_radians = np.deg2rad(test_value)
    expected_matrix = np.array((
        np.cos(-test_value_radians),
        0,
        np.sin(-test_value_radians),
        0,
        0,
        1,
        0,
        0,
        -np.sin(-test_value_radians),
        0,
        np.cos(-test_value_radians),
        0,
        0,
        0,
        0,
        1,
    ))
    assert np.allclose(expected_matrix,
                       np.array(test_matrix.data()),
                       atol=1.0e-7)
Example #5
0
def test_can_set_transform_properties(nexus_wrapper):

    initial_name = "slartibartfast"

    transform = create_transform(nexus_wrapper, initial_name)

    test_name = "beeblebrox"
    test_value = 34.0
    test_vector = QVector3D(0.0, 0.0, 1.0)
    test_type = "Rotation"

    transform.name = test_name
    transform.value = test_value
    transform.vector = test_vector
    transform.type = test_type

    assert (
        transform.name == test_name
    ), "Expected the transform name to match what was in the NeXus file"
    assert (
        transform.value == test_value
    ), "Expected the transform value to match what was in the NeXus file"
    assert (
        transform.vector == test_vector
    ), "Expected the transform vector to match what was in the NeXus file"
    assert (
        transform.type == test_type
    ), "Expected the transform type to match what was in the NeXus file"
Example #6
0
def create_transform(nexus_file, name):
    initial_value = 42
    initial_vector = QVector3D(1.0, 0.0, 0.0)
    initial_type = "Translation"
    dataset = _add_transform_to_file(nexus_file, name, initial_value,
                                     initial_vector, initial_type)
    return Transformation(nexus_file, dataset)
Example #7
0
def test_transform_type_is_capitalised(test_input, nexus_wrapper):
    test_name = "slartibartfast"
    test_value = 42
    test_vector = QVector3D(1.0, 0.0, 0.0)
    transform_dataset = _add_transform_to_file(nexus_wrapper, test_name,
                                               test_value, test_vector,
                                               test_input)
    transform = Transformation(nexus_wrapper, transform_dataset)
    assert transform.type == "Translation"
Example #8
0
def test_can_get_translation_as_4_by_4_matrix(nexus_wrapper):

    test_value = 42.0
    # Note, it should not matter if this is not set to a unit vector
    test_vector = QVector3D(2.0, 0.0, 0.0)
    test_type = "Translation"
    dataset = _add_transform_to_file(nexus_wrapper, "test_transform",
                                     test_value, test_vector, test_type)
    transformation = Transformation(nexus_wrapper, dataset)

    test_matrix = transformation.qmatrix
    expected_matrix = np.array(
        (1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, test_value, 0, 0, 1))
    assert np.allclose(expected_matrix, np.array(test_matrix.data()))
Example #9
0
def test_ui_value_for_transform_with_array_magnitude_of_strings_returns_zero(
    nexus_wrapper, ):
    transform_name = "transform1"
    array = ["a1", "b1", "c1"]
    transform_value = np.asarray(array, dtype=h5py.special_dtype(vlen=str))

    transform_dataset = _add_transform_to_file(
        nexus_wrapper,
        transform_name,
        transform_value,
        QVector3D(1, 0, 0),
        TransformationType.TRANSLATION,
    )

    transformation = Transformation(nexus_wrapper, transform_dataset)
    assert transformation.ui_value == 0
Example #10
0
def test_ui_value_for_transform_with_array_magnitude_returns_first_value(
        nexus_wrapper):
    transform_name = "transform1"
    array = [1.1, 2.2, 3.3]
    transform_value = np.asarray(array, dtype=float)

    transform_dataset = _add_transform_to_file(
        nexus_wrapper,
        transform_name,
        transform_value,
        QVector3D(1, 0, 0),
        TransformationType.TRANSLATION,
    )

    transformation = Transformation(nexus_wrapper, transform_dataset)
    assert transformation.ui_value == array[0]
Example #11
0
def test_transforms_with_no_dependees_return_None_for_depends_on(
        file, nexus_wrapper):
    component_name = "component_1"

    component1 = add_component_to_file(nexus_wrapper,
                                       component_name=component_name)
    # make depends_on point to relative transformations group
    component1.group["depends_on"] = "transformations/transform1"

    transformations_group = component1.group.create_group("transformations")

    transform1_name = "transform1"
    transform1_dataset = transformations_group.create_dataset(transform1_name,
                                                              data=1)
    transform1_dataset.attrs[CommonAttrs.VECTOR] = qvector3d_to_numpy_array(
        QVector3D(1, 0, 0))
    transform1_dataset.attrs[
        CommonAttrs.TRANSFORMATION_TYPE] = TransformationType.TRANSLATION

    transform1_dataset.attrs["depends_on"] = "."
    transformation = Transformation(nexus_wrapper, transform1_dataset)

    assert not transformation.depends_on