Skip to content

Commit

Permalink
fix for 915: account for different use cases of median
Browse files Browse the repository at this point in the history
  • Loading branch information
keflavich committed Jun 20, 2024
1 parent 8ae6705 commit 4d7182f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spectral_cube/spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,17 @@ def median(self, axis=None, iterate_rays=False, **kwargs):
except ImportError:
bnok = False

how = kwargs.pop('how', None)
if how == 'slice' and (not isinstance(axis, (list, tuple)) or len(axis) != 2):
raise ValueError("Cannot compute median slicewise unless you're compressing over two axes.")

Check warning on line 1186 in spectral_cube/spectral_cube.py

View check run for this annotation

Codecov / codecov/patch

spectral_cube/spectral_cube.py#L1186

Added line #L1186 was not covered by tests
elif how == 'ray':
if axis not in (0, 1, 2):
raise ValueError("Cannot compute median raywise unless you're compressing over one axis.")

Check warning on line 1189 in spectral_cube/spectral_cube.py

View check run for this annotation

Codecov / codecov/patch

spectral_cube/spectral_cube.py#L1188-L1189

Added lines #L1188 - L1189 were not covered by tests
else:
if not iterate_rays:
iterate_rays = True
warnings.warn("how='ray' was specified in call to median; this is setting iterate_rays=True")

Check warning on line 1193 in spectral_cube/spectral_cube.py

View check run for this annotation

Codecov / codecov/patch

spectral_cube/spectral_cube.py#L1191-L1193

Added lines #L1191 - L1193 were not covered by tests

# slicewise median is nonsense, must force how = 'cube'
# bottleneck.nanmedian does not allow axis to be a list or tuple
if bnok and not iterate_rays and not isinstance(axis, (list, tuple)):
Expand All @@ -1194,6 +1205,11 @@ def median(self, axis=None, iterate_rays=False, **kwargs):
result = self.apply_numpy_function(np.nanmedian, axis=axis,
projection=True, unit=self.unit,
how='cube',**kwargs)
elif iterate_rays:
result = self.apply_numpy_function(
nanmedian if bnok else np.nanmedian if hasattr(np, 'nanmedian') else np.median,
axis=axis, projection=True, unit=self.unit, how='ray',
check_endian=True, **kwargs)
else:
log.debug("Using numpy median iterating over rays")
result = self.apply_function(np.median, projection=True, axis=axis,
Expand Down

0 comments on commit 4d7182f

Please sign in to comment.