pyxem / pyxem

An open-source Python library for multi-dimensional diffraction microscopy.
https://pyxem.readthedocs.io
GNU General Public License v3.0
148 stars 85 forks source link

`test_results_dict_to_crystal_map` test failure with mkl #960

Closed ericpre closed 12 months ago

ericpre commented 1 year ago

From https://github.com/ericpre/hyperspy-bundle/actions/runs/6761260297/job/18375961006

test_results_dict_to_crystal_map fails with mkl but not with openblas.

=================================== FAILURES ===================================
_______________________ test_results_dict_to_crystal_map _______________________

test_library_phases_multi = <diffsims.libraries.structure_library.StructureLibrary object at 0x7f8cb5f2c050>
test_lib_gen = <diffsims.generators.library_generator.DiffractionLibraryGenerator object at 0x7f8cb5f2d9d0>

    @pytest.mark.filterwarnings("ignore:Property 'correlation' was expected")
    def test_results_dict_to_crystal_map(test_library_phases_multi, test_lib_gen):
        """Test getting a :class:`orix.crystal_map.CrystalMap` from returns
        from :func:`index_dataset_with_template_rotation`.
        """
        # Map and signal shapes
        nav_shape = (2, 3)
        sig_shape = (80, 80)
        test_set = np.zeros(nav_shape + sig_shape)

        # Diffraction conditions
        diff_lib = test_lib_gen.get_diffraction_library(
            test_library_phases_multi,
            calibration=0.015,
            reciprocal_radius=1.18,
            half_shape=tuple(np.array(sig_shape) // 2),
            with_direct_beam=False,
        )

        # Simulate multi-phase results
        phase_id = np.zeros(nav_shape, dtype=int)
        phase_id[:, [0, 2]] = 1
        phase_names = list(diff_lib.keys())

        # Simulate patterns
        # TODO: Remove version check after diffsims 0.5.0 is released
        from packaging.version import Version
        import diffsims
        from scipy.ndimage import rotate

        diffsims_version = Version(diffsims.__version__)
        if diffsims_version > Version("0.4.2"):
            sim_kwargs = dict(shape=sig_shape, sigma=4)
        else:  # pragma: no cover
            sim_kwargs = dict(size=sig_shape[0], sigma=4)
        for idx in np.ndindex(*nav_shape):
            i = phase_id[idx]
            j = int(idx[1] / 2)
            test_pattern = diff_lib[phase_names[i]]["simulations"][
                j
            ].get_diffraction_pattern(**sim_kwargs)
            test_set[idx] = test_pattern

        test_set[0, 0] = rotate(test_set[0, 0], -30, reshape=False)
        # Perform template matching
        n_best = 3
        results, phase_dict = index_dataset_with_template_rotation(
            Signal2D(test_set),
            diff_lib,
            phases=phase_names,
            n_best=n_best,
        )

        # Extract various results once
        phase_id = results["phase_index"].reshape((-1, n_best))
        ori = np.deg2rad(results["orientation"].reshape((-1, n_best, 3)))
>       assert (
            abs(results["orientation"][0, 0, 0, 0] - 30) < 1
            or abs(results["orientation"][0, 0, 0, 0] - 165) < 1
        )
E       assert (315.0 < 1 or 180.0 < 1)
E        +  where 315.0 = abs((345.0 - 30))
E        +  and   180.0 = abs((345.0 - 165))

/home/runner/work/hyperspy-bundle/hyperspy-bundle/new_distribution/lib/python3.11/site-packages/pyxem/tests/utils/test_indexation_utils.py:128: AssertionError
CSSFrancis commented 1 year ago

@ericpre I think this is just an effect of the symmetry of the diffraction pattern. There is 8? fold symmetry in the diffraction pattern so the acceptable answers are probably some variation of 15/30. It probably comes down to some rounding error... Or at least that is my best guess...