/usr/lib/python3/dist-packages/pandas/sparse/tests/test_libsparse.py is in python3-pandas 0.14.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | from pandas import Series
import nose
from numpy import nan
import numpy as np
import operator
from numpy.testing import assert_almost_equal, assert_equal
import pandas.util.testing as tm
from pandas.core.sparse import SparseSeries
from pandas import DataFrame
from pandas._sparse import IntIndex, BlockIndex
import pandas._sparse as splib
TEST_LENGTH = 20
plain_case = dict(xloc=[0, 7, 15],
xlen=[3, 5, 5],
yloc=[2, 9, 14],
ylen=[2, 3, 5],
intersect_loc=[2, 9, 15],
intersect_len=[1, 3, 4])
delete_blocks = dict(xloc=[0, 5],
xlen=[4, 4],
yloc=[1],
ylen=[4],
intersect_loc=[1],
intersect_len=[3])
split_blocks = dict(xloc=[0],
xlen=[10],
yloc=[0, 5],
ylen=[3, 7],
intersect_loc=[0, 5],
intersect_len=[3, 5])
skip_block = dict(xloc=[10],
xlen=[5],
yloc=[0, 12],
ylen=[5, 3],
intersect_loc=[12],
intersect_len=[3])
no_intersect = dict(xloc=[0, 10],
xlen=[4, 6],
yloc=[5, 17],
ylen=[4, 2],
intersect_loc=[],
intersect_len=[])
def check_cases(_check_case):
def _check_case_dict(case):
_check_case(case['xloc'], case['xlen'], case['yloc'], case['ylen'],
case['intersect_loc'], case['intersect_len'])
_check_case_dict(plain_case)
_check_case_dict(delete_blocks)
_check_case_dict(split_blocks)
_check_case_dict(skip_block)
_check_case_dict(no_intersect)
# one or both is empty
_check_case([0], [5], [], [], [], [])
_check_case([], [], [], [], [], [])
def test_index_make_union():
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)
"""
x: ----
y: ----
r: --------
"""
xloc = [0]
xlen = [5]
yloc = [5]
ylen = [4]
eloc = [0]
elen = [9]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: ----- -----
y: ----- --
"""
xloc = [0, 10]
xlen = [5, 5]
yloc = [2, 17]
ylen = [5, 2]
eloc = [0, 10, 17]
elen = [7, 5, 2]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: ------
y: -------
r: ----------
"""
xloc = [1]
xlen = [5]
yloc = [3]
ylen = [5]
eloc = [1]
elen = [7]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: ------ -----
y: -------
r: -------------
"""
xloc = [2, 10]
xlen = [4, 4]
yloc = [4]
ylen = [8]
eloc = [2]
elen = [12]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: --- -----
y: -------
r: -------------
"""
xloc = [0, 5]
xlen = [3, 5]
yloc = [0]
ylen = [7]
eloc = [0]
elen = [10]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: ------ -----
y: ------- ---
r: -------------
"""
xloc = [2, 10]
xlen = [4, 4]
yloc = [4, 13]
ylen = [8, 4]
eloc = [2]
elen = [15]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: ----------------------
y: ---- ---- ---
r: ----------------------
"""
xloc = [2]
xlen = [15]
yloc = [4, 9, 14]
ylen = [3, 2, 2]
eloc = [2]
elen = [15]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
"""
x: ---- ---
y: --- ---
"""
xloc = [0, 10]
xlen = [3, 3]
yloc = [5, 15]
ylen = [2, 2]
eloc = [0, 5, 10, 15]
elen = [3, 2, 3, 2]
_check_case(xloc, xlen, yloc, ylen, eloc, elen)
# TODO: different-length index objects
def test_lookup():
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)
# corner cases
def test_intersect():
def _check_correct(a, b, expected):
result = a.intersect(b)
assert(result.equals(expected))
def _check_length_exc(a, longer):
nose.tools.assert_raises(Exception, a.intersect, longer)
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())
check_cases(_check_case)
class TestBlockIndex(tm.TestCase):
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])))
def test_check_integrity(self):
locs = []
lengths = []
# 0-length OK
index = BlockIndex(0, locs, lengths)
# also OK even though empty
index = BlockIndex(1, locs, lengths)
# block extend beyond end
self.assertRaises(Exception, BlockIndex, 10, [5], [10])
# block overlap
self.assertRaises(Exception, BlockIndex, 10, [2, 5], [5, 3])
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)
def test_to_block_index(self):
index = BlockIndex(10, [0, 5], [4, 5])
self.assertIs(index.to_block_index(), index)
class TestIntIndex(tm.TestCase):
def test_equals(self):
index = IntIndex(10, [0, 1, 2, 3, 4])
self.assertTrue(index.equals(index))
self.assertFalse(index.equals(IntIndex(10, [0, 1, 2, 3])))
def test_to_block_index(self):
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.assert_isinstance(xbindex, BlockIndex)
self.assertTrue(xbindex.equals(xindex))
self.assertTrue(ybindex.equals(yindex))
check_cases(_check_case)
def test_to_int_index(self):
index = IntIndex(10, [2, 3, 4, 5, 6])
self.assertIs(index.to_int_index(), index)
class TestSparseOperators(tm.TestCase):
def _nan_op_tests(self, sparse_op, python_op):
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
result_block_vals, rb_index = sparse_op(x, xindex, y, yindex)
result_int_vals, ri_index = sparse_op(x, xdindex, y, ydindex)
self.assertTrue(rb_index.to_int_index().equals(ri_index))
assert_equal(result_block_vals, result_int_vals)
# check versus Series...
xseries = Series(x, xdindex.indices)
yseries = Series(y, ydindex.indices)
series_result = python_op(xseries, yseries).valid()
assert_equal(result_block_vals, series_result.values)
assert_equal(result_int_vals, series_result.values)
check_cases(_check_case)
def _op_tests(self, sparse_op, python_op):
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 = sparse_op(
x, xindex, xfill, y, yindex, yfill)
result_int_vals, ri_index = sparse_op(x, xdindex, xfill,
y, ydindex, yfill)
self.assertTrue(rb_index.to_int_index().equals(ri_index))
assert_equal(result_block_vals, result_int_vals)
# 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)
assert_equal(result_block_vals, series_result.values)
assert_equal(result_int_vals, series_result.values)
check_cases(_check_case)
# too cute? oh but how I abhor code duplication
check_ops = ['add', 'sub', 'mul', 'truediv', 'floordiv']
def make_nanoptestf(op):
def f(self):
sparse_op = getattr(splib, 'sparse_nan%s' % op)
python_op = getattr(operator, op)
self._nan_op_tests(sparse_op, python_op)
f.__name__ = 'test_nan%s' % op
return f
def make_optestf(op):
def f(self):
sparse_op = getattr(splib, 'sparse_%s' % op)
python_op = getattr(operator, op)
self._op_tests(sparse_op, python_op)
f.__name__ = 'test_%s' % op
return f
for op in check_ops:
f = make_nanoptestf(op)
g = make_optestf(op)
setattr(TestSparseOperators, f.__name__, f)
setattr(TestSparseOperators, g.__name__, g)
del f
del g
if __name__ == '__main__':
import nose
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
exit=False)
|