autodmri.blocks

 1import numpy as np
 2import numbers
 3
 4from numpy.lib.stride_tricks import as_strided
 5
 6# New BSD License
 7
 8# Copyright (c) 2007-2019 The scikit-learn developers.
 9# All rights reserved.
10
11
12# Redistribution and use in source and binary forms, with or without
13# modification, are permitted provided that the following conditions are met:
14
15#   a. Redistributions of source code must retain the above copyright notice,
16#      this list of conditions and the following disclaimer.
17#   b. Redistributions in binary form must reproduce the above copyright
18#      notice, this list of conditions and the following disclaimer in the
19#      documentation and/or other materials provided with the distribution.
20#   c. Neither the name of the Scikit-learn Developers  nor the names of
21#      its contributors may be used to endorse or promote products
22#      derived from this software without specific prior written
23#      permission.
24
25
26# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29# ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
30# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
36# DAMAGE.
37
38
39# Stolen from https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/feature_extraction/image.py
40def extract_patches(arr, patch_shape, extraction_step, flatten=True):
41    """Extracts patches of any n-dimensional array in place using strides.
42    Given an n-dimensional array it will return a 2n-dimensional array with
43    the first n dimensions indexing patch position and the last n indexing
44    the patch content. This operation is immediate (O(1)). A reshape
45    performed on the first n dimensions will cause numpy to copy data, leading
46    to a list of extracted patches.
47
48    Parameters
49    ----------
50    arr : ndarray
51        n-dimensional array of which patches are to be extracted
52    patch_shape : integer or tuple of length arr.ndim
53        Indicates the shape of the patches to be extracted. If an
54        integer is given, the shape will be a hypercube of
55        sidelength given by its value.
56    extraction_step : integer or tuple of length arr.ndim
57        Indicates step size at which extraction shall be performed.
58        If integer is given, then the step is uniform in all dimensions.
59    Returns
60    -------
61    patches : strided ndarray
62        2n-dimensional array indexing patches on first n dimensions and
63        containing patches on the last n dimensions. These dimensions
64        are fake, but this way no data is copied. A simple reshape invokes
65        a copying operation to obtain a list of patches:
66        result.reshape([-1] + list(patch_shape))
67    """
68
69    arr_ndim = arr.ndim
70
71    if isinstance(patch_shape, numbers.Number):
72        patch_shape = tuple([patch_shape] * arr_ndim)
73    if isinstance(extraction_step, numbers.Number):
74        extraction_step = tuple([extraction_step] * arr_ndim)
75
76    patch_strides = arr.strides
77
78    slices = tuple(slice(None, None, st) for st in extraction_step)
79    indexing_strides = arr[slices].strides
80
81    patch_indices_shape = ((np.array(arr.shape) - np.array(patch_shape)) //
82                           np.array(extraction_step)) + 1
83
84    shape = tuple(list(patch_indices_shape) + list(patch_shape))
85    strides = tuple(list(indexing_strides) + list(patch_strides))
86
87    patches = as_strided(arr, shape=shape, strides=strides)
88
89    if flatten:
90        patches = patches.reshape([-1] + list(patch_shape))
91
92    return patches
def extract_patches(arr, patch_shape, extraction_step, flatten=True):
41def extract_patches(arr, patch_shape, extraction_step, flatten=True):
42    """Extracts patches of any n-dimensional array in place using strides.
43    Given an n-dimensional array it will return a 2n-dimensional array with
44    the first n dimensions indexing patch position and the last n indexing
45    the patch content. This operation is immediate (O(1)). A reshape
46    performed on the first n dimensions will cause numpy to copy data, leading
47    to a list of extracted patches.
48
49    Parameters
50    ----------
51    arr : ndarray
52        n-dimensional array of which patches are to be extracted
53    patch_shape : integer or tuple of length arr.ndim
54        Indicates the shape of the patches to be extracted. If an
55        integer is given, the shape will be a hypercube of
56        sidelength given by its value.
57    extraction_step : integer or tuple of length arr.ndim
58        Indicates step size at which extraction shall be performed.
59        If integer is given, then the step is uniform in all dimensions.
60    Returns
61    -------
62    patches : strided ndarray
63        2n-dimensional array indexing patches on first n dimensions and
64        containing patches on the last n dimensions. These dimensions
65        are fake, but this way no data is copied. A simple reshape invokes
66        a copying operation to obtain a list of patches:
67        result.reshape([-1] + list(patch_shape))
68    """
69
70    arr_ndim = arr.ndim
71
72    if isinstance(patch_shape, numbers.Number):
73        patch_shape = tuple([patch_shape] * arr_ndim)
74    if isinstance(extraction_step, numbers.Number):
75        extraction_step = tuple([extraction_step] * arr_ndim)
76
77    patch_strides = arr.strides
78
79    slices = tuple(slice(None, None, st) for st in extraction_step)
80    indexing_strides = arr[slices].strides
81
82    patch_indices_shape = ((np.array(arr.shape) - np.array(patch_shape)) //
83                           np.array(extraction_step)) + 1
84
85    shape = tuple(list(patch_indices_shape) + list(patch_shape))
86    strides = tuple(list(indexing_strides) + list(patch_strides))
87
88    patches = as_strided(arr, shape=shape, strides=strides)
89
90    if flatten:
91        patches = patches.reshape([-1] + list(patch_shape))
92
93    return patches

Extracts patches of any n-dimensional array in place using strides. Given an n-dimensional array it will return a 2n-dimensional array with the first n dimensions indexing patch position and the last n indexing the patch content. This operation is immediate (O(1)). A reshape performed on the first n dimensions will cause numpy to copy data, leading to a list of extracted patches.

Parameters

arr : ndarray n-dimensional array of which patches are to be extracted patch_shape : integer or tuple of length arr.ndim Indicates the shape of the patches to be extracted. If an integer is given, the shape will be a hypercube of sidelength given by its value. extraction_step : integer or tuple of length arr.ndim Indicates step size at which extraction shall be performed. If integer is given, then the step is uniform in all dimensions.

Returns

patches : strided ndarray 2n-dimensional array indexing patches on first n dimensions and containing patches on the last n dimensions. These dimensions are fake, but this way no data is copied. A simple reshape invokes a copying operation to obtain a list of patches: result.reshape([-1] + list(patch_shape))