From bfa4dba15485b4192f8bbe13345e9658c97ecf76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Picca=20Fr=C3=A9d=C3=A9ric-Emmanuel?= Date: Sat, 7 Oct 2017 07:59:01 +0200 Subject: New upstream version 0.6.0+dfsg --- silx/gui/plot/Colors.py | 217 +++++------------------------------------------- 1 file changed, 23 insertions(+), 194 deletions(-) (limited to 'silx/gui/plot/Colors.py') diff --git a/silx/gui/plot/Colors.py b/silx/gui/plot/Colors.py index 7a3cd97..2d44d4d 100644 --- a/silx/gui/plot/Colors.py +++ b/silx/gui/plot/Colors.py @@ -24,20 +24,18 @@ # ###########################################################################*/ """Color conversion function, color dictionary and colormap tools.""" -__authors__ = ["V.A. Sole", "T. VINCENT"] +from __future__ import absolute_import + +__authors__ = ["V.A. Sole", "T. Vincent"] __license__ = "MIT" -__date__ = "16/01/2017" +__date__ = "15/05/2017" +from silx.utils.deprecation import deprecated import logging - import numpy -import matplotlib -import matplotlib.colors -import matplotlib.cm - -from . import MPLColormap +from .Colormap import Colormap _logger = logging.getLogger(__name__) @@ -143,159 +141,7 @@ def cursorColorForColormap(colormapName): return _COLORMAP_CURSOR_COLORS.get(colormapName, 'black') -_CMAPS = {} # Store additional colormaps - - -def getMPLColormap(name): - """Returns matplotlib colormap corresponding to given name - - :param str name: The name of the colormap - :return: The corresponding colormap - :rtype: matplolib.colors.Colormap - """ - if not _CMAPS: # Lazy initialization of own colormaps - cdict = {'red': ((0.0, 0.0, 0.0), - (1.0, 1.0, 1.0)), - 'green': ((0.0, 0.0, 0.0), - (1.0, 0.0, 0.0)), - 'blue': ((0.0, 0.0, 0.0), - (1.0, 0.0, 0.0))} - _CMAPS['red'] = matplotlib.colors.LinearSegmentedColormap( - 'red', cdict, 256) - - cdict = {'red': ((0.0, 0.0, 0.0), - (1.0, 0.0, 0.0)), - 'green': ((0.0, 0.0, 0.0), - (1.0, 1.0, 1.0)), - 'blue': ((0.0, 0.0, 0.0), - (1.0, 0.0, 0.0))} - _CMAPS['green'] = matplotlib.colors.LinearSegmentedColormap( - 'green', cdict, 256) - - cdict = {'red': ((0.0, 0.0, 0.0), - (1.0, 0.0, 0.0)), - 'green': ((0.0, 0.0, 0.0), - (1.0, 0.0, 0.0)), - 'blue': ((0.0, 0.0, 0.0), - (1.0, 1.0, 1.0))} - _CMAPS['blue'] = matplotlib.colors.LinearSegmentedColormap( - 'blue', cdict, 256) - - # Temperature as defined in spslut - cdict = {'red': ((0.0, 0.0, 0.0), - (0.5, 0.0, 0.0), - (0.75, 1.0, 1.0), - (1.0, 1.0, 1.0)), - 'green': ((0.0, 0.0, 0.0), - (0.25, 1.0, 1.0), - (0.75, 1.0, 1.0), - (1.0, 0.0, 0.0)), - 'blue': ((0.0, 1.0, 1.0), - (0.25, 1.0, 1.0), - (0.5, 0.0, 0.0), - (1.0, 0.0, 0.0))} - # but limited to 256 colors for a faster display (of the colorbar) - _CMAPS['temperature'] = \ - matplotlib.colors.LinearSegmentedColormap( - 'temperature', cdict, 256) - - # reversed gray - cdict = {'red': ((0.0, 1.0, 1.0), - (1.0, 0.0, 0.0)), - 'green': ((0.0, 1.0, 1.0), - (1.0, 0.0, 0.0)), - 'blue': ((0.0, 1.0, 1.0), - (1.0, 0.0, 0.0))} - - _CMAPS['reversed gray'] = \ - matplotlib.colors.LinearSegmentedColormap( - 'yerg', cdict, 256) - - if name in _CMAPS: - return _CMAPS[name] - elif hasattr(MPLColormap, name): # viridis and sister colormaps - return getattr(MPLColormap, name) - else: - # matplotlib built-in - return matplotlib.cm.get_cmap(name) - - -def getMPLScalarMappable(colormap, data=None): - """Returns matplotlib ScalarMappable corresponding to colormap - - :param dict colormap: The colormap to convert - :param numpy.ndarray data: - The data on which the colormap is applied. - If provided, it is used to compute autoscale. - :return: matplotlib object corresponding to colormap - :rtype: matplotlib.cm.ScalarMappable - """ - assert colormap is not None - - if colormap['name'] is not None: - cmap = getMPLColormap(colormap['name']) - - else: # No name, use custom colors - if 'colors' not in colormap: - raise ValueError( - 'addImage: colormap no name nor list of colors.') - colors = numpy.array(colormap['colors'], copy=True) - assert len(colors.shape) == 2 - assert colors.shape[-1] in (3, 4) - if colors.dtype == numpy.uint8: - # Convert to float in [0., 1.] - colors = colors.astype(numpy.float32) / 255. - cmap = matplotlib.colors.ListedColormap(colors) - - if colormap['normalization'].startswith('log'): - vmin, vmax = None, None - if not colormap['autoscale']: - if colormap['vmin'] > 0.: - vmin = colormap['vmin'] - if colormap['vmax'] > 0.: - vmax = colormap['vmax'] - - if vmin is None or vmax is None: - _logger.warning('Log colormap with negative bounds, ' + - 'changing bounds to positive ones.') - elif vmin > vmax: - _logger.warning('Colormap bounds are inverted.') - vmin, vmax = vmax, vmin - - # Set unset/negative bounds to positive bounds - if (vmin is None or vmax is None) and data is not None: - finiteData = data[numpy.isfinite(data)] - posData = finiteData[finiteData > 0] - if vmax is None: - # 1. as an ultimate fallback - vmax = posData.max() if posData.size > 0 else 1. - if vmin is None: - vmin = posData.min() if posData.size > 0 else vmax - if vmin > vmax: - vmin = vmax - - norm = matplotlib.colors.LogNorm(vmin, vmax) - - else: # Linear normalization - if colormap['autoscale']: - if data is None: - vmin, vmax = None, None - else: - finiteData = data[numpy.isfinite(data)] - vmin = finiteData.min() - vmax = finiteData.max() - else: - vmin = colormap['vmin'] - vmax = colormap['vmax'] - if vmin > vmax: - _logger.warning('Colormap bounds are inverted.') - vmin, vmax = vmax, vmin - - norm = matplotlib.colors.Normalize(vmin, vmax) - - return matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap) - - +@deprecated(replacement='silx.gui.plot.Colormap.applyColormap') def applyColormapToData(data, name='gray', normalization='linear', @@ -324,36 +170,19 @@ def applyColormapToData(data, :return: The computed RGBA image :rtype: numpy.ndarray of uint8 """ - # Debian 7 specific support - # No transparent colormap with matplotlib < 1.2.0 - # Add support for transparent colormap for uint8 data with - # colormap with 256 colors, linear norm, [0, 255] range - if matplotlib.__version__ < '1.2.0': - if name is None and colors is not None: - colors = numpy.array(colors, copy=False) - if (colors.shape[-1] == 4 and - not numpy.all(numpy.equal(colors[3], 255))): - # This is a transparent colormap - if (colors.shape == (256, 4) and - normalization == 'linear' and - not autoscale and - vmin == 0 and vmax == 255 and - data.dtype == numpy.uint8): - # Supported case, convert data to RGBA - return colors[data.reshape(-1)].reshape( - data.shape + (4,)) - else: - _logger.warning( - 'matplotlib %s does not support transparent ' - 'colormap.', matplotlib.__version__) - - colormap = dict(name=name, - normalization=normalization, - autoscale=autoscale, - vmin=vmin, - vmax=vmax, - colors=colors) - scalarMappable = getMPLScalarMappable(colormap, data) - rgbaImage = scalarMappable.to_rgba(data, bytes=True) - - return rgbaImage + colormap = Colormap(name=name, + normalization=normalization, + vmin=vmin, + vmax=vmax, + colors=colors) + return colormap.applyToData(data) + + +@deprecated(replacement='silx.gui.plot.Colormap.getSupportedColormaps') +def getSupportedColormaps(): + """Get the supported colormap names as a tuple of str. + + The list should at least contain and start by: + ('gray', 'reversed gray', 'temperature', 'red', 'green', 'blue') + """ + return Colormap.getSupportedColormaps() -- cgit v1.2.3