diff --git a/README.md b/README.md index a8d23ed..73d2c0b 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,16 @@ 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 or npy file with mask data. + * `'mask_ds': str` [Optional] - Name of the dataset containing mask in the hdf5 file, default is `"mask_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 diff --git a/dap/algos/__init__.py b/dap/algos/__init__.py index 501d88d..d5e8f0b 100644 --- a/dap/algos/__init__.py +++ b/dap/algos/__init__.py @@ -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 diff --git a/dap/algos/addmaskfile.py b/dap/algos/addmaskfile.py new file mode 100644 index 0000000..772900a --- /dev/null +++ b/dap/algos/addmaskfile.py @@ -0,0 +1,36 @@ +import h5py +import numpy as np + + +DEFAULT_MASK_DATASET = "mask_data" + + +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 + + mask_file = results.get("mask_file", None) + if not mask_file: + return + mask_dataset = results.get("mask_ds", DEFAULT_MASK_DATASET) + + # Support for hdf5 and npy + if mask_file.endswith(".npy"): + try: + mask = np.load(mask_file) + except Exception as error: + results["mask_error"] = f"Error loading mask data from NumPy file {mask_file}:\n{error}" + return + else: + try: + with h5py.File(mask_file, "r") as h5f: + mask = np.asarray(h5f[mask_dataset], dtype=np.bool) + except Exception as error: + results["mask_error"] = 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: + results["mask_error"] = f"Error applying additional mask from file {mask_file}:\n{error}" diff --git a/dap/algos/jfdata.py b/dap/algos/jfdata.py index bd2fa1e..1a578bf 100644 --- a/dap/algos/jfdata.py +++ b/dap/algos/jfdata.py @@ -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) # further changes pixel_mask_pf in place self.id_pixel_mask_corrected = new_id_pixel_mask_corrected self.pixel_mask_pf = pixel_mask_pf