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

Add a preferred_cmap attribute to visual.py #2131

Merged
merged 1 commit into from
Aug 5, 2020
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: 1 addition & 1 deletion glue/core/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def __init__(self):
self._disable = False

def register(self, obj, label, group=None):
""" Register label with object (possibly disamgiguating)
""" Register label with object (possibly disambiguating)

:param obj: The object to label
:param label: The desired label
Expand Down
2 changes: 2 additions & 0 deletions glue/core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ def _save_style(style, context):
@loader(VisualAttributes)
def _load_style(rec, context):
result = VisualAttributes()
if 'preferred_cmap' in result._atts:
result._atts.remove('preferred_cmap')
for attr in result._atts:
setattr(result, attr, rec[attr])
return result
Expand Down
24 changes: 21 additions & 3 deletions glue/core/visual.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from matplotlib.colors import ColorConverter
from matplotlib.cm import get_cmap

from glue.config import settings

from echo import callback_property, HasCallbackProperties

# Define acceptable line styles
Expand All @@ -25,7 +27,7 @@ class VisualAttributes(HasCallbackProperties):

"""

def __init__(self, parent=None, washout=False, color=None, alpha=None):
def __init__(self, parent=None, washout=False, color=None, alpha=None, preferred_cmap=None):

super(VisualAttributes, self).__init__()

Expand All @@ -36,9 +38,10 @@ def __init__(self, parent=None, washout=False, color=None, alpha=None):

self.parent = parent
self._atts = ['color', 'alpha', 'linewidth', 'linestyle', 'marker',
'markersize']
'markersize', 'preferred_cmap']
self.color = color
self.alpha = alpha
self.preferred_cmap = preferred_cmap
self.linewidth = 1
self.linestyle = 'solid'
self.marker = 'o'
Expand Down Expand Up @@ -94,6 +97,20 @@ def color(self, value):
else:
self._color = value

@callback_property
def preferred_cmap(self):
"""
A preferred colormap specified using Matplotlib notation
"""
kakirastern marked this conversation as resolved.
Show resolved Hide resolved
return self._preferred_cmap

@preferred_cmap.setter
def preferred_cmap(self, value):
if isinstance(value, str):
self._preferred_cmap = get_cmap(value)
else:
self._preferred_cmap = value

@callback_property
def alpha(self):
"""
Expand Down Expand Up @@ -163,7 +180,8 @@ def __setattr__(self, attribute, value):

# Check that the attribute exists (don't allow new attributes)
allowed = set(['color', 'linewidth', 'linestyle',
'alpha', 'parent', 'marker', 'markersize'])
'alpha', 'parent', 'marker', 'markersize',
'preferred_cmap'])
if attribute not in allowed and not attribute.startswith('_'):
raise Exception("Attribute %s does not exist" % attribute)

Expand Down
8 changes: 4 additions & 4 deletions glue/viewers/image/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ class ImageViewerState(MatplotlibDataViewerState):
'which the images are shown')
slices = DDCProperty(docstring='The current slice along all dimensions')
color_mode = DDSCProperty(0, docstring='Whether each layer can have '
'its own colormap (``Colormaps``) or '
'whether each layer is assigned '
'a single color (``One color per layer``)')
'its own colormap (``Colormaps``) or '
'whether each layer is assigned '
'a single color (``One color per layer``)')

dpi = DDCProperty(72, docstring='The resolution (in dots per inch) of density maps, if present')

Expand Down Expand Up @@ -544,7 +544,7 @@ def __init__(self, layer=None, viewer_state=None, **kwargs):
self.update_from_dict(kwargs)

if self.cmap is None:
self.cmap = colormaps.members[0][1]
self.cmap = self.layer.style.preferred_cmap or colormaps.members[0][1]

def _update_attribute(self, *args):
if self.layer is not None:
Expand Down