8000 Add a channel_axis argument to functions in the skimage.color module by grlee77 · Pull Request #5462 · scikit-image/scikit-image · GitHub
[go: up one dir, main page]

Skip to content

Add a channel_axis argument to functions in the skimage.color module #5462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
51b9dcb
TST: channel_axis tests for rgba2rgb
grlee77 Jul 2, 2021
4e73211
Add channel_axis kwarg to rgb2hsv and hsv2rgb
grlee77 Jul 2, 2021
9996eb1
use existing channel_as_last_axis decorator for hsv2rgb and rgb2hsv
grlee77 Jul 2, 2021
954371e
add channel_axis to rgb2xyz, xyz2rgb
grlee77 Jul 2, 2021
9f76f7c
add channel_axis to rgb2luv, luv2rgb, xyz2luv, luv2xyz
grlee77 Jul 2, 2021
ec4ea97
add channel_axis to combine_stains, separate_stains, rgb2hed, hed2rgb
grlee77 Jul 2, 2021
31ea1ed
add channel_axis support to remaining functions in colorconv.py
grlee77 Jul 2, 2021
54ff440
support user-specified channel_axis for label2rgb
grlee77 Jul 2, 2021
37fd7ff
support channel_axis for deltaE_*
grlee77 Jul 2, 2021
3d82e2c
add channel_axis option to adapt_rgb decorator, each_channel and hsv_…
grlee77 Jul 2, 2021
8063049
support use of channel_axis in functions wrapped by adapt_rgb
grlee77 Jul 2, 2021
a3d456e
DOC: update docstrings to describe channel_axis
grlee77 Jul 2, 2021
bb21a4f
BUG: fix label2rgb when an RGB image is provided and channel_axis != -1
grlee77 Jul 2, 2021
f0184fd
pep8 fixes
grlee77 Jul 8, 2021
c72a767
pep8 fixes
grlee77 Jul 8, 2021
c3c9cc9
Merge remote-tracking branch 'upstream/main' into color-channel_axis
grlee77 Jul 13, 2021
501d929
Revert "support use of channel_axis in functions wrapped by adapt_rgb"
grlee77 Jul 17, 2021
287150e
Revert "add channel_axis option to adapt_rgb decorator, each_channel …
grlee77 Jul 17, 2021
a40d108
update shape description in labl2rgb docstring
grlee77 Jul 17, 2021
7f0f3a2
Merge remote-tracking branch 'upstream/main' into color-channel_axis
grlee77 Jul 19, 2021
0155d1b
Apply suggestions from code review
grlee77 Jul 26, 2021
444284d
switch from ValueError to np.AxisError for invalid axis in rgba2rgb
grlee77 Jul 26, 2021
e16510f
raise TypeError on non-integer axis
grlee77 Jul 26, 2021
5abb68e
TST: test additional negative channel_axis values
grlee77 Aug 5, 2021
03a8650
STYLE: rename _reshape_nd to reshape_nd
grlee77 Aug 5, 2021
8b504a7
move identity helper to skimage._shared.utils
grlee77 Aug 5, 2021
bfe1cd1
Merge remote-tracking branch 'upstream/main' into color-channel_axis
grlee77 Aug 9, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions skimage/_shared/utils.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import inspect
import functools
import numbers
import sys
import warnings
from collections.abc import Iterable

import numpy as np
from numpy.lib import NumpyVersion
import scipy
from numpy.lib import NumpyVersion

from ..util import img_as_float
from ._warnings import all_warnings, warn

__all__ = ['deprecated', 'get_bound_method_class', 'all_warnings',
'safe_as_int', 'check_nD', 'check_shape_equality', 'warn']
'safe_as_int', 'check_shape_equality', 'check_nD', 'warn',
'reshape_nd', 'identity']


class skimage_deprecation(Warning):
Expand Down Expand Up @@ -211,7 +211,6 @@ def fixed_func(*args, **kwargs):
convert = {True: -1, False: None}
kwargs['channel_axis'] = convert[kwargs.pop('multichannel')]


# Call the function with the fixed arguments
return func(*args, **kwargs)
return fixed_func
Expand Down Expand Up @@ -457,6 +456,41 @@ def slice_at_axis(sl, axis):
return (slice(None),) * axis + (sl,) + (...,)


def reshape_nd(arr, ndim, dim):
"""Reshape a 1D array to have n dimensions, all singletons but one.

Parameters
----------
arr : array, shape (N,)
Input array
ndim : int
Number of desired dimensions of reshaped array.
dim : int
Which dimension/axis will not be singleton-sized.

Returns
-------
arr_reshaped : array, shape ([1, ...], N, [1,...])
View of `arr` reshaped to the desired shape.

Examples
--------
>>> rng = np.random.default_rng()
>>> arr = rng.random(7)
>>> _reshape_nd(arr, 2, 0).shape
(7, 1)
>>> _reshape_nd(arr, 3, 1).shape
(1, 7, 1)
>>> _reshape_nd(arr, 4, -1).shape
(1, 1, 1, 7)
"""
if arr.ndim != 1:
raise ValueError("arr must be a 1D array")
new_shape = [1] * ndim
new_shape[dim] = -1
return np.reshape(arr, new_shape)


def check_nD(array, ndim, arg_name='image'):
"""
Verify an array meets the desired ndims and array isn't empty.
Expand All @@ -478,8 +512,10 @@ def check_nD(array, ndim, arg_name='image'):
ndim = [ndim]
if array.size == 0:
raise ValueError(msg_empty_array % (arg_name))
if not array.ndim in ndim:
raise ValueError(msg_incorrect_dim % (arg_name, '-or-'.join([str(n) for n in ndim])))
if array.ndim not in ndim:
raise ValueError(
msg_incorrect_dim % (arg_name, '-or-'.join([str(n) for n in ndim]))
)


def convert_to_float(image, preserve_range):
Expand Down Expand Up @@ -627,3 +663,8 @@ def _supported_float_type(input_dtype, allow_complex=False):
if not allow_complex and input_dtype.kind == 'c':
raise ValueError("complex valued input is not supported")
return new_float_type.get(input_dtype.char, np.float64)


def identity(image, *args, **kwargs):
"""Returns the first argument unmodified."""
return image
4 changes: 2 additions & 2 deletions skimage/color/adapt_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
__all__ = ['adapt_rgb', 'hsv_value', 'each_channel']


def is_rgb_like(image):
def is_rgb_like(image, channel_axis=-1):
"""Return True if the image *looks* like it's RGB.

This function should not be public because it is only intended to be used
for functions that don't accept volumes as input, since checking an image's
shape is fragile.
"""
return (image.ndim == 3) and (image.shape[2] in (3, 4))
return (image.ndim == 3) and (image.shape[channel_axis] in (3, 4))


def adapt_rgb(apply_to_rgb):
Expand Down
Loading
0