~ubuntu-branches/ubuntu/karmic/python-scipy/karmic

« back to all changes in this revision

Viewing changes to Lib/io/tests/test_array_import.py

  • Committer: Bazaar Package Importer
  • Author(s): Daniel T. Chen (new)
  • Date: 2005-03-16 02:15:29 UTC
  • Revision ID: james.westby@ubuntu.com-20050316021529-xrjlowsejs0cijig
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
# This python script tests the numpyio module.
 
4
# also check out numpyio.fread.__doc__ and other method docstrings.
 
5
 
 
6
import os,sys
 
7
import unittest
 
8
from unittest import TestCase
 
9
from scipy_test.testing import *
 
10
set_package_path()
 
11
import io
 
12
from io import numpyio
 
13
del sys.path[0]
 
14
 
 
15
 
 
16
import Numeric
 
17
N = Numeric
 
18
import tempfile
 
19
 
 
20
class test_numpyio(TestCase):
 
21
    def check_basic(self):
 
22
        # Generate some data
 
23
        a = 255*rand(20)
 
24
        # Open a file
 
25
        fname = tempfile.mktemp('.dat')
 
26
        fid = open(fname,"wb")
 
27
        # Write the data as shorts
 
28
        numpyio.fwrite(fid,20,a,Numeric.Int16)
 
29
        fid.close()
 
30
        # Reopen the file and read in data
 
31
        fid = open(fname,"rb")
 
32
        print "\nDon't worry about a warning regarding the number of bytes read."
 
33
        b = numpyio.fread(fid,1000000,Numeric.Int16,Numeric.Int)
 
34
        fid.close()
 
35
        assert(N.product(a.astype(N.Int16) == b))
 
36
        os.remove(fname)
 
37
 
 
38
class test_read_array(TestCase):
 
39
    def check_complex(self):
 
40
        a = rand(13,4) + 1j*rand(13,4)
 
41
        fname = tempfile.mktemp('.dat')
 
42
        io.write_array(fname,a)
 
43
        b = io.read_array(fname,atype=N.Complex)
 
44
        assert_array_almost_equal(a,b,decimal=4)
 
45
        os.remove(fname)
 
46
 
 
47
    def check_float(self):
 
48
        a = rand(3,4)*30
 
49
        fname = tempfile.mktemp('.dat')
 
50
        io.write_array(fname,a)
 
51
        b = io.read_array(fname)
 
52
        assert_array_almost_equal(a,b,decimal=4)
 
53
        os.remove(fname)
 
54
 
 
55
    def check_integer(self):
 
56
        from scipy import stats
 
57
        a = stats.randint.rvs(1,20,size=(3,4))
 
58
        fname = tempfile.mktemp('.dat')
 
59
        io.write_array(fname,a)
 
60
        b = io.read_array(fname,atype=N.Int)
 
61
        assert_array_equal(a,b)
 
62
        os.remove(fname)
 
63
 
 
64
if __name__ == "__main__":
 
65
    ScipyTest('io.array_import').run()
 
66
 
 
67