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

Added stokes_data public method #751

Merged
merged 14 commits into from
Nov 3, 2021
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
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ jobs:
- name: Install testing dependencies
run: python -m pip install tox codecov
- name: Run tests with ${{ matrix.name }}
if: ${{ contains(matrix.toxenv,'-cov') }}
run: tox -v -e ${{ matrix.toxenv }}
- name: Upload coverage to codecov
if: ${{ contains(matrix.toxenv,'-cov') }}
Expand Down
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
0.6.1.dev (unreleased)
----------------------
- none yet
- Fix memory issue when calling statistics() on FITS cubes. #752

0.6 (2021-09-30)
----------------
Expand Down
20 changes: 9 additions & 11 deletions spectral_cube/dask_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,20 +760,18 @@ def statistics(self):

data = self._get_filled_data(fill=np.nan)

try:
from bottleneck import nanmin, nanmax, nansum
except ImportError:
from numpy import nanmin, nanmax, nansum

def compute_stats(chunk, *args):
return np.array([np.sum(~np.isnan(chunk)),
nanmin(chunk),
nanmax(chunk),
nansum(chunk),
nansum(chunk * chunk)])
# NOTE: we used to but do not use bottleneck here anymore, as it
# does not seem to provide any performance improvements and also
# has a memory leak when dealing with >f4 dtypes.
return np.array([[[[np.sum(~np.isnan(chunk)),
np.nanmin(chunk),
np.nanmax(chunk),
np.nansum(chunk),
np.nansum(chunk * chunk)]]]])

with dask.config.set(**self._scheduler_kwargs):
results = da.map_blocks(compute_stats, data.reshape(-1)).compute()
results = da.map_blocks(compute_stats, data, new_axis=3).compute()

count_values, min_values, max_values, sum_values, ssum_values = results.reshape((-1, 5)).T

Expand Down
22 changes: 21 additions & 1 deletion spectral_cube/stokes_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from . import wcs_utils
from .masks import BooleanArrayMask, is_broadcastable_and_smaller

__all__ = ['StokesSpectalCube']
__all__ = ['StokesSpectralCube']

VALID_STOKES = ['I', 'Q', 'U', 'V', 'RR', 'LL', 'RL', 'LR', 'XX', 'XY', 'YX', 'YY',
'RX', 'RY', 'LX', 'LY', 'XR,', 'XL', 'YR', 'YL', 'PP', 'PQ', 'QP', 'QQ',
Expand Down Expand Up @@ -61,10 +61,30 @@ def __init__(self, stokes_data, mask=None, meta=None, fill_value=None):

self._mask = mask

def __getitem__(self, key):
if key in self._stokes_data:
return self._stokes_data[key]
else:
raise KeyError("Key {0} does not exist in this cube.".format(key))

def __setitem__(self, key, item):
if key in self._stokes_data:
self._stokes_data[key] = item
else:
errmsg = "Assigning new Stokes axes is not yet supported."
raise NotImplementedError(errmsg)

@property
def shape(self):
return self._shape

@property
def stokes_data(self):
"""
The underlying data
"""
return self._stokes_data

@property
def mask(self):
"""
Expand Down
12 changes: 12 additions & 0 deletions spectral_cube/tests/test_stokes_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,15 @@ def test_separate_mask(self, use_dask):

cube2 = cube1.I.with_mask(mask3)
assert_equal(cube2.mask.include(), (mask1 & mask2[0] & mask3).include())

def test_key_access_valid(self, use_dask):
stokes_data = OrderedDict()
stokes_data['I'] = SpectralCube(self.data[0], wcs=self.wcs, use_dask=use_dask)
stokes_data['Q'] = SpectralCube(self.data[1], wcs=self.wcs, use_dask=use_dask)
stokes_data['U'] = SpectralCube(self.data[2], wcs=self.wcs, use_dask=use_dask)
stokes_data['V'] = SpectralCube(self.data[3], wcs=self.wcs, use_dask=use_dask)
cube = StokesSpectralCube(stokes_data)
assert_equal(cube['I'],cube._stokes_data['I'])
assert_equal(cube['Q'],cube._stokes_data['Q'])
assert_equal(cube['U'],cube._stokes_data['U'])
assert_equal(cube['V'],cube._stokes_data['V'])