Commit 0434e92b authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Alignment: added test for peak fitting function



Signed-off-by: Nicola Vigano's avatarNicola VIGANÒ <nicola.vigano@esrf.fr>
parent f83f7735
......@@ -5,10 +5,16 @@ import h5py
import scipy.ndimage
from silx.third_party.EdfFile import EdfFile
from nabu.preproc.alignment import CenterOfRotation, DetectorTranslationAlongBeam
from nabu.preproc.alignment import CenterOfRotation, DetectorTranslationAlongBeam, AlignmentBase
from nabu.testutils import utilstest
@pytest.fixture(scope="class")
def bootstrap_base(request):
cls = request.cls
cls.abs_tol = 2.5e-2
@pytest.fixture(scope="class")
def bootstrap_cor(request):
cls = request.cls
......@@ -45,6 +51,46 @@ def get_data_h5(*dataset_path):
return data, px
@pytest.mark.usefixtures("bootstrap_base")
class TestAlignmentBase(object):
def test_peak_fitting_2d_3x3(self):
# Fit a 3 x 3 grid
fy = np.linspace(-1, 1, 3)
fx = np.linspace(-1, 1, 3)
yy, xx = np.meshgrid(fy, fx, indexing="ij")
peak_pos_yx = np.random.rand(2) * 1.6 - 0.8
f_vals = np.exp(-((yy - peak_pos_yx[0]) ** 2 + (xx - peak_pos_yx[1]) ** 2) / 100)
fitted_peak_pos_yx = AlignmentBase.refine_max_position_2d(f_vals, fy, fx)
message = (
"Computed peak position: (%f, %f) " % (*fitted_peak_pos_yx,)
+ " and real peak position (%f, %f) do not coincide." % (*peak_pos_yx,)
+ " Difference: (%f, %f)," % (*(fitted_peak_pos_yx - peak_pos_yx),)
+ " tolerance: %f" % self.abs_tol
)
assert np.all(np.isclose(peak_pos_yx, fitted_peak_pos_yx, atol=self.abs_tol)), message
def test_peak_fitting_2d_error_checking(self):
# Fit a 3 x 3 grid
fy = np.linspace(-1, 1, 3)
fx = np.linspace(-1, 1, 3)
yy, xx = np.meshgrid(fy, fx, indexing="ij")
peak_pos_yx = np.random.rand(2) + 1.5
f_vals = np.exp(-((yy - peak_pos_yx[0]) ** 2 + (xx - peak_pos_yx[1]) ** 2) / 100)
with pytest.raises(ValueError) as ex:
AlignmentBase.refine_max_position_2d(f_vals, fy, fx)
message = (
"Error should have been raised about the peak being fitted outside margins, "
+ "other error raised instead:\n%s" % str(ex.value)
)
assert "positions are outide the margins of input" in str(ex.value), message
@pytest.mark.usefixtures("bootstrap_cor")
class TestCor(object):
def test_cor_posx(self):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment