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_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('/foo/bar[0:10]', np.arange(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_set_array(self): # Valid empty: pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]') new_data = np.arange(10).astype(np.double) pm['/foo/bar[0:10]'] = new_data assert_array_equal(new_data, pm.data[0:10]) # Valid nonempty: pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) new_data = np.arange(10).astype(np.double) pm['/foo/bar[0:10]'] = new_data assert_array_equal(new_data, pm.data[0:10])
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_create(self): # Empty selector, empty data (force index dtype of ground truth to # object because neurokernel.plsel.SelectorMethods.make_index() creates # indexes with dtype=object): pm = PortMapper('') assert_series_equal( pm.portmap, pd.Series([], dtype=np.int_, index=pd.MultiIndex(levels=[[]], labels=[[]], names=[0]))) 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_set_scalar(self): pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) pm['/foo/baz[0:5]'] = 1.0 assert_array_equal(np.ones(5), pm['/foo/baz[0:5]'])
def test_get_ports_nonzero(self): pm = PortMapper('/foo[0:5]', np.array([0, 1, 0, 1, 0])) self.assertSequenceEqual(pm.get_ports_nonzero(), [('foo', 1), ('foo', 3)])
def test_get_ports_as_inds(self): pm = PortMapper('/foo[0:5]', np.array([0, 1, 0, 1, 0])) np.allclose( pm.get_ports_as_inds(lambda x: np.asarray(x, dtype=np.bool)), [1, 3])
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_sub(self): pm = PortMapper('/foo/bar[0:5],/foo/baz[0:5]', self.data, np.arange(5, 15)) np.allclose(self.data[5:10], pm['/foo/bar[0:5]'])
def test_get_discontinuous(self): pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) np.allclose(self.data[[0, 2, 4, 6]], pm['/foo/bar[0,2,4,6]'])
def test_get_ports_as_inds(self): pm = PortMapper('/foo[0:5]', np.array([0, 1, 0, 1, 0])) np.allclose(pm.get_ports_as_inds(lambda x: np.asarray(x, dtype=np.bool)), [1, 3])
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_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_set_discontinuous(self): pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) pm['/foo/*[0:2]'] = 1.0 np.allclose(np.ones(4), pm['/foo/*[0:2]'])
def test_dtype(self): pm = PortMapper('/foo/bar[0:10],/foo/baz[0:10]', self.data) assert pm.dtype == np.float64