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 group #2297

Merged
merged 6 commits into from
May 19, 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
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Full changelog
v1.4.0 (unreleased)
-------------------

* Add support for specifying visual attributes when creating a subset group. [#2297]

* Modify profile viewer so that when in 'Sum' mode, parts of profiles
with no valid values are NaN rather than zero.

Expand Down
6 changes: 3 additions & 3 deletions glue/core/data_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def register_to_hub(self, hub):
lambda msg: self._sync_link_manager(),
filter=lambda x: x.sender in self._data)

def new_subset_group(self, label=None, subset_state=None):
def new_subset_group(self, label=None, subset_state=None, **kwargs):
"""
Create and return a new Subset Group.

Expand All @@ -265,11 +265,11 @@ def new_subset_group(self, label=None, subset_state=None):
:class:`~glue.core.subset_group.SubsetGroup`
"""
from glue.core.subset_group import SubsetGroup
color = settings.SUBSET_COLORS[self._sg_count % len(settings.SUBSET_COLORS)]
kwargs.setdefault("color", settings.SUBSET_COLORS[self._sg_count % len(settings.SUBSET_COLORS)])
self._sg_count += 1
label = label or 'Subset %i' % self._sg_count

result = SubsetGroup(color=color, label=label, subset_state=subset_state)
result = SubsetGroup(label=label, subset_state=subset_state, **kwargs)
self._subset_groups.append(result)
result.register(self)
return result
Expand Down
14 changes: 8 additions & 6 deletions glue/core/subset_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __setgluestate__(cls, rec, context):

class SubsetGroup(HubListener):

def __init__(self, color=settings.SUBSET_COLORS[0], alpha=0.5, label=None, subset_state=None):
def __init__(self, label=None, subset_state=None, **kwargs):
"""
Create a new empty SubsetGroup

Expand All @@ -105,11 +105,13 @@ def __init__(self, color=settings.SUBSET_COLORS[0], alpha=0.5, label=None, subse

self.label = label

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)

@contract(data='isinstance(DataCollection)')
def register(self, data):
Expand Down
34 changes: 34 additions & 0 deletions glue/core/tests/test_subset_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,40 @@ def test_deleted_subsets_dont_respawn(self):
self.dc.append(d)
assert len(d.subsets) == 0

def test_visual_attributes_default(self):
num_sg = len(self.dc.subset_groups)
sg = self.dc.new_subset_group()
expected_color = settings.SUBSET_COLORS[num_sg % len(settings.SUBSET_COLORS)]

# VisualAttributes stores color with lowercase letters
# but the values in settings.SUBSET_COLORS use uppercase
expected_color = expected_color.lower()
assert sg.style.color == expected_color
assert sg.style.alpha == 0.5
assert sg.style.linewidth == 2.5
assert sg.style.markersize == 7
assert sg.style.linestyle == 'solid'
assert sg.style.marker == 'o'
assert sg.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'
)
sg = self.dc.new_subset_group(**visual_attributes)
for attr, value in visual_attributes.items():
if attr == 'preferred_cmap':
from matplotlib.cm import get_cmap
assert sg.style.preferred_cmap == get_cmap(visual_attributes[attr])
else:
assert getattr(sg.style, attr, None) == value


class TestSerialze(TestSubsetGroup):

Expand Down
6 changes: 4 additions & 2 deletions glue/core/visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class VisualAttributes(HasCallbackProperties):
The size of the marker. Default is 3.
"""

DEFAULT_ATTS = ['color', 'alpha', 'linewidth', 'linestyle', 'marker',
'markersize', 'preferred_cmap']

def __init__(self, parent=None, color=None, alpha=None, preferred_cmap=None, linewidth=1, linestyle='solid', marker='o', markersize=3):

super(VisualAttributes, self).__init__()
Expand All @@ -47,8 +50,7 @@ def __init__(self, parent=None, color=None, alpha=None, preferred_cmap=None, lin
alpha = alpha or settings.DATA_ALPHA

self.parent = parent
self._atts = ['color', 'alpha', 'linewidth', 'linestyle', 'marker',
'markersize', 'preferred_cmap']
self._atts = self.DEFAULT_ATTS.copy()
self.color = color
self.alpha = alpha
self.preferred_cmap = preferred_cmap
Expand Down