-
-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathconfig.py
More file actions
176 lines (143 loc) · 7.14 KB
/
config.py
File metadata and controls
176 lines (143 loc) · 7.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""
The config module is responsible for managing the configuration of zarr and is based on the Donfig python library.
For selecting custom implementations of codecs, pipelines, buffers and ndbuffers, first register the implementations
in the registry and then select them in the config.
Example:
An implementation of the bytes codec in a class ``your.module.NewBytesCodec`` requires the value of ``codecs.bytes``
to be ``your.module.NewBytesCodec``. Donfig can be configured programmatically, by environment variables, or from
YAML files in standard locations.
.. code-block:: python
from your.module import NewBytesCodec
from zarr.core.config import register_codec, config
register_codec("bytes", NewBytesCodec)
config.set({"codecs.bytes": "your.module.NewBytesCodec"})
Instead of setting the value programmatically with ``config.set``, you can also set the value with an environment
variable. The environment variable ``ZARR_CODECS__BYTES`` can be set to ``your.module.NewBytesCodec``. The double
underscore ``__`` is used to indicate nested access.
.. code-block:: bash
export ZARR_CODECS__BYTES="your.module.NewBytesCodec"
For more information, see the Donfig documentation at https://github.com/pytroll/donfig.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Literal, cast
from donfig import Config as DConfig
if TYPE_CHECKING:
from donfig.config_obj import ConfigSet
from zarr.core.dtype.wrapper import ZDType
class BadConfigError(ValueError):
_msg = "bad Config: %r"
# These values are used for rough categorization of data types
# we use this for choosing a default encoding scheme based on the data type. Specifically,
# these categories are keys in a configuration dictionary.
# it is not a part of the ZDType class because these categories are more of an implementation detail
# of our config system rather than a useful attribute of any particular data type.
DTypeCategory = Literal["variable-length-string", "default"]
class Config(DConfig): # type: ignore[misc]
"""The Config will collect configuration from config files and environment variables
Example environment variables:
Grabs environment variables of the form "ZARR_FOO__BAR_BAZ=123" and
turns these into config variables of the form ``{"foo": {"bar-baz": 123}}``
It transforms the key and value in the following way:
- Lower-cases the key text
- Treats ``__`` (double-underscore) as nested access
- Calls ``ast.literal_eval`` on the value
"""
def reset(self) -> None:
self.clear()
self.refresh()
def enable_gpu(self) -> ConfigSet:
"""
Configure Zarr to use GPUs where possible.
"""
return self.set(
{"buffer": "zarr.buffer.gpu.Buffer", "ndbuffer": "zarr.buffer.gpu.NDBuffer"}
)
# these keys were removed from the config as part of the 3.1.0 release.
# these deprecations should be removed in 3.1.1 or thereabouts.
deprecations = {
"array.v2_default_compressor.numeric": None,
"array.v2_default_compressor.string": None,
"array.v2_default_compressor.bytes": None,
"array.v2_default_filters.string": None,
"array.v2_default_filters.bytes": None,
"array.v3_default_filters.numeric": None,
"array.v3_default_filters.raw": None,
"array.v3_default_filters.bytes": None,
"array.v3_default_serializer.numeric": None,
"array.v3_default_serializer.string": None,
"array.v3_default_serializer.bytes": None,
"array.v3_default_compressors.string": None,
"array.v3_default_compressors.bytes": None,
"array.v3_default_compressors": None,
}
# The default configuration for zarr
config = Config(
"zarr",
defaults=[
{
"default_zarr_format": 3,
"array": {
"order": "C",
"write_empty_chunks": False,
},
"async": {"concurrency": 10, "timeout": None, "use_uvloop": False},
"threading": {"max_workers": None},
"json_indent": 2,
"codec_pipeline": {
"path": "zarr.core.codec_pipeline.BatchedCodecPipeline",
"batch_size": 1,
},
"codecs": {
"blosc": "zarr.codecs.blosc.BloscCodec",
"gzip": "zarr.codecs.gzip.GzipCodec",
"zstd": "zarr.codecs.zstd.ZstdCodec",
"bytes": "zarr.codecs.bytes.BytesCodec",
"endian": "zarr.codecs.bytes.BytesCodec", # compatibility with earlier versions of ZEP1
"crc32c": "zarr.codecs.crc32c_.Crc32cCodec",
"sharding_indexed": "zarr.codecs.sharding.ShardingCodec",
"transpose": "zarr.codecs.transpose.TransposeCodec",
"vlen-utf8": "zarr.codecs.vlen_utf8.VLenUTF8Codec",
"vlen-bytes": "zarr.codecs.vlen_utf8.VLenBytesCodec",
"numcodecs.bz2": "zarr.codecs.numcodecs.BZ2",
"numcodecs.crc32": "zarr.codecs.numcodecs.CRC32",
"numcodecs.crc32c": "zarr.codecs.numcodecs.CRC32C",
"numcodecs.lz4": "zarr.codecs.numcodecs.LZ4",
"numcodecs.lzma": "zarr.codecs.numcodecs.LZMA",
"numcodecs.zfpy": "zarr.codecs.numcodecs.ZFPY",
"numcodecs.adler32": "zarr.codecs.numcodecs.Adler32",
"numcodecs.astype": "zarr.codecs.numcodecs.AsType",
"numcodecs.bitround": "zarr.codecs.numcodecs.BitRound",
"numcodecs.blosc": "zarr.codecs.numcodecs.Blosc",
"numcodecs.delta": "zarr.codecs.numcodecs.Delta",
"numcodecs.fixedscaleoffset": "zarr.codecs.numcodecs.FixedScaleOffset",
"numcodecs.fletcher32": "zarr.codecs.numcodecs.Fletcher32",
"numcodecs.gzip": "zarr.codecs.numcodecs.GZip",
"numcodecs.jenkins_lookup3": "zarr.codecs.numcodecs.JenkinsLookup3",
"numcodecs.pcodec": "zarr.codecs.numcodecs.PCodec",
"numcodecs.packbits": "zarr.codecs.numcodecs.PackBits",
"numcodecs.shuffle": "zarr.codecs.numcodecs.Shuffle",
"numcodecs.quantize": "zarr.codecs.numcodecs.Quantize",
"numcodecs.zlib": "zarr.codecs.numcodecs.Zlib",
"numcodecs.zstd": "zarr.codecs.numcodecs.Zstd",
},
"buffer": "zarr.buffer.cpu.Buffer",
"ndbuffer": "zarr.buffer.cpu.NDBuffer",
}
],
deprecations=deprecations,
)
def parse_indexing_order(data: Any) -> Literal["C", "F"]:
if data in ("C", "F"):
return cast("Literal['C', 'F']", data)
msg = f"Expected one of ('C', 'F'), got {data} instead."
raise ValueError(msg)
def categorize_data_type(dtype: ZDType[Any, Any]) -> DTypeCategory:
"""
Classify a ZDType. The return value is a string which belongs to the type ``DTypeCategory``.
This is used by the config system to determine how to encode arrays with the associated data type
when the user has not specified a particular serialization scheme.
"""
from zarr.core.dtype import VariableLengthUTF8
if isinstance(dtype, VariableLengthUTF8):
return "variable-length-string"
return "default"