Skip to content

add VLenNDArray #200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added fixture/vlen-ndarray/array.00.npy
Binary file not shown.
Binary file added fixture/vlen-ndarray/array.01.npy
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.00/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "|i1",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.00/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.00/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.01/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "<i2",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.01/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.01/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.02/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "<i4",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.02/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.02/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.03/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "<i8",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.03/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.03/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.04/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "|u1",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.04/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.04/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.05/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "<u2",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.05/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.05/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.06/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "<u4",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.06/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.06/encoded.01.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions fixture/vlen-ndarray/codec.07/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"dtype": "<u8",
"id": "vlen-ndarray"
}
Binary file added fixture/vlen-ndarray/codec.07/encoded.00.dat
Binary file not shown.
Binary file added fixture/vlen-ndarray/codec.07/encoded.01.dat
Binary file not shown.
7 changes: 7 additions & 0 deletions numcodecs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,10 @@
register_codec(VLenArray)
except ImportError: # pragma: no cover
pass

try:
from numcodecs import vlen_nd
from numcodecs.vlen_nd import VLenNDArray
register_codec(VLenNDArray)
except ImportError: # pragma: no cover
pass
79 changes: 7 additions & 72 deletions numcodecs/blosc.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion numcodecs/compat_ext.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions numcodecs/lz4.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions numcodecs/tests/test_vlen_ndarray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, division
import unittest


import numpy as np
import pytest


from numcodecs.compat import PY2
try:
from numcodecs.vlen_nd import VLenNDArray
except ImportError: # pragma: no cover
raise unittest.SkipTest("vlen-ndarray not available")
from numcodecs.tests.common import (check_config, check_repr,
check_encode_decode_array,
check_backwards_compatibility,
assert_array_items_equal)


arrays = [
np.array([np.array([1, 2, 3]),
np.array([[4]]),
np.array([[5, 6], [7, 8]])] * 300, dtype=object),
np.array([np.array([1, 2, 3]),
np.array([[4]]),
np.array([[5, 6], [7, 8]])] * 300, dtype=object).reshape(90, 10),
]


codecs = [
VLenNDArray('<i1'),
VLenNDArray('<i2'),
VLenNDArray('<i4'),
VLenNDArray('<i8'),
VLenNDArray('<u1'),
VLenNDArray('<u2'),
VLenNDArray('<u4'),
VLenNDArray('<u8'),
]


def test_encode_decode():
for arr in arrays:
for codec in codecs:
check_encode_decode_array(arr, codec)


def test_config():
codec = VLenNDArray('<i8')
check_config(codec)


def test_repr():
check_repr("VLenNDArray(dtype='<i8')")


def test_backwards_compatibility():
check_backwards_compatibility(VLenNDArray.codec_id, arrays, codecs)


def test_encode_errors():
codec = VLenNDArray('<i8')
with pytest.raises(ValueError):
codec.encode('foo')
with pytest.raises(ValueError):
codec.encode(['foo', 'bar'])


def test_decode_errors():
codec = VLenNDArray('<i8')
with pytest.raises(TypeError):
codec.decode(1234)
# these should look like corrupt data
with pytest.raises(ValueError):
codec.decode(b'foo')
with pytest.raises(ValueError):
codec.decode(np.arange(2, 3, dtype='i4'))
with pytest.raises(ValueError):
codec.decode(np.arange(10, 20, dtype='i4'))
with pytest.raises(ValueError if PY2 else TypeError):
# exports old-style buffer interface on PY2, hence ValueError
codec.decode(u'foo')

# test out parameter
enc = codec.encode(arrays[0])
with pytest.raises(TypeError):
codec.decode(enc, out=b'foo')
with pytest.raises(TypeError):
codec.decode(enc, out=u'foo')
with pytest.raises(TypeError):
codec.decode(enc, out=123)
with pytest.raises(ValueError):
codec.decode(enc, out=np.zeros(10, dtype='i4'))


def test_encode_none():
a = np.array([[1, 3], None, [[4, 7]]], dtype=object)
codec = VLenNDArray(int)
enc = codec.encode(a)
dec = codec.decode(enc)
expect = np.array([np.array([1, 3]),
np.array([]),
np.array([[4, 7]])], dtype=object)
assert_array_items_equal(expect, dec)
8 changes: 4 additions & 4 deletions numcodecs/vlen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading