| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
import functools |
| 4 |
import math |
| 5 |
|
| 6 |
import dask.array as da |
| 7 |
import numpy as np |
| 8 |
import scipy |
| 9 |
|
| 10 |
from ..dispatch._dispatch_ndinterp import ( |
| 11 |
dispatch_spline_filter, |
| 12 |
dispatch_spline_filter1d, |
| 13 |
) |
| 14 |
from ..ndfilters._utils import _get_depth_boundary, _update_wrapper |
| 15 |
|
| 16 |
|
| 17 |
# magnitude of the maximum filter pole for each order |
| 18 |
# (obtained from scipy/ndimage/src/ni_splines.c) |
| 19 |
_maximum_pole = { |
| 20 |
2: 0.171572875253809902396622551580603843, |
| 21 |
3: 0.267949192431122706472553658494127633, |
| 22 |
4: 0.361341225900220177092212841325675255, |
| 23 |
5: 0.430575347099973791851434783493520110, |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
def _get_default_depth(order, tol=1e-8): |
| 28 |
"""Determine the approximate depth needed for a given tolerance. |
| 29 |
|
| 30 |
Here depth is chosen as the smallest integer such that ``|p| ** n < tol`` |
| 31 |
where `|p|` is the magnitude of the largest pole in the IIR filter. |
| 32 |
""" |
| 33 |
|
| 34 |
return math.ceil(np.log(tol) / np.log(_maximum_pole[order])) |
| 35 |
|
| 36 |
|
| 37 |
@_update_wrapper(scipy.ndimage.spline_filter) |
| 38 |
def spline_filter( |
| 39 |
image, |
| 40 |
order=3, |
| 41 |
output=np.float64, |
| 42 |
mode='mirror', |
| 43 |
output_chunks=None, |
| 44 |
*, |
| 45 |
depth=None, |
| 46 |
**kwargs |
| 47 |
): |
| 48 |
|
| 49 |
if not isinstance(image, da.core.Array): |
| 50 |
image = da.from_array(image) |
| 51 |
|
| 52 |
# use dispatching mechanism to determine backend |
| 53 |
spline_filter_method = dispatch_spline_filter(image) |
| 54 |
|
| 55 |
try: |
| 56 |
dtype = np.dtype(output) |
| 57 |
except TypeError: # pragma: no cover |
| 58 |
raise TypeError( # pragma: no cover |
| 59 |
"Could not coerce the provided output to a dtype. " |
| 60 |
"Passing array to output is not currently supported." |
| 61 |
) |
| 62 |
|
| 63 |
if depth is None: |
| 64 |
depth = _get_default_depth(order) |
| 65 |
|
| 66 |
if mode == 'wrap': |
| 67 |
raise NotImplementedError( |
| 68 |
"mode='wrap' is unsupported. It is recommended to use 'grid-wrap' " |
| 69 |
"instead." |
| 70 |
) |
| 71 |
|
| 72 |
# Note: depths of 12 and 24 give results matching SciPy to approximately |
| 73 |
# single and double precision accuracy, respectively. |
| 74 |
boundary = "periodic" if mode == 'grid-wrap' else "none" |
| 75 |
depth, boundary = _get_depth_boundary(image.ndim, depth, boundary) |
| 76 |
|
| 77 |
# cannot pass a func kwarg named "output" to map_overlap |
| 78 |
spline_filter_method = functools.partial(spline_filter_method, |
| 79 |
output=dtype) |
| 80 |
|
| 81 |
result = image.map_overlap( |
| 82 |
spline_filter_method, |
| 83 |
depth=depth, |
| 84 |
boundary=boundary, |
| 85 |
dtype=dtype, |
| 86 |
meta=image._meta, |
| 87 |
# spline_filter kwargs |
| 88 |
order=order, |
| 89 |
mode=mode, |
| 90 |
) |
| 91 |
|
| 92 |
return result |
| 93 |
|
| 94 |
|
| 95 |
@_update_wrapper(scipy.ndimage.spline_filter1d) |
| 96 |
def spline_filter1d( |
| 97 |
image, |
| 98 |
order=3, |
| 99 |
axis=-1, |
| 100 |
output=np.float64, |
| 101 |
mode='mirror', |
| 102 |
output_chunks=None, |
| 103 |
*, |
| 104 |
depth=None, |
| 105 |
**kwargs |
| 106 |
): |
| 107 |
|
| 108 |
if not isinstance(image, da.core.Array): |
| 109 |
image = da.from_array(image) |
| 110 |
|
| 111 |
# use dispatching mechanism to determine backend |
| 112 |
spline_filter1d_method = dispatch_spline_filter1d(image) |
| 113 |
|
| 114 |
try: |
| 115 |
dtype = np.dtype(output) |
| 116 |
except TypeError: # pragma: no cover |
| 117 |
raise TypeError( # pragma: no cover |
| 118 |
"Could not coerce the provided output to a dtype. " |
| 119 |
"Passing array to output is not currently supported." |
| 120 |
) |
| 121 |
|
| 122 |
if depth is None: |
| 123 |
depth = _get_default_depth(order) |
| 124 |
|
| 125 |
# use depth 0 on all axes except the filtered axis |
| 126 |
if not np.isscalar(depth): |
| 127 |
raise ValueError("depth must be a scalar value") |
| 128 |
depths = [0] * image.ndim |
| 129 |
depths[axis] = depth |
| 130 |
|
| 131 |
if mode == 'wrap': |
| 132 |
raise NotImplementedError( |
| 133 |
"mode='wrap' is unsupported. It is recommended to use 'grid-wrap' " |
| 134 |
"instead." |
| 135 |
) |
| 136 |
|
| 137 |
# cannot pass a func kwarg named "output" to map_overlap |
| 138 |
spline_filter1d_method = functools.partial(spline_filter1d_method, |
| 139 |
output=dtype) |
| 140 |
|
| 141 |
result = image.map_overlap( |
| 142 |
spline_filter1d_method, |
| 143 |
depth=tuple(depths), |
| 144 |
boundary="periodic" if mode == 'grid-wrap' else "none", |
| 145 |
dtype=dtype, |
| 146 |
meta=image._meta, |
| 147 |
# spline_filter1d kwargs |
| 148 |
order=order, |
| 149 |
axis=axis, |
| 150 |
mode=mode, |
| 151 |
) |
| 152 |
|
| 153 |
return result |