|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import absolute_import, print_function, division |
| 3 | + |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from numpy.testing import assert_array_equal |
| 7 | +from nose.tools import eq_ as eq |
| 8 | + |
| 9 | + |
| 10 | +from numcodecs.categorize import Categorize |
| 11 | +from numcodecs.tests.common import check_encode_decode, check_config |
| 12 | +from numcodecs.compat import PY2 |
| 13 | + |
| 14 | + |
| 15 | +labels = [b'foo', b'bar', b'baz', b'quux'] |
| 16 | +labels_u = [u'ƒöõ', u'ßàř', u'ßāẑ', u'ƪùüx'] |
| 17 | +labels_num = [1000000, 2000000, 3000000] |
| 18 | +arrays = [ |
| 19 | + np.random.choice(labels, size=1000), |
| 20 | + np.random.choice(labels, size=(100, 10)), |
| 21 | + np.random.choice(labels, size=(10, 10, 10)), |
| 22 | + np.random.choice(labels, size=1000).reshape(100, 10, order='F'), |
| 23 | +] |
| 24 | +arrays_u = [ |
| 25 | + np.random.choice(labels_u, size=1000), |
| 26 | + np.random.choice(labels_u, size=(100, 10)), |
| 27 | + np.random.choice(labels_u, size=(10, 10, 10)), |
| 28 | + np.random.choice(labels_u, size=1000).reshape(100, 10, order='F'), |
| 29 | +] |
| 30 | +arrays_num = [ |
| 31 | + np.random.choice(labels_num, size=1000), |
| 32 | + np.random.choice(labels_num, size=(100, 10)), |
| 33 | + np.random.choice(labels_num, size=(10, 10, 10)), |
| 34 | + np.random.choice(labels_num, size=1000).reshape(100, 10, order='F'), |
| 35 | +] |
| 36 | + |
| 37 | + |
| 38 | +def test_encode_decode(): |
| 39 | + |
| 40 | + # string dtype |
| 41 | + for arr in arrays: |
| 42 | + codec = Categorize(labels, dtype=arr.dtype) |
| 43 | + check_encode_decode(arr, codec) |
| 44 | + |
| 45 | + # unicode dtype |
| 46 | + for arr in arrays_u: |
| 47 | + codec = Categorize(labels_u, dtype=arr.dtype) |
| 48 | + check_encode_decode(arr, codec) |
| 49 | + |
| 50 | + # other dtype |
| 51 | + for arr in arrays_num: |
| 52 | + codec = Categorize(labels_num, dtype=arr.dtype) |
| 53 | + check_encode_decode(arr, codec) |
| 54 | + |
| 55 | + |
| 56 | +def test_encode(): |
| 57 | + arr = np.array([b'foo', b'bar', b'foo', b'baz', b'quux']) |
| 58 | + # miss off quux |
| 59 | + codec = Categorize(labels=labels[:-1], dtype=arr.dtype, astype='u1') |
| 60 | + |
| 61 | + # test encoding |
| 62 | + expect = np.array([1, 2, 1, 3, 0], dtype='u1') |
| 63 | + enc = codec.encode(arr) |
| 64 | + assert_array_equal(expect, enc) |
| 65 | + eq(expect.dtype, enc.dtype) |
| 66 | + |
| 67 | + # test decoding with unexpected value |
| 68 | + dec = codec.decode(enc) |
| 69 | + expect = arr.copy() |
| 70 | + expect[expect == b'quux'] = b'' |
| 71 | + assert_array_equal(expect, dec) |
| 72 | + eq(arr.dtype, dec.dtype) |
| 73 | + |
| 74 | + |
| 75 | +def test_encode_unicode(): |
| 76 | + arr = np.array([u'ƒöõ', u'ßàř', u'ƒöõ', u'ßāẑ', u'ƪùüx']) |
| 77 | + # miss off quux |
| 78 | + codec = Categorize(labels=labels_u[:-1], dtype=arr.dtype, astype='u1') |
| 79 | + |
| 80 | + # test encoding |
| 81 | + expect = np.array([1, 2, 1, 3, 0], dtype='u1') |
| 82 | + enc = codec.encode(arr) |
| 83 | + assert_array_equal(expect, enc) |
| 84 | + eq(expect.dtype, enc.dtype) |
| 85 | + |
| 86 | + # test decoding with unexpected value |
| 87 | + dec = codec.decode(enc) |
| 88 | + expect = arr.copy() |
| 89 | + expect[expect == u'ƪùüx'] = u'' |
| 90 | + assert_array_equal(expect, dec) |
| 91 | + eq(arr.dtype, dec.dtype) |
| 92 | + |
| 93 | + |
| 94 | +def test_config(): |
| 95 | + codec = Categorize(labels=labels, dtype='S4') |
| 96 | + check_config(codec) |
| 97 | + codec = Categorize(labels=labels_u, dtype='U4') |
| 98 | + check_config(codec) |
| 99 | + |
| 100 | + |
| 101 | +def test_repr(): |
| 102 | + if not PY2: |
| 103 | + |
| 104 | + dtype = '|S5' |
| 105 | + astype = '|u1' |
| 106 | + codec = Categorize(labels=labels, dtype=dtype, astype=astype) |
| 107 | + expect = "Categorize(dtype='|S5', astype='|u1', " \ |
| 108 | + "labels=[b'foo', b'bar', b'baz', ...])" |
| 109 | + actual = repr(codec) |
| 110 | + eq(expect, actual) |
| 111 | + |
| 112 | + dtype = '<U5' |
| 113 | + astype = '|u1' |
| 114 | + codec = Categorize(labels=labels_u, dtype=dtype, astype=astype) |
| 115 | + expect = "Categorize(dtype='<U5', astype='|u1', " \ |
| 116 | + "labels=['ƒöõ', 'ßàř', 'ßāẑ', ...])" |
| 117 | + actual = repr(codec) |
| 118 | + eq(expect, actual) |
0 commit comments