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

Customizable limits percentile and behavior on updating bins when resetting limits #2455

Merged
merged 3 commits into from
Nov 14, 2023
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
9 changes: 7 additions & 2 deletions glue/viewers/histogram/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class HistogramViewerState(MatplotlibDataViewerState):
common_n_bin = DDCProperty(True, docstring='The number of bins to use for '
'all numerical components')

x_limits_percentile = DDCProperty(100, docstring="Percentile to use when automatically determining x limits")

update_bins_on_reset_limits = DDCProperty(True, docstring="Whether to update the bins to match the view when resetting limits")

def __init__(self, **kwargs):

super(HistogramViewerState, self).__init__()
Expand Down Expand Up @@ -67,9 +71,10 @@ def _reset_x_limits(self, *args):
if self.x_att is None:
return
with delay_callback(self, 'hist_x_min', 'hist_x_max', 'x_min', 'x_max', 'x_log'):
self.x_lim_helper.percentile = 100
self.x_lim_helper.percentile = self.x_limits_percentile
self.x_lim_helper.update_values(force=True)
self.update_bins_to_view()
if self.update_bins_on_reset_limits:
self.update_bins_to_view()

def reset_limits(self):
self._reset_x_limits()
Expand Down
39 changes: 39 additions & 0 deletions glue/viewers/histogram/tests/test_viewer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
from numpy.testing import assert_allclose

from astropy.utils import NumpyRNGContext

Expand Down Expand Up @@ -80,3 +81,41 @@ def test_incompatible_datasets():
viewer.state.x_att = data2.id['y']

viewer.state.hist_n_bin = 20


def test_reset_limits():

data1 = Data(x=np.arange(1000), label='data')

app = Application()
app.data_collection.append(data1)

viewer = app.new_data_viewer(SimpleHistogramViewer)
viewer.add_data(data1)

viewer.state.reset_limits()

assert_allclose(viewer.state.x_min, 0)
assert_allclose(viewer.state.x_max, 999)

viewer.state.x_limits_percentile = 90

viewer.state.reset_limits()

assert_allclose(viewer.state.x_min, 49.95)
assert_allclose(viewer.state.x_max, 949.05)

assert_allclose(viewer.state.hist_x_min, 49.95)
assert_allclose(viewer.state.hist_x_max, 949.05)

viewer.state.update_bins_on_reset_limits = False

viewer.state.x_limits_percentile = 80

viewer.state.reset_limits()

assert_allclose(viewer.state.x_min, 99.9)
assert_allclose(viewer.state.x_max, 899.1)

assert_allclose(viewer.state.hist_x_min, 49.95)
assert_allclose(viewer.state.hist_x_max, 949.05)
7 changes: 5 additions & 2 deletions glue/viewers/scatter/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class ScatterViewerState(MatplotlibDataViewerState):
plot_mode = DDSCProperty(docstring="Whether to plot the data in cartesian, polar or another projection")
angle_unit = DDSCProperty(docstring="Whether to use radians or degrees for any angular coordinates")

x_limits_percentile = DDCProperty(100, docstring="Percentile to use when automatically determining x limits")
y_limits_percentile = DDCProperty(100, docstring="Percentile to use when automatically determining y limits")

def __init__(self, **kwargs):

super(ScatterViewerState, self).__init__()
Expand Down Expand Up @@ -70,13 +73,13 @@ def __init__(self, **kwargs):
def _reset_x_limits(self, *args):
if self.x_att is None:
return
self.x_lim_helper.percentile = 100
self.x_lim_helper.percentile = self.x_limits_percentile
self.x_lim_helper.update_values(force=True)

def _reset_y_limits(self, *args):
if self.y_att is None:
return
self.y_lim_helper.percentile = 100
self.y_lim_helper.percentile = self.y_limits_percentile
self.y_lim_helper.update_values(force=True)

def reset_limits(self):
Expand Down
35 changes: 35 additions & 0 deletions glue/viewers/scatter/tests/test_viewer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import numpy as np
from numpy.testing import assert_allclose

from glue.tests.visual.helpers import visual_test

from glue.viewers.scatter.viewer import SimpleScatterViewer
Expand All @@ -24,3 +27,35 @@ def test_simple_viewer():
app.data_collection.new_subset_group(label='subset1', subset_state=data1.id['x'] > 2)

return viewer.figure


def test_reset_limits():

data1 = Data(x=np.arange(1000), y=np.arange(1000) + 1000, label='data')

app = Application()
app.data_collection.append(data1)

viewer = app.new_data_viewer(SimpleScatterViewer)
viewer.add_data(data1)

viewer.state.reset_limits()

# Note that there is a margin included which is why the limits are not 0 to 999

assert_allclose(viewer.state.x_min, -39.96)
assert_allclose(viewer.state.x_max, 1038.96)

assert_allclose(viewer.state.y_min, 1000 - 39.96)
assert_allclose(viewer.state.y_max, 1000 + 1038.96)

viewer.state.x_limits_percentile = 90
viewer.state.y_limits_percentile = 80

viewer.state.reset_limits()

assert_allclose(viewer.state.x_min, 13.986)
assert_allclose(viewer.state.x_max, 985.014)

assert_allclose(viewer.state.y_min, 1000 + 67.932)
assert_allclose(viewer.state.y_max, 1000 + 931.068)