Skip to content

Commit

Permalink
Add dtype argument to asarray
Browse files Browse the repository at this point in the history
  • Loading branch information
constantinpape committed Feb 17, 2021
1 parent 6cdcde4 commit 40b751e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/python/module/z5py/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ def __init__(self, dset_impl, handle, parent, name, n_threads=1):
self._parent = parent
self._name = name

def __array__(self):
return self[...]
def __array__(self, dtype=None):
""" Create a numpy array containing the whole dataset.
NOTE: Datasets are not interchangeble with arrays!
Every time this method is called the whole dataset is loaded into memory!
"""
arr = self[...]
if dtype is not None:
arr = arr.astype(dtype, copy=False)
return arr

@staticmethod
def _to_zarr_compression_options(compression, compression_options):
Expand Down
10 changes: 5 additions & 5 deletions src/python/test/test_asarray.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
import pickle
import sys
from shutil import rmtree

Expand All @@ -15,13 +14,12 @@
class TestAsarray(unittest.TestCase):

def setUp(self):
sample_data = np.array([ [3, 4], [7, 8]])
self.sample_data = np.random.randint(0, 10000, size=(10, 10))
self.test_fn = 'test.n5'
self.test_ds = 'test'
with z5py.File(self.test_fn, 'w') as zfh:
zfh.create_dataset(self.test_ds, data=sample_data)
zfh.create_dataset(self.test_ds, data=self.sample_data)
self.ff = z5py.File(self.test_fn, 'r')


def tearDown(self):
try:
Expand All @@ -31,7 +29,9 @@ def tearDown(self):

def test_asarray(self):
uniques = np.unique(self.ff[self.test_ds])
self.assertEqual(len(uniques), 4)
expected = np.unique(self.sample_data)
self.assertTrue(np.array_equal(uniques, expected))


if __name__ == '__main__':
unittest.main()

0 comments on commit 40b751e

Please sign in to comment.