Beispiel #1
0
 def test_copy(self):
     # Ensure that modifying pm0 doesn't modify any other mapper created from it:
     pm0 = BasePortMapper('/foo[0:5]', np.arange(5))
     pm1 = BasePortMapper('/foo[0:5]', np.arange(5))
     pm2 = pm0.copy()
     pm0.portmap[('foo', 0)] = 10
     assert_series_equal(pm2.portmap, pm1.portmap)
Beispiel #2
0
    def test_get_map(self):
        # Try to get selector that is in the mapper:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]')
        self.assertSequenceEqual(pm.get_map('/bar[0:5]').tolist(), range(5, 10))

        # Try to get selector that is not in the mapper:
        self.assertSequenceEqual(pm.get_map('/foo[5:10]').tolist(), [])
Beispiel #3
0
 def test_copy(self):
     # Ensure that modifying pm0 doesn't modify any other mapper created from it:
     pm0 = BasePortMapper('/foo[0:5]', np.arange(5))
     pm1 = BasePortMapper('/foo[0:5]', np.arange(5))
     pm2 = pm0.copy()
     pm0.portmap[('foo', 0)] = 10
     assert_series_equal(pm2.portmap, pm1.portmap)
Beispiel #4
0
    def test_get_map(self):
        # Try to get selector that is in the mapper:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]')
        self.assertSequenceEqual(
            pm.get_map('/bar[0:5]').tolist(), range(5, 10))

        # Try to get selector that is not in the mapper:
        self.assertSequenceEqual(pm.get_map('/foo[5:10]').tolist(), [])
Beispiel #5
0
    def test_inds_to_ports(self):
        # Without a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]')
        self.assertSequenceEqual(pm.inds_to_ports([4, 5]),
                                 [('foo', 4), ('bar', 0)])

        # With a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]', range(10, 20))
        self.assertSequenceEqual(pm.inds_to_ports([14, 15]),
                                 [('foo', 4), ('bar', 0)])
Beispiel #6
0
 def test_create(self):
     portmap = np.arange(5)
     pm = BasePortMapper('/foo[0:5]', portmap)
     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)
Beispiel #7
0
    def test_from_index(self):
        # Without a specified port map:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]')
        pm1 = BasePortMapper.from_index(pm0.index)
        assert_series_equal(pm0.portmap, pm1.portmap)

        # With a specified port map:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]', range(5)*2)
        pm1 = BasePortMapper.from_index(pm0.index, range(5)*2)
        assert_series_equal(pm0.portmap, pm1.portmap)

        # Ensure that modifying the map sequence used to create the
        # port mapper doesn't have the side effect of altering the created
        # mapper:
        index = pd.MultiIndex(levels=[[u'foo'], [0, 1, 2, 3, 4]],
                              labels=[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4]],
                              names=[0, 1])
        portmap = np.arange(5)
        pm1 = BasePortMapper.from_index(index, portmap)
        portmap[0] = 10
        assert_array_equal(pm1.portmap.values, np.arange(5))
Beispiel #8
0
    def test_from_index(self):
        # Without a specified port map:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]')
        pm1 = BasePortMapper.from_index(pm0.index)
        assert_series_equal(pm0.portmap, pm1.portmap)

        # With a specified port map:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]', range(5) * 2)
        pm1 = BasePortMapper.from_index(pm0.index, range(5) * 2)
        assert_series_equal(pm0.portmap, pm1.portmap)

        # Ensure that modifying the map sequence used to create the
        # port mapper doesn't have the side effect of altering the created
        # mapper:
        index = pd.MultiIndex(levels=[[u'foo'], [0, 1, 2, 3, 4]],
                              labels=[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4]],
                              names=[0, 1])
        portmap = np.arange(5)
        pm1 = BasePortMapper.from_index(index, portmap)
        portmap[0] = 10
        assert_array_equal(pm1.portmap.values, np.arange(5))
Beispiel #9
0
    def test_ports_to_inds(self):
        # Without a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]')
        np.allclose(pm.ports_to_inds('/foo[4],/bar[0]'), [4, 5])

        # Nonexistent ports should return an empty index array:
        i = pm.ports_to_inds('/baz')
        assert len(i) == 0 and i.dtype == np.int64

        # With a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]', range(10, 20))
        np.allclose(pm.ports_to_inds('/foo[4],/bar[0]'), [14, 15])

        i = pm.ports_to_inds('/baz')
        assert len(i) == 0 and i.dtype == np.int64
Beispiel #10
0
    def test_inds_to_ports(self):
        # Without a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]')
        self.assertSequenceEqual(pm.inds_to_ports([4, 5]), [('foo', 4),
                                                            ('bar', 0)])

        # With a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]', range(10, 20))
        self.assertSequenceEqual(pm.inds_to_ports([14, 15]), [('foo', 4),
                                                              ('bar', 0)])
Beispiel #11
0
    def test_ports_to_inds(self):
        # Without a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]')
        np.allclose(pm.ports_to_inds('/foo[4],/bar[0]'), [4, 5])

        # Nonexistent ports should return an empty index array:
        i = pm.ports_to_inds('/baz')
        assert len(i) == 0 and i.dtype == np.int64

        # With a specified port map:
        pm = BasePortMapper('/foo[0:5],/bar[0:5]', range(10, 20))
        np.allclose(pm.ports_to_inds('/foo[4],/bar[0]'), [14, 15])

        i = pm.ports_to_inds('/baz')
        assert len(i) == 0 and i.dtype == np.int64
Beispiel #12
0
 def test_set_map(self):
     pm = BasePortMapper('/foo[0:5],/bar[0:5]')
     pm.set_map('/bar[0:5]', range(5))
     self.assertSequenceEqual(pm.portmap.ix[5:10].tolist(), range(5))
Beispiel #13
0
    def test_equals(self):
        # Check that mappers containing the same ports/indices are deemed equal:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]')
        pm1 = BasePortMapper('/foo[0:5],/bar[0:5]')
        assert pm0.equals(pm1)
        assert pm1.equals(pm0)

        # Check that mappers containing the same ports/indices in 
        # different orders are deemed equal:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]', range(10))
        pm1 = BasePortMapper('/bar[0:5],/foo[0:5]', range(5, 10)+range(5))
        assert pm0.equals(pm1)
        assert pm1.equals(pm0)

        # Check that mappers containing different ports/indices are deemed non-equal:
        pm0 = BasePortMapper('/foo[0:5],/bar[1:5]/bar[0]')
        pm1 = BasePortMapper('/foo[0:5],/bar[0:5]')
        assert not pm0.equals(pm1)
        assert not pm1.equals(pm0)
Beispiel #14
0
 def test_set_map(self):
     pm = BasePortMapper('/foo[0:5],/bar[0:5]')
     pm.set_map('/bar[0:5]', range(5))
     self.assertSequenceEqual(pm.portmap.ix[5:10].tolist(), range(5))
Beispiel #15
0
    def test_equals(self):
        # Check that mappers containing the same ports/indices are deemed equal:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]')
        pm1 = BasePortMapper('/foo[0:5],/bar[0:5]')
        assert pm0.equals(pm1)
        assert pm1.equals(pm0)

        # Check that mappers containing the same ports/indices in
        # different orders are deemed equal:
        pm0 = BasePortMapper('/foo[0:5],/bar[0:5]', range(10))
        pm1 = BasePortMapper('/bar[0:5],/foo[0:5]', range(5, 10) + range(5))
        assert pm0.equals(pm1)
        assert pm1.equals(pm0)

        # Check that mappers containing different ports/indices are deemed non-equal:
        pm0 = BasePortMapper('/foo[0:5],/bar[1:5]/bar[0]')
        pm1 = BasePortMapper('/foo[0:5],/bar[0:5]')
        assert not pm0.equals(pm1)
        assert not pm1.equals(pm0)
Beispiel #16
0
 def test_len(self):
     pm = BasePortMapper('/foo[0:5],/bar[0:5]')
     assert len(pm) == 10