Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid redundant full-cube-passes when region submasking #801

Merged
merged 5 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ install_requires =

[options.extras_require]
test =
mock
pytest-astropy
pytest-cov
docs =
Expand All @@ -37,6 +38,10 @@ novis =
reproject
scipy
all =
mock
pytest-astropy
pytest-cov
sphinx-astropy
zarr
fsspec
distributed
Expand Down
2 changes: 1 addition & 1 deletion spectral_cube/analysis_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ def stack_spectra(cube, velocity_surface, v0=None,
pix_shifts = vdiff_sign * ((velocity_surface.to(vel_unit) -
v0.to(vel_unit)) / vdiff).value[xy_posns]

# May a header copy so we can start altering
# Make a header copy so we can start altering
new_header = cube[:, 0, 0].header.copy()

if pad_edges:
Expand Down
17 changes: 14 additions & 3 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2050,7 +2050,8 @@ def subcube_from_crtfregion(self, crtf_region, allow_empty=False):

return self.subcube_from_regions(region_list, allow_empty)

def subcube_from_regions(self, region_list, allow_empty=False):
def subcube_from_regions(self, region_list, allow_empty=False,
minimize=True):
"""
Extract a masked subcube from a list of ``regions.Region`` object
(only functions on celestial dimensions)
Expand All @@ -2062,6 +2063,12 @@ def subcube_from_regions(self, region_list, allow_empty=False):
allow_empty: bool, optional
If this is False, an exception will be raised if the region
contains no overlap with the cube. Default is False.
minimize : bool
Run :meth:`~SpectralCube.minimal_subcube`. This is mostly redundant, since the
bounding box of the region is already used, but it will sometimes
slice off a one-pixel rind depending on the details of the region
shape. If minimize is disabled, there will potentially be a ring
of NaN values around the outside.
"""
import regions

Expand Down Expand Up @@ -2123,10 +2130,14 @@ def subcube_from_regions(self, region_list, allow_empty=False):
maskarray = np.zeros(subcube.shape[1:], dtype='bool')
maskarray[:] = mask.data[slices_small]

masked_subcube = subcube.with_mask(BooleanArrayMask(maskarray, subcube.wcs, shape=subcube.shape))
BAM = BooleanArrayMask(maskarray, subcube.wcs, shape=subcube.shape)
masked_subcube = subcube.with_mask(BAM)
# by using ceil / floor above, we potentially introduced a NaN buffer
# that we can now crop out
return masked_subcube.minimal_subcube(spatial_only=True)
if minimize:
return masked_subcube.minimal_subcube(spatial_only=True)
else:
return masked_subcube

def _velocity_freq_conversion_regions(self, ranges, veltypes, restfreqs):
"""
Expand Down