[Numpy-svn] r6334 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Mon Jan 26 15:04:33 CST 2009


Author: pierregm
Date: 2009-01-26 15:04:26 -0600 (Mon, 26 Jan 2009)
New Revision: 6334

Modified:
   trunk/numpy/lib/_iotools.py
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test_io.py
Log:
* _iotools.StringConverter :
    - add a _checked attribute to indicate whether the converter has been upgraded or not.
    - switched the default value for bool to False
* io.genfromtxt:
    - fixed for the case where a whole column is masked: switch to bool or the common dtype (if needed)



Modified: trunk/numpy/lib/_iotools.py
===================================================================
--- trunk/numpy/lib/_iotools.py	2009-01-24 08:02:14 UTC (rev 6333)
+++ trunk/numpy/lib/_iotools.py	2009-01-26 21:04:26 UTC (rev 6334)
@@ -294,7 +294,7 @@
 
     """
     #
-    _mapper = [(nx.bool_, str2bool, None),
+    _mapper = [(nx.bool_, str2bool, False),
                (nx.integer, int, -1),
                (nx.floating, float, nx.nan),
                (complex, complex, nx.nan+0j),
@@ -354,7 +354,7 @@
         if dtype_or_func is None:
             self.func = str2bool
             self._status = 0
-            self.default = default
+            self.default = default or False
             ttype = np.bool
         else:
             # Is the input a np.dtype ?
@@ -396,6 +396,7 @@
         #
         self._callingfunction = self._strict_call
         self.type = ttype
+        self._checked = False
     #
     def _loose_call(self, value):
         try:
@@ -408,6 +409,8 @@
             return self.func(value)
         except ValueError:
             if value.strip() in self.missing_values:
+                if not self._status:
+                    self._checked = False
                 return self.default
             raise ValueError("Cannot convert string '%s'" % value)
     #
@@ -421,6 +424,7 @@
     The order in which the converters are tested is read from the
     :attr:`_status` attribute of the instance.
         """
+        self._checked = True
         try:
             self._strict_call(value)
         except ValueError:

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2009-01-24 08:02:14 UTC (rev 6333)
+++ trunk/numpy/lib/io.py	2009-01-26 21:04:26 UTC (rev 6334)
@@ -883,18 +883,21 @@
     # Reset the dtype
     data = rows
     if dtype is None:
-        # Get the dtypes from the first row
-        coldtypes = [np.array(val).dtype for val in data[0]]
-        # Find the columns with strings, and take the largest number of chars.
-        strcolidx = [i for (i, v) in enumerate(coldtypes) if v.char == 'S']
+        # Get the dtypes from the types of the converters
+        coldtypes = [conv.type for conv in converters]
+        # Find the columns with strings...
+        strcolidx = [i for (i, v) in enumerate(coldtypes)
+                     if v in (type('S'), np.string_)]
+        # ... and take the largest number of chars.
         for i in strcolidx:
             coldtypes[i] = "|S%i" % max(len(row[i]) for row in data)
         #
         if names is None:
             # If the dtype is uniform, don't define names, else use ''
-            base = coldtypes[0]
-            if np.all([(dt == base) for dt in coldtypes]):
-                (ddtype, mdtype) = (base, np.bool)
+            base = set([c.type for c in converters if c._checked])
+            
+            if len(base) == 1:
+                (ddtype, mdtype) = (list(base)[0], np.bool)
             else:
                 ddtype = [('', dt) for dt in coldtypes]
                 mdtype = [('', np.bool) for dt in coldtypes]

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2009-01-24 08:02:14 UTC (rev 6333)
+++ trunk/numpy/lib/tests/test_io.py	2009-01-26 21:04:26 UTC (rev 6334)
@@ -685,6 +685,23 @@
         assert_equal(test.mask, control.mask)
 
 
+    def test_with_masked_column_uniform(self):
+        "Test masked column"
+        data = StringIO.StringIO('1 2 3\n4 5 6\n')
+        test = np.genfromtxt(data, missing='2,5', dtype=None, usemask=True)
+        control = ma.array([[1, 2, 3], [4, 5, 6]], mask=[[0, 1, 0],[0, 1, 0]])
+        assert_equal(test, control)
+
+    def test_with_masked_column_various(self):
+        "Test masked column"
+        data = StringIO.StringIO('True 2 3\nFalse 5 6\n')
+        test = np.genfromtxt(data, missing='2,5', dtype=None, usemask=True)
+        control = ma.array([(1, 2, 3), (0, 5, 6)],
+                           mask=[(0, 1, 0),(0, 1, 0)],
+                           dtype=[('f0', bool), ('f1', bool), ('f2', int)])
+        assert_equal(test, control)
+
+
     def test_recfromtxt(self):
         #
         data = StringIO.StringIO('A,B\n0,1\n2,3')



More information about the Numpy-svn mailing list