Allow to apply additional mask that is read out of file - NumPy or hdf5

This commit is contained in:
2025-06-25 11:15:43 +02:00
parent a10c70028f
commit 84486c6ff9
5 changed files with 53 additions and 4 deletions

View File

@@ -116,7 +116,7 @@ options:
* `'wf_method': 'div'|'sub'` - Method of white field correction - either division or subtraction is supported.
Algorithm Output:
* `'is_white_field_corrected': bool` - Indicates whether white field correction took place.
* `'white_field_correction_applied': 1/0` - Indicates whether the algorithm ran successfully.
* Image is changed **in-place**.
* **streakfinder Algorithm**
@@ -231,6 +231,18 @@ options:
Use the `'apply_additional_mask': 0/1` - Input flag to enable this functionality.
* **Additional Mask from file**
Alternative to previous additional masking, mask data is read from specified file. NumPy and HDF5 formats are supported.
Input parameters:
* `'apply_additional_mask': 1/0` - Input flag to enable this functionality.
* `'mask_file': str` - Path to the hdf5 file with mask data.
* `'mask_ds': str` [Optional] - Name of the dataset containing mask in the hdf5 file, default is `"data/data"`.
Algorithm Output:
* `'mask_from_file_applied': 1/0` - Indicates whether the algorithm ran successfully.
* **Filter based on pulse picker information**
If the event propagation capability is accessible for the detector and the pulse picker information is correctly configured for propagation, the filtration based on pulse picker information becomes feasible by using the

View File

@@ -1,5 +1,6 @@
from .addmask import calc_apply_additional_mask
from .addmaskfile import calc_apply_additional_mask_from_file
from .aggregation import calc_apply_aggregation
from .jfdata import JFData
from .mask import calc_mask_pixels

35
dap/algos/addmaskfile.py Normal file
View File

@@ -0,0 +1,35 @@
import h5py
import numpy as np
def calc_apply_additional_mask_from_file(results, pixel_mask_pf):
apply_additional_mask = results.get("apply_additional_mask_from_file", False)
if not apply_additional_mask:
return
results["mask_from_file_applied"] = 0
mask_file = results.get("mask_file", None)
if not mask_file:
return
mask_dataset = results.get("mask_ds", "data/data")
# Support for hdf5 and npy
if mask_file.endswith(".npy"):
try:
mask = np.load(mask_file)
except Exception as error:
print(f"Error loading mask data from NumPy file {mask_file}:\n{error}")
return
else:
try:
with h5py.File(mask_file, "r") as mask_file:
mask = np.asarray(mask_file[mask_dataset])
except Exception as error:
print(f"Error loading mask from hdf5 file {mask_file}:\n{error}")
return
try:
np.multiply(pixel_mask_pf, mask, out=pixel_mask_pf)
except Exception as error:
print(f"Error applying additional mask from file {mask_file}:\n{error}")
else:
results["mask_from_file_applied"] = 1

View File

@@ -3,6 +3,7 @@ import numpy as np
import jungfrau_utils as ju
from .addmask import calc_apply_additional_mask
from .addmaskfile import calc_apply_additional_mask_from_file
class JFData:
@@ -58,6 +59,7 @@ class JFData:
pixel_mask_pf = np.ascontiguousarray(pixel_mask_corrected)
calc_apply_additional_mask(results, pixel_mask_pf) # changes pixel_mask_pf in place
calc_apply_additional_mask_from_file(results, pixel_mask_pf) # changes pixel_mask_pf in place
self.id_pixel_mask_corrected = new_id_pixel_mask_corrected
self.pixel_mask_pf = pixel_mask_pf

View File

@@ -29,12 +29,11 @@ def calc_apply_whitefield_correction(results, data):
"""
In-place white field correction of the detector data
"""
results["is_white_field_corrected"] = False
do_whitefield_correction = results.get("do_whitefield_correction", False)
if not do_whitefield_correction:
print(f"No whitefield correction")
return
results["white_field_correction_applied"] = 0
params_required = [
"wf_data_file",
"wf_method",
@@ -70,4 +69,4 @@ def calc_apply_whitefield_correction(results, data):
print(f"ERROR: White field correction failed.\n"
f"{error=}")
else:
results["is_white_field_corrected"] = True
results["white_field_correction_applied"] = 1