Commit 8b5d3691 authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Complete TestFlatField with simplified test mechanism

parent 312834c3
Pipeline #73043 passed with stage
in 6 minutes and 11 seconds
......@@ -134,6 +134,8 @@ def generate_test_flatfield_generalized(
@pytest.fixture(scope="class")
def bootstrap(request):
cls = request.cls
cls.tmp_files = []
cls.tmp_dirs = []
cls.n_radios = 10
cls.n_z = 100
cls.n_x = 512
......@@ -141,23 +143,15 @@ def bootstrap(request):
cls.ctx = get_cuda_context()
yield
# Tear-down
# for fname in ["flat.edf", "dark.edf", "flat00.edf", "flat09.edf"]:
# os.remove(os.path.join(cls.tempdir, fname))
# os.rmdir(cls.tempdir)
for fname in cls.tmp_files:
os.remove(fname)
for dname in cls.tmp_dirs:
os.rmdir(dname)
@pytest.mark.usefixtures("bootstrap")
class TestFlatField:
@staticmethod
def check_normalized_radios(radios_corr, expected_values):
# must be the same value everywhere in the radio
std = np.std(np.std(radios_corr, axis=-1), axis=-1)
assert np.max(np.abs(std)) < 1e-7
# radios values must be 0, -2, -4, ...
assert np.allclose(radios_corr[:, 0, 0], expected_values)
@staticmethod
def get_test_elements(case_name):
def get_test_elements(self, case_name):
config = flatfield_tests_cases[case_name]
radios_stack, flats_url, darks_url = generate_test_flatfield_generalized(
config["image_shape"],
......@@ -169,13 +163,18 @@ class TestFlatField:
config["darks_values"],
"test_ff.h5",
)
return (
radios_stack,
flats_url,
darks_url,
config,
flats_url[list(flats_url.keys())[0]].file_path(),
)
fname = flats_url[list(flats_url.keys())[0]].file_path()
self.tmp_files.append(fname)
self.tmp_dirs.append(os.path.dirname(fname))
return radios_stack, flats_url, darks_url, config
@staticmethod
def check_normalized_radios(radios_corr, expected_values):
# must be the same value everywhere in the radio
std = np.std(np.std(radios_corr, axis=-1), axis=-1)
assert np.max(np.abs(std)) < 1e-7
# radios values must be 0, -2, -4, ...
assert np.allclose(radios_corr[:, 0, 0], expected_values)
def test_flatfield_simple(self):
"""
......@@ -184,7 +183,7 @@ class TestFlatField:
(I - D)/(F - D) where I = (1, 2, ...), D = 1, F = 0.5
= (0, -2, -4, -6, ...)
"""
radios_stack, flats_url, darks_url, config, fname = self.get_test_elements(
radios_stack, flats_url, darks_url, config = self.get_test_elements(
"simple_nearest_interp"
)
......@@ -196,10 +195,9 @@ class TestFlatField:
"""
Same as test_flatfield_simple, but in a vertical subregion of the radios.
"""
radios_stack, flats_url, darks_url, config, fname = self.get_test_elements(
radios_stack, flats_url, darks_url, config = self.get_test_elements(
"simple_nearest_interp"
)
end_z = 51
radios_chunk = np.copy(radios_stack[:, :end_z, :])
# we only have a chunk in memory. Instantiate the class with the
......@@ -225,10 +223,9 @@ class TestFlatField:
= (I-D)/(F-D)
= (I-1)/I
"""
radios_stack, flats_url, darks_url, config, fname = self.get_test_elements(
radios_stack, flats_url, darks_url, config = self.get_test_elements(
"two_flats_no_radios_indices"
)
flatfield = FlatFieldDataUrls(radios_stack.shape, flats_url, darks_url)
radios_corr = flatfield.normalize_radios(np.copy(radios_stack))
self.check_normalized_radios(radios_corr, config["expected_result"])
......@@ -252,20 +249,22 @@ class TestFlatField:
"""
Test the flat-field with cuda back-end.
"""
d_radios = garray.to_gpu(self.radios_stack.astype("f"))
radios_stack, flats_url, darks_url, config = self.get_test_elements(
"two_flats_no_radios_indices"
)
d_radios = garray.to_gpu(radios_stack.astype("f"))
cuda_flatfield = CudaFlatFieldDataUrls(
d_radios.shape,
self.flats_url,
self.dark_url,
flats_url,
darks_url,
)
cuda_flatfield.normalize_radios(d_radios)
radios_corr = d_radios.get()
I = np.arange(1, self.n_radios + 1)
self.check_normalized_radios(radios_corr, (I - 1) / I)
self.check_normalized_radios(radios_corr, config["expected_result"])
# Linear interpolation, two flats, one dark
def test_twoflats_simple(self):
config = flatfield_tests_cases["double_flat_simple"]
config = flatfield_tests_cases["two_flats_with_radios_indices"]
radios, flats, darks = generate_test_flatfield_generalized(
config["image_shape"],
config["radios_indices"],
......@@ -274,18 +273,12 @@ class TestFlatField:
config["flats_values"],
config["darks_indices"],
config["darks_values"],
self.h5_fname,
"test_twoflats.h5",
)
FF = FlatFieldDataUrls(radios.shape, flats, darks, radios_indices=config["radios_indices"])
FF.normalize_radios(radios)
assert (
np.max(np.std(radios.reshape(radios.shape[0], -1), axis=-1)) < 1e-6
), "Normalized radios should have the same values for all pixels"
assert np.allclose(radios[:, 0, 0], config["expected_result"], atol=self.tol)
self.check_normalized_radios(radios, config["expected_result"])
fname = flats[list(flats.keys())[0]].file_path()
os.remove(fname)
os.rmdir(os.path.dirname(fname))
# This test should be closer to the ESRF standard setting.
......@@ -444,7 +437,6 @@ class TestFlatFieldH5:
# N_i = (R_i - D)/(F_i - D) = i*(F_i - 1)/( F_i - 1) = i
#
def generate_test_flatfield(n_radios, radio_shape, flat_interval, h5_fname):
radios = np.zeros((n_radios,) + radio_shape, "f")
dark_data = np.ones(radios.shape[1:], "f")
......
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