mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-08 11:42:39 +08:00
process -> apply, more docs
This commit is contained in:
@@ -2,7 +2,7 @@ from .dtype import (img_as_float, img_as_int, img_as_uint, img_as_ubyte,
|
||||
img_as_bool, dtype_limits)
|
||||
from .shape import view_as_blocks, view_as_windows
|
||||
from .noise import random_noise
|
||||
from .process import process_chunks
|
||||
from .apply import apply_chunks
|
||||
|
||||
from .arraypad import pad, crop
|
||||
from ._regular_grid import regular_grid
|
||||
@@ -21,5 +21,5 @@ __all__ = ['img_as_float',
|
||||
'crop',
|
||||
'random_noise',
|
||||
'regular_grid',
|
||||
'process_chunks',
|
||||
'apply_chunks',
|
||||
'unique_rows']
|
||||
|
||||
@@ -3,7 +3,7 @@ from multiprocessing import cpu_count
|
||||
|
||||
import dask.array as da
|
||||
|
||||
__all__ = ['process_chunks']
|
||||
__all__ = ['apply_chunks']
|
||||
|
||||
|
||||
def _get_chunks(shape, ncpu):
|
||||
@@ -44,8 +44,8 @@ def _get_chunks(shape, ncpu):
|
||||
return tuple(chunks)
|
||||
|
||||
|
||||
def process_chunks(function, array, chunks=None, depth=0,
|
||||
mode=None, extra_arguments=(), extra_keywords={}):
|
||||
def apply_chunks(function, array, chunks=None, depth=0, mode=None,
|
||||
extra_arguments=(), extra_keywords={}):
|
||||
"""Map a function in parallel across an array.
|
||||
|
||||
Split an array into possibly overlapping chunks of a given depth and
|
||||
@@ -57,26 +57,26 @@ def process_chunks(function, array, chunks=None, depth=0,
|
||||
function : function
|
||||
Function to be mapped which takes an array as an argument.
|
||||
array : numpy array
|
||||
array which the function will be applied to.
|
||||
chunks : int, tuple, or tuple of tuples
|
||||
One tuple of length array.ndim or a list of tuples of length ndim.
|
||||
Where each subtuple adds to the size of the array in the corresponding
|
||||
dimension. If None, the array is broken up into chunks based on the
|
||||
number of available cpus.
|
||||
depth : int
|
||||
integer equal to the depth of the internal external padding
|
||||
mode : 'reflect', 'periodic', 'wrap', 'nearest'
|
||||
Array which the function will be applied to.
|
||||
chunks : int, tuple, or tuple of tuples, optional
|
||||
A single integer is interpreted as the length of one side of a square
|
||||
chunk that should be tiled across the array. One tuple of length
|
||||
``array.ndim`` represents the shape of a chunk, and it is tiled across
|
||||
the array. A list of tuples of length ``ndim``, where each sub-tuple
|
||||
is a sequence of chunk sizes along the corresponding dimension. If
|
||||
None, the array is broken up into chunks based on the number of
|
||||
available cpus. More information about chunks is in the documentation
|
||||
`here <https://dask.pydata.org/en/latest/array-design.html>`_.
|
||||
depth : int, optional
|
||||
Integer equal to the depth of the added boundary cells. Defaults to
|
||||
zero.
|
||||
mode : 'reflect', 'periodic', 'wrap', 'nearest', optional
|
||||
type of external boundary padding
|
||||
extra_arguments : tuple
|
||||
extra_arguments : tuple, optional
|
||||
Tuple of arguments to be passed to the function.
|
||||
extra_keywords : dictionary
|
||||
extra_keywords : dictionary, optional
|
||||
Dictionary of keyword arguments to be passed to the function.
|
||||
|
||||
Notes
|
||||
-----
|
||||
Be careful choosing the depth so that it is never larger than the length of
|
||||
a chunk.
|
||||
|
||||
"""
|
||||
if chunks is None:
|
||||
shape = array.shape
|
||||
@@ -2,21 +2,18 @@ import numpy as np
|
||||
from numpy.testing import assert_array_almost_equal
|
||||
|
||||
from skimage.filters import threshold_adaptive, gaussian_filter
|
||||
from skimage.util import process_chunks
|
||||
from skimage.util import apply_chunks
|
||||
|
||||
|
||||
def test_process_chunks():
|
||||
def test_apply_chunks():
|
||||
# data
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
|
||||
# wrapp the function we're applying
|
||||
def wrapped_thresh(arr):
|
||||
return threshold_adaptive(arr, 3, mode='reflect')
|
||||
|
||||
# apply the filter
|
||||
expected1 = threshold_adaptive(a, 3)
|
||||
result1 = process_chunks(wrapped_thresh, a, chunks=(6, 6),
|
||||
depth=5)
|
||||
result1 = apply_chunks(threshold_adaptive, a, chunks=(6, 6), depth=5,
|
||||
extra_arguments=(3,),
|
||||
extra_keywords={'mode': 'reflect'})
|
||||
|
||||
assert_array_almost_equal(result1, expected1)
|
||||
|
||||
@@ -24,8 +21,7 @@ def test_process_chunks():
|
||||
return gaussian_filter(arr, 1, mode='reflect')
|
||||
|
||||
expected2 = gaussian_filter(a, 1, mode='reflect')
|
||||
result2 = process_chunks(wrapped_gauss, a, chunks=(6, 6),
|
||||
depth=5)
|
||||
result2 = apply_chunks(wrapped_gauss, a, chunks=(6, 6), depth=5)
|
||||
|
||||
assert_array_almost_equal(result2, expected2)
|
||||
|
||||
@@ -37,28 +33,27 @@ def test_no_chunks():
|
||||
return arr + 42
|
||||
|
||||
expected = add_42(a)
|
||||
result = process_chunks(add_42, a)
|
||||
result = apply_chunks(add_42, a)
|
||||
|
||||
assert_array_almost_equal(result, expected)
|
||||
|
||||
|
||||
def test_process_chunks_wrap():
|
||||
def test_apply_chunks_wrap():
|
||||
def wrapped(arr):
|
||||
return gaussian_filter(arr, 1, mode='wrap')
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
expected = gaussian_filter(a, 1, mode='wrap')
|
||||
result = process_chunks(wrapped, a, chunks=(6, 6),
|
||||
depth=5, mode='wrap')
|
||||
result = apply_chunks(wrapped, a, chunks=(6, 6), depth=5, mode='wrap')
|
||||
|
||||
assert_array_almost_equal(result, expected)
|
||||
|
||||
|
||||
def test_process_chunks_nearest():
|
||||
def test_apply_chunks_nearest():
|
||||
def wrapped(arr):
|
||||
return gaussian_filter(arr, 1, mode='nearest')
|
||||
a = np.arange(144).reshape(12, 12).astype(float)
|
||||
expected = gaussian_filter(a, 1, mode='nearest')
|
||||
result = process_chunks(wrapped, a, chunks=(6, 6),
|
||||
depth={0: 5, 1: 5}, mode='nearest')
|
||||
result = apply_chunks(wrapped, a, chunks=(6, 6), depth={0: 5, 1: 5},
|
||||
mode='nearest')
|
||||
|
||||
assert_array_almost_equal(result, expected)
|
||||
Reference in New Issue
Block a user