|
1 | 1 | from distutils.version import LooseVersion
|
| 2 | +from typing import Iterable |
2 | 3 |
|
3 |
| -import dask.array as da |
4 | 4 | import numpy as np
|
5 |
| -from dask import __version__ as dask_version |
| 5 | + |
| 6 | +try: |
| 7 | + import dask.array as da |
| 8 | + from dask import __version__ as dask_version |
| 9 | +except ImportError: |
| 10 | + dask_version = "0.0.0" |
| 11 | + da = None |
6 | 12 |
|
7 | 13 | if LooseVersion(dask_version) >= LooseVersion("2.0.0"):
|
8 | 14 | meta_from_array = da.utils.meta_from_array
|
@@ -89,3 +95,76 @@ def meta_from_array(x, ndim=None, dtype=None):
|
89 | 95 | meta = meta.astype(dtype)
|
90 | 96 |
|
91 | 97 | return meta
|
| 98 | + |
| 99 | + |
| 100 | +if LooseVersion(dask_version) >= LooseVersion("2.8.1"): |
| 101 | + median = da.median |
| 102 | +else: |
| 103 | + # Copied from dask v2.8.1 |
| 104 | + # Used under the terms of Dask's license, see licenses/DASK_LICENSE. |
| 105 | + def median(a, axis=None, keepdims=False): |
| 106 | + """ |
| 107 | + This works by automatically chunking the reduced axes to a single chunk |
| 108 | + and then calling ``numpy.median`` function across the remaining dimensions |
| 109 | + """ |
| 110 | + |
| 111 | + if axis is None: |
| 112 | + raise NotImplementedError( |
| 113 | + "The da.median function only works along an axis. " |
| 114 | + "The full algorithm is difficult to do in parallel" |
| 115 | + ) |
| 116 | + |
| 117 | + if not isinstance(axis, Iterable): |
| 118 | + axis = (axis,) |
| 119 | + |
| 120 | + axis = [ax + a.ndim if ax < 0 else ax for ax in axis] |
| 121 | + |
| 122 | + a = a.rechunk({ax: -1 if ax in axis else "auto" for ax in range(a.ndim)}) |
| 123 | + |
| 124 | + result = a.map_blocks( |
| 125 | + np.median, |
| 126 | + axis=axis, |
| 127 | + keepdims=keepdims, |
| 128 | + drop_axis=axis if not keepdims else None, |
| 129 | + chunks=[1 if ax in axis else c for ax, c in enumerate(a.chunks)] |
| 130 | + if keepdims |
| 131 | + else None, |
| 132 | + ) |
| 133 | + |
| 134 | + return result |
| 135 | + |
| 136 | + |
| 137 | +if LooseVersion(dask_version) > LooseVersion("2.9.0"): |
| 138 | + nanmedian = da.nanmedian |
| 139 | +else: |
| 140 | + |
| 141 | + def nanmedian(a, axis=None, keepdims=False): |
| 142 | + """ |
| 143 | + This works by automatically chunking the reduced axes to a single chunk |
| 144 | + and then calling ``numpy.nanmedian`` function across the remaining dimensions |
| 145 | + """ |
| 146 | + |
| 147 | + if axis is None: |
| 148 | + raise NotImplementedError( |
| 149 | + "The da.nanmedian function only works along an axis. " |
| 150 | + "The full algorithm is difficult to do in parallel" |
| 151 | + ) |
| 152 | + |
| 153 | + if not isinstance(axis, Iterable): |
| 154 | + axis = (axis,) |
| 155 | + |
| 156 | + axis = [ax + a.ndim if ax < 0 else ax for ax in axis] |
| 157 | + |
| 158 | + a = a.rechunk({ax: -1 if ax in axis else "auto" for ax in range(a.ndim)}) |
| 159 | + |
| 160 | + result = a.map_blocks( |
| 161 | + np.nanmedian, |
| 162 | + axis=axis, |
| 163 | + keepdims=keepdims, |
| 164 | + drop_axis=axis if not keepdims else None, |
| 165 | + chunks=[1 if ax in axis else c for ax, c in enumerate(a.chunks)] |
| 166 | + if keepdims |
| 167 | + else None, |
| 168 | + ) |
| 169 | + |
| 170 | + return result |
0 commit comments