Skip to content

Commit

Permalink
try to avoid redundant full-cube-passes when region submasking
Browse files Browse the repository at this point in the history
  • Loading branch information
keflavich committed May 16, 2022
1 parent f92fe7d commit b62dda8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
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
15 changes: 12 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=False):
"""
Extract a masked subcube from a list of ``regions.Region`` object
(only functions on celestial dimensions)
Expand All @@ -2062,6 +2063,10 @@ 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 `minimal_subcube`? This should be redundant, since the
bounding box of the region is already used, but it might slice off
an extra few pixels depending on the details of the region shape.
"""
import regions

Expand Down Expand Up @@ -2123,10 +2128,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.celestial, shape=subcube.shape[1:])
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

0 comments on commit b62dda8

Please sign in to comment.