~ubuntu-branches/ubuntu/trusty/python-numpy/trusty-updates

« back to all changes in this revision

Viewing changes to numpy/core/tests/test_shape_base.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-28 19:03:02 UTC
  • mfrom: (7.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20130128190302-1kyak9j26902djjg
Tags: 1:1.7.0~rc1-1ubuntu1
Merge with Debian; remaining changes:

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import warnings
1
2
from numpy.testing import *
2
3
from numpy.core import array, atleast_1d, atleast_2d, atleast_3d, vstack, \
3
 
        hstack, newaxis
 
4
        hstack, newaxis, concatenate, arange
4
5
 
5
6
class TestAtleast1d(TestCase):
6
7
    def test_0D_array(self):
37
38
        assert_(atleast_1d(3.0).shape == (1,))
38
39
        assert_(atleast_1d([[2,3],[4,5]]).shape == (2,2))
39
40
 
 
41
 
40
42
class TestAtleast2d(TestCase):
41
43
    def test_0D_array(self):
42
44
        a = array(1); b = array(2);
97
99
        desired = [a,b]
98
100
        assert_array_equal(res,desired)
99
101
 
 
102
 
100
103
class TestHstack(TestCase):
101
104
    def test_0D_array(self):
102
105
        a = array(1); b = array(2);
116
119
        desired = array([[1,1],[2,2]])
117
120
        assert_array_equal(res,desired)
118
121
 
 
122
 
119
123
class TestVstack(TestCase):
120
124
    def test_0D_array(self):
121
125
        a = array(1); b = array(2);
141
145
        desired = array([[1,2],[1,2]])
142
146
        assert_array_equal(res,desired)
143
147
 
 
148
def test_concatenate():
 
149
    # Test concatenate function
 
150
    # No arrays raise ValueError
 
151
    assert_raises(ValueError, concatenate, ())
 
152
    # Scalars cannot be concatenated
 
153
    assert_raises(ValueError, concatenate, (0,))
 
154
    assert_raises(ValueError, concatenate, (array(0),))
 
155
    # One sequence returns unmodified (but as array)
 
156
    r4 = list(range(4))
 
157
    assert_array_equal(concatenate((r4,)), r4)
 
158
    # Any sequence
 
159
    assert_array_equal(concatenate((tuple(r4),)), r4)
 
160
    assert_array_equal(concatenate((array(r4),)), r4)
 
161
    # 1D default concatenation
 
162
    r3 = list(range(3))
 
163
    assert_array_equal(concatenate((r4, r3)), r4 + r3)
 
164
    # Mixed sequence types
 
165
    assert_array_equal(concatenate((tuple(r4), r3)), r4 + r3)
 
166
    assert_array_equal(concatenate((array(r4), r3)), r4 + r3)
 
167
    # Explicit axis specification
 
168
    assert_array_equal(concatenate((r4, r3), 0), r4 + r3)
 
169
    # Including negative
 
170
    assert_array_equal(concatenate((r4, r3), -1), r4 + r3)
 
171
    # 2D
 
172
    a23 = array([[10, 11, 12], [13, 14, 15]])
 
173
    a13 = array([[0, 1, 2]])
 
174
    res = array([[10, 11, 12], [13, 14, 15], [0, 1, 2]])
 
175
    assert_array_equal(concatenate((a23, a13)), res)
 
176
    assert_array_equal(concatenate((a23, a13), 0), res)
 
177
    assert_array_equal(concatenate((a23.T, a13.T), 1), res.T)
 
178
    assert_array_equal(concatenate((a23.T, a13.T), -1), res.T)
 
179
    # Arrays much match shape
 
180
    assert_raises(ValueError, concatenate, (a23.T, a13.T), 0)
 
181
    # 3D
 
182
    res = arange(2 * 3 * 7).reshape((2, 3, 7))
 
183
    a0 = res[..., :4]
 
184
    a1 = res[..., 4:6]
 
185
    a2 = res[..., 6:]
 
186
    assert_array_equal(concatenate((a0, a1, a2), 2), res)
 
187
    assert_array_equal(concatenate((a0, a1, a2), -1), res)
 
188
    assert_array_equal(concatenate((a0.T, a1.T, a2.T), 0), res.T)
 
189
 
 
190
 
 
191
def test_concatenate_sloppy0():
 
192
    # Versions of numpy < 1.7.0 ignored axis argument value for 1D arrays.  We
 
193
    # allow this for now, but in due course we will raise an error
 
194
    r4 = list(range(4))
 
195
    r3 = list(range(3))
 
196
    assert_array_equal(concatenate((r4, r3), 0), r4 + r3)
 
197
    warnings.simplefilter('ignore', DeprecationWarning)
 
198
    try:
 
199
        assert_array_equal(concatenate((r4, r3), -10), r4 + r3)
 
200
        assert_array_equal(concatenate((r4, r3), 10), r4 + r3)
 
201
    finally:
 
202
        warnings.filters.pop(0)
 
203
    # Confirm DepractionWarning raised
 
204
    warnings.simplefilter('always', DeprecationWarning)
 
205
    warnings.simplefilter('error', DeprecationWarning)
 
206
    try:
 
207
        assert_raises(DeprecationWarning, concatenate, (r4, r3), 10)
 
208
    finally:
 
209
        warnings.filters.pop(0)
 
210
        warnings.filters.pop(0)
 
211
 
 
212
 
144
213
if __name__ == "__main__":
145
214
    run_module_suite()