nifty_utilities.py 2.28 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
22

Martin Reinecke's avatar
Martin Reinecke committed
23

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

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

    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.
47
    """
theos's avatar
theos committed
48

Martin Reinecke's avatar
Martin Reinecke committed
49
    if shape is None:
50
        raise ValueError("shape cannot be None.")
51

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

Ultima's avatar
Ultima committed
68

Martin Reinecke's avatar
Martin Reinecke committed
69
70
def cast_iseq_to_tuple(seq):
    if seq is None:
71
        return None
Martin Reinecke's avatar
Martin Reinecke committed
72
73
74
    if np.isscalar(seq):
        return (int(seq),)
    return tuple(int(item) for item in seq)