Skip to content

Commit

Permalink
Fix fits mask merge behaviour
Browse files Browse the repository at this point in the history
- Fixes #23 -- switch to logical operations
- Creates switch to make ever more restrictive masks during merging
by logical_and vs default of logical_or
  • Loading branch information
bennahugo committed Mar 1, 2023
1 parent 5e3ad86 commit 7823dda
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions breizorro/breizorro.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)')
Expand Down Expand Up @@ -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'

Expand Down

0 comments on commit 7823dda

Please sign in to comment.