Commit f83f7735 authored by Nicola Vigano's avatar Nicola Vigano
Browse files

Alignment: refactored tests into two distinct classes for two distinct functionalities



Signed-off-by: Nicola Vigano's avatarNicola VIGANÒ <nicola.vigano@esrf.fr>
parent 4c89c345
......@@ -10,16 +10,24 @@ from nabu.testutils import utilstest
@pytest.fixture(scope="class")
def bootstrap(request):
def bootstrap_cor(request):
cls = request.cls
cls.data, cls.px = get_data_h5("tworadios.h5")
cls.cor_tol = 0.2
cls.abs_tol = 0.2
@pytest.fixture(scope="class")
def bootstrap_dtr(request):
cls = request.cls
# loading alignxc edf files from Christian. The last one is the dark. The first 6 are images for translation 0,1...6
transcor_legacydatas = np.array([EdfFile(utilstest.getfile("alignxc%04d.edf" % i)).GetData(0,
DataType="FloatValue") for i in range(7)])
images = np.array([EdfFile(utilstest.getfile("alignxc%04d.edf" % i)).GetData(0, DataType="FloatValue") for i in range(7)])
align_imgs, dark_img = images[:-1], images[-1]
# removing dark
cls.transcor_legacydatas = (transcor_legacydatas[:-1] - transcor_legacydatas[-1])
cls.align_images = align_imgs - dark_img
cls.img_pos = (1 + np.arange(6)) * 0.01
cls.expected_shifts_vh = np.array((2551.25, 308.9))
def get_data_h5(*dataset_path):
......@@ -37,8 +45,8 @@ def get_data_h5(*dataset_path):
return data, px
@pytest.mark.usefixtures("bootstrap")
class TestCor:
@pytest.mark.usefixtures("bootstrap_cor")
class TestCor(object):
def test_cor_posx(self):
radio1 = self.data[0, :, :]
radio2 = np.fliplr(self.data[1, :, :])
......@@ -46,11 +54,8 @@ class TestCor:
CoR_calc = CenterOfRotation()
cor_position = CoR_calc.find_shift(radio1, radio2)
message = (
"Computed CoR %f " % cor_position
+ " and real CoR %f do not coincide" % self.px
)
assert np.abs(self.px - cor_position) < self.cor_tol, message
message = "Computed CoR %f " % cor_position + " and real CoR %f do not coincide" % self.px
assert np.abs(self.px - cor_position) < self.abs_tol, message
def test_noisy_cor_posx(self):
radio1 = np.fmax(self.data[0, :, :], 0)
......@@ -62,11 +67,8 @@ class TestCor:
CoR_calc = CenterOfRotation()
cor_position = CoR_calc.find_shift(radio1, radio2, median_filt_shape=(3, 3))
message = (
"Computed CoR %f " % cor_position
+ " and real CoR %f do not coincide" % self.px
)
assert np.abs(self.px - cor_position) < self.cor_tol, message
message = "Computed CoR %f " % cor_position + " and real CoR %f do not coincide" % self.px
assert np.abs(self.px - cor_position) < self.abs_tol, message
def test_noisyHF_cor_posx(self):
""" test with noise at high frequencies
......@@ -87,28 +89,20 @@ class TestCor:
CoR_calc = CenterOfRotation()
# cor_position = CoR_calc.find_shift(radio1, radio2)
cor_position = CoR_calc.find_shift(
radio1, radio2, low_pass=(6.0, 0.3)
)
cor_position = CoR_calc.find_shift(radio1, radio2, low_pass=(6.0, 0.3))
message = (
"Computed CoR %f " % cor_position
+ " and real CoR %f do not coincide" % self.px
)
assert np.abs(self.px - cor_position) < self.cor_tol, message
message = "Computed CoR %f " % cor_position + " and real CoR %f do not coincide" % self.px
assert np.abs(self.px - cor_position) < self.abs_tol, message
def test_cor_posx_linear(self):
radio1 = self.data[0, :, :]
radio2 = np.fliplr(self.data[1, :, :])
CoR_calc = CenterOfRotation()
cor_position = CoR_calc.find_shift(radio1, radio2, padding_mode='constant')
cor_position = CoR_calc.find_shift(radio1, radio2, padding_mode="constant")
message = (
"Computed CoR %f " % cor_position
+ " and real CoR %f do not coincide" % self.px
)
assert np.abs(self.px - cor_position) < self.cor_tol, message
message = "Computed CoR %f " % cor_position + " and real CoR %f do not coincide" % self.px
assert np.abs(self.px - cor_position) < self.abs_tol, message
def test_error_checking_001(self):
CoR_calc = CenterOfRotation()
......@@ -119,13 +113,8 @@ class TestCor:
with pytest.raises(ValueError) as ex:
CoR_calc.find_shift(radio1, radio2)
message = (
"Error should have been raised about img #1 shape, other error raised instead:\n%s"
% str(ex.value)
)
assert "Images need to be 2-dimensional. Shape of image #1" in str(
ex.value
), message
message = "Error should have been raised about img #1 shape, other error raised instead:\n%s" % str(ex.value)
assert "Images need to be 2-dimensional. Shape of image #1" in str(ex.value), message
def test_error_checking_002(self):
CoR_calc = CenterOfRotation()
......@@ -136,13 +125,8 @@ class TestCor:
with pytest.raises(ValueError) as ex:
CoR_calc.find_shift(radio1, radio2)
message = (
"Error should have been raised about img #2 shape, other error raised instead:\n%s"
% str(ex.value)
)
assert "Images need to be 2-dimensional. Shape of image #2" in str(
ex.value
), message
message = "Error should have been raised about img #2 shape, other error raised instead:\n%s" % str(ex.value)
assert "Images need to be 2-dimensional. Shape of image #2" in str(ex.value), message
def test_error_checking_003(self):
CoR_calc = CenterOfRotation()
......@@ -153,34 +137,33 @@ class TestCor:
with pytest.raises(ValueError) as ex:
CoR_calc.find_shift(radio1, radio2)
message = (
"Error should have been raised about different image shapes, "
+ "other error raised instead:\n%s" % str(ex.value)
message = "Error should have been raised about different image shapes, " + "other error raised instead:\n%s" % str(
ex.value
)
assert "Images need to be of the same shape" in str(ex.value), message
@pytest.mark.usefixtures("bootstrap_dtr")
class TestDetectorTranslation(object):
def test_alignxc(self):
T_calc = DetectorTranslationAlongBeam()
found_shifts_list = []
ab_v, ab_h = T_calc.find_shift(
self.transcor_legacydatas,
img_pos=(1+np.arange(6))*0.01,
roi_yxhw=None,
median_filt_shape=None,
padding_mode=None,
peak_fit_radius=1,
equispaced_increments=True,
found_shifts_list=found_shifts_list
shifts_v, shifts_h = T_calc.find_shift(self.align_images, self.img_pos, found_shifts_list=found_shifts_list,)
reference_shifts_list = [
[-121.39034751, 11.29080154],
[-117.01864405, 3.81080018],
[-115.66542712, 9.73375546],
[-116.72357645, 16.71435394],
[6.024768, 20.28406386],
]
message = "Computed shifts coefficients %s and found ones %s do not coincide" % (
(shifts_v, shifts_h),
self.expected_shifts_vh,
)
assert np.all(np.isclose(self.expected_shifts_vh, [shifts_v, shifts_h], atol=0.1)), message
reference_coeffs = (2551.2529860886843, 308.9007837781174)
reference_shifts_list = [[-121.39034751, 11.29080154], [-117.01864405, 3.81080018], [-115.66542712, 9.73375546],
[-116.72357645, 16.71435394], [6.024768, 20.28406386]]
message = ("Computed shifts coefficients %s and found ones %s do not coincide" % ((ab_v, ab_h), reference_coeffs))
assert (np.abs(np.array(reference_coeffs) - np.array([ab_v, ab_h]))).max() < 0.01, message
message = ("Computed shifts %s and found ones %s do not coincide" % (found_shifts_list, reference_shifts_list))
assert (np.abs(np.array(found_shifts_list) - np.array(reference_shifts_list))).max() < 0.0001, message
message = "Computed shifts %s and found ones %s do not coincide" % (found_shifts_list, reference_shifts_list)
assert np.all(np.isclose(found_shifts_list, reference_shifts_list, 0.0001)), message
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