File: test_array_import.py

package info (click to toggle)
python-scipy 0.7.2%2Bdfsg1-1%2Bdeb6u1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze-lts
  • size: 28,572 kB
  • ctags: 36,183
  • sloc: cpp: 216,880; fortran: 76,016; python: 71,833; ansic: 62,118; makefile: 243; sh: 17
file content (68 lines) | stat: -rw-r--r-- 2,028 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python

# This python script tests the numpyio module.
# also check out numpyio.fread.__doc__ and other method docstrings.

import os
from numpy.testing import *
import scipy.io as io
from scipy.io import numpyio
from scipy.io import array_import

import numpy.oldnumeric as N
import tempfile

class TestNumpyio(TestCase):
    def test_basic(self):
        # Generate some data
        a = 255*rand(20)
        # Open a file
        fname = tempfile.mktemp('.dat')
        fid = open(fname,"wb")
        # Write the data as shorts
        numpyio.fwrite(fid,20,a,N.Int16)
        fid.close()
        # Reopen the file and read in data
        fid = open(fname,"rb")
        if verbose >= 3:
            print "\nDon't worry about a warning regarding the number of bytes read."
        b = numpyio.fread(fid,1000000,N.Int16,N.Int)
        fid.close()
        assert(N.product(a.astype(N.Int16) == b,axis=0))
        os.remove(fname)

class TestReadArray(TestCase):
    def test_complex(self):
        a = rand(13,4) + 1j*rand(13,4)
        fname = tempfile.mktemp('.dat')
        io.write_array(fname,a)
        b = io.read_array(fname,atype=N.Complex)
        assert_array_almost_equal(a,b,decimal=4)
        os.remove(fname)

    def test_float(self):
        a = rand(3,4)*30
        fname = tempfile.mktemp('.dat')
        io.write_array(fname,a)
        b = io.read_array(fname)
        assert_array_almost_equal(a,b,decimal=4)
        os.remove(fname)

    def test_integer(self):
        from scipy import stats
        a = stats.randint.rvs(1,20,size=(3,4))
        fname = tempfile.mktemp('.dat')
        io.write_array(fname,a)
        b = io.read_array(fname,atype=a.dtype.char)
        assert_array_equal(a,b)
        os.remove(fname)

class TestRegression(TestCase):
    def test_get_open_file_works_with_filelike_objects(self):
        f = tempfile.TemporaryFile()
        f2 = array_import.get_open_file(f)
        assert f2 is f
        f.close()

if __name__ == "__main__":
    run_module_suite()