Skip to content

Commit 092a1e0

Browse files
committed
add write_data argument
1 parent 021ca95 commit 092a1e0

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/zarr/api/synchronous.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,7 @@ def create_array(
897897
def from_array(
898898
data: Array | npt.ArrayLike,
899899
store: str | StoreLike,
900+
write_data: bool = True,
900901
*,
901902
name: str | None = None,
902903
chunks: Literal["auto", "keep"] | ChunkCoords = "keep",
@@ -922,6 +923,10 @@ def from_array(
922923
The array to copy.
923924
store : str or Store
924925
Store or path to directory in file system or name of zip file for the new array.
926+
write_data : bool, default True
927+
Whether to copy the data from the input array to the new array.
928+
If ``write_data`` is ``False``, the new array will be created with the same metadata as the
929+
input array, but without any data.
925930
name : str or None, optional
926931
The name of the array within the store. If ``name`` is ``None``, the array will be located
927932
at the root of the store.
@@ -1014,6 +1019,7 @@ def from_array(
10141019
zarr.core.array.from_array(
10151020
data,
10161021
store,
1022+
write_data,
10171023
name=name,
10181024
chunks=chunks,
10191025
shards=shards,

src/zarr/core/array.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,6 +3738,7 @@ class ShardsConfigParam(TypedDict):
37383738
async def from_array(
37393739
data: Array | npt.ArrayLike,
37403740
store: str | StoreLike,
3741+
write_data: bool = True,
37413742
*,
37423743
name: str | None = None,
37433744
chunks: Literal["auto", "keep"] | ChunkCoords = "keep",
@@ -3766,6 +3767,10 @@ async def from_array(
37663767
name : str or None, optional
37673768
The name of the array within the store. If ``name`` is ``None``, the array will be located
37683769
at the root of the store.
3770+
write_data : bool, default True
3771+
Whether to copy the data from the input array to the new array.
3772+
If ``write_data`` is ``False``, the new array will be created with the same metadata as the
3773+
input array, but without any data.
37693774
chunks : ChunkCoords or "auto" or "keep", optional
37703775
Chunk shape of the array.
37713776
If not specified, defaults to the chunk shape of the data array.
@@ -3893,6 +3898,9 @@ async def from_array(
38933898
compressors = "auto"
38943899
if serializer == "keep":
38953900
serializer = "auto"
3901+
if not hasattr(data, "dtype") or not hasattr(data, "shape"):
3902+
data = np.array(data)
3903+
print(data.shape)
38963904
new_array = await create_array(
38973905
store,
38983906
name=name,
@@ -3913,29 +3921,30 @@ async def from_array(
39133921
overwrite=overwrite,
39143922
config=config,
39153923
)
3916-
if isinstance(data, Array):
3924+
if write_data:
3925+
if isinstance(data, Array):
39173926

3918-
async def _copy_region(chunk_coords: ChunkCoords | slice, _data: Array) -> None:
3919-
arr = await _data._async_array.getitem(chunk_coords)
3920-
await new_array.setitem(chunk_coords, arr)
3927+
async def _copy_region(chunk_coords: ChunkCoords | slice, _data: Array) -> None:
3928+
arr = await _data._async_array.getitem(chunk_coords)
3929+
await new_array.setitem(chunk_coords, arr)
39213930

3922-
# Stream data from the source array to the new array
3923-
await concurrent_map(
3924-
[(region, data) for region in new_array._iter_chunk_regions()],
3925-
_copy_region,
3926-
zarr.core.config.config.get("async.concurrency"),
3927-
)
3928-
else:
3931+
# Stream data from the source array to the new array
3932+
await concurrent_map(
3933+
[(region, data) for region in new_array._iter_chunk_regions()],
3934+
_copy_region,
3935+
zarr.core.config.config.get("async.concurrency"),
3936+
)
3937+
else:
39293938

3930-
async def _copy_region(chunk_coords: ChunkCoords | slice, _data: npt.ArrayLike) -> None:
3931-
await new_array.setitem(chunk_coords, _data[chunk_coords])
3939+
async def _copy_region(chunk_coords: ChunkCoords | slice, _data: npt.ArrayLike) -> None:
3940+
await new_array.setitem(chunk_coords, _data[chunk_coords])
39323941

3933-
# Stream data from the source array to the new array
3934-
await concurrent_map(
3935-
[(region, data) for region in new_array._iter_chunk_regions()],
3936-
_copy_region,
3937-
zarr.core.config.config.get("async.concurrency"),
3938-
)
3942+
# Stream data from the source array to the new array
3943+
await concurrent_map(
3944+
[(region, data) for region in new_array._iter_chunk_regions()],
3945+
_copy_region,
3946+
zarr.core.config.config.get("async.concurrency"),
3947+
)
39393948
return new_array
39403949

39413950

0 commit comments

Comments
 (0)