Commit ce99de44 authored by Pierre Paleo's avatar Pierre Paleo
Browse files

Do flat+dark in one single pase: remove image_type logic

parent 04055bba
......@@ -213,8 +213,7 @@ class FlatFieldLoader:
"""
def __init__(
self, data_url, image_keys, image_type,
reduction_method="mean", lookup_files=None, logger=None
self, data_url, image_keys, lookup_files=None, results_file=None, logger=None
):
"""
Initialize a FlatFieldLoader helper.
......@@ -226,11 +225,6 @@ class FlatFieldLoader:
image_keys: list of int
List of keys corresponding to each image of the data volume.
See Nexus Format specification, NXtomo, "image_key".
image_type: str
Which image type to load. Can be "flats" or "darks".
reduction_method: str, optional
Which reduction method to use. Can be "mean", "median" or "sum".
Default is "mean".
lookup_files: list of DataUrl, optional
List of paths (DataUrl) to inspect to load existing "final" flats/darks.
logger: Logger object, optional
......@@ -238,36 +232,10 @@ class FlatFieldLoader:
"""
self.data_url = data_url
self.image_keys = image_keys
self._set_image_type(image_type)
self._set_reduction_method(reduction_method)
self.lookup_files = lookup_files or []
self.logger = LoggerOrPrint(logger)
def _set_image_type(self, image_type):
img_types = {
"flats": "flats",
"flat": "flats",
ImageKey.FLAT_FIELD.value: "flats",
"darks": "darks",
"dark": "darks",
ImageKey.DARK_FIELD.value: "darks",
}
check_supported(image_type, img_types, "Image type")
self.image_type = img_types[image_type]
def _set_reduction_method(self, reduction_method):
red_methods = {
"mean": np.mean,
"median": np.median,
"sum": np.sum
}
check_supported(reduction_method, red_methods, "reduction method")
self.reduction_method = reduction_method
self.reduction_function = red_methods[reduction_method]
def browse_existing_flatfield_results(self):
"""
Attempt at loading already computed "final" darks and flats from existing files.
......@@ -277,10 +245,10 @@ class FlatFieldLoader:
for fpath in self.lookup_files:
existing_data = self.load_existing_flatfield(fpath)
if existing_data is not None:
self.logger.info("Loaded %s from %s" % (self.image_type, fpath.file_path()))
self.logger.info("Loaded flats/darks from %s" % (fpath.file_path()))
break
if existing_data is None:
self.logger.debug("%s not loaded from any file" % self.image_type)
self.logger.debug("Flats/darks not loaded from any file")
return existing_data
......@@ -298,12 +266,14 @@ class FlatFieldLoader:
try:
with HDF5File(file_url.file_path(), "r", swmr=True) as f:
results = f[results_path]
for key, val in results[self.image_type].items():
res[key] = val[:]
for what in ["darks", "flats"]:
res[what] = {}
for key, val in results[what].items():
res[what][key] = val[:]
except Exception as exc:
self.logger.error(
"Could not load %s from %s: %s"
% (self.image_type, file_url.file_path(), str(exc))
"Could not load darks/flats from %s: %s"
% (file_url.file_path(), str(exc))
)
res = None
return res
......@@ -379,15 +349,16 @@ class FlatFieldLoader:
existing_imgs = self.browse_existing_flatfield_results()
if existing_imgs is not None:
return existing_imgs
img_slices = self.get_data_slices(self.image_keys, val_to_nxkey[self.image_type])
if img_slices == []:
self.logger.error("No %s found in %s" % (self.image_type, self.data_url.file_path()))
return None
res = {}
for data_slice in img_slices:
data_chunk = self.get_data_chunk(data_slice)
img = self.apply_reduction(data_chunk)
res[data_slice.start] = img
res = {"flats": {}, "darks": {}}
for what in res.keys():
img_slices = self.get_data_slices(self.image_keys, val_to_nxkey[what])
if img_slices == []:
self.logger.error("No %s found in %s" % (what, self.data_url.file_path()))
res[what] = None
for data_slice in img_slices:
data_chunk = self.get_data_chunk(data_slice)
img = np.mean(data_chunk)
res[what][data_slice.start] = img
return res
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