Skip to content

Commit 8af931c

Browse files
Merge pull request #683 from DiamondLightSource/imgda-650
Fix failure to ignore darks/flats when getting the other from separate file
2 parents c94fccd + d501782 commit 8af931c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

httomo/darks_flats.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,38 @@ def get_separate(config: DarksFlatsFileConfig):
144144
preview_config.detector_x.start : preview_config.detector_x.stop,
145145
]
146146

147+
if (
148+
darks_config.ignore
149+
and not flats_config.ignore
150+
and darks_config.file != flats_config.file
151+
):
152+
flats = get_separate(flats_config)
153+
darks = np.zeros(
154+
(
155+
1,
156+
preview_config.detector_y.stop - preview_config.detector_y.start,
157+
preview_config.detector_x.stop - preview_config.detector_x.start,
158+
),
159+
dtype=flats.dtype,
160+
)
161+
return darks, flats
162+
163+
if (
164+
not darks_config.ignore
165+
and flats_config.ignore
166+
and darks_config.file != flats_config.file
167+
):
168+
darks = get_separate(darks_config)
169+
flats = np.ones(
170+
(
171+
1,
172+
preview_config.detector_y.stop - preview_config.detector_y.start,
173+
preview_config.detector_x.stop - preview_config.detector_x.start,
174+
),
175+
dtype=darks.dtype,
176+
)
177+
return darks, flats
178+
147179
if darks_config.file != flats_config.file:
148180
darks = get_separate(darks_config)
149181
flats = get_separate(flats_config)

tests/test_get_darks_flats.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,93 @@ def test_get_darks_flats_different_file(preview_config: PreviewConfig):
297297

298298
np.testing.assert_array_equal(loaded_flats, flats)
299299
np.testing.assert_array_equal(loaded_darks, darks)
300+
301+
302+
def test_ignore_darks_get_separate_flats():
303+
DARKS_CONFIG = DarksFlatsFileConfig(
304+
file=Path(__file__).parent / "",
305+
data_path="",
306+
image_key_path=None,
307+
ignore=True,
308+
)
309+
FLATS_CONFIG = DarksFlatsFileConfig(
310+
file=Path(__file__).parent / "test_data/i12/separate_flats_darks/flat_field.h5",
311+
data_path="/1-NoProcessPlugin-tomo/data",
312+
image_key_path=None,
313+
ignore=False,
314+
)
315+
PREVIEW_CONFIG = PreviewConfig(
316+
angles=PreviewDimConfig(start=0, stop=180),
317+
detector_y=PreviewDimConfig(start=0, stop=128),
318+
detector_x=PreviewDimConfig(start=0, stop=160),
319+
)
320+
321+
loaded_darks, loaded_flats = get_darks_flats(
322+
DARKS_CONFIG,
323+
FLATS_CONFIG,
324+
PREVIEW_CONFIG,
325+
)
326+
327+
expected_darks = np.zeros(
328+
(
329+
1,
330+
PREVIEW_CONFIG.detector_y.stop - PREVIEW_CONFIG.detector_y.start,
331+
PREVIEW_CONFIG.detector_x.stop - PREVIEW_CONFIG.detector_x.start,
332+
),
333+
dtype=np.uint16,
334+
)
335+
336+
with h5py.File(FLATS_CONFIG.file, "r") as f:
337+
expected_flats = f[FLATS_CONFIG.data_path][
338+
:,
339+
PREVIEW_CONFIG.detector_y.start : PREVIEW_CONFIG.detector_y.stop,
340+
PREVIEW_CONFIG.detector_x.start : PREVIEW_CONFIG.detector_x.stop,
341+
]
342+
343+
np.testing.assert_array_equal(loaded_flats, expected_flats)
344+
np.testing.assert_array_equal(loaded_darks, expected_darks)
345+
346+
347+
def test_ignore_flats_get_separate_darks():
348+
DARKS_CONFIG = DarksFlatsFileConfig(
349+
file=Path(__file__).parent / "test_data/i12/separate_flats_darks/dark_field.h5",
350+
data_path="/1-NoProcessPlugin-tomo/data",
351+
image_key_path=None,
352+
ignore=False,
353+
)
354+
FLATS_CONFIG = DarksFlatsFileConfig(
355+
file=Path(__file__).parent / "",
356+
data_path="",
357+
image_key_path=None,
358+
ignore=True,
359+
)
360+
PREVIEW_CONFIG = PreviewConfig(
361+
angles=PreviewDimConfig(start=0, stop=180),
362+
detector_y=PreviewDimConfig(start=0, stop=128),
363+
detector_x=PreviewDimConfig(start=0, stop=160),
364+
)
365+
366+
loaded_darks, loaded_flats = get_darks_flats(
367+
DARKS_CONFIG,
368+
FLATS_CONFIG,
369+
PREVIEW_CONFIG,
370+
)
371+
372+
expected_flats = np.ones(
373+
(
374+
1,
375+
PREVIEW_CONFIG.detector_y.stop - PREVIEW_CONFIG.detector_y.start,
376+
PREVIEW_CONFIG.detector_x.stop - PREVIEW_CONFIG.detector_x.start,
377+
),
378+
dtype=np.uint16,
379+
)
380+
381+
with h5py.File(DARKS_CONFIG.file, "r") as f:
382+
expected_darks = f[DARKS_CONFIG.data_path][
383+
:,
384+
PREVIEW_CONFIG.detector_y.start : PREVIEW_CONFIG.detector_y.stop,
385+
PREVIEW_CONFIG.detector_x.start : PREVIEW_CONFIG.detector_x.stop,
386+
]
387+
388+
np.testing.assert_array_equal(loaded_flats, expected_flats)
389+
np.testing.assert_array_equal(loaded_darks, expected_darks)

0 commit comments

Comments
 (0)