/usr/lib/python2.7/dist-packages/chaco/tests/datarange_1d_test_case.py is in python-chaco 4.5.0-1.
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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | import unittest
from numpy import arange, array, zeros, inf
from numpy.testing import assert_equal
from traits.api import HasTraits, Instance, Bool, on_trait_change
from chaco.api import DataRange1D, ArrayDataSource
class Foo(HasTraits):
"""
This class is used to test the firing of the `updated` event of DataRange1D.
"""
range = Instance(DataRange1D)
range_updated = Bool(False)
@on_trait_change('range.updated')
def range_changed(self):
self.range_updated = True
class DataRangeTestCase(unittest.TestCase):
def test_empty_range(self):
r = DataRange1D()
self.assertEqual(r.low, -inf)
self.assertEqual(r.high, inf)
self.assertEqual(r.low_setting, "auto")
self.assertEqual(r.high_setting, "auto")
r.low = 5.0
r.high = 10.0
self.assertEqual(r.low_setting, 5.0)
self.assertEqual(r.high_setting, 10.0)
self.assertEqual(r.low, 5.0)
self.assertEqual(r.high, 10.0)
return
def test_set_bounds1(self):
"""Change both low and high with set_bounds()."""
foo = Foo(range=DataRange1D(low=0.0, high=1.0))
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and set the bounds with set_bounds().
foo.range_updated = False
foo.range.set_bounds(-1.0, 2.0)
# Verify the values.
self.assertEqual(foo.range.low, -1.0)
self.assertEqual(foo.range.high, 2.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_set_bounds2(self):
"""Change only the high value with set_bounds()."""
foo = Foo(range=DataRange1D(low=0.0, high=1.0))
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and set the bounds with set_bounds().
foo.range_updated = False
foo.range.set_bounds(0.0, 2.0)
# Verify the values.
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 2.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_set_bounds3(self):
"""Change only the low value with set_bounds()."""
foo = Foo(range=DataRange1D(low=0.0, high=1.0))
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and set the bounds with set_bounds().
foo.range_updated = False
foo.range.set_bounds(0.5, 1.0)
# Verify the values.
self.assertEqual(foo.range.low, 0.5)
self.assertEqual(foo.range.high, 1.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_set_bounds4(self):
"""Set set_bounds() with high='track'."""
foo = Foo(range=DataRange1D(tracking_amount=1.0))
foo.range.low_setting = 0.0
foo.range.high_setting = 'track'
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and set the bounds with set_bounds().
foo.range_updated = False
foo.range.set_bounds(100.0, 'track')
print foo.range.low, foo.range.high
# Verify the values.
self.assertEqual(foo.range.low, 100.0)
self.assertEqual(foo.range.high, 101.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_set_bounds5(self):
"""Set set_bounds() with low='track'."""
foo = Foo(range=DataRange1D(tracking_amount=1.0))
foo.range.low_setting = 'track'
foo.range.high_setting = 1.0
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and set the bounds with set_bounds().
foo.range_updated = False
foo.range.set_bounds('track', 100.0)
# Verify the values.
self.assertEqual(foo.range.low, 99.0)
self.assertEqual(foo.range.high, 100.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_set_tracking_amount(self):
"""Test setting the tracking amount using the set_tracking_amount() method."""
foo = Foo(range=DataRange1D(tracking_amount=1.0))
foo.range.low_setting = 'track'
foo.range.high_setting = 1.0
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and change the tracking amount.
foo.range_updated = False
foo.range.set_tracking_amount(2.0)
# Verify the values.
self.assertEqual(foo.range.low, -1.0)
self.assertEqual(foo.range.high, 1.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_scale_tracking_amount(self):
"""Test setting the tracking amount using the scale_tracking_amount() method."""
foo = Foo(range=DataRange1D(tracking_amount=1.0))
foo.range.low_setting = 'track'
foo.range.high_setting = 1.0
# Paranoid check first (not the main point of this test):
self.assertEqual(foo.range.low, 0.0)
self.assertEqual(foo.range.high, 1.0)
# Now reset foo's range_updated flag and change the tracking amount.
foo.range_updated = False
foo.range.scale_tracking_amount(0.5)
# Verify the values.
self.assertEqual(foo.range.low, 0.5)
self.assertEqual(foo.range.high, 1.0)
# Verify that the `updated` event fired.
self.assert_(foo.range_updated)
def test_single_source(self):
r = DataRange1D()
ary = arange(10.0)
ds = ArrayDataSource(ary)
r.sources.append(ds)
self.assertEqual(r.low, 0.0)
self.assertEqual(r.high, 9.0)
r.low = 3.0
r.high = 6.0
self.assertEqual(r.low_setting, 3.0)
self.assertEqual(r.high_setting, 6.0)
self.assertEqual(r.low, 3.0)
self.assertEqual(r.high, 6.0)
r.refresh()
self.assertEqual(r.low_setting, 3.0)
self.assertEqual(r.high_setting, 6.0)
self.assertEqual(r.low, 3.0)
self.assertEqual(r.high, 6.0)
r.low = "auto"
self.assertEqual(r.low_setting, "auto")
self.assertEqual(r.low, 0.0)
return
def test_constant_value(self):
r = DataRange1D()
ary = array([3.14])
ds = ArrayDataSource(ary)
r.add(ds)
# A constant value > 1.0, by default, gets a range that brackets
# it to the nearest power of ten above and below
self.assertEqual(r.low, 1.0)
self.assertEqual(r.high, 10.0)
r.remove(ds)
ds = ArrayDataSource(array([31.4]))
r.add(ds)
self.assertEqual(r.low, 10.0)
self.assertEqual(r.high, 100.0)
r.remove(ds)
ds = ArrayDataSource(array([0.125]))
r.add(ds)
self.assertEqual(r.low, 0.0)
self.assertEqual(r.high, 0.25)
r.remove(ds)
ds = ArrayDataSource(array([-0.125]))
r.add(ds)
self.assertEqual(r.low, -0.25)
self.assertEqual(r.high, 0.0)
return
def test_multi_source(self):
ds1 = ArrayDataSource(array([3, 4, 5, 6, 7]))
ds2 = ArrayDataSource(array([5, 10, 15, 20]))
r = DataRange1D(ds1, ds2)
self.assertEqual(r.low, 3.0)
self.assertEqual(r.high, 20.0)
return
def test_clip_data(self):
r = DataRange1D(low=2.0, high=10.0)
ary = array([1, 3, 4, 9.8, 10.2, 12])
assert_equal(r.clip_data(ary) , array([3.0,4.0,9.8]))
r = DataRange1D(low=10, high=20)
ary = array([5, 10, 15, 20, 25, 30])
assert_equal(r.clip_data(ary) , array([10, 15, 20]))
assert_equal(r.clip_data(ary[::-1]) , array([20, 15, 10]))
r = DataRange1D(low=2.0, high=2.5)
assert_equal(len(r.clip_data(ary)) , 0)
return
def test_mask_data(self):
r = DataRange1D(low=2.0, high=10.0)
ary = array([1, 3, 4, 9.8, 10.2, 12])
assert_equal(r.mask_data(ary) , array([0,1,1,1,0,0], 'b'))
r = DataRange1D(low=10, high=20)
ary = array([5, 10, 15, 20, 25, 30])
target_mask = array([0,1,1,1,0,0], 'b')
assert_equal(r.mask_data(ary) , target_mask)
assert_equal(r.mask_data(ary[::-1]) , target_mask[::-1])
r = DataRange1D(low=2.0, high=2.5)
assert_equal(r.mask_data(ary) , zeros(len(ary)))
return
def test_bound_data(self):
r = DataRange1D(low=2.9, high=6.1)
ary = arange(10)
assert_equal(r.bound_data(ary) , (3,6))
# test non-monotonic data
ary = array([-5,-4,-7,-8,-2,1,2,3,4,5,4,3,8,9,10,9,8])
bounds = r.bound_data(ary)
assert_equal(bounds , (7,11))
return
def test_custom_bounds_func(self):
def custom_func(low, high, margin, tight_bounds):
assert_equal(low, 0.0)
assert_equal(high, 9.0)
assert_equal(tight_bounds, False)
assert_equal(margin, 1.0)
return -999., 999.
r = DataRange1D(tight_bounds=False, margin=1.0, bounds_func=custom_func)
ary = arange(10.0)
ds = ArrayDataSource(ary)
r.sources.append(ds)
assert_equal(r.low, -999.)
assert_equal(r.high, 999.)
def test_inf_in_source(self):
r = DataRange1D()
ary1 = array([1.0, inf])
ds1 = ArrayDataSource(ary1)
r.sources.append(ds1)
self.assertEqual(r.low, 1.0)
self.assertEqual(r.high, inf)
data = array([-100.0, 0.0, 100.0])
assert_equal(r.clip_data(data) , array([100.0]))
r = DataRange1D()
ary2 = array([-inf, 1.0])
ds2 = ArrayDataSource(ary2)
r.sources.append(ds2)
self.assertEqual(r.low, -inf)
self.assertEqual(r.high, 1.0)
r.sources.append(ds1)
self.assertEqual(r.low, -inf)
self.assertEqual(r.high, inf)
if __name__ == '__main__':
import nose
nose.run()
|