Skip to content

Commit

Permalink
add moment progressbar and allow kwargs in progressbar
Browse files Browse the repository at this point in the history
  • Loading branch information
keflavich committed Oct 11, 2024
1 parent 1a3d301 commit 221b74a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
20 changes: 18 additions & 2 deletions spectral_cube/_moments.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from .cube_utils import iterator_strategy
from .np_compat import allbadtonan
from .utils import ProgressBar

"""
Functions to compute moment maps in a variety of ways
Expand Down Expand Up @@ -87,7 +88,7 @@ def _slice1(cube, axis):
return result / weights


def moment_slicewise(cube, order, axis):
def moment_slicewise(cube, order, axis, progressbar=False):
"""
Compute moments by accumulating the result 1 slice at a time
"""
Expand All @@ -108,18 +109,25 @@ def moment_slicewise(cube, order, axis):
# possible for mom2, not sure about general case
mom1 = _slice1(cube, axis)

if progressbar:
progressbar = ProgressBar(cube.shape[axis], desc='Moment raywise: ')
pbu = progressbar.update
else:
pbu = lambda: True

for i in range(cube.shape[axis]):
view[axis] = i
plane = cube._get_filled_data(fill=0, view=tuple(view))
result += (plane *
(pix_cen[tuple(view)] - mom1) ** order *
pix_size)
weights += plane * pix_size
pbu()

return (result / weights)


def moment_raywise(cube, order, axis):
def moment_raywise(cube, order, axis, progressbar=False):
"""
Compute moments by accumulating the answer one ray at a time
"""
Expand All @@ -129,6 +137,12 @@ def moment_raywise(cube, order, axis):
pix_cen = cube._pix_cen()[axis]
pix_size = cube._pix_size_slice(axis)

if progressbar:
progressbar = ProgressBar(out.size, desc='Moment raywise: ')
pbu = progressbar.update
else:
pbu = lambda: True

for x, y, slc in cube._iter_rays(axis):
# the intensity, i.e. the weights
include = cube._mask.include(data=cube._data, wcs=cube._wcs, view=slc,
Expand All @@ -151,6 +165,8 @@ def moment_raywise(cube, order, axis):
ordern /= data.sum()

out[x, y] = ordern

pbu()
return out

def moment_cubewise(cube, order, axis):
Expand Down
24 changes: 12 additions & 12 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,7 @@ def _pix_size(self):

return dspectral, dy, dx

def moment(self, order=0, axis=0, how='auto'):
def moment(self, order=0, axis=0, how='auto', **kwargs):
"""
Compute moments along the spectral axis.
Expand Down Expand Up @@ -1662,7 +1662,7 @@ def moment(self, order=0, axis=0, how='auto'):
return ValueError("Invalid how. Must be in %s" %
sorted(list(dispatch.keys())))

out = dispatch[how](self, order, axis)
out = dispatch[how](self, order, axis, **kwargs)

# apply units
if order == 0:
Expand Down Expand Up @@ -1693,31 +1693,31 @@ def moment(self, order=0, axis=0, how='auto'):
return Projection(out, copy=False, wcs=new_wcs, meta=meta,
header=self._nowcs_header)

def moment0(self, axis=0, how='auto'):
def moment0(self, axis=0, how='auto', **kwargs):
"""
Compute the zeroth moment along an axis.
See :meth:`moment`.
"""
return self.moment(axis=axis, order=0, how=how)
return self.moment(axis=axis, order=0, how=how, **kwargs)

def moment1(self, axis=0, how='auto'):
def moment1(self, axis=0, how='auto', **kwargs):
"""
Compute the 1st moment along an axis.
For an explanation of the ``axis`` and ``how`` parameters, see :meth:`moment`.
"""
return self.moment(axis=axis, order=1, how=how)
return self.moment(axis=axis, order=1, how=how, **kwargs)

def moment2(self, axis=0, how='auto'):
def moment2(self, axis=0, how='auto', **kwargs):
"""
Compute the 2nd moment along an axis.
For an explanation of the ``axis`` and ``how`` parameters, see :meth:`moment`.
"""
return self.moment(axis=axis, order=2, how=how)
return self.moment(axis=axis, order=2, how=how, **kwargs)

def linewidth_sigma(self, how='auto'):
def linewidth_sigma(self, how='auto', **kwargs):
"""
Compute a (sigma) linewidth map along the spectral axis.
Expand All @@ -1726,15 +1726,15 @@ def linewidth_sigma(self, how='auto'):
with np.errstate(invalid='ignore'):
with warnings.catch_warnings():
warnings.simplefilter("ignore", VarianceWarning)
return np.sqrt(self.moment2(how=how))
return np.sqrt(self.moment2(how=how, **kwargs))

def linewidth_fwhm(self, how='auto'):
def linewidth_fwhm(self, how='auto', **kwargs):
"""
Compute a (FWHM) linewidth map along the spectral axis.
For an explanation of the ``how`` parameter, see :meth:`moment`.
"""
return self.linewidth_sigma() * SIGMA2FWHM
return self.linewidth_sigma(**kwargs) * SIGMA2FWHM

@property
def spectral_axis(self):
Expand Down

0 comments on commit 221b74a

Please sign in to comment.