nifty_utilities.py 6.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Theo Steininger's avatar
Theo Steininger committed
13
14
15
16
17
#
# Copyright(C) 2013-2017 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.
Ultima's avatar
Ultima committed
18

Martin Reinecke's avatar
Martin Reinecke committed
19
from builtins import next, range
Ultima's avatar
Ultima committed
20
import numpy as np
21
from itertools import product
Martin Reinecke's avatar
Martin Reinecke committed
22
from functools import reduce
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
23
from .domain_object import DomainObject
24

Martin Reinecke's avatar
Martin Reinecke committed
25

26
27
def get_slice_list(shape, axes):
    """
theos's avatar
theos committed
28
29
    Helper function which generates slice list(s) to traverse over all
    combinations of axes, other than the selected axes.
Jait Dixit's avatar
Jait Dixit committed
30
31
32
33

    Parameters
    ----------
    shape: tuple
theos's avatar
theos committed
34
        Shape of the data array to traverse over.
Jait Dixit's avatar
Jait Dixit committed
35
    axes: tuple
theos's avatar
theos committed
36
        Axes which should not be iterated over.
Jait Dixit's avatar
Jait Dixit committed
37
38
39
40
41
42
43
44
45
46
47
48

    Yields
    -------
    list
        The next list of indices and/or slice objects for each dimension.

    Raises
    ------
    ValueError
        If shape is empty.
    ValueError
        If axes(axis) does not match shape.
49
    """
theos's avatar
theos committed
50

51
    if not shape:
52
        raise ValueError("shape cannot be None.")
53

54
55
    if axes:
        if not all(axis < len(shape) for axis in axes):
56
            raise ValueError("axes(axis) does not match shape.")
57
        axes_select = [0 if x in axes else 1 for x, y in enumerate(shape)]
Jait Dixit's avatar
Jait Dixit committed
58
        axes_iterables = \
Martin Reinecke's avatar
Martin Reinecke committed
59
            [list(range(y)) for x, y in enumerate(shape) if x not in axes]
60
61
62
63
64
        for index in product(*axes_iterables):
            it_iter = iter(index)
            slice_list = [
                next(it_iter)
                if axis else slice(None, None) for axis in axes_select
Jait Dixit's avatar
Jait Dixit committed
65
                ]
66
67
68
            yield slice_list
    else:
        yield [slice(None, None)]
Ultima's avatar
Ultima committed
69

Ultima's avatar
Ultima committed
70

71
def cast_axis_to_tuple(axis, length=None):
72
73
74
75
76
77
    if axis is None:
        return None
    try:
        axis = tuple(int(item) for item in axis)
    except(TypeError):
        if np.isscalar(axis):
Jait Dixit's avatar
Jait Dixit committed
78
            axis = (int(axis),)
79
        else:
80
            raise TypeError("Could not convert axis-input to tuple of ints")
81

82
83
84
    if length is not None:
        # shift negative indices to positive ones
        axis = tuple(item if (item >= 0) else (item + length) for item in axis)
85

86
87
88
        # Deactivated this, in order to allow for the ComposedOperator
        # remove duplicate entries
        # axis = tuple(set(axis))
89

90
91
92
        # assert that all entries are elements in [0, length]
        for elem in axis:
            assert (0 <= elem < length)
93

94
95
96
    return axis


97
98
def parse_domain(domain):
    if domain is None:
Martin Reinecke's avatar
Martin Reinecke committed
99
100
101
        return ()
    if isinstance(domain, DomainObject):
        return (domain,)
102

Martin Reinecke's avatar
Martin Reinecke committed
103
104
    if not isinstance(domain, tuple):
        domain = tuple(domain)
105
    for d in domain:
106
        if not isinstance(d, DomainObject):
107
            raise TypeError(
Martin Reinecke's avatar
Martin Reinecke committed
108
                "Given object contains something that is not an "
109
                "instance of DomainObject-class.")
110
    return domain
Martin Reinecke's avatar
Martin Reinecke committed
111

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
112

Martin Reinecke's avatar
Martin Reinecke committed
113
114
115
116
117
118
119
def domains2shape(domain):
    domain = parse_domain(domain)
    shape_tuple = tuple(sp.shape for sp in domain)
    if len(shape_tuple) == 0:
        return ()
    else:
        return reduce(lambda x, y: x + y, shape_tuple)
Martin Reinecke's avatar
Martin Reinecke committed
120

Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
121

Martin Reinecke's avatar
Martin Reinecke committed
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def bincount_axis(obj, minlength=None, weights=None, axis=None):
    if minlength is not None:
        length = max(np.amax(obj) + 1, minlength)
    else:
        length = np.amax(obj) + 1

    if obj.shape == ():
        raise ValueError("object of too small depth for desired array")
    data = obj

    # if present, parse the axis keyword and transpose/reorder self.data
    # such that all affected axes follow each other. Only if they are in a
    # sequence flattening will be possible
    if axis is not None:
        # do the reordering
        ndim = len(obj.shape)
        axis = sorted(cast_axis_to_tuple(axis, length=ndim))
        reordering = [x for x in range(ndim) if x not in axis]
        reordering += axis

        data = np.transpose(data, reordering)
        if weights is not None:
            weights = np.transpose(weights, reordering)

        reord_axis = list(range(ndim-len(axis), ndim))

        # semi-flatten the dimensions in `axis`, i.e. after reordering
        # the last ones.
        semi_flat_dim = reduce(lambda x, y: x*y,
                               data.shape[ndim-len(reord_axis):])
        flat_shape = data.shape[:ndim-len(reord_axis)] + (semi_flat_dim, )
    else:
        flat_shape = (reduce(lambda x, y: x*y, data.shape), )

    data = np.ascontiguousarray(data.reshape(flat_shape))
    if weights is not None:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
158
        weights = np.ascontiguousarray(weights.reshape(flat_shape))
Martin Reinecke's avatar
Martin Reinecke committed
159
160
161

    # compute the local bincount results
    # -> prepare the local result array
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
162
    result_dtype = np.int if weights is None else np.float
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
163
    local_counts = np.empty(flat_shape[:-1] + (length, ), dtype=result_dtype)
Martin Reinecke's avatar
Martin Reinecke committed
164
165
    # iterate over all entries in the surviving axes and compute the local
    # bincounts
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
166
    for slice_list in get_slice_list(flat_shape, axes=(len(flat_shape)-1,)):
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
167
        current_weights = None if weights is None else weights[slice_list]
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
168
169
170
        local_counts[slice_list] = np.bincount(data[slice_list],
                                               weights=current_weights,
                                               minlength=length)
Martin Reinecke's avatar
Martin Reinecke committed
171
172
173
174
175
176
177
178
179
180
181
182
183

    # restore the original ordering
    # place the bincount stuff at the location of the first `axis` entry
    if axis is not None:
        # axis has been sorted above
        insert_position = axis[0]
        new_ndim = len(local_counts.shape)
        return_order = (list(range(0, insert_position)) +
                        [new_ndim-1, ] +
                        list(range(insert_position, new_ndim-1)))
        local_counts = np.ascontiguousarray(
                            local_counts.transpose(return_order))
    return local_counts