|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +from __future__ import absolute_import, print_function, division |
| 3 | +from tempfile import mkdtemp |
| 4 | +import atexit |
| 5 | +import shutil |
| 6 | + |
| 7 | + |
| 8 | +import numpy as np |
| 9 | +import pytest |
| 10 | + |
| 11 | + |
| 12 | +from zarr.creation import array, open_array |
| 13 | +from zarr.storage import DirectoryStore |
| 14 | + |
| 15 | + |
| 16 | +@pytest.mark.parametrize( |
| 17 | + "dtype", |
| 18 | + map(np.dtype, [ |
| 19 | + # NOTE(onalant): unstructured dtypes |
| 20 | + "f8", "10f8", "(10, 10)f8", |
| 21 | + # NOTE(onalant): structured dtypes |
| 22 | + "i8, f8", "i8, 10f8", "i8, (10, 10)f8", "i8, (10, 10)f8, (5, 10, 15)u1", |
| 23 | + # NOTE(onalant): nested dtypes |
| 24 | + [("f0", "i8"), ("f1", [("f0", "f8"), ("f1", "10f8"), ("f2", "(10, 10)f8")])] |
| 25 | + ]) |
| 26 | +) |
| 27 | +def test_write_read(dtype): |
| 28 | + |
| 29 | + path = mkdtemp() |
| 30 | + atexit.register(shutil.rmtree, path) |
| 31 | + shape = (100, 100) |
| 32 | + sbytes = np.random.bytes(np.product(shape) * dtype.itemsize) |
| 33 | + s = np.frombuffer(sbytes, dtype=dtype) |
| 34 | + # NOTE(onalant): np.frombuffer only creates 1D arrays; expand to shape |
| 35 | + s.reshape(shape + s.shape[1:]) |
| 36 | + |
| 37 | + store = DirectoryStore(path) |
| 38 | + z = array(s, store=store) |
| 39 | + |
| 40 | + assert(s.dtype == z.dtype) |
| 41 | + assert(s.shape == z.shape) |
| 42 | + assert(s.tobytes() == z[:].tobytes()) |
| 43 | + |
| 44 | + del store |
| 45 | + del z |
| 46 | + |
| 47 | + store = DirectoryStore(path) |
| 48 | + z = open_array(store) |
| 49 | + |
| 50 | + assert(s.dtype == z.dtype) |
| 51 | + assert(s.shape == z.shape) |
| 52 | + assert(s.tobytes() == z[:].tobytes()) |
| 53 | + |
0 commit comments