-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added decorator to show way to process data (#31)
- Loading branch information
Showing
7 changed files
with
219 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
__version__ = "0.0.16" | ||
__version__ = "0.0.17" | ||
|
||
from .decorators import * | ||
from .functions import * | ||
from .utils import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
from functools import wraps | ||
from typing import Callable | ||
|
||
import numpy as np | ||
|
||
from albucore.utils import MONO_CHANNEL_DIMENSIONS, NUM_MULTI_CHANNEL_DIMENSIONS, P | ||
|
||
if sys.version_info >= (3, 10): | ||
from typing import Concatenate | ||
else: | ||
from typing_extensions import Concatenate | ||
|
||
|
||
def contiguous( | ||
func: Callable[Concatenate[np.ndarray, P], np.ndarray], | ||
) -> Callable[Concatenate[np.ndarray, P], np.ndarray]: | ||
"""Ensure that input img is contiguous and the output array is also contiguous.""" | ||
|
||
@wraps(func) | ||
def wrapped_function(img: np.ndarray, *args: P.args, **kwargs: P.kwargs) -> np.ndarray: | ||
# Ensure the input array is contiguous | ||
img = np.require(img, requirements=["C_CONTIGUOUS"]) | ||
# Call the original function with the contiguous input | ||
result = func(img, *args, **kwargs) | ||
# Ensure the output array is contiguous | ||
if not result.flags["C_CONTIGUOUS"]: | ||
return np.require(result, requirements=["C_CONTIGUOUS"]) | ||
|
||
return result | ||
|
||
return wrapped_function | ||
|
||
|
||
def preserve_channel_dim( | ||
func: Callable[Concatenate[np.ndarray, P], np.ndarray], | ||
) -> Callable[Concatenate[np.ndarray, P], np.ndarray]: | ||
"""Preserve dummy channel dim.""" | ||
|
||
@wraps(func) | ||
def wrapped_function(img: np.ndarray, *args: P.args, **kwargs: P.kwargs) -> np.ndarray: | ||
shape = img.shape | ||
result = func(img, *args, **kwargs) | ||
if len(shape) == NUM_MULTI_CHANNEL_DIMENSIONS and shape[-1] == 1 and result.ndim == MONO_CHANNEL_DIMENSIONS: | ||
return np.expand_dims(result, axis=-1) | ||
|
||
if len(shape) == MONO_CHANNEL_DIMENSIONS and result.ndim == NUM_MULTI_CHANNEL_DIMENSIONS: | ||
return result[:, :, 0] | ||
return result | ||
|
||
return wrapped_function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters