• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

espdev / csaps / 162

pending completion
162

push

travis-ci

web-flow
Fix call numpy.pad function for numpy<=1.16 (#16)

* (#15) fix call numpy.pad function for numpy==1.16
* bump version to 0.10.1

414 of 414 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/csaps/_reshape.py
1
# -*- coding: utf-8 -*-
2

3
import typing as ty
1✔
4
import numpy as np
1✔
5

6

7
def to_2d(arr: np.ndarray, axis: int) -> np.ndarray:
1✔
8
    """Transforms the shape of N-D array to 2-D NxM array
9

10
    The function transforms N-D array to 2-D NxM array along given axis,
11
    where N is dimension and M is the nember of elements.
12

13
    The function does not create a copy.
14

15
    Parameters
16
    ----------
17
    arr : np.array
18
        N-D array
19

20
    axis : int
21
        Axis that will be used for transform array shape
22

23
    Returns
24
    -------
25
    arr2d : np.ndarray
26
        2-D NxM array view
27

28
    Raises
29
    ------
30
    ValueError : axis is out of array axes
31

32
    See Also
33
    --------
34
    from_2d
35

36
    Examples
37
    --------
38

39
    .. code-block:: python
40

41
        >>> shape = (2, 3, 4)
42
        >>> arr = np.arange(1, np.prod(shape)+1).reshape(shape)
43
        >>> arr_2d = to_2d(arr, axis=1)
44
        >>> print(arr)
45
        [[[ 1  2  3  4]
46
          [ 5  6  7  8]
47
          [ 9 10 11 12]]
48

49
         [[13 14 15 16]
50
          [17 18 19 20]
51
          [21 22 23 24]]]
52
        >>> print(arr_2d)
53
        [[ 1  5  9]
54
         [ 2  6 10]
55
         [ 3  7 11]
56
         [ 4  8 12]
57
         [13 17 21]
58
         [14 18 22]
59
         [15 19 23]
60
         [16 20 24]]
61

62
    """
63

64
    arr = np.asarray(arr)
1✔
65
    axis = arr.ndim + axis if axis < 0 else axis
1✔
66

67
    if axis >= arr.ndim:  # pragma: no cover
68
        raise ValueError(f'axis {axis} is out of array axes {arr.ndim}')
69

70
    tr_axes = list(range(arr.ndim))
1✔
71
    tr_axes.pop(axis)
1✔
72
    tr_axes.append(axis)
1✔
73

74
    new_shape = (np.prod(arr.shape) // arr.shape[axis], arr.shape[axis])
1✔
75

76
    return arr.transpose(tr_axes).reshape(new_shape)
1✔
77

78

79
def from_2d(arr: np.ndarray, shape: ty.Sequence[int], axis: int) -> np.ndarray:
1✔
80
    """Transforms 2-d NxM array to N-D array using given shape and axis
81

82
    Parameters
83
    ----------
84
    arr : np.ndarray
85
        2-D NxM array, where N is dimension and M is the nember of elements
86

87
    shape : tuple, list
88
        The shape of N-D array
89

90
    axis : int
91
        Axis that have been used for transform array shape
92

93
    Returns
94
    -------
95
    arrnd : np.ndarray
96
        N-D array
97

98
    Raises
99
    ------
100
    ValueError : axis is out of N-D array axes
101

102
    See Also
103
    --------
104
    to_2d
105

106
    Examples
107
    --------
108

109
    .. code-block:: python
110

111
        >>> shape = (2, 3, 4)
112
        >>> arr_2d = np.arange(1, np.prod(shape)+1).reshape(2*4, 3)
113
        >>> print(arr_2d)
114
        [[ 1,  2,  3],
115
         [ 4,  5,  6],
116
         [ 7,  8,  9],
117
         [10, 11, 12],
118
         [13, 14, 15],
119
         [16, 17, 18],
120
         [19, 20, 21],
121
         [22, 23, 24]]
122
        >>> arr = from_2d(arr_2d, shape=shape, axis=1)
123
        >>> print(arr)
124
        [[[ 1,  4,  7, 10],
125
          [ 2,  5,  8, 11],
126
          [ 3,  6,  9, 12]],
127

128
        [[13, 16, 19, 22],
129
         [14, 17, 20, 23],
130
         [15, 18, 21, 24]]]
131

132
    """
133

134
    arr = np.asarray(arr)
1✔
135
    ndim = len(shape)
1✔
136
    axis = ndim + axis if axis < 0 else axis
1✔
137

138
    if axis >= ndim:  # pragma: no cover
139
        raise ValueError(f'axis {axis} is out of N-D array axes {ndim}')
140

141
    new_shape = list(shape)
1✔
142
    new_shape.pop(axis)
1✔
143
    new_shape.append(shape[axis])
1✔
144

145
    tr_axes = list(range(ndim))
1✔
146
    tr_axes.insert(axis, tr_axes.pop(-1))
1✔
147

148
    return arr.reshape(new_shape).transpose(tr_axes)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc