Example #1
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)

            xdindex = xindex.to_int_index()
            ydindex = yindex.to_int_index()

            x = np.arange(xindex.npoints) * 10. + 1
            y = np.arange(yindex.npoints) * 100. + 1

            xfill = 0
            yfill = 2

            result_block_vals, rb_index, bfill = sparse_op(x, xindex, xfill, y,
                                                           yindex, yfill)
            result_int_vals, ri_index, ifill = sparse_op(x, xdindex, xfill, y,
                                                         ydindex, yfill)

            self.assertTrue(rb_index.to_int_index().equals(ri_index))
            tm.assert_numpy_array_equal(result_block_vals, result_int_vals)
            self.assertEqual(bfill, ifill)

            # check versus Series...
            xseries = Series(x, xdindex.indices)
            xseries = xseries.reindex(np.arange(TEST_LENGTH)).fillna(xfill)

            yseries = Series(y, ydindex.indices)
            yseries = yseries.reindex(np.arange(TEST_LENGTH)).fillna(yfill)

            series_result = python_op(xseries, yseries)
            series_result = series_result.reindex(ri_index.indices)

            tm.assert_numpy_array_equal(result_block_vals,
                                        series_result.values)
            tm.assert_numpy_array_equal(result_int_vals, series_result.values)
    def test_to_int_index(self):
        locs = [0, 10]
        lengths = [4, 6]
        exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15]

        block = BlockIndex(20, locs, lengths)
        dense = block.to_int_index()

        assert_equal(dense.indices, exp_inds)
Example #3
0
    def test_to_int_index(self):
        locs = [0, 10]
        lengths = [4, 6]
        exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15]

        block = BlockIndex(20, locs, lengths)
        dense = block.to_int_index()

        assert_equal(dense.indices, exp_inds)
Example #4
0
    def test_to_int_index(self):
        locs = [0, 10]
        lengths = [4, 6]
        exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15]

        block = BlockIndex(20, locs, lengths)
        dense = block.to_int_index()

        tm.assert_numpy_array_equal(dense.indices,
                                    np.array(exp_inds, dtype=np.int32))
Example #5
0
    def test_to_int_index(self):
        locs = [0, 10]
        lengths = [4, 6]
        exp_inds = [0, 1, 2, 3, 10, 11, 12, 13, 14, 15]

        block = BlockIndex(20, locs, lengths)
        dense = block.to_int_index()

        tm.assert_numpy_array_equal(dense.indices,
                                    np.array(exp_inds, dtype=np.int32))
Example #6
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)

            # see if survive the round trip
            xbindex = xindex.to_int_index().to_block_index()
            ybindex = yindex.to_int_index().to_block_index()
            tm.assertIsInstance(xbindex, BlockIndex)
            self.assertTrue(xbindex.equals(xindex))
            self.assertTrue(ybindex.equals(yindex))
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
            bresult = xindex.make_union(yindex)
            assert (isinstance(bresult, BlockIndex))
            assert_equal(bresult.blocs, eloc)
            assert_equal(bresult.blengths, elen)

            ixindex = xindex.to_int_index()
            iyindex = yindex.to_int_index()
            iresult = ixindex.make_union(iyindex)
            assert (isinstance(iresult, IntIndex))
            assert_equal(iresult.indices, bresult.to_int_index().indices)
    def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
        xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
        yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
        expected = BlockIndex(TEST_LENGTH, eloc, elen)
        longer_index = BlockIndex(TEST_LENGTH + 1, yloc, ylen)

        _check_correct(xindex, yindex, expected)
        _check_correct(xindex.to_int_index(), yindex.to_int_index(),
                       expected.to_int_index())

        _check_length_exc(xindex, longer_index)
        _check_length_exc(xindex.to_int_index(), longer_index.to_int_index())
Example #9
0
    def test_check_integrity(self):
        locs = []
        lengths = []

        # 0-length OK
        # TODO: index variables are not used...is that right?
        index = BlockIndex(0, locs, lengths)  # noqa

        # also OK even though empty
        index = BlockIndex(1, locs, lengths)  # noqa

        # block extend beyond end
        self.assertRaises(Exception, BlockIndex, 10, [5], [10])

        # block overlap
        self.assertRaises(Exception, BlockIndex, 10, [2, 5], [5, 3])
Example #10
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
            bresult = xindex.make_union(yindex)
            assert (isinstance(bresult, BlockIndex))
            tm.assert_numpy_array_equal(bresult.blocs,
                                        np.array(eloc, dtype=np.int32))
            tm.assert_numpy_array_equal(bresult.blengths,
                                        np.array(elen, dtype=np.int32))

            ixindex = xindex.to_int_index()
            iyindex = yindex.to_int_index()
            iresult = ixindex.make_union(iyindex)
            assert (isinstance(iresult, IntIndex))
            tm.assert_numpy_array_equal(iresult.indices,
                                        bresult.to_int_index().indices)
Example #11
0
    def test_lookup_basics(self):
        def _check(index):
            assert (index.lookup(0) == -1)
            assert (index.lookup(5) == 0)
            assert (index.lookup(7) == 2)
            assert (index.lookup(8) == -1)
            assert (index.lookup(9) == -1)
            assert (index.lookup(10) == -1)
            assert (index.lookup(11) == -1)
            assert (index.lookup(12) == 3)
            assert (index.lookup(17) == 8)
            assert (index.lookup(18) == -1)

        bindex = BlockIndex(20, [5, 12], [3, 6])
        iindex = bindex.to_int_index()

        _check(bindex)
        _check(iindex)
Example #12
0
    def test_lookup_basics(self):
        def _check(index):
            assert (index.lookup(0) == -1)
            assert (index.lookup(5) == 0)
            assert (index.lookup(7) == 2)
            assert (index.lookup(8) == -1)
            assert (index.lookup(9) == -1)
            assert (index.lookup(10) == -1)
            assert (index.lookup(11) == -1)
            assert (index.lookup(12) == 3)
            assert (index.lookup(17) == 8)
            assert (index.lookup(18) == -1)

        bindex = BlockIndex(20, [5, 12], [3, 6])
        iindex = bindex.to_int_index()

        _check(bindex)
        _check(iindex)
Example #13
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)

            xdindex = xindex.to_int_index()
            ydindex = yindex.to_int_index()

            x = np.arange(xindex.npoints) * 10. + 1
            y = np.arange(yindex.npoints) * 100. + 1

            xfill = 0
            yfill = 2

            result_block_vals, rb_index, bfill = sparse_op(
                x, xindex, xfill, y, yindex, yfill)
            result_int_vals, ri_index, ifill = sparse_op(
                x, xdindex, xfill, y, ydindex, yfill)

            self.assertTrue(rb_index.to_int_index().equals(ri_index))
            tm.assert_numpy_array_equal(result_block_vals, result_int_vals)
            self.assertEqual(bfill, ifill)

            # check versus Series...
            xseries = Series(x, xdindex.indices)
            xseries = xseries.reindex(np.arange(TEST_LENGTH)).fillna(xfill)

            yseries = Series(y, ydindex.indices)
            yseries = yseries.reindex(np.arange(TEST_LENGTH)).fillna(yfill)

            series_result = python_op(xseries, yseries)
            series_result = series_result.reindex(ri_index.indices)

            tm.assert_numpy_array_equal(result_block_vals,
                                        series_result.values)
            tm.assert_numpy_array_equal(result_int_vals, series_result.values)
Example #14
0
    def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
        xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
        yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
        bresult = xindex.make_union(yindex)
        assert (isinstance(bresult, BlockIndex))
        assert_equal(bresult.blocs, eloc)
        assert_equal(bresult.blengths, elen)

        ixindex = xindex.to_int_index()
        iyindex = yindex.to_int_index()
        iresult = ixindex.make_union(iyindex)
        assert (isinstance(iresult, IntIndex))
        assert_equal(iresult.indices, bresult.to_int_index().indices)
Example #15
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)

            # see if survive the round trip
            xbindex = xindex.to_int_index().to_block_index()
            ybindex = yindex.to_int_index().to_block_index()
            tm.assertIsInstance(xbindex, BlockIndex)
            self.assertTrue(xbindex.equals(xindex))
            self.assertTrue(ybindex.equals(yindex))
Example #16
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
            bresult = xindex.make_union(yindex)
            assert (isinstance(bresult, BlockIndex))
            tm.assert_numpy_array_equal(bresult.blocs,
                                        np.array(eloc, dtype=np.int32))
            tm.assert_numpy_array_equal(bresult.blengths,
                                        np.array(elen, dtype=np.int32))

            ixindex = xindex.to_int_index()
            iyindex = yindex.to_int_index()
            iresult = ixindex.make_union(iyindex)
            assert (isinstance(iresult, IntIndex))
            tm.assert_numpy_array_equal(iresult.indices,
                                        bresult.to_int_index().indices)
Example #17
0
 def _read_block_index(self, group, key):
     length = getattr(group._v_attrs, '%s_length' % key)
     blocs = _read_array(group, '%s_blocs' % key)
     blengths = _read_array(group, '%s_blengths' % key)
     return BlockIndex(length, blocs, blengths)
Example #18
0
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)
            expected = BlockIndex(TEST_LENGTH, eloc, elen)
            longer_index = BlockIndex(TEST_LENGTH + 1, yloc, ylen)

            _check_correct(xindex, yindex, expected)
            _check_correct(xindex.to_int_index(), yindex.to_int_index(),
                           expected.to_int_index())

            _check_length_exc(xindex, longer_index)
            _check_length_exc(xindex.to_int_index(),
                              longer_index.to_int_index())
Example #19
0
 def test_to_block_index(self):
     index = BlockIndex(10, [0, 5], [4, 5])
     self.assertIs(index.to_block_index(), index)
Example #20
0
    def test_equals(self):
        index = BlockIndex(10, [0, 4], [2, 5])

        self.assertTrue(index.equals(index))
        self.assertFalse(index.equals(BlockIndex(10, [0, 4], [2, 6])))
Example #21
0
    def test_equals(self):
        index = BlockIndex(10, [0, 4], [2, 5])

        self.assertTrue(index.equals(index))
        self.assertFalse(index.equals(BlockIndex(10, [0, 4], [2, 6])))
Example #22
0
 def test_to_block_index(self):
     index = BlockIndex(10, [0, 5], [4, 5])
     self.assertIs(index.to_block_index(), index)