/usr/lib/python2.7/dist-packages/chaco/cmap_image_plot.py is in python-chaco 4.4.1-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | #
# (C) Copyright 2013 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
# LICENSE.txt
#
from numpy import zeros
# Enthought library imports.
from traits.api import Any, Bool, Float, Instance, Property, Tuple
# Local relative imports
from image_plot import ImagePlot
from abstract_colormap import AbstractColormap
from speedups import apply_selection_fade
class CMapImagePlot(ImagePlot):
""" Colormapped image plot. Takes a value data object whose elements are
scalars, and renders them as a colormapped image.
"""
# TODO: Modify ImageData to explicitly support scalar value arrays
#------------------------------------------------------------------------
# Data-related traits
#------------------------------------------------------------------------
# Maps from scalar data values in self.data.value to color tuples
value_mapper = Instance(AbstractColormap)
# Convenience property for value_mapper as color_mapper
color_mapper = Property
# Convenience property for accessing the data range of the mapper.
value_range = Property
# alpha value to use to fade out unselected data points when there is an
# active selection
fade_alpha = Float(0.3)
#fade_background = Tuple((255,255,255))
# RGB color to use to fade out unselected points.
fade_background = Tuple((0,0,0))
# whether to pre-compute the full colormapped RGB(A) image
cache_full_map = Bool(True)
#------------------------------------------------------------------------
# Private Traits
#------------------------------------------------------------------------
# Is the mapped image valid?
_mapped_image_cache_valid = Bool(False)
# Cache of the fully mapped RGB(A) image.
_cached_mapped_image = Any
#------------------------------------------------------------------------
# Public methods
#------------------------------------------------------------------------
def __init__(self, **kwargs):
super(CMapImagePlot, self).__init__(**kwargs)
if self.value_mapper:
self.value_mapper.on_trait_change(self._update_value_mapper,
"updated")
if self.value:
self.value.on_trait_change(self._update_selections,
"metadata_changed")
def set_value_selection(self, val):
""" Sets a range of values in the value data source as selected.
"""
if val is not None:
low, high = val
data = self.value.get_data()
new_mask = (data>=low) & (data<=high)
self.value.metadata["selection_masks"] = [new_mask]
else:
del self.value.metadata["selection_masks"]
self._update_selections()
#------------------------------------------------------------------------
# Base2DPlot interface
#------------------------------------------------------------------------
def _render(self, gc):
""" Ensures that the cached image is valid.
Called before _render() is called. Implements the Base2DPlot interface.
"""
if not self._mapped_image_cache_valid:
if 'selection_masks' in self.value.metadata:
self._compute_cached_image(self.value.metadata['selection_masks'])
else:
self._compute_cached_image()
ImagePlot._render(self, gc)
#------------------------------------------------------------------------
# Private methods
#------------------------------------------------------------------------
def _cmap_values(self, data, selection_masks=None):
""" Maps the data to RGB(A) with optional selection masks overlayed
"""
# get the RGBA values from the color map as uint8
mapped_image = self.value_mapper.map_uint8(data)
if selection_masks is not None:
# construct a composite mask
if len(selection_masks) > 0:
mask = zeros(mapped_image.shape[:2], dtype=bool)
for m in selection_masks:
mask = mask | m
else:
mask = zeros(self._cached_mapped_image.shape[:2], dtype=bool)
# Apply the selection fade, from speedups.py
apply_selection_fade(mapped_image, mask,
self.fade_alpha, self.fade_background)
return mapped_image
def _compute_cached_image(self, selection_masks=None):
""" Updates the cached image.
"""
if self.cache_full_map:
if not self._mapped_image_cache_valid:
self._cached_mapped_image = self._cmap_values(self.value.data,
selection_masks)
self._mapped_image_cache_valid = True
mapped_value = self._cached_mapped_image
ImagePlot._compute_cached_image(self, mapped_value)
else:
self._mapped_image_cache_valid = True
ImagePlot._compute_cached_image(self, self.value.data, mapper=lambda data:
self._cmap_values(data))
def _update_value_mapper(self):
self._mapped_image_cache_valid = False
self._image_cache_valid = False
self.invalidate_draw()
def _update_selections(self):
self._mapped_image_cache_valid = False
self._image_cache_valid = False
self.invalidate_draw()
#------------------------------------------------------------------------
# Properties
#------------------------------------------------------------------------
def _get_value_range(self):
return self.value_mapper.range
def _set_value_range(self, val):
self.value_mapper.range = val
def _get_color_mapper(self):
return self.value_mapper
def _set_color_mapper(self, val):
self.value_mapper = val
#------------------------------------------------------------------------
# Event handlers
#------------------------------------------------------------------------
def _value_mapper_changed(self, old, new):
if old is not None:
old.on_trait_change(self._update_value_mapper,
"updated", remove=True)
if new is not None:
new.on_trait_change(self._update_value_mapper, "updated")
if old and new:
if new.range is None and old.range is not None:
new.range = old.range
self._update_value_mapper()
def _value_data_changed_fired(self):
super(CMapImagePlot, self)._value_data_changed_fired()
self._mapped_image_cache_valid = False
return
def _index_data_changed_fired(self):
super(CMapImagePlot, self)._index_data_changed_fired()
self._mapped_image_cache_valid = False
return
def _cache_full_map_changed(self):
self._mapped_image_cache_valid = False
|