Ignore:
Timestamp:
Feb 10, 2009, 11:11:04 AM (15 years ago)
Author:
rwilson
Message:

Initial commit of numpy changes. Still a long way to go.

Location:
branches/numpy
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/utilities/test_numerical_tools.py

    r6174 r6304  
    33
    44import unittest
    5 import Numeric as num
     5import numpy as num
    66
    77from math import sqrt, pi
     
    6868        A = [1,2,3,4]
    6969        B = ensure_numeric(A)
    70         assert type(B) == num.ArrayType
    71         assert B.typecode() == 'l'
     70        assert isinstance(B, num.ndarray)
     71        assert B.dtype.char == 'l'
    7272        assert B[0] == 1 and B[1] == 2 and B[2] == 3 and B[3] == 4
    7373
    7474        A = [1,2,3.14,4]
    7575        B = ensure_numeric(A)
    76         assert type(B) == num.ArrayType
    77         assert B.typecode() == 'd'
     76        assert isinstance(B, num.ndarray)
     77        assert B.dtype.char == 'd'
    7878        assert B[0] == 1 and B[1] == 2 and B[2] == 3.14 and B[3] == 4
    7979
    8080        A = [1,2,3,4]
    81         B = ensure_numeric(A, num.Float)
    82         assert type(B) == num.ArrayType
    83         assert B.typecode() == 'd'
     81        B = ensure_numeric(A, num.float)
     82        assert isinstance(B, num.ndarray)
     83        assert B.dtype.char == 'd'
    8484        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    8585
    8686        A = [1,2,3,4]
    87         B = ensure_numeric(A, num.Float)
    88         assert type(B) == num.ArrayType
    89         assert B.typecode() == 'd'
     87        B = ensure_numeric(A, num.float)
     88        assert isinstance(B, num.ndarray)
     89        assert B.dtype.char == 'd'
    9090        assert B[0] == 1.0 and B[1] == 2.0 and B[2] == 3.0 and B[3] == 4.0
    9191
    92         A = num.array([1,2,3,4], num.Int)      #array default#
     92        A = num.array([1,2,3,4])
    9393        B = ensure_numeric(A)
    94         assert type(B) == num.ArrayType
    95         assert B.typecode() == 'l'       
     94        assert isinstance(B, num.ndarray)
     95        assert B.dtype.char == 'l'       
    9696        assert num.alltrue(A == B)   
    9797        assert A is B   #Same object
    9898
    99         A = num.array([1,2,3,4], num.Int)      #array default#
    100         B = ensure_numeric(A, num.Float)
    101         assert type(B) == num.ArrayType
    102         assert B.typecode() == 'd'       
     99# THIS FAILS!  ASSUMED TYPE IS num.int!?
     100#        # check default num.array type, which is supposed to be num.float
     101#        A = num.array((1,2,3,4))
     102#        assert isinstance(A, num.ndarray)
     103#        assert A.dtype.char == 'd', \
     104#                "Expected dtype='d', got '%s'" % A.dtype.char
     105
     106        A = num.array([1,2,3,4])
     107        B = ensure_numeric(A, num.float)
     108        assert isinstance(B, num.ndarray)
     109        assert A.dtype.char == 'l'       
     110        assert B.dtype.char == 'd'       
    103111        assert num.alltrue(A == B)   
    104         assert A is not B   #Not the same object
     112        assert A is not B   # Not the same object
    105113
    106114        # Check scalars
    107115        A = 1
    108         B = ensure_numeric(A, num.Float)
    109         #print A, B[0], len(B), type(B)
    110         #print B.shape
     116        B = ensure_numeric(A, num.float)
    111117        assert num.alltrue(A == B)
    112 
    113         B = ensure_numeric(A, num.Int)       
    114         #print A, B
    115         #print B.shape
     118#        print 'A=%s' % str(A)
     119#        print 'B=%s, B.shape=%s' % (str(B), str(B.shape))
     120
     121        B = ensure_numeric(A, num.int)       
    116122        assert num.alltrue(A == B)
    117123
     124        # try to simulate getting (x,0) shape
     125        data_points = [[ 413634. ],]
     126        array_data_points = ensure_numeric(data_points)
     127        #if not (0,) == array_data_points.shape:
     128        #    assert len(array_data_points.shape) == 2
     129        #    assert array_data_points.shape[1] == 2
     130
     131
     132    def NO_test_ensure_numeric_char(self):
     133        '''numpy can't handle this'''
     134
    118135        # Error situation
    119 
    120         B = ensure_numeric('hello', num.Int)               
     136        B = ensure_numeric('hello', num.int)               
    121137        assert num.allclose(B, [104, 101, 108, 108, 111])
    122138
     
    343359        assert x == 5.
    344360
    345                                    
     361
     362################################################################################
     363# Test the is_num_????() functions.
     364################################################################################
     365
     366    def test_is_float(self):
     367        def t(val, expected):
     368            if expected == True:
     369                msg = 'should be num.float?'
     370            else:
     371                msg = 'should not be num.float?'
     372            msg = '%s (%s) %s' % (str(val), type(val), msg)
     373            assert is_num_float(val) == expected, msg
     374
     375        t(1, False)
     376        t(1.0, False)
     377        t('abc', False)
     378        t(None, False)
     379        t(num.array(None), False)
     380        # can't create array(None, num.int)
     381#        t(num.array(None, num.int), False)
     382        t(num.array(None, num.float), True)
     383        t(num.array(()), True)
     384        t(num.array((), num.int), False)
     385        t(num.array((), num.float), True)
     386        t(num.array((1), num.int), False)
     387        t(num.array((1), num.float), True)
     388
     389        t(num.array((1,2)), False)
     390        t(num.array((1,2), num.int), False)
     391        t(num.array((1,2), num.float), True)
     392        t(num.array([1,2]), False)
     393        t(num.array([1,2], num.int), False)
     394        t(num.array([1,2], num.float), True)
     395
     396        t(num.array((1.0,2.0)), True)
     397        t(num.array((1.0,2.0), num.int), False)
     398        t(num.array((1.0,2.0), num.float), True)
     399        t(num.array([1.0,2.0]), True)
     400        t(num.array([1.0,2.0], num.int), False)
     401        t(num.array([1.0,2.0], num.float), True)
     402
     403        t(num.array(((1.0,2.0),(3.0,4.0))), True)
     404        t(num.array(((1.0,2.0),(3.0,4.0)), num.int), False)
     405        t(num.array(((1.0,2.0),(3.0,4.0)), num.float), True)
     406        t(num.array([[1.0,2.0],[3.0,4.0]]), True)
     407        t(num.array([1.0,2.0], num.int), False)
     408        t(num.array([1.0,2.0], num.float), True)
     409
     410        t(num.array('abc'), False)
     411        t(num.array('abc', num.character), False)
     412        # can't create array as int from string
     413#        t(num.array('abc', num.int), False)
     414        # can't create array as float from string
     415#        t(num.array('abc', num.float), True)
     416
     417    def test_is_int(self):
     418        def t(val, expected):
     419            if expected == True:
     420                msg = 'should be num.int?'
     421            else:
     422                msg = 'should not be num.int?'
     423            msg = '%s (%s) %s' % (str(val), type(val), msg)
     424            assert is_num_int(val) == expected, msg
     425
     426        t(1, False)
     427        t(1.0, False)
     428        t('abc', False)
     429        t(None, False)
     430        t(num.array(None), False)
     431        # can't create array(None, num.int)
     432#        t(num.array(None, num.int), True)
     433        t(num.array(None, num.float), False)
     434        t(num.array((), num.int), True)
     435        t(num.array(()), False)
     436        t(num.array((), num.float), False)
     437        t(num.array((1), num.int), True)
     438        t(num.array((1), num.float), False)
     439
     440        t(num.array((1,2)), True)
     441        t(num.array((1,2), num.int), True)
     442        t(num.array((1,2), num.float), False)
     443        t(num.array([1,2]), True)
     444        t(num.array([1,2], num.int), True)
     445        t(num.array([1,2], num.float), False)
     446
     447        t(num.array((1.0,2.0)), False)
     448        t(num.array((1.0,2.0), num.int), True)
     449        t(num.array((1.0,2.0), num.float), False)
     450        t(num.array([1.0,2.0]), False)
     451        t(num.array([1.0,2.0], num.int), True)
     452        t(num.array([1.0,2.0], num.float), False)
     453
     454        t(num.array(((1.0,2.0),(3.0,4.0))), False)
     455        t(num.array(((1.0,2.0),(3.0,4.0)), num.int), True)
     456        t(num.array(((1.0,2.0),(3.0,4.0)), num.float), False)
     457        t(num.array([[1.0,2.0],[3.0,4.0]]), False)
     458        t(num.array([1.0,2.0], num.int), True)
     459        t(num.array([1.0,2.0], num.float), False)
     460
     461        t(num.array('abc'), False)
     462        t(num.array('abc', num.character), False)
     463        # can't create array as int from string
     464#        t(num.array('abc', num.int), True)
     465        # can't create array as float from string
     466#        t(num.array('abc', num.float), False)
     467
    346468
    347469#-------------------------------------------------------------
    348470if __name__ == "__main__":
    349     suite = unittest.makeSuite(Test_Numerical_Tools,'test')
    350     #suite = unittest.makeSuite(Test_Numerical_Tools,'test_err')
     471    #suite = unittest.makeSuite(Test_Numerical_Tools,'test')
     472    suite = unittest.makeSuite(Test_Numerical_Tools,'test_is_')
    351473    runner = unittest.TextTestRunner()
    352474    runner.run(suite)
Note: See TracChangeset for help on using the changeset viewer.