def test_create(self): # Empty selector, empty data: pm = PortMapper('') assert_series_equal(pm.portmap, pd.Series([], dtype=np.int64)) assert_array_equal(pm.data, np.array([])) # Non-empty selector, empty data: pm = PortMapper('/foo[0:3]') assert_series_equal( pm.portmap, pd.Series( np.arange(3), pd.MultiIndex(levels=[['foo'], [0, 1, 2]], labels=[[0, 0, 0], [0, 1, 2]], names=[0, 1]))) assert_array_equal(pm.data, np.array([])) # Empty selector, non-empty data: self.assertRaises(Exception, PortMapper, '', [1, 2, 3]) # Non-empty selector, non-empty data: data = np.random.rand(5) portmap = np.arange(5) pm = PortMapper('/foo[0:5]', data, portmap) assert_array_equal(pm.data, data) s = pd.Series( np.arange(5), pd.MultiIndex(levels=[['foo'], [0, 1, 2, 3, 4]], labels=[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4]], names=[0, 1])) assert_series_equal(pm.portmap, s)
def test_get(self): # Mapper with data: pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) np.allclose(self.data[0:10], pm['/foo/bar[0:10]']) pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]') # Mapper without data: self.assertRaises(Exception, pm.__getitem__, '/foo/bar[0]')
def test_set(self): # Mapper with data: pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) pm['/foo/baz[0:5]'] = 1.0 np.allclose(np.ones(5), pm['/foo/baz[0:5]']) # Mapper without data: pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]') self.assertRaises(Exception, pm.__setitem__, '/foo/bar[0]', 0)
def test_from_pm(self): # Ensure that modifying pm0 doesn't modify any other mapper created from it: data = np.random.rand(5) portmap = np.arange(5) pm0 = PortMapper('/foo[0:5]', data, portmap) pm1 = PortMapper('/foo[0:5]', data, portmap) pm2 = PortMapper.from_pm(pm0) data[0] = 1.0 pm0.data[1] = 1.0 pm0.portmap[('foo', 0)] = 10 assert_array_equal(pm2.data, pm1.data) assert_series_equal(pm2.portmap, pm1.portmap)
def test_get_ports(self): pm = PortMapper(np.arange(10), '/foo/bar[0:10]') self.assertSequenceEqual(pm.get_ports(lambda x: x < 5), [('foo', 'bar', 0), ('foo', 'bar', 1), ('foo', 'bar', 2), ('foo', 'bar', 3), ('foo', 'bar', 4)]) i = np.array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0], dtype=np.bool) self.assertSequenceEqual(pm.get_ports(i), [('foo', 'bar', 0), ('foo', 'bar', 1), ('foo', 'bar', 2), ('foo', 'bar', 3), ('foo', 'bar', 4)])
def test_copy(self): # Ensure that modifying pm0 doesn't modify any other mapper created from it: data = np.random.rand(5) portmap = np.arange(5) pm0 = PortMapper('/foo[0:5]', data, portmap) pm1 = PortMapper('/foo[0:5]', data, portmap) pm2 = pm0.copy() data[0] = 1.0 pm0.data[1] = 1.0 pm0.portmap[('foo', 0)] = 10 assert_array_equal(pm2.data, pm1.data) assert_series_equal(pm2.portmap, pm1.portmap) data = np.random.rand(5) pm0 = PortMapper('/foo[0:5]', data, portmap, False) pm1 = pm0.copy() data[0] = 1.0 assert pm0.data[0] == 1.0
def test_dtype(self): pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) assert pm.dtype == np.float64
def test_get_sub(self): pm = PortMapper(self.data, '/foo/bar[0:5],/foo/baz[0:5]', np.arange(5, 15)) np.allclose(self.data[5:10], pm['/foo/bar[0:5]'])
def test_get_discontinuous(self): pm = PortMapper(self.data, '/foo/bar[0:10],/foo/baz[0:10]') np.allclose(self.data[[0, 2, 4, 6]], pm['/foo/bar[0,2,4,6]'])
def test_get(self): pm = PortMapper(self.data, '/foo/bar[0:10],/foo/baz[0:10]') np.allclose(self.data[0:10], pm['/foo/bar[0:10]'])
def test_ports_to_inds(self): pm = PortMapper(np.random.rand(10), '/foo[0:5],/bar[0:5]') np.allclose(pm.ports_to_inds('/foo[4],/bar[0]'), [4, 5])
def test_set_by_inds(self): data = np.random.rand(3) pm = PortMapper('/foo[0:3]', data) new_data = np.arange(2).astype(np.double) pm.set_by_inds([0, 1], new_data) assert_array_equal(new_data, pm.get_by_inds([0, 1]))
def test_inds_to_ports(self): pm = PortMapper(np.random.rand(10), '/foo[0:5],/bar[0:5]') self.assertSequenceEqual(pm.inds_to_ports([4, 5]), [('foo', 4), ('bar', 0)])
def test_set(self): pm = PortMapper(self.data, '/foo/bar[0:10],/foo/baz[0:10]') pm['/foo/baz[0:5]'] = 1.0 np.allclose(np.ones(5), pm['/foo/baz[0:5]'])
def test_get_by_inds(self): data = np.random.rand(3) pm = PortMapper('/foo[0:3]', data) assert_array_equal(data[[0, 1]], pm.get_by_inds([0, 1]))
def test_equals(self): pm0 = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) pm1 = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) assert pm0.equals(pm1) assert pm1.equals(pm0) pm0 = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) pm1 = PortMapper('/foo/bar[0:10],/foo/baz[1:10],/foo/baz[0]', self.data) assert not pm0.equals(pm1) assert not pm1.equals(pm0) pm0 = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', np.arange(20)) pm1 = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', np.concatenate((np.arange(10), np.arange(10)))) assert not pm0.equals(pm1) assert not pm1.equals(pm0)
def test_get_ports_as_inds(self): pm = PortMapper(np.array([0, 1, 0, 1, 0]), '/foo[0:5]') np.allclose( pm.get_ports_as_inds(lambda x: np.asarray(x, dtype=np.bool)), [1, 3])
def test_get_ports_nonzero(self): pm = PortMapper(np.array([0, 1, 0, 1, 0]), '/foo[0:5]') self.assertSequenceEqual(pm.get_ports_nonzero(), [('foo', 1), ('foo', 3)])
def test_set_discontinuous(self): pm = PortMapper(self.data, '/foo/bar[0:10],/foo/baz[0:10]') pm['/foo/*[0:2]'] = 1.0 np.allclose(np.ones(4), pm['/foo/*[0:2]'])
def test_get_ports_as_inds(self): pm = PortMapper(np.array([0, 1, 0, 1, 0]), '/foo[0:5]') np.allclose(pm.get_ports_as_inds(lambda x: np.asarray(x, dtype=np.bool)), [1, 3])