Skip to content

Commit

Permalink
fixed compat
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme1 committed Nov 30, 2024
1 parent db8f9a8 commit 9eee515
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 45 deletions.
7 changes: 5 additions & 2 deletions imops/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
from numpy import VisibleDeprecationWarning

try:
from scipy.ndimage._morphology._ni_support import _normalize_sequence as normalize_sequence
from scipy.ndimage._morphology import _ni_support
except ImportError:
from scipy.ndimage.morphology._ni_support import _normalize_sequence as normalize_sequence
from scipy.ndimage.morphology import _ni_support

try:
from scipy.spatial import QhullError
except ImportError:
from scipy.spatial.qhull import QhullError

from scipy.ndimage._nd_image import euclidean_feature_transform # noqa


normalize_sequence = _ni_support._normalize_sequence # noqa
10 changes: 7 additions & 3 deletions imops/crop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

import numpy as np

from .backend import BackendLike
Expand All @@ -6,7 +8,9 @@
from .utils import AxesLike, AxesParams, assert_subdtype, broadcast_axis, fill_by_indices


def crop_to_shape(x: np.ndarray, shape: AxesLike, axis: AxesLike = None, ratio: AxesParams = 0.5) -> np.ndarray:
def crop_to_shape(
x: np.ndarray, shape: AxesLike, axis: Optional[AxesLike] = None, ratio: AxesParams = 0.5
) -> np.ndarray:
"""
Crop `x` to match `shape` along `axis`.
Expand Down Expand Up @@ -57,8 +61,8 @@ def crop_to_shape(x: np.ndarray, shape: AxesLike, axis: AxesLike = None, ratio:
def crop_to_box(
x: np.ndarray,
box: np.ndarray,
axis: AxesLike = None,
padding_values: AxesParams = None,
axis: Optional[AxesLike] = None,
padding_values: Optional[AxesParams] = None,
num_threads: int = _NUMERIC_DEFAULT_NUM_THREADS,
backend: BackendLike = None,
) -> np.ndarray:
Expand Down
4 changes: 2 additions & 2 deletions imops/interp1d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import Optional, Union
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -71,7 +71,7 @@ def __init__(
kind: Union[int, str] = 'linear',
axis: int = -1,
copy: bool = True,
bounds_error: bool = None,
bounds_error: Optional[bool] = None,
fill_value: Union[float, str] = np.nan,
assume_sorted: bool = False,
num_threads: int = -1,
Expand Down
7 changes: 4 additions & 3 deletions imops/interp2d.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from platform import python_version
from typing import Optional

import numpy as np
from scipy.spatial import KDTree
Expand Down Expand Up @@ -47,9 +48,9 @@ class Linear2DInterpolator(Linear2DInterpolatorCpp):
def __init__(
self,
points: np.ndarray,
values: np.ndarray = None,
values: Optional[np.ndarray] = None,
num_threads: int = 1,
triangles: np.ndarray = None,
triangles: Optional[np.ndarray] = None,
**kwargs,
):
if triangles is not None:
Expand Down Expand Up @@ -77,7 +78,7 @@ def __init__(
# FIXME: add backend dispatch
self.num_threads = normalize_num_threads(num_threads, Cython(), warn_stacklevel=3)

def __call__(self, points: np.ndarray, values: np.ndarray = None, fill_value: float = 0.0) -> np.ndarray:
def __call__(self, points: np.ndarray, values: Optional[np.ndarray] = None, fill_value: float = 0.0) -> np.ndarray:
"""
Evaluate the interpolant
Expand Down
12 changes: 6 additions & 6 deletions imops/measure.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from collections import namedtuple
from platform import python_version
from typing import List, NamedTuple, Sequence, Tuple, Union
from typing import List, NamedTuple, Optional, Sequence, Tuple, Union
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -32,12 +32,12 @@
# TODO: Make it work and test on immutable arrays as soon as `cc3d` package is fixed
def label(
label_image: np.ndarray,
background: int = None,
connectivity: int = None,
background: Optional[int] = None,
connectivity: Optional[int] = None,
return_num: bool = False,
return_labels: bool = False,
return_sizes: bool = False,
dtype: type = None,
dtype: Optional[type] = None,
) -> Union[np.ndarray, NamedTuple]:
"""
Fast version of `skimage.measure.label` which optionally returns number of connected components, labels and sizes.
Expand Down Expand Up @@ -139,8 +139,8 @@ def label(

def center_of_mass(
array: np.ndarray,
labels: np.ndarray = None,
index: Union[int, Sequence[int]] = None,
labels: Union[np.ndarray, None] = None,
index: Union[int, Sequence[int], None] = None,
num_threads: int = -1,
backend: BackendLike = None,
) -> Union[Tuple[float, ...], List[Tuple[float, ...]]]:
Expand Down
24 changes: 12 additions & 12 deletions imops/morphology.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Tuple, Union
from typing import Callable, Optional, Tuple, Union
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -32,8 +32,8 @@ def morphology_op_wrapper(
) -> Callable:
def wrapped(
image: np.ndarray,
footprint: np.ndarray = None,
output: np.ndarray = None,
footprint: Optional[np.ndarray] = None,
output: Optional[np.ndarray] = None,
boxed: bool = False,
num_threads: int = -1,
backend: BackendLike = None,
Expand Down Expand Up @@ -163,8 +163,8 @@ def wrapped(

def binary_dilation(
image: np.ndarray,
footprint: np.ndarray = None,
output: np.ndarray = None,
footprint: Optional[np.ndarray] = None,
output: Optional[np.ndarray] = None,
boxed: bool = False,
num_threads: int = -1,
backend: BackendLike = None,
Expand Down Expand Up @@ -217,8 +217,8 @@ def binary_dilation(

def binary_erosion(
image: np.ndarray,
footprint: np.ndarray = None,
output: np.ndarray = None,
footprint: Optional[np.ndarray] = None,
output: Optional[np.ndarray] = None,
boxed: bool = False,
num_threads: int = -1,
backend: BackendLike = None,
Expand Down Expand Up @@ -271,8 +271,8 @@ def binary_erosion(

def binary_closing(
image: np.ndarray,
footprint: np.ndarray = None,
output: np.ndarray = None,
footprint: Optional[np.ndarray] = None,
output: Optional[np.ndarray] = None,
boxed: bool = False,
num_threads: int = -1,
backend: BackendLike = None,
Expand Down Expand Up @@ -326,8 +326,8 @@ def binary_closing(

def binary_opening(
image: np.ndarray,
footprint: np.ndarray = None,
output: np.ndarray = None,
footprint: Optional[np.ndarray] = None,
output: Optional[np.ndarray] = None,
boxed: bool = False,
num_threads: int = -1,
backend: BackendLike = None,
Expand Down Expand Up @@ -371,7 +371,7 @@ def binary_opening(

def distance_transform_edt(
image: np.ndarray,
sampling: Tuple[float] = None,
sampling: Optional[Tuple[float]] = None,
return_distances: bool = True,
return_indices: bool = False,
num_threads: int = -1,
Expand Down
8 changes: 4 additions & 4 deletions imops/numeric.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Sequence, Union
from typing import Callable, Optional, Sequence, Union

import numpy as np

Expand Down Expand Up @@ -99,7 +99,7 @@ def _choose_cython_copy(ndim: int, is_fp16: bool, fast: bool) -> Callable:
def pointwise_add(
nums: np.ndarray,
summand: Union[np.array, int, float],
output: np.ndarray = None,
output: Optional[np.ndarray] = None,
num_threads: int = _NUMERIC_DEFAULT_NUM_THREADS,
backend: BackendLike = None,
) -> np.ndarray:
Expand Down Expand Up @@ -256,7 +256,7 @@ def fill_(
def full(
shape: Union[int, Sequence[int]],
fill_value: Union[np.number, int, float],
dtype: Union[type, str] = None,
dtype: Union[type, str, None] = None,
order: str = 'C',
num_threads: int = _NUMERIC_DEFAULT_NUM_THREADS,
backend: BackendLike = None,
Expand Down Expand Up @@ -302,7 +302,7 @@ def full(

def copy(
nums: np.ndarray,
output: np.ndarray = None,
output: Optional[np.ndarray] = None,
order: str = 'K',
num_threads: int = _NUMERIC_DEFAULT_NUM_THREADS,
backend: BackendLike = None,
Expand Down
8 changes: 4 additions & 4 deletions imops/pad.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Callable, Sequence, Union
from typing import Callable, Optional, Sequence, Union

import numpy as np

Expand All @@ -10,7 +10,7 @@
def pad(
x: np.ndarray,
padding: Union[AxesLike, Sequence[Sequence[int]]],
axis: AxesLike = None,
axis: Optional[AxesLike] = None,
padding_values: Union[AxesParams, Callable] = 0,
num_threads: int = _NUMERIC_DEFAULT_NUM_THREADS,
backend: BackendLike = None,
Expand Down Expand Up @@ -76,7 +76,7 @@ def pad(
def pad_to_shape(
x: np.ndarray,
shape: AxesLike,
axis: AxesLike = None,
axis: Optional[AxesLike] = None,
padding_values: Union[AxesParams, Callable] = 0,
ratio: AxesParams = 0.5,
num_threads: int = _NUMERIC_DEFAULT_NUM_THREADS,
Expand Down Expand Up @@ -135,7 +135,7 @@ def pad_to_shape(
def pad_to_divisible(
x: np.ndarray,
divisor: AxesLike,
axis: AxesLike = None,
axis: Optional[AxesLike] = None,
padding_values: Union[AxesParams, Callable] = 0,
ratio: AxesParams = 0.5,
remainder: AxesLike = 0,
Expand Down
8 changes: 4 additions & 4 deletions imops/radon.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Sequence, Tuple, Union
from typing import Optional, Sequence, Tuple, Union

import numpy as np
from scipy.fftpack import fft, ifft
Expand All @@ -15,7 +15,7 @@

def radon(
image: np.ndarray,
axes: Tuple[int, int] = None,
axes: Optional[Tuple[int, int]] = None,
theta: Union[int, Sequence[float]] = 180,
return_fill: bool = False,
num_threads: int = -1,
Expand Down Expand Up @@ -104,8 +104,8 @@ def radon(

def inverse_radon(
sinogram: np.ndarray,
axes: Tuple[int, int] = None,
theta: Union[int, Sequence[float]] = None,
axes: Optional[Tuple[int, int]] = None,
theta: Union[int, Sequence[float], None] = None,
fill_value: float = 0,
a: float = 0,
b: float = 1,
Expand Down
4 changes: 3 additions & 1 deletion imops/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def wrapper(
return wrapper


def build_slices(start: Sequence[int], stop: Sequence[int] = None, step: Sequence[int] = None) -> Tuple[slice, ...]:
def build_slices(
start: Sequence[int], stop: Optional[Sequence[int]] = None, step: Optional[Sequence[int]] = None
) -> Tuple[slice, ...]:
"""
Returns a tuple of slices built from `start` and `stop` with `step`.
Expand Down
8 changes: 4 additions & 4 deletions imops/zoom.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from platform import python_version
from typing import Callable, Sequence, Union
from typing import Callable, Optional, Sequence, Union
from warnings import warn

import numpy as np
Expand Down Expand Up @@ -72,7 +72,7 @@ def _choose_numba_zoom(ndim: int, order: int) -> Callable:
def zoom(
x: np.ndarray,
scale_factor: AxesParams,
axis: AxesLike = None,
axis: Optional[AxesLike] = None,
order: int = 1,
fill_value: Union[float, Callable] = 0,
num_threads: int = -1,
Expand Down Expand Up @@ -129,7 +129,7 @@ def zoom(
def zoom_to_shape(
x: np.ndarray,
shape: AxesLike,
axis: AxesLike = None,
axis: Optional[AxesLike] = None,
order: int = 1,
fill_value: Union[float, Callable] = 0,
num_threads: int = -1,
Expand Down Expand Up @@ -191,7 +191,7 @@ def zoom_to_shape(
def _zoom(
image: np.ndarray,
zoom: Sequence[float],
output: np.ndarray = None,
output: Optional[np.ndarray] = None,
order: int = 1,
mode: str = 'constant',
cval: float = 0.0,
Expand Down

0 comments on commit 9eee515

Please sign in to comment.