|
18 | 18 |
|
19 | 19 | from zarr.abc.codec import Codec |
20 | 20 | from zarr.api.asynchronous import ArrayLike, PathLike |
| 21 | + from zarr.core.array import ( |
| 22 | + ArrayBytesCodecParam, |
| 23 | + CompressorsParam, |
| 24 | + FiltersParam, |
| 25 | + ShardsParam, |
| 26 | + ) |
21 | 27 | from zarr.core.array_spec import ArrayConfig, ArrayConfigParams |
22 | 28 | from zarr.core.buffer import NDArrayLike |
23 | | - from zarr.core.chunk_key_encodings import ChunkKeyEncoding |
24 | | - from zarr.core.common import JSON, AccessModeLiteral, ChunkCoords, MemoryOrder, ZarrFormat |
| 29 | + from zarr.core.chunk_key_encodings import ChunkKeyEncoding, ChunkKeyEncodingParams |
| 30 | + from zarr.core.common import ( |
| 31 | + JSON, |
| 32 | + AccessModeLiteral, |
| 33 | + ChunkCoords, |
| 34 | + MemoryOrder, |
| 35 | + ShapeLike, |
| 36 | + ZarrFormat, |
| 37 | + ) |
25 | 38 | from zarr.storage import StoreLike |
26 | 39 |
|
27 | 40 | __all__ = [ |
@@ -534,6 +547,31 @@ def create_group( |
534 | 547 | attributes: dict[str, Any] | None = None, |
535 | 548 | storage_options: dict[str, Any] | None = None, |
536 | 549 | ) -> Group: |
| 550 | + """Create a group. |
| 551 | +
|
| 552 | + Parameters |
| 553 | + ---------- |
| 554 | + store : Store or str |
| 555 | + Store or path to directory in file system. |
| 556 | + path : str, optional |
| 557 | + Group path within store. |
| 558 | + overwrite : bool, optional |
| 559 | + If True, pre-existing data at ``path`` will be deleted before |
| 560 | + creating the group. |
| 561 | + zarr_format : {2, 3, None}, optional |
| 562 | + The zarr format to use when saving. |
| 563 | + If no ``zarr_format`` is provided, the default format will be used. |
| 564 | + This default can be changed by modifying the value of ``default_zarr_format`` |
| 565 | + in :mod:`zarr.core.config`. |
| 566 | + storage_options : dict |
| 567 | + If using an fsspec URL to create the store, these will be passed to |
| 568 | + the backend implementation. Ignored otherwise. |
| 569 | +
|
| 570 | + Returns |
| 571 | + ------- |
| 572 | + Group |
| 573 | + The new group. |
| 574 | + """ |
537 | 575 | return Group( |
538 | 576 | sync( |
539 | 577 | async_api.create_group( |
@@ -700,8 +738,156 @@ def create( |
700 | 738 | ) |
701 | 739 |
|
702 | 740 |
|
703 | | -def create_array(*args: Any, **kwargs: Any) -> Array: |
704 | | - return Array(sync(zarr.core.array.create_array(*args, **kwargs))) |
| 741 | +def create_array( |
| 742 | + store: str | StoreLike, |
| 743 | + *, |
| 744 | + name: str | None = None, |
| 745 | + shape: ShapeLike, |
| 746 | + dtype: npt.DTypeLike, |
| 747 | + chunks: ChunkCoords | Literal["auto"] = "auto", |
| 748 | + shards: ShardsParam | None = None, |
| 749 | + filters: FiltersParam | None = "auto", |
| 750 | + compressors: CompressorsParam = "auto", |
| 751 | + array_bytes_codec: ArrayBytesCodecParam = "auto", |
| 752 | + fill_value: Any | None = None, |
| 753 | + order: MemoryOrder | None = None, |
| 754 | + zarr_format: ZarrFormat | None = 3, |
| 755 | + attributes: dict[str, JSON] | None = None, |
| 756 | + chunk_key_encoding: ChunkKeyEncoding | ChunkKeyEncodingParams | None = None, |
| 757 | + dimension_names: Iterable[str] | None = None, |
| 758 | + storage_options: dict[str, Any] | None = None, |
| 759 | + overwrite: bool = False, |
| 760 | + config: ArrayConfig | ArrayConfigParams | None = None, |
| 761 | +) -> Array: |
| 762 | + """Create an array. |
| 763 | +
|
| 764 | + Parameters |
| 765 | + ---------- |
| 766 | + store : str or Store |
| 767 | + Store or path to directory in file system or name of zip file. |
| 768 | + name : str or None, optional |
| 769 | + The name of the array within the store. If ``name`` is ``None``, the array will be located |
| 770 | + at the root of the store. |
| 771 | + shape : ChunkCoords |
| 772 | + Shape of the array. |
| 773 | + dtype : npt.DTypeLike |
| 774 | + Data type of the array. |
| 775 | + chunks : ChunkCoords, optional |
| 776 | + Chunk shape of the array. |
| 777 | + If not specified, default are guessed based on the shape and dtype. |
| 778 | + shards : ChunkCoords, optional |
| 779 | + Shard shape of the array. The default value of ``None`` results in no sharding at all. |
| 780 | + filters : Iterable[Codec], optional |
| 781 | + Iterable of filters to apply to each chunk of the array, in order, before serializing that |
| 782 | + chunk to bytes. |
| 783 | +
|
| 784 | + For Zarr v3, a "filter" is a codec that takes an array and returns an array, |
| 785 | + and these values must be instances of ``ArrayArrayCodec``, or dict representations |
| 786 | + of ``ArrayArrayCodec``. |
| 787 | + If ``filters`` and ``compressors`` are not specified, then the default codecs for |
| 788 | + Zarr v3 will be used. |
| 789 | + These defaults can be changed by modifying the value of ``array.v3_default_codecs`` |
| 790 | + in :mod:`zarr.core.config`. |
| 791 | + Use ``None`` to omit default filters. |
| 792 | +
|
| 793 | + For Zarr v2, a "filter" can be any numcodecs codec; you should ensure that the |
| 794 | + the order if your filters is consistent with the behavior of each filter. |
| 795 | + If no ``filters`` are provided, a default set of filters will be used. |
| 796 | + These defaults can be changed by modifying the value of ``array.v2_default_filters`` |
| 797 | + in :mod:`zarr.core.config`. |
| 798 | + Use ``None`` to omit default filters. |
| 799 | + compressors : Iterable[Codec], optional |
| 800 | + List of compressors to apply to the array. Compressors are applied in order, and after any |
| 801 | + filters are applied (if any are specified). |
| 802 | +
|
| 803 | + For Zarr v3, a "compressor" is a codec that takes a bytestrea, and |
| 804 | + returns another bytestream. Multiple compressors my be provided for Zarr v3. |
| 805 | + If ``filters`` and ``compressors`` are not specified, then the default codecs for |
| 806 | + Zarr v3 will be used. |
| 807 | + These defaults can be changed by modifying the value of ``array.v3_default_codecs`` |
| 808 | + in :mod:`zarr.core.config`. |
| 809 | + Use ``None`` to omit default compressors. |
| 810 | +
|
| 811 | + For Zarr v2, a "compressor" can be any numcodecs codec. Only a single compressor may |
| 812 | + be provided for Zarr v2. |
| 813 | + If no ``compressors`` are provided, a default compressor will be used. |
| 814 | + These defaults can be changed by modifying the value of ``array.v2_default_compressor`` |
| 815 | + in :mod:`zarr.core.config`. |
| 816 | + Use ``None`` to omit the default compressor. |
| 817 | + array_bytes_codec : dict[str, JSON] | ArrayBytesCodec, optional |
| 818 | + Array-to-bytes codec to use for encoding the array data. |
| 819 | + Zarr v3 only. Zarr v2 arrays use implicit array-to-bytes conversion. |
| 820 | + If no ``array_bytes_codec`` is provided, the `zarr.codecs.BytesCodec` codec will be used. |
| 821 | + fill_value : Any, optional |
| 822 | + Fill value for the array. |
| 823 | + order : {"C", "F"}, optional |
| 824 | + The memory of the array (default is "C"). |
| 825 | + For Zarr v2, this parameter sets the memory order of the array. |
| 826 | + For Zarr v3, this parameter is deprecated, because memory order |
| 827 | + is a runtime parameter for Zarr v3 arrays. The recommended way to specify the memory |
| 828 | + order for Zarr v3 arrays is via the ``config`` parameter, e.g. ``{'config': 'C'}``. |
| 829 | + If no ``order`` is provided, a default order will be used. |
| 830 | + This default can be changed by modifying the value of ``array.order`` in :mod:`zarr.core.config`. |
| 831 | + zarr_format : {2, 3}, optional |
| 832 | + The zarr format to use when saving. |
| 833 | + attributes : dict, optional |
| 834 | + Attributes for the array. |
| 835 | + chunk_key_encoding : ChunkKeyEncoding, optional |
| 836 | + A specification of how the chunk keys are represented in storage. |
| 837 | + For Zarr v3, the default is ``{"name": "default", "separator": "/"}}``. |
| 838 | + For Zarr v2, the default is ``{"name": "v2", "separator": "."}}``. |
| 839 | + dimension_names : Iterable[str], optional |
| 840 | + The names of the dimensions (default is None). |
| 841 | + Zarr v3 only. Zarr v2 arrays should not use this parameter. |
| 842 | + storage_options : dict, optional |
| 843 | + If using an fsspec URL to create the store, these will be passed to the backend implementation. |
| 844 | + Ignored otherwise. |
| 845 | + overwrite : bool, default False |
| 846 | + Whether to overwrite an array with the same name in the store, if one exists. |
| 847 | + config : ArrayConfig or ArrayConfigParams, optional |
| 848 | + Runtime configuration for the array. |
| 849 | +
|
| 850 | + Returns |
| 851 | + ------- |
| 852 | + Array |
| 853 | + The array. |
| 854 | +
|
| 855 | + Examples |
| 856 | + -------- |
| 857 | + >>> import zarr |
| 858 | + >>> store = zarr.storage.MemoryStore(mode='w') |
| 859 | + >>> arr = await zarr.create_array( |
| 860 | + >>> store=store, |
| 861 | + >>> shape=(100,100), |
| 862 | + >>> chunks=(10,10), |
| 863 | + >>> dtype='i4', |
| 864 | + >>> fill_value=0) |
| 865 | + <Array memory://140349042942400 shape=(100, 100) dtype=int32> |
| 866 | + """ |
| 867 | + return Array( |
| 868 | + sync( |
| 869 | + zarr.core.array.create_array( |
| 870 | + store=store, |
| 871 | + name=name, |
| 872 | + shape=shape, |
| 873 | + dtype=dtype, |
| 874 | + chunks=chunks, |
| 875 | + shards=shards, |
| 876 | + filters=filters, |
| 877 | + compressors=compressors, |
| 878 | + array_bytes_codec=array_bytes_codec, |
| 879 | + fill_value=fill_value, |
| 880 | + order=order, |
| 881 | + zarr_format=zarr_format, |
| 882 | + attributes=attributes, |
| 883 | + chunk_key_encoding=chunk_key_encoding, |
| 884 | + dimension_names=dimension_names, |
| 885 | + storage_options=storage_options, |
| 886 | + overwrite=overwrite, |
| 887 | + config=config, |
| 888 | + ) |
| 889 | + ) |
| 890 | + ) |
705 | 891 |
|
706 | 892 |
|
707 | 893 | # TODO: add type annotations for kwargs |
|
0 commit comments