Skip to content

Commit 6166c87

Browse files
authored
Make chunk manager an option in set_options (pydata#9362)
* Make chunk manager an option in `set_options` * Add docs * Add unit test * Add what's new entry
1 parent 6b6a3bd commit 6166c87

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

doc/internals/chunked-arrays.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ Once the chunkmanager subclass has been registered, xarray objects wrapping the
9191
The latter two methods ultimately call the chunkmanager's implementation of ``.from_array``, to which they pass the ``from_array_kwargs`` dict.
9292
The ``chunked_array_type`` kwarg selects which registered chunkmanager subclass to dispatch to. It defaults to ``'dask'``
9393
if Dask is installed, otherwise it defaults to whichever chunkmanager is registered if only one is registered.
94-
If multiple chunkmanagers are registered it will raise an error by default.
94+
If multiple chunkmanagers are registered, the ``chunk_manager`` configuration option (which can be set using :py:func:`set_options`)
95+
will be used to determine which chunkmanager to use, defaulting to ``'dask'``.
9596

9697
Parallel processing without chunks
9798
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

doc/whats-new.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ v2024.07.1 (unreleased)
2222

2323
New Features
2424
~~~~~~~~~~~~
25-
25+
- Make chunk manager an option in ``set_options`` (:pull:`9362`).
26+
By `Tom White <https://github.com/tomwhite>`_.
2627

2728
Breaking changes
2829
~~~~~~~~~~~~~~~~

xarray/core/options.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
Options = Literal[
1212
"arithmetic_join",
13+
"chunk_manager",
1314
"cmap_divergent",
1415
"cmap_sequential",
1516
"display_max_rows",
@@ -36,6 +37,7 @@
3637
class T_Options(TypedDict):
3738
arithmetic_broadcast: bool
3839
arithmetic_join: Literal["inner", "outer", "left", "right", "exact"]
40+
chunk_manager: str
3941
cmap_divergent: str | Colormap
4042
cmap_sequential: str | Colormap
4143
display_max_rows: int
@@ -62,6 +64,7 @@ class T_Options(TypedDict):
6264
OPTIONS: T_Options = {
6365
"arithmetic_broadcast": True,
6466
"arithmetic_join": "inner",
67+
"chunk_manager": "dask",
6568
"cmap_divergent": "RdBu_r",
6669
"cmap_sequential": "viridis",
6770
"display_max_rows": 12,
@@ -172,7 +175,9 @@ class set_options:
172175
- "override": if indexes are of same size, rewrite indexes to be
173176
those of the first object with that dimension. Indexes for the same
174177
dimension must have the same size in all objects.
175-
178+
chunk_manager : str, default: "dask"
179+
Chunk manager to use for chunked array computations when multiple
180+
options are installed.
176181
cmap_divergent : str or matplotlib.colors.Colormap, default: "RdBu_r"
177182
Colormap to use for divergent data plots. If string, must be
178183
matplotlib built-in colormap. Can also be a Colormap object

xarray/namedarray/parallelcompat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import numpy as np
1616

17+
from xarray.core.options import OPTIONS
1718
from xarray.core.utils import emit_user_level_warning
1819
from xarray.namedarray.pycompat import is_chunked_array
1920

@@ -101,8 +102,8 @@ def guess_chunkmanager(
101102
# use the only option available
102103
manager = next(iter(chunkmanagers.keys()))
103104
else:
104-
# default to trying to use dask
105-
manager = "dask"
105+
# use the one in options (default dask)
106+
manager = OPTIONS["chunk_manager"]
106107

107108
if isinstance(manager, str):
108109
if manager not in chunkmanagers:

xarray/tests/test_parallelcompat.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import numpy as np
77
import pytest
88

9+
from xarray import set_options
910
from xarray.core.types import T_Chunks, T_DuckArray, T_NormalizedChunks
1011
from xarray.namedarray._typing import _Chunks
1112
from xarray.namedarray.daskmanager import DaskManager
@@ -152,6 +153,11 @@ def test_get_chunkmanger(self, register_dummy_chunkmanager) -> None:
152153
chunkmanager = guess_chunkmanager("dummy")
153154
assert isinstance(chunkmanager, DummyChunkManager)
154155

156+
def test_get_chunkmanger_via_set_options(self, register_dummy_chunkmanager) -> None:
157+
with set_options(chunk_manager="dummy"):
158+
chunkmanager = guess_chunkmanager(None)
159+
assert isinstance(chunkmanager, DummyChunkManager)
160+
155161
def test_fail_on_nonexistent_chunkmanager(self) -> None:
156162
with pytest.raises(ValueError, match="unrecognized chunk manager foo"):
157163
guess_chunkmanager("foo")

0 commit comments

Comments
 (0)