nifty_utilities.py 3.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# NIFTy
# Copyright (C) 2017  Theo Steininger
#
# Author: Theo Steininger
#
# 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/>.
Ultima's avatar
Ultima committed
18
19

import numpy as np
20
21
22
23
from itertools import product

def get_slice_list(shape, axes):
    """
theos's avatar
theos committed
24
25
    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
26
27
28
29

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

    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.
45
    """
theos's avatar
theos committed
46

47
    if not shape:
48
        raise ValueError("shape cannot be None.")
49

50
51
    if axes:
        if not all(axis < len(shape) for axis in axes):
52
            raise ValueError("axes(axis) does not match shape.")
53
        axes_select = [0 if x in axes else 1 for x, y in enumerate(shape)]
Jait Dixit's avatar
Jait Dixit committed
54
        axes_iterables = \
55
56
57
58
59
60
            [range(y) for x, y in enumerate(shape) if x not in axes]
        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
61
                ]
62
63
64
65
            yield slice_list
    else:
        yield [slice(None, None)]
        return
Ultima's avatar
Ultima committed
66

Ultima's avatar
Ultima committed
67

68
69
70
71
72
73
74
75

def cast_axis_to_tuple(axis, length):
    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
76
            axis = (int(axis),)
77
78
        else:
            raise TypeError(
79
                "Could not convert axis-input to tuple of ints")
80
81
82

    # shift negative indices to positive ones
    axis = tuple(item if (item >= 0) else (item + length) for item in axis)
83

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

    # assert that all entries are elements in [0, length]
    for elem in axis:
Jait Dixit's avatar
Jait Dixit committed
90
        assert (0 <= elem < length)
91

92
93
94
    return axis


95
def parse_domain(domain):
96
    from nifty.domain_object import DomainObject
97
98
    if domain is None:
        domain = ()
99
    elif isinstance(domain, DomainObject):
100
101
102
103
104
        domain = (domain,)
    elif not isinstance(domain, tuple):
        domain = tuple(domain)

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