From 7823ddaccd8eb8e9e04d58d7fc9bbd9234bfd414 Mon Sep 17 00:00:00 2001 From: Benjamin Hugo Date: Thu, 2 Mar 2023 00:14:53 +0200 Subject: [PATCH] Fix fits mask merge behaviour - Fixes #23 -- switch to logical operations - Creates switch to make ever more restrictive masks during merging by logical_and vs default of logical_or --- breizorro/breizorro.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/breizorro/breizorro.py b/breizorro/breizorro.py index 2e86f56..c2cd899 100644 --- a/breizorro/breizorro.py +++ b/breizorro/breizorro.py @@ -108,11 +108,13 @@ def resolve_island(isl_spec, mask_image, wcs, ignore_missing=False): raise ValueError(f"coordinates {c} do not select a valid island") return value -def add_regions(mask_image, regs, wcs): +def add_regions(mask_image, regs, wcs, logicaland=False): for reg in regs: if hasattr(reg, 'to_pixel'): reg = reg.to_pixel(wcs) - mask_image += reg.to_mask().to_image(mask_image.shape) + mask_image = np.logical_and(mask_image, reg.to_mask().to_image(mask_image.shape)) \ + if logicaland else np.logical_or(mask_image, reg.to_mask().to_image(mask_image.shape)) + def remove_regions(mask_image, regs, wcs): for reg in regs: @@ -145,6 +147,8 @@ def main(): help='Merge in one or more masks or region files') parser.add_argument('--subtract', dest='subtract', metavar="MASK(s)|REG(s)", nargs='+', help='Subract one or more masks or region files') + parser.add_argument('--merge_and', dest='mergeand', action='store_true', + help='Uses logical AND when merging instead of logical OR - the effect creates ever more restrictive masks') parser.add_argument('--number-islands', dest='islands', action='store_true', default=False, help='Number the islands detected (default=do not number islands)') @@ -248,11 +252,13 @@ def load_fits_or_region(filename): fits, regs = load_fits_or_region(merge) if fits: LOGGER.info(f"Treating {merge} as a FITS mask") - mask_image += fits[0] + mask_image = np.logical_and(mask_image, fits[0] > 1e-30) \ + if args.mergeand else np.logical_or(mask_image, fits[0] > 1e-30) + LOGGER.info("Merged into mask") else: LOGGER.info(f"Merging in {len(regs)} regions from {merge}") - add_regions(mask_image, regs, wcs) + add_regions(mask_image, regs, wcs, args.mergeand) mask_image = mask_image != 0 mask_header['BUNIT'] = 'mask'