Skip to content

Commit

Permalink
Include unit tests for inverse distortion
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Singer committed Nov 17, 2023
1 parent 5e53d43 commit e6bf943
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions conda.recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ requirements:
- numpy >=1.20 # [build_platform != target_platform]
# Numba is only here to make sure we use a version of numpy that is compatible
- numba # [build_platform != target_platform]
- pybind11 # [build_platform != target_platform]
host:
- python {{ python }}
- numpy >=1.20
Expand Down
11 changes: 8 additions & 3 deletions hexrd/distortion/ge_41rt.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
"""GE41RT Detector Distortion"""
import numpy as np
from typing import List

from hexrd import constants as cnst
import numpy as np
import numba

from .distortionabc import DistortionABC
from .registry import _RegisterDistortionClass
from .utils import newton

from hexrd import constants as cnst
from hexrd.extensions import inverse_distortion

RHO_MAX = 204.8 # max radius in mm for ge detector

# NOTE: Deprecated in favor of inverse_distortion.ge_41rt_inverse_distortion
@numba.njit(nogil=True, cache=True, fastmath=True)
def _ge_41rt_inverse_distortion(inputs, rhoMax, params):
def _ge_41rt_inverse_distortion(inputs: np.ndarray[np.float64, np.float64],
rhoMax: float,
params: List[float]):
radii = np.hypot(inputs[:, 0], inputs[:, 1])
inverted_radii = np.reciprocal(radii)
cosines = inputs[:, 0]*inverted_radii
Expand Down
27 changes: 21 additions & 6 deletions tests/test_inverse_distortion.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import pickle

import numpy as np
import sys
sys.path.append('..')
from hexrd.distortion.ge_41rt import _ge_41rt_inverse_distortion
from hexrd.extensions import inverse_distortion

RHO_MAX = 204.8
params = [-2.277777438488093e-05, -8.763805995117837e-05, -0.00047451698761967085]
Expand Down Expand Up @@ -65,22 +67,35 @@
def test_known_values():
xy_in = np.array([[140.40087891, 117.74253845]])
expected_output = np.array([[140.44540352, 117.77987754]])
xy_out = _ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
xy_out = inverse_distortion.ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
assert np.allclose(xy_out, expected_output)

def test_big_input():
xy_out = _ge_41rt_inverse_distortion(big_test_in, RHO_MAX, params)
xy_out = inverse_distortion.ge_41rt_inverse_distortion(big_test_in, RHO_MAX, params)
assert np.allclose(xy_out, big_test_out)

def test_large_input():
xy_in = np.array([[1e5, 1e5]])
xy_out = _ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
xy_out = inverse_distortion.ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
# No specific expected output here, just ensure it doesn't fail
assert xy_out.shape == xy_in.shape

def test_logged_data():
# Load logged data
with open('data/inverse_distortion_in_out.pkl', 'rb') as f:
logged_data = pickle.load(f)

logged_inputs = logged_data['inputs']
logged_outputs = logged_data['outputs']
logged_params = logged_data['params']

for xy_in, xy_out_expected, params in zip(logged_inputs, logged_outputs, logged_params):
xy_out = inverse_distortion.ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
assert np.allclose(xy_out, xy_out_expected, atol=1e-6)

def test_random_values():
np.random.seed(42) # Set seed for reproducibility
np.random.seed(42)
xy_in = np.random.rand(10, 2) * 200
xy_out = _ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
xy_out = inverse_distortion.ge_41rt_inverse_distortion(xy_in, RHO_MAX, params)
# Verify function does not raise any exception
assert xy_out.shape == xy_in.shape

0 comments on commit e6bf943

Please sign in to comment.