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

Allow specifying visual attributes when creating subset #2333

Merged
merged 2 commits into from
Oct 13, 2022
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
8 changes: 4 additions & 4 deletions glue/core/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def data(self):
color='color|None',
label='string|None',
returns=Subset)
def new_subset(self, subset=None, color=None, label=None, **kwargs):
def new_subset(self, subset=None, **kwargs):
"""
Create a new subset, and attach to self.

Expand All @@ -200,9 +200,9 @@ def new_subset(self, subset=None, color=None, label=None, **kwargs):
Manually-instantiated subsets will **not** be represented properly by the UI.
"""
nsub = len(self.subsets)
color = color or settings.SUBSET_COLORS[nsub % len(settings.SUBSET_COLORS)]
label = label or "%s.%i" % (self.label, nsub + 1)
new_subset = Subset(self, color=color, label=label, **kwargs)
kwargs.setdefault("color", settings.SUBSET_COLORS[nsub % len(settings.SUBSET_COLORS)])
kwargs.setdefault("label", "%s.%i" % (self.label, nsub + 1))
new_subset = Subset(self, **kwargs)
if subset is not None:
new_subset.subset_state = subset.subset_state.copy()

Expand Down
16 changes: 9 additions & 7 deletions glue/core/subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Subset(object):
color='color',
alpha=float,
label='string|None')
def __init__(self, data, color=settings.SUBSET_COLORS[0], alpha=0.5, label=None):
def __init__(self, data, **kwargs):
"""Create a new subset object.

Note: the preferred way for creating subsets is via
Expand All @@ -68,15 +68,17 @@ def __init__(self, data, color=settings.SUBSET_COLORS[0], alpha=0.5, label=None)
self._broadcasting = False # must be first def

self.data = data
self.label = label # trigger disambiguation
self.label = kwargs.get("label", None) # trigger disambiguation

self.subset_state = SubsetState() # calls proper setter method

self.style = VisualAttributes(parent=self)
self.style.markersize *= 2.5
self.style.linewidth *= 2.5
self.style.color = color
self.style.alpha = alpha
visual_args = {k: v for k, v in kwargs.items() if k in VisualAttributes.DEFAULT_ATTS}
visual_args.setdefault("color", settings.SUBSET_COLORS[0])
visual_args.setdefault("alpha", 0.5)
visual_args.setdefault("linewidth", 2.5)
visual_args.setdefault("markersize", 7)

self.style = VisualAttributes(parent=self, **visual_args)

# We assign a UUID which can then be used for example in equations
# for derived components - the idea is that this doesn't change over
Expand Down
32 changes: 32 additions & 0 deletions glue/core/tests/test_subset.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from glue.tests.helpers import requires_astropy, requires_scipy, SCIPY_INSTALLED
from ..exceptions import IncompatibleAttribute
from .. import DataCollection, ComponentLink
from ...config import settings
from ..data import Data, Component
from ..roi import CategoricalROI, RectangularROI, Projected3dROI, CircularROI
from ..message import SubsetDeleteMessage
Expand Down Expand Up @@ -194,6 +195,37 @@ def test_state_bad_type(self):
with pytest.raises(TypeError):
s.subset_state = 5

def test_visual_attributes_default(self):
s = Subset(Data())
expected_color = settings.SUBSET_COLORS[0].lower()
style = s.style
assert style.color == expected_color
assert style.alpha == 0.5
assert style.linewidth == 2.5
assert style.markersize == 7
assert style.linestyle == 'solid'
assert style.marker == 'o'
assert style.preferred_cmap is None

def test_visual_attributes(self):
visual_attributes = dict(
color="#ff7f00",
alpha=0.3,
linewidth=4,
markersize=10,
marker='x',
linestyle='dashed',
preferred_cmap='viridis'
)
d = Data(x=[1, 2, 3])
s = d.new_subset(**visual_attributes)
for attr, value in visual_attributes.items():
if attr == 'preferred_cmap':
from matplotlib.cm import get_cmap
assert s.style.preferred_cmap == get_cmap(visual_attributes[attr])
else:
assert getattr(s.style, attr, None) == value


target_states = ((op.and_, AndState),
(op.or_, OrState),
Expand Down