1
1
import numpy as np
2
2
import matplotlib .mlab as mlab
3
+ import tempfile
4
+ from nose .tools import raises
3
5
4
6
def test_colinear_pca ():
5
7
a = mlab .PCA ._get_colinear ()
@@ -8,3 +10,25 @@ def test_colinear_pca():
8
10
assert (np .allclose (pca .fracs [2 :], 0. ))
9
11
assert (np .allclose (pca .Y [:,2 :], 0. ))
10
12
13
+ def test_recarray_csv_roundtrip ():
14
+ expected = np .recarray ((99 ,),
15
+ [('x' ,np .float ),('y' ,np .float ),('t' ,np .float )])
16
+ expected ['x' ][0 ] = 1
17
+ expected ['y' ][1 ] = 2
18
+ expected ['t' ][2 ] = 3
19
+ fd = tempfile .TemporaryFile (suffix = 'csv' )
20
+ mlab .rec2csv (expected ,fd )
21
+ fd .seek (0 )
22
+ actual = mlab .csv2rec (fd )
23
+ fd .close ()
24
+ assert np .allclose ( expected ['x' ], actual ['x' ] )
25
+ assert np .allclose ( expected ['y' ], actual ['y' ] )
26
+ assert np .allclose ( expected ['t' ], actual ['t' ] )
27
+
28
+ @raises (ValueError )
29
+ def test_rec2csv_bad_shape ():
30
+ bad = np .recarray ((99 ,4 ),[('x' ,np .float ),('y' ,np .float )])
31
+ fd = tempfile .TemporaryFile (suffix = 'csv' )
32
+
33
+ # the bad recarray should trigger a ValueError for having ndim > 1.
34
+ mlab .rec2csv (bad ,fd )
0 commit comments