def recalculate(self):
     if self.data_range is not None:
         low, high = self.data_range.low, self.data_range.high
         x = _xfunc(low, high)
         new_array = p_yes(self.a, self.m, self.k, x)
         ArrayDataSource.set_data(self, new_array)
     else:
         self._data = np.array([], dtype='f')
    def test_reverse_construction(self):
        mapper = LinearMapper()
        r = DataRange1D()
        ds = ArrayDataSource()
        ary = array([1, 2, 3, 4, 5, 6, 7])

        mapper.range = r
        mapper.low_pos = 1.0
        mapper.high_pos = 7.0
        r.add(ds)
        ds.set_data(ary)

        self.assertTrue(r.low == 1)
        self.assertTrue(r.high == 7)
        screen_pts = mapper.map_screen(array([1, 3, 7]))
        self.assertTrue(tuple(screen_pts) == (1.0, 3.0, 7.0))
        return
    def test_reverse_construction(self):
        mapper = LinearMapper()
        r = DataRange1D()
        ds = ArrayDataSource()
        ary = array([1,2,3,4,5,6,7])

        mapper.range = r
        mapper.low_pos = 1.0
        mapper.high_pos = 7.0
        r.add(ds)
        ds.set_data(ary)

        self.assertTrue(r.low == 1)
        self.assertTrue(r.high == 7)
        screen_pts = mapper.map_screen(array([1,3,7]))
        self.assertTrue(tuple(screen_pts) == (1.0, 3.0, 7.0))
        return
示例#4
0
    def test_piecewise_construction(self):
        ary = array([1, 2, 3, 4, 5, 6, 7])
        ds = ArrayDataSource()
        ds.set_data(ary)
        r = DataRange1D()
        r.add(ds)
        self.assert_(r.low_setting == "auto")
        self.assert_(r.high_setting == "auto")
        self.assert_(r.low == 1)
        self.assert_(r.high == 7)

        mapper = LinearMapper()
        mapper.range = r
        mapper.low_pos = 1.0
        mapper.high_pos = 7.0
        screen_pts = mapper.map_screen(array([1, 3, 7]))
        self.assert_(tuple(screen_pts) == (1.0, 3.0, 7.0))
        return
    def test_piecewise_construction(self):
        ary = array([1,2,3,4,5,6,7])
        ds = ArrayDataSource()
        ds.set_data(ary)
        r = DataRange1D()
        r.add(ds)
        self.assert_(r.low_setting == "auto")
        self.assert_(r.high_setting == "auto")
        self.assert_(r.low == 1)
        self.assert_(r.high == 7)

        mapper = LinearMapper()
        mapper.range = r
        mapper.low_pos = 1.0
        mapper.high_pos = 7.0
        screen_pts = mapper.map_screen(array([1,3,7]))
        self.assert_(tuple(screen_pts) == (1.0, 3.0, 7.0))
        return
示例#6
0
	class PlotterWindow(HasTraits):
		plot = Instance(OverlayPlotContainer)
		timer = Instance(Timer)

		traits_view = View(
			Item('plot',editor=ComponentEditor(),show_label=False),
			width=500,height=500,resizable=True,title="Plotter")

		def __init__(self,data_q,num):
			super(PlotterWindow,self).__init__()

			self.data_q = data_q

			self.x = ArrayDataSource(zeros(num))
			self.y = ArrayDataSource(zeros(num))
			self.v = MultiArrayDataSource(zeros((num,2)))

			xrange = DataRange1D()
			xrange.add(self.x)
			yrange = DataRange1D()
			yrange.add(self.y)

			quiverplot = QuiverPlot(index=self.x,value=self.y,vectors=self.v,
				index_mapper=LinearMapper(range=xrange),
				value_mapper=LinearMapper(range=yrange),
				bgcolor='white')
			add_default_axes(quiverplot)
			add_default_grids(quiverplot)

			quiverplot.tools.append(PanTool(quiverplot,constrain_key='shift'))
			quiverplot.overlays.append(ZoomTool(quiverplot))

			self.plot = OverlayPlotContainer(quiverplot, padding=50)
			self.timer = Timer(50.0, self.onTimer)

			self.configure_traits()

		def onTimer(self,*args):
			d = self.data_q.get()

			self.x.set_data(d[0])
			self.y.set_data(d[1])
			self.v.set_data(d[2])
示例#7
0
class ArrayDataSourceTestCase(UnittestTools, unittest.TestCase):
    def setUp(self):
        self.myarray = arange(10)
        self.mymask = array([i % 2 for i in self.myarray], dtype=bool)
        self.data_source = ArrayDataSource(self.myarray)

    def test_init_defaults(self):
        data_source = ArrayDataSource()
        assert_array_equal(data_source._data, [])
        self.assertEqual(data_source.value_dimension, "scalar")
        self.assertEqual(data_source.index_dimension, "scalar")
        self.assertEqual(data_source.sort_order, "none")
        self.assertFalse(data_source.is_masked())
        self.assertEqual(data_source.persist_data, True)

    def test_basic_setup(self):
        assert_array_equal(self.myarray, self.data_source._data)
        self.assertEqual(self.data_source.value_dimension, "scalar")
        self.assertEqual(self.data_source.sort_order, "none")
        self.assertFalse(self.data_source.is_masked())

    def test_set_data(self):
        new_array = arange(0, 20, 2)

        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.set_data(new_array)

        assert_array_equal(new_array, self.data_source._data)
        self.assertEqual(self.data_source.get_bounds(), (0, 18))
        self.assertEqual(self.data_source.sort_order, "none")

    def test_set_data_ordered(self):
        new_array = arange(20, 0, -2)

        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.set_data(new_array, sort_order='descending')

        assert_array_equal(new_array, self.data_source._data)
        self.assertEqual(self.data_source.get_bounds(), (2, 20))
        self.assertEqual(self.data_source.sort_order, "descending")

    def test_set_mask(self):
        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.set_mask(self.mymask)

        assert_array_equal(self.myarray, self.data_source._data)
        assert_array_equal(self.mymask, self.data_source._cached_mask)
        self.assertTrue(self.data_source.is_masked())
        self.assertEqual(self.data_source.get_bounds(), (0, 9))

    def test_remove_mask(self):
        self.data_source.set_mask(self.mymask)
        self.assertTrue(self.data_source.is_masked())

        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.remove_mask()

        assert_array_equal(self.myarray, self.data_source._data)
        self.assertIsNone(self.data_source._cached_mask, None)
        self.assertFalse(self.data_source.is_masked())
        self.assertEqual(self.data_source.get_bounds(), (0, 9))

    def test_get_data(self):
        assert_array_equal(self.myarray, self.data_source.get_data())

    def test_get_data_no_data(self):
        data_source = ArrayDataSource(None)

        assert_array_equal(data_source.get_data(), 0.0)

    def test_get_data_mask(self):
        self.data_source.set_mask(self.mymask)

        data, mask = self.data_source.get_data_mask()
        assert_array_equal(data, self.myarray)
        assert_array_equal(mask, self.mymask)

    @unittest.skip('get_data_mask() fails in this case')
    def test_get_data_mask_no_data(self):
        data_source = ArrayDataSource(None)

        data, mask = data_source.get_data_mask()
        assert_array_equal(data, 0.0)
        assert_array_equal(mask, True)

    def test_get_data_mask_no_mask(self):
        data, mask = self.data_source.get_data_mask()
        assert_array_equal(data, self.myarray)
        assert_array_equal(mask, ones(shape=10, dtype=bool))

    def test_bounds(self):
        # ascending
        bounds = self.data_source.get_bounds()
        self.assertEqual(bounds, (0, 9))

        # descending
        myarray = arange(10)[::-1]
        data_source = ArrayDataSource(myarray, sort_order="descending")
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, 9))

        # no order
        myarray = array([12, 3, 0, 9, 2, 18, 3])
        data_source = ArrayDataSource(myarray, sort_order="none")
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, 18))

    def test_bounds_length_one(self):
        # this is special-cased in the code, so exercise the code path
        data_source = ArrayDataSource(array([1.0]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (1.0, 1.0))

    def test_bounds_length_zero(self):
        # this is special-cased in the code, so exercise the code path
        data_source = ArrayDataSource(array([]))
        bounds = data_source.get_bounds()
        # XXX this is sort of inconsistent with test_bounds_all_nans()
        self.assertEqual(bounds, (0, 0))

    def test_bounds_empty(self):
        data_source = ArrayDataSource()
        bounds = data_source.get_bounds()
        # XXX this is sort of inconsistent with test_bounds_all_nans()
        self.assertEqual(bounds, (0, 0))

    def test_bounds_all_nans(self):
        myarray = empty(10)
        myarray[:] = nan
        sd = ArrayDataSource(myarray)
        bounds = sd.get_bounds()
        self.assertTrue(isnan(bounds[0]))
        self.assertTrue(isnan(bounds[1]))

    def test_bounds_some_nan(self):
        data_source = ArrayDataSource(array([np.nan, 3, 0, 9, np.nan, 18, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, 18))

    def test_bounds_negative_inf(self):
        data_source = ArrayDataSource(array([12, 3, -np.inf, 9, 2, 18, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (-np.inf, 18))

    def test_bounds_positive_inf(self):
        data_source = ArrayDataSource(array([12, 3, 0, 9, 2, np.inf, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, np.inf))

    def test_bounds_negative_positive_inf(self):
        data_source = ArrayDataSource(array([12, 3, -np.inf, 9, 2, np.inf, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (-np.inf, np.inf))

    def test_bounds_non_numeric(self):
        myarray = np.array([u'abc', u'foo', u'bar', u'def'],
                           dtype=six.text_type)
        data_source = ArrayDataSource(myarray)
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (u'abc', u'def'))

    def test_data_size(self):
        # We know that ArrayDataTestCase always returns the exact length of
        # its data
        myarray = arange(913)
        data_source = ArrayDataSource(myarray)
        self.assertEqual(len(myarray), data_source.get_size())

    def test_reverse_map(self):
        # sort_order ascending
        myarray = arange(10)
        data_source = ArrayDataSource(myarray, sort_order='ascending')

        self.assertEqual(data_source.reverse_map(4.0), 4)

        # sort_order descending
        myarray = arange(10)[::-1]
        data_source = ArrayDataSource(myarray, sort_order='descending')

        self.assertEqual(data_source.reverse_map(4.0), 5)

        # sort_order none
        myarray = array([12, 3, 0, 9, 2, 18, 3])
        data_source = ArrayDataSource(myarray, sort_order='none')

        with self.assertRaises(NotImplementedError):
            data_source.reverse_map(3)

    def test_metadata(self):
        self.assertEqual(self.data_source.metadata, {
            'annotations': [],
            'selections': []
        })

    def test_metadata_changed(self):
        with self.assertTraitChanges(self.data_source,
                                     'metadata_changed',
                                     count=1):
            self.data_source.metadata = {'new_metadata': True}

    def test_metadata_items_changed(self):
        with self.assertTraitChanges(self.data_source,
                                     'metadata_changed',
                                     count=1):
            self.data_source.metadata['new_metadata'] = True

    def test_serialization_state(self):
        state = self.data_source.__getstate__()
        self.assertNotIn('value_dimension', state)
        self.assertNotIn('index_dimension', state)
        self.assertNotIn('persist_data', state)

    @unittest.skip("persist_data probably shouldn't be persisted")
    def test_serialization_state_no_persist(self):
        self.data_source.persist_data = False

        state = self.data_source.__getstate__()
        self.assertNotIn('value_dimension', state)
        self.assertNotIn('index_dimension', state)
        self.assertNotIn('persist_data', state)
        for key in [
                "_data", "_cached_mask", "_cached_bounds", "_min_index",
                "_max_index"
        ]:
            self.assertIn(key, state)

    @unittest.skip("I think this is just broken")
    def test_serialization_post_load(self):
        self.data_source.set_mask(self.mymask)

        pickled_data_source = pickle.dumps(self.data_source)
        unpickled_data_source = pickle.loads(pickled_data_source)
        unpickled_data_source._post_load()

        self.assertEqual(unpickled_data_source._cached_bounds, ())
        self.assertEqual(unpickled_data_source._cached_mask, None)

        assert_array_equal(self.data_source.get_data(),
                           unpickled_data_source.get_data())

        mask = unpickled_data_source.get_data_mask()[1]
        assert_array_equal(mask, ones(10))
示例#8
0
class ArrayDataSourceTestCase(UnittestTools, unittest.TestCase):

    def setUp(self):
        self.myarray = arange(10)
        self.mymask = array([i % 2 for i in self.myarray], dtype=bool)
        self.data_source = ArrayDataSource(self.myarray)

    def test_init_defaults(self):
        data_source = ArrayDataSource()
        assert_array_equal(data_source._data, [])
        self.assertEqual(data_source.value_dimension, "scalar")
        self.assertEqual(data_source.index_dimension, "scalar")
        self.assertEqual(data_source.sort_order, "none")
        self.assertFalse(data_source.is_masked())
        self.assertEqual(data_source.persist_data, True)

    def test_basic_setup(self):
        assert_array_equal(self.myarray, self.data_source._data)
        self.assertEqual(self.data_source.value_dimension, "scalar")
        self.assertEqual(self.data_source.sort_order, "none")
        self.assertFalse(self.data_source.is_masked())

    def test_set_data(self):
        new_array = arange(0, 20, 2)

        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.set_data(new_array)

        assert_array_equal(new_array, self.data_source._data)
        self.assertEqual(self.data_source.get_bounds(), (0, 18))
        self.assertEqual(self.data_source.sort_order, "none")

    def test_set_data_ordered(self):
        new_array = arange(20, 0, -2)

        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.set_data(new_array, sort_order='descending')

        assert_array_equal(new_array, self.data_source._data)
        self.assertEqual(self.data_source.get_bounds(), (2, 20))
        self.assertEqual(self.data_source.sort_order, "descending")

    def test_set_mask(self):
        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.set_mask(self.mymask)

        assert_array_equal(self.myarray, self.data_source._data)
        assert_array_equal(self.mymask, self.data_source._cached_mask)
        self.assertTrue(self.data_source.is_masked())
        self.assertEqual(self.data_source.get_bounds(), (0, 9))

    def test_remove_mask(self):
        self.data_source.set_mask(self.mymask)
        self.assertTrue(self.data_source.is_masked())

        with self.assertTraitChanges(self.data_source, 'data_changed',
                                     count=1):
            self.data_source.remove_mask()

        assert_array_equal(self.myarray, self.data_source._data)
        self.assertIsNone(self.data_source._cached_mask, None)
        self.assertFalse(self.data_source.is_masked())
        self.assertEqual(self.data_source.get_bounds(), (0, 9))

    def test_get_data(self):
        assert_array_equal(self.myarray, self.data_source.get_data())

    def test_get_data_no_data(self):
        data_source = ArrayDataSource(None)

        assert_array_equal(data_source.get_data(), 0.0)

    def test_get_data_mask(self):
        self.data_source.set_mask(self.mymask)

        data, mask = self.data_source.get_data_mask()
        assert_array_equal(data, self.myarray)
        assert_array_equal(mask, self.mymask)

    @unittest.skip('get_data_mask() fails in this case')
    def test_get_data_mask_no_data(self):
        data_source = ArrayDataSource(None)

        data, mask = data_source.get_data_mask()
        assert_array_equal(data, 0.0)
        assert_array_equal(mask, True)

    def test_get_data_mask_no_mask(self):
        data, mask = self.data_source.get_data_mask()
        assert_array_equal(data, self.myarray)
        assert_array_equal(mask, ones(shape=10, dtype=bool))

    def test_bounds(self):
        # ascending
        bounds = self.data_source.get_bounds()
        self.assertEqual(bounds, (0, 9))

        # descending
        myarray = arange(10)[::-1]
        data_source = ArrayDataSource(myarray, sort_order="descending")
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, 9))

        # no order
        myarray = array([12, 3, 0, 9, 2, 18, 3])
        data_source = ArrayDataSource(myarray, sort_order="none")
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, 18))

    def test_bounds_length_one(self):
        # this is special-cased in the code, so exercise the code path
        data_source = ArrayDataSource(array([1.0]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (1.0, 1.0))

    def test_bounds_length_zero(self):
        # this is special-cased in the code, so exercise the code path
        data_source = ArrayDataSource(array([]))
        bounds = data_source.get_bounds()
        # XXX this is sort of inconsistent with test_bounds_all_nans()
        self.assertEqual(bounds, (0, 0))

    def test_bounds_empty(self):
        data_source = ArrayDataSource()
        bounds = data_source.get_bounds()
        # XXX this is sort of inconsistent with test_bounds_all_nans()
        self.assertEqual(bounds, (0, 0))

    def test_bounds_all_nans(self):
        myarray = empty(10)
        myarray[:] = nan
        sd = ArrayDataSource(myarray)
        bounds = sd.get_bounds()
        self.assertTrue(isnan(bounds[0]))
        self.assertTrue(isnan(bounds[1]))

    def test_bounds_some_nan(self):
        data_source = ArrayDataSource(array([np.nan, 3, 0, 9, np.nan, 18, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, 18))

    def test_bounds_negative_inf(self):
        data_source = ArrayDataSource(array([12, 3, -np.inf, 9, 2, 18, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (-np.inf, 18))

    def test_bounds_positive_inf(self):
        data_source = ArrayDataSource(array([12, 3, 0, 9, 2, np.inf, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (0, np.inf))

    def test_bounds_negative_positive_inf(self):
        data_source = ArrayDataSource(array([12, 3, -np.inf, 9, 2, np.inf, 3]))
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (-np.inf, np.inf))

    def test_bounds_non_numeric(self):
        myarray = np.array([u'abc', u'foo', u'bar', u'def'], dtype=unicode)
        data_source = ArrayDataSource(myarray)
        bounds = data_source.get_bounds()
        self.assertEqual(bounds, (u'abc', u'def'))

    def test_data_size(self):
        # We know that ArrayDataTestCase always returns the exact length of
        # its data
        myarray = arange(913)
        data_source = ArrayDataSource(myarray)
        self.assertEqual(len(myarray), data_source.get_size())

    def test_reverse_map(self):
        # sort_order ascending
        myarray = arange(10)
        data_source = ArrayDataSource(myarray, sort_order='ascending')

        self.assertEqual(data_source.reverse_map(4.0), 4)

        # sort_order descending
        myarray = arange(10)[::-1]
        data_source = ArrayDataSource(myarray, sort_order='descending')

        self.assertEqual(data_source.reverse_map(4.0), 5)

        # sort_order none
        myarray = array([12, 3, 0, 9, 2, 18, 3])
        data_source = ArrayDataSource(myarray, sort_order='none')

        with self.assertRaises(NotImplementedError):
            data_source.reverse_map(3)

    def test_metadata(self):
        self.assertEqual(self.data_source.metadata,
                         {'annotations': [], 'selections': []})

    def test_metadata_changed(self):
        with self.assertTraitChanges(self.data_source, 'metadata_changed',
                                     count=1):
            self.data_source.metadata = {'new_metadata': True}

    def test_metadata_items_changed(self):
        with self.assertTraitChanges(self.data_source, 'metadata_changed',
                                     count=1):
            self.data_source.metadata['new_metadata'] = True

    def test_serialization_state(self):
        state = self.data_source.__getstate__()
        self.assertNotIn('value_dimension', state)
        self.assertNotIn('index_dimension', state)
        self.assertNotIn('persist_data', state)

    @unittest.skip("persist_data probably shouldn't be persisted")
    def test_serialization_state_no_persist(self):
        self.data_source.persist_data = False

        state = self.data_source.__getstate__()
        self.assertNotIn('value_dimension', state)
        self.assertNotIn('index_dimension', state)
        self.assertNotIn('persist_data', state)
        for key in ["_data", "_cached_mask", "_cached_bounds", "_min_index",
                    "_max_index"]:
            self.assertIn(key, state)

    @unittest.skip("I think this is just broken")
    def test_serialization_post_load(self):
        self.data_source.set_mask(self.mymask)

        pickled_data_source = pickle.dumps(self.data_source)
        unpickled_data_source = pickle.loads(pickled_data_source)
        unpickled_data_source._post_load()

        self.assertEqual(unpickled_data_source._cached_bounds, ())
        self.assertEqual(unpickled_data_source._cached_mask, None)

        assert_array_equal(self.data_source.get_data(),
                           unpickled_data_source.get_data())

        mask = unpickled_data_source.get_data_mask()[1]
        assert_array_equal(mask, ones(10))