astropy / specutils

An Astropy coordinated package for astronomical spectroscopy. Maintainers: @rosteen @keflavich @eteq
http://specutils.readthedocs.io/en/latest/
166 stars 127 forks source link

Create a mask from regions #552

Open hcferguson opened 4 years ago

hcferguson commented 4 years ago

I'm not finding in the documentation any function to create a mask from a set of regions. Seems like we should have that, and maybe the inverse as well.

Something like this...

def mask_from_regions(spectrum,regions):
    mask = np.zeros(spectrum.spectral_axis.shape,dtype=np.bool)
    for r in regions:
        submask = (spectrum.spectral_axis>r.lower) & (spectrum.spectral_axis<=r.upper)
        mask = mask | submask
    spectrum.mask = mask
hcferguson commented 4 years ago

Of course there is a question of whether the bounds should be inclusive or not. In the example above the bounds are exclusive at the lower end and inclusive at the upper end, which seems like it's a fairly common convention. Having them be exclusive would mean that concatenating a bunch of adjacent masks could result in gaps.

The document on extract_region doesn't say what it is doing at the boundaries. Looking at the extract_spectral_region.py code, it is doing

right_index = int(np.floor(spectrum.wcs.world_to_pixel([right_reg_in_spec_unit]))) + 1

So this looks like it is inclusive at the top end.