/usr/lib/python2.7/dist-packages/chaco/tests/colormapper_test_case.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 | import unittest
from numpy import allclose, array, ravel
from chaco.api import ArrayDataSource, ColorMapper, DataRange1D
class LinearSegmentedColormapTestCase(unittest.TestCase):
def setUp(self):
""" Set up called before each test case. """
_gray_data = {'red': [(0., 0, 0), (1., 1.0, 1.0)],
'green': [(0., 0, 0), (1., 1.0, 1.0)],
'blue': [(0., 0, 0), (1., 1.0, 1.0)]}
self.colormap = ColorMapper.from_segment_map(_gray_data)
self.colormap.range = DataRange1D()
def test_simple_map(self):
a = ArrayDataSource(array([0.0, 0.5, 1.0]))
self.colormap.range.add(a)
b = self.colormap.map_screen(a.get_data())
self.colormap.range.remove(a)
expected = array([0.0, 0.5, 1.0])
close = allclose(ravel(b[:,:1]), expected, atol=0.02)
self.assert_(close,
"Simple map failed. Expected %s. Got %s" % (expected, b[:,:1]))
return
def test_change_min_max(self):
""" Test that changing min_value and max_value does not break map. """
datarange = self.colormap.range
# Perform a dummy mapping.
a = ArrayDataSource(array([0.0, 0.5, 1.0]))
datarange.add(a)
b = self.colormap.map_screen(a.get_data())
datarange.remove(a)
# Update the min_value.
datarange.low = -1.0
# Test that the map still works.
a = ArrayDataSource(array([-1.0, 0.0, 1.0]))
datarange.add(a)
b = self.colormap.map_screen(a.get_data())
datarange.remove(a)
expected = array([0.0, 0.5, 1.0])
close = allclose(ravel(b[:,:1]), expected, atol=0.02)
self.assert_(close,
"Changing min value broke map. Expected %s. Got %s" % (expected, b[:,:1]))
# Update the max_value.
datarange.high = 0.0
# Test that the map still works.
a = ArrayDataSource(array([-1.0, -0.5, 0.0]))
datarange.add(a)
b = self.colormap.map_screen(a.get_data())
datarange.remove(a)
expected = array([0.0, 0.5, 1.0])
close = allclose(ravel(b[:,:1]), expected, atol=0.02)
self.assert_(close,
"Changing min value broke map. Expected %s. Got %s" % (expected, b[:,:1]))
return
def test_array_factory(self):
""" Test that the array factory creates valid colormap. """
colors = array([[0.0,0.0,0.0], [1.0,1.0,1.0]])
cm = ColorMapper.from_palette_array(colors)
cm.range = DataRange1D()
ar = ArrayDataSource(array([0.0, 0.5, 1.0]))
cm.range.add(ar)
b = cm.map_screen(ar.get_data())
cm.range.remove(ar)
expected = array([0.0, 0.5, 1.0])
self.assertTrue(allclose(ravel(b[:,:1]), expected, atol=0.02),
"Array factory failed. Expected %s. Got %s" % (expected, b[:,:1]))
return
def test_alpha_palette(self):
""" Create a colormap with a varying alpha channel from a palette array.
"""
cm = ColorMapper.from_palette_array([[0.0,0.0,0.0,0.5],[1.0,1.0,1.0,1.0]])
sd = {'alpha': [(0.0, 0.5, 0.5), (1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
'red': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]}
assert cm._segmentdata == sd
def test_alpha_segment_data(self):
""" Create a colormap with a varying alpha channel from segment data.
"""
sd = {'alpha': [(0.0, 0.5, 0.5), (1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
'red': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]}
cm = ColorMapper.from_segment_map(sd)
assert cm._segmentdata == sd
def test_no_alpha(self):
""" Check that the defaults when no alpha is specified are correct.
"""
sd = {'alpha': [(0.0, 1.0, 1.0), (1.0, 1.0, 1.0)],
'blue': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
'green': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)],
'red': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)]}
assert self.colormap._segmentdata == sd
## def test_no_interpolation(self):
## grayscale_colors = array([[0.0,0.0,0.0,1.0], [1.0, 1.0, 1.0, 1.0]])
## grayscale_bins = array([0.0, 1.0])
## grayscale_steps = array([1])
## colormap = LinearSegmentedColormap(
## grayscale_colors, grayscale_bins, grayscale_steps
## )
## a = array([0.0, 0.25, 0.75, 1.0])
## b = colormap.map_array(a)
## result = ravel(b[:,:1])
## expected = array([0.0, 0.0, 1.0, 1.0])
## close = allclose(result, expected, atol=0.02)
## self.assert_(close,
## "Map with no interpolation broken. Expected %s. Got %s" % (expected, result))
## def test_value_bands(self):
## grayscale_colors = array([[0.0,0.0,0.0,1.0], [1.0, 1.0, 1.0, 1.0]])
## grayscale_bins = array([0.0, 1.0])
## grayscale_steps = array([1])
## colormap = LinearSegmentedColormap(
## grayscale_colors, grayscale_bins, grayscale_steps
## )
## colormap._recalculate()
## print '**************', colormap._color_bands, colormap._value_bands
if __name__ == '__main__':
import nose
nose.run()
|