From 381fe7527308e6a00442de5e26a13c4466911125 Mon Sep 17 00:00:00 2001 From: Kris Stern Date: Thu, 7 May 2020 03:28:26 +0800 Subject: [PATCH] Debug to make the new preferred_cmap attribute of visual to work Edit files to remove unused imports Comment out problematic set_preferred_cmap line Get rid of unneeded set_preferred_cmap lines Update docstring of preferred_cmap callback property in visual Clean up after conducting more thorough testing Fix typo in doctring Tidy up and debug Remove preferred_cmap as a DeferredDrawCallbackProperty Remove unused line Add preferred_cmap attribute for SunPy SJI maps Tidy up by removing print statement for testing Make changes according to reviewer suggestion Make changes according to reviewer's suggestions Make changes according to reviewer's suggestions Make modifications according to reviewer suggestions --- glue/core/registry.py | 2 +- glue/core/state.py | 2 ++ glue/core/visual.py | 24 +++++++++++++++++++++--- glue/viewers/image/state.py | 8 ++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/glue/core/registry.py b/glue/core/registry.py index 98b983925..b5e325077 100644 --- a/glue/core/registry.py +++ b/glue/core/registry.py @@ -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 diff --git a/glue/core/state.py b/glue/core/state.py index f214b2303..413d00393 100644 --- a/glue/core/state.py +++ b/glue/core/state.py @@ -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 diff --git a/glue/core/visual.py b/glue/core/visual.py index bce2ef72b..07b73d1f3 100644 --- a/glue/core/visual.py +++ b/glue/core/visual.py @@ -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 @@ -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__() @@ -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' @@ -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 + """ + 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): """ @@ -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) diff --git a/glue/viewers/image/state.py b/glue/viewers/image/state.py index 40215b9f5..762676aa0 100644 --- a/glue/viewers/image/state.py +++ b/glue/viewers/image/state.py @@ -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') @@ -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: