Skip to content

Commit

Permalink
Merge pull request #1614 from astrofrog/limits-with-inf
Browse files Browse the repository at this point in the history
Fix StateAtributeLimitsHelper if inf/-inf values are present in data
  • Loading branch information
astrofrog authored Mar 20, 2018
2 parents 576a2da + 527a144 commit 42dd03d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ v0.12.6 (unreleased)

* Fixed size of markers when value for size is out of vmin/vmax range. [#1609]

* Fix a bug that caused viewer limits to be calculated incorrectly if
inf/-inf values were present in the data. [#1614]

* Fixed a bug which caused the y-axis in the PV slice viewer to be
incorrect if the WCS could not be computed. [#1615]

Expand Down
18 changes: 9 additions & 9 deletions glue/core/state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,24 +339,24 @@ def update_values(self, force=False, use_default_modifiers=False, **properties):
self.set(lower=0.1, upper=1, percentile=percentile, log=log)
return

# NOTE: we can't use np.nanmin/np.nanmax or nanpercentile below as
# they don't exclude inf/-inf
if data_values.dtype.kind != 'M':
data_values = data_values[np.isfinite(data_values)]

if percentile == 100:

if data_values.dtype.kind == 'M':
lower = data_values.min()
upper = data_values.max()
else:
lower = np.nanmin(data_values)
upper = np.nanmax(data_values)
lower = np.min(data_values)
upper = np.max(data_values)

else:

try:
lower = np.nanpercentile(data_values, exclude)
upper = np.nanpercentile(data_values, 100 - exclude)
except AttributeError: # Numpy < 1.9
data_values = data_values[~np.isnan(data_values)]
lower = np.percentile(data_values, exclude)
upper = np.percentile(data_values, 100 - exclude)
lower = np.percentile(data_values, exclude)
upper = np.percentile(data_values, 100 - exclude)

if self.data_component.categorical:
lower = np.floor(lower - 0.5) + 0.5
Expand Down
37 changes: 37 additions & 0 deletions glue/core/tests/test_state_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,40 @@ def test_state_serialization_datetime64():
state2 = clone(state1)

assert state2.a == np.datetime64(100, 'D')


def test_nan_inf_minmax():

data = Data(x=[3, 1, -2, np.inf, np.nan], label='test_data')

class SimpleState(State):

layer = CallbackProperty()
comp = CallbackProperty()
lower = CallbackProperty()
upper = CallbackProperty()
percentile = CallbackProperty()
log = CallbackProperty()

state = SimpleState()

helper = StateAttributeLimitsHelper(state, attribute='comp', # noqa
lower='lower', upper='upper',
percentile='percentile', log='log')

state.data = data
state.comp = data.id['x']

assert state.lower == -2
assert state.upper == +3

state.log = True

assert state.lower == +1
assert state.upper == +3

state.log = False
state.percentile = 99

assert_allclose(state.lower, -1.97)
assert_allclose(state.upper, +2.98)

0 comments on commit 42dd03d

Please sign in to comment.