|
7 | 7 | from nose.tools import assert_raises
|
8 | 8 | import numpy as np
|
9 | 9 | from numpy.testing import assert_array_equal
|
| 10 | +from numcodecs import Zlib |
10 | 11 |
|
11 | 12 |
|
12 |
| -from zarr.convenience import open, save, save_group, load, copy_store |
| 13 | +from zarr.convenience import open, save, save_group, load, copy_store, copy |
13 | 14 | from zarr.storage import atexit_rmtree
|
14 | 15 | from zarr.core import Array
|
15 |
| -from zarr.hierarchy import Group |
| 16 | +from zarr.hierarchy import Group, group |
16 | 17 |
|
17 | 18 |
|
18 | 19 | def test_open_array():
|
@@ -177,3 +178,80 @@ def test_copy_store():
|
177 | 178 | assert 'foo' in dest
|
178 | 179 | assert 'bar/baz' not in dest
|
179 | 180 | assert 'bar/qux' in dest
|
| 181 | + |
| 182 | + |
| 183 | +def test_copy(): |
| 184 | + |
| 185 | + source = group() |
| 186 | + foo = source.create_group('foo') |
| 187 | + foo.attrs['experiment'] = 'weird science' |
| 188 | + baz = foo.create_dataset('bar/baz', data=np.arange(100), chunks=50) |
| 189 | + baz.attrs['units'] = 'metres' |
| 190 | + |
| 191 | + # copy array with default options |
| 192 | + dest = group() |
| 193 | + copy(source['foo/bar/baz'], dest) |
| 194 | + a = dest['baz'] # defaults to use source name |
| 195 | + assert isinstance(a, Array) |
| 196 | + assert a.dtype == baz.dtype |
| 197 | + assert a.shape == baz.shape |
| 198 | + assert a.chunks == baz.chunks |
| 199 | + assert a.compressor == baz.compressor |
| 200 | + assert_array_equal(a[:], baz[:]) |
| 201 | + assert a.attrs['units'] == 'metres' |
| 202 | + |
| 203 | + # copy array with name |
| 204 | + dest = group() |
| 205 | + copy(source['foo/bar/baz'], dest, name='qux') |
| 206 | + assert 'baz' not in dest |
| 207 | + a = dest['qux'] |
| 208 | + assert isinstance(a, Array) |
| 209 | + assert a.dtype == baz.dtype |
| 210 | + assert a.shape == baz.shape |
| 211 | + assert a.chunks == baz.chunks |
| 212 | + assert a.compressor == baz.compressor |
| 213 | + assert_array_equal(a[:], baz[:]) |
| 214 | + assert a.attrs['units'] == 'metres' |
| 215 | + |
| 216 | + # copy array, provide creation options |
| 217 | + compressor = Zlib(1) |
| 218 | + chunks = True |
| 219 | + copy(source['foo/bar/baz'], dest, without_attrs=True, compressor=compressor, |
| 220 | + chunks=chunks) |
| 221 | + a = dest['baz'] |
| 222 | + assert isinstance(a, Array) |
| 223 | + assert a.dtype == baz.dtype |
| 224 | + assert a.shape == baz.shape |
| 225 | + assert a.chunks != baz.chunks # autochunking was requested |
| 226 | + assert a.compressor == compressor |
| 227 | + assert_array_equal(a[:], baz[:]) |
| 228 | + assert 'units' not in a.attrs |
| 229 | + |
| 230 | + # copy group, default options |
| 231 | + dest = group() |
| 232 | + copy(source['foo'], dest) |
| 233 | + g = dest['foo'] # defaults to use source name |
| 234 | + assert isinstance(g, Group) |
| 235 | + assert g.attrs['experiment'] == 'weird science' |
| 236 | + a = g['bar/baz'] |
| 237 | + assert a.dtype == baz.dtype |
| 238 | + assert a.shape == baz.shape |
| 239 | + assert a.chunks == baz.chunks |
| 240 | + assert a.compressor == baz.compressor |
| 241 | + assert_array_equal(a[:], baz[:]) |
| 242 | + assert a.attrs['units'] == 'metres' |
| 243 | + |
| 244 | + # copy group, non-default options |
| 245 | + dest = group() |
| 246 | + copy(source['foo'], dest, name='qux', without_attrs=True) |
| 247 | + assert 'foo' not in dest |
| 248 | + g = dest['qux'] |
| 249 | + assert isinstance(g, Group) |
| 250 | + assert 'experiment' not in g.attrs |
| 251 | + a = g['bar/baz'] |
| 252 | + assert a.dtype == baz.dtype |
| 253 | + assert a.shape == baz.shape |
| 254 | + assert a.chunks == baz.chunks |
| 255 | + assert a.compressor == baz.compressor |
| 256 | + assert_array_equal(a[:], baz[:]) |
| 257 | + assert 'units' not in a.attrs |
0 commit comments