[Scipy-svn] r2343 - in trunk/Lib/io: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Thu Nov 30 14:14:36 EST 2006


Author: matthew.brett at gmail.com
Date: 2006-11-30 13:14:12 -0600 (Thu, 30 Nov 2006)
New Revision: 2343

Modified:
   trunk/Lib/io/recaster.py
   trunk/Lib/io/tests/test_recaster.py
Log:
And more tests on recaster

Modified: trunk/Lib/io/recaster.py
===================================================================
--- trunk/Lib/io/recaster.py	2006-11-30 18:49:45 UTC (rev 2342)
+++ trunk/Lib/io/recaster.py	2006-11-30 19:14:12 UTC (rev 2343)
@@ -265,27 +265,32 @@
             raise TypeError, 'Do not recognize array kind %s' % dtk
             
     def downcast_complex(self, arr):
+        ''' Downcasts complex array to smaller type if possible '''
         # can we downcast to float?
         dt = arr.dtype
         dti = ceil(dt.itemsize / 2)
         sctypes = self.sized_sctypes['f']
         flts = [t[0] for i, t in enumerate(sctypes) if t[1] <= dti]
-        test_arr = arr.astype(flts[0])
-        rtol, atol = self.tols_from_sctype(dt.type)
-        if allclose(arr, test_arr, rtol, atol):
-            return self.downcast_float(test_arr)
-        # try downcasting to another complex type
-        return self.smallest_same_kind(arr)
+        if flts: # There are smaller floats to try
+            test_arr = arr.astype(flts[0])
+            rtol, atol = self.tols_from_sctype(dt.type)
+            if allclose(arr, test_arr, rtol, atol):
+                arr = test_arr
+        # try downcasting to int or another complex type
+        return self.downcast_to_int_or_same(arr)
     
-    def downcast_float(self, arr):
+    def downcast_to_int_or_same(self, arr):
+        ''' Downcast to integer or smaller of same kind '''
         # Try integer
         test_arr = self.downcast_integer(arr)
         rtol, atol = self.tols_from_sctype(arr.dtype.type)
         if allclose(arr, test_arr, rtol, atol):
             return test_arr
-        # Otherwise descend the float types
+        # Otherwise descend the types of same kind
         return self.smallest_same_kind(arr)
 
+    downcast_float = downcast_to_int_or_same
+
     def downcast_integer(self, arr):
         ''' Downcasts arr to integer
 

Modified: trunk/Lib/io/tests/test_recaster.py
===================================================================
--- trunk/Lib/io/tests/test_recaster.py	2006-11-30 18:49:45 UTC (rev 2342)
+++ trunk/Lib/io/tests/test_recaster.py	2006-11-30 19:14:12 UTC (rev 2343)
@@ -92,8 +92,11 @@
             assert rt == T, 'Expected %s, got %s type' % (T, rt)
         
     def test_downcasts(self):
-        value = 1
+        value = 100
         R = self.recaster
-        A = N.array(value, N.complex128)
-        B = R.downcast_complex(A)
-        assert B.dtype.type == N.int32
+        for T in (N.complex128, N.complex64,
+                  N.float64, N.uint64):
+            B = R.downcast(N.array(value, T))
+            assert B is not None, 'Got None for %s' % T
+            assert B.dtype.type == N.int32
+        




More information about the Scipy-svn mailing list