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

HDF5DatasetAnalyzer: rework get_results_file mechanism

parent bc5e95a3
Pipeline #36170 passed with stages
in 10 minutes and 44 seconds
import os
import posixpath
from tempfile import mkdtemp
import numpy as np
from silx.io.url import DataUrl
from tomoscan.esrf.edfscan import EDFTomoScan
from tomoscan.esrf.hdf5scan import HDF5TomoScan
from ..thirdparty.tomwer_load_flats_darks import get_flats_frm_process_file, get_darks_frm_process_file
from .nxflatfield import NXFlatField
from ..utils import is_writeable
from .utils import is_hdf5_extension
dataset_infos = {
......@@ -222,26 +225,44 @@ class HDF5DatasetAnalyzer(DatasetAnalyzer):
return True
def _get_results_file(self):
results_relfname = os.path.splitext(
os.path.basename(self.dataset_scanner.master_file)
)[0] + "_nabu_processes.hdf5"
# Attempt 1: write in the same dir as the master NX file
results_dir = os.path.dirname(self.dataset_scanner.master_file)
if is_writeable(results_dir):
return os.path.join(results_dir, results_relfname)
# Attempt 2: write in the "output" dir, if specified
out_dir = self.extra_options["output_dir"]
if out_dir is not None and is_writeable(out_dir):
return os.path.join(out_dir, results_relfname)
# Last attempt: write in a temporary directory
tempdir = mkdtemp(prefix="nabu_flatfield_")
return os.path.join(tempdir, results_relfname)
def _compute_or_load_flats(self):
processes_file = self.processes_file
if processes_file is None:
processes_file = os.path.join(self.dataset_scanner.path, "nabu_processes.h5")
# By default, write in processes_file. Not sure it is a good idea
results_file = processes_file
# Don't write in processes_file if flatfield = forced
if self.extra_options["force_flatfield"]:
results_file = None
lookup_files = [
DataUrl(
file_path=processes_file,
data_path=os.path.join(self.dataset_scanner.entry, "flat_field_images")
)
]
h5_entry = self.entry or "entry"
h5_path = posixpath.join(h5_entry, "flat_field_images")
if self.processes_file is None:
lookup_files = None
else:
lookup_files = [
DataUrl(file_path=self.processes_file, data_path=h5_path)
]
# In this case, we always want to return a dict of DataUrl,
# so we have to write the final flats/darks somewhere.
# Therefore results_url cannot be None when instantiating NXFlatField.
# It will be ignored if data can be loaded from self.processes_file
results_file = self._get_results_file()
results_url = DataUrl(file_path=results_file, data_path=h5_path)
self._nxflatfield = NXFlatField(
self._get_dataset_hdf5_url(),
self.dataset_scanner.image_key,
lookup_files=lookup_files,
results_file=results_file,
results_url=results_url,
force_load_existing_results=self.extra_options["force_flatfield"],
flats_reduction="median",
darks_reduction="mean"
......
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