tests.test_geometry

PyDRex: Tests for geometric conversions and projections.

 1"""> PyDRex: Tests for geometric conversions and projections."""
 2
 3import numpy as np
 4from pydrex import geometry as _geo
 5from pydrex import io as _io
 6
 7
 8def test_poles_example(hkl, ref_axes):
 9    """Test poles (directions of crystallographic axes) of example data."""
10    ref_data = np.load(
11        _io.resolve_path(
12            _io.data("outputs")
13            / f"example_CPO_poles_{_io.stringify(hkl)}{ref_axes}.npz"
14        )
15    )
16    resampled_data = np.load(
17        _io.resolve_path(_io.data("outputs") / "example_CPO_resampled.npz")
18    )
19    xvals, yvals, zvals = _geo.poles(
20        resampled_data["orientations"],
21        hkl=hkl,
22        ref_axes=ref_axes,
23    )
24    np.testing.assert_allclose(ref_data["xvals"], xvals, atol=1e-16, rtol=0)
25    np.testing.assert_allclose(ref_data["yvals"], yvals, atol=1e-16, rtol=0)
26    np.testing.assert_allclose(ref_data["zvals"], zvals, atol=1e-16, rtol=0)
27
28
29def test_lambert_equal_area(seed):
30    """Test Lambert equal area projection."""
31    x, y = np.mgrid[-1:1:11j, -1:1:11j]
32    x_flat = [j for i in x for j in i]
33    y_flat = [j for i in y for j in i]
34    # Uniform samples on the unit disk, this is tested in the Shirley doctest example.
35    x_disk, y_disk = _geo.shirley_concentric_squaredisk(x_flat, y_flat)
36    # Project onto the unit sphere by adding z = ± (1 - r).
37    # Then project back onto the disk using Lambert equal-area, should be the same.
38    rng = np.random.default_rng(seed=seed)
39    sign = rng.integers(low=0, high=2, size=len(x_disk))
40    x_laea, y_laea = _geo.lambert_equal_area(
41        x_disk, y_disk, (-1) ** sign * (1 - (x_disk**2 + y_disk**2))
42    )
43    np.testing.assert_allclose(x_disk, x_laea, atol=1e-15, rtol=0)
44    np.testing.assert_allclose(y_disk, y_laea, atol=1e-15, rtol=0)
def test_poles_example(hkl, ref_axes):
 9def test_poles_example(hkl, ref_axes):
10    """Test poles (directions of crystallographic axes) of example data."""
11    ref_data = np.load(
12        _io.resolve_path(
13            _io.data("outputs")
14            / f"example_CPO_poles_{_io.stringify(hkl)}{ref_axes}.npz"
15        )
16    )
17    resampled_data = np.load(
18        _io.resolve_path(_io.data("outputs") / "example_CPO_resampled.npz")
19    )
20    xvals, yvals, zvals = _geo.poles(
21        resampled_data["orientations"],
22        hkl=hkl,
23        ref_axes=ref_axes,
24    )
25    np.testing.assert_allclose(ref_data["xvals"], xvals, atol=1e-16, rtol=0)
26    np.testing.assert_allclose(ref_data["yvals"], yvals, atol=1e-16, rtol=0)
27    np.testing.assert_allclose(ref_data["zvals"], zvals, atol=1e-16, rtol=0)

Test poles (directions of crystallographic axes) of example data.

def test_lambert_equal_area(seed):
30def test_lambert_equal_area(seed):
31    """Test Lambert equal area projection."""
32    x, y = np.mgrid[-1:1:11j, -1:1:11j]
33    x_flat = [j for i in x for j in i]
34    y_flat = [j for i in y for j in i]
35    # Uniform samples on the unit disk, this is tested in the Shirley doctest example.
36    x_disk, y_disk = _geo.shirley_concentric_squaredisk(x_flat, y_flat)
37    # Project onto the unit sphere by adding z = ± (1 - r).
38    # Then project back onto the disk using Lambert equal-area, should be the same.
39    rng = np.random.default_rng(seed=seed)
40    sign = rng.integers(low=0, high=2, size=len(x_disk))
41    x_laea, y_laea = _geo.lambert_equal_area(
42        x_disk, y_disk, (-1) ** sign * (1 - (x_disk**2 + y_disk**2))
43    )
44    np.testing.assert_allclose(x_disk, x_laea, atol=1e-15, rtol=0)
45    np.testing.assert_allclose(y_disk, y_laea, atol=1e-15, rtol=0)

Test Lambert equal area projection.