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

BUGFIX: Argmax/argmin raywise needs to return int #924

Merged
merged 7 commits into from
Oct 18, 2024
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
16 changes: 12 additions & 4 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,8 @@ def argmax(self, axis=None, how='auto', **kwargs):
"""
return self.apply_numpy_function(np.nanargmax, fill=-np.inf,
reduce=False, projection=False,
how=how, axis=axis, **kwargs)
how=how, axis=axis,
**kwargs)

@aggregation_docstring
@warn_slow
Expand All @@ -808,7 +809,8 @@ def argmin(self, axis=None, how='auto', **kwargs):
"""
return self.apply_numpy_function(np.nanargmin, fill=np.inf,
reduce=False, projection=False,
how=how, axis=axis, **kwargs)
how=how, axis=axis,
**kwargs)

def _argmaxmin_world(self, axis, method, **kwargs):
'''
Expand Down Expand Up @@ -997,7 +999,8 @@ def _cube_on_cube_operation(self, function, cube, equivalencies=[], **kwargs):

def apply_function(self, function, axis=None, weights=None, unit=None,
projection=False, progressbar=False,
update_function=None, keep_shape=False, **kwargs):
update_function=None, keep_shape=False,
**kwargs):
"""
Apply a function to valid data along the specified axis or to the whole
cube, optionally using a weight array that is the same shape (or at
Expand Down Expand Up @@ -1054,7 +1057,12 @@ def apply_function(self, function, axis=None, weights=None, unit=None,
nz = self.shape[axis] if keep_shape else 1

# allocate memory for output array
out = np.empty([nz, nx, ny]) * np.nan
# check dtype first (for argmax/argmin)
result = function(np.arange(3, dtype=self._data.dtype), **kwargs)
if 'int' in str(result.dtype):
out = np.zeros([nz, nx, ny], dtype=result.dtype)
else:
out = np.empty([nz, nx, ny]) * np.nan

if progressbar:
progressbar = ProgressBar(nx*ny)
Expand Down
10 changes: 10 additions & 0 deletions spectral_cube/tests/test_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,16 @@ def test_argmin(self):
self._check_numpy(self.c.argmin, d, np.nanargmin)
self.c = self.d = None

def test_arg_rays(self, use_dask):
"""
regression test: argmax must have integer dtype
"""
if not use_dask:
result = self.c.argmax(how='ray')
assert 'int' in str(result.dtype)
result = self.c.argmin(how='ray')
assert 'int' in str(result.dtype)

@pytest.mark.parametrize('iterate_rays', (True,False))
def test_median(self, iterate_rays, use_dask):
# Make sure that medians ignore empty/bad/NaN values
Expand Down
Loading