[Numpy-svn] r5339 - in trunk/numpy: lib lib/tests ma

numpy-svn@scip... numpy-svn@scip...
Thu Jul 3 01:15:17 CDT 2008


Author: rkern
Date: 2008-07-03 01:15:14 -0500 (Thu, 03 Jul 2008)
New Revision: 5339

Modified:
   trunk/numpy/lib/_datasource.py
   trunk/numpy/lib/getlimits.py
   trunk/numpy/lib/polynomial.py
   trunk/numpy/lib/tests/test__datasource.py
   trunk/numpy/lib/utils.py
   trunk/numpy/ma/core.py
   trunk/numpy/ma/extras.py
Log:
Reduce numpy's import times by delaying a few time consuming imports to the point of actual use and global instantiations of finfo. Thanks to David Cournapeau for tracking down and fixing the import part of the problem.

Modified: trunk/numpy/lib/_datasource.py
===================================================================
--- trunk/numpy/lib/_datasource.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/lib/_datasource.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -37,7 +37,6 @@
 import os
 import tempfile
 from shutil import rmtree
-from urllib2 import urlopen, URLError
 from urlparse import urlparse
 
 # TODO: .zip support, .tar support?
@@ -196,6 +195,9 @@
         Creates a copy of the file in the datasource cache.
 
         """
+        # We import these here because importing urllib2 is slow and
+        # a significant fraction of numpy's total import time.
+        from urllib2 import urlopen, URLError
 
         upath = self.abspath(path)
 
@@ -337,6 +339,9 @@
             is accessible if it exists in either location.
 
         """
+        # We import this here because importing urllib2 is slow and
+        # a significant fraction of numpy's total import time.
+        from urllib2 import URLError
 
         # Test local path
         if os.path.exists(path):

Modified: trunk/numpy/lib/getlimits.py
===================================================================
--- trunk/numpy/lib/getlimits.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/lib/getlimits.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -21,14 +21,16 @@
     }
 
 class finfo(object):
-    """Machine limits for floating point types.
+    """ Machine limits for floating point types.
 
-    :Parameters:
-        dtype : floating point type or instance
+    Parameters
+    ----------
+    dtype : floating point type, dtype, or instance
+        The kind of floating point data type to get information about.
 
-    :SeeAlso:
-      - numpy.lib.machar.MachAr
-
+    See Also
+    --------
+    numpy.lib.machar.MachAr
     """
 
     _finfo_cache = {}

Modified: trunk/numpy/lib/polynomial.py
===================================================================
--- trunk/numpy/lib/polynomial.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/lib/polynomial.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -17,8 +17,6 @@
 from numpy.lib.function_base import trim_zeros, sort_complex
 eigvals = None
 lstsq = None
-_single_eps = finfo(NX.single).eps
-_double_eps = finfo(NX.double).eps
 
 class RankWarning(UserWarning):
     """Issued by polyfit when Vandermonde matrix is rank deficient.
@@ -301,11 +299,7 @@
 
     # set rcond
     if rcond is None :
-        xtype = x.dtype
-        if xtype == NX.single or xtype == NX.csingle :
-            rcond = len(x)*_single_eps
-        else :
-            rcond = len(x)*_double_eps
+        rcond = len(x)*finfo(x.dtype).eps
 
     # scale x to improve condition number
     scale = abs(x).max()

Modified: trunk/numpy/lib/tests/test__datasource.py
===================================================================
--- trunk/numpy/lib/tests/test__datasource.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/lib/tests/test__datasource.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -5,6 +5,7 @@
 from tempfile import mkdtemp, mkstemp, NamedTemporaryFile
 from shutil import rmtree
 from urlparse import urlparse
+from urllib2 import URLError
 
 from numpy.testing import *
 
@@ -16,7 +17,7 @@
         tmpfile = NamedTemporaryFile(prefix='urltmp_')
         return tmpfile
     else:
-        raise datasource.URLError('Name or service not known')
+        raise URLError('Name or service not known')
 
 # Rebind urlopen during testing.  For a 'real' test, uncomment the rebinding
 # below.

Modified: trunk/numpy/lib/utils.py
===================================================================
--- trunk/numpy/lib/utils.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/lib/utils.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -1,11 +1,9 @@
-import compiler
 import os
 import sys
-import inspect
 import pkgutil
 import types
 import re
-import pydoc
+
 from numpy.core.numerictypes import obj2sctype, generic
 from numpy.core.multiarray import dtype as _dtype
 from numpy.core import product, ndarray
@@ -327,7 +325,8 @@
                 p[0]*(x**N-1) + p[1]*(x**N-2) + ... + p[N-2]*x + p[N-1]
     """
     global _namedict, _dictlist
-    import pydoc
+    # Local import to speed up numpy's import time.
+    import pydoc, inspect
 
     if hasattr(object,'_ppimport_importer') or \
        hasattr(object, '_ppimport_module'):
@@ -467,6 +466,8 @@
 def source(object, output=sys.stdout):
     """Write source for this object to output.
     """
+    # Local import to speed up numpy's import time.
+    import inspect
     try:
         print >> output,  "In file: %s\n" % inspect.getsourcefile(object)
         print >> output,  inspect.getsource(object)
@@ -599,6 +600,8 @@
 
     """
     global _lookfor_caches
+    # Local import to speed up numpy's import time.
+    import inspect
 
     if module is None:
         module = "numpy"
@@ -751,6 +754,8 @@
       ...
     SyntaxError: Unknown name: dict
     """
+    # Local import to speed up numpy's import time.
+    import compiler
     walker = SafeEval()
     try:
         ast = compiler.parse(source, "eval")

Modified: trunk/numpy/ma/core.py
===================================================================
--- trunk/numpy/ma/core.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/ma/core.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -77,7 +77,6 @@
 MaskType = np.bool_
 nomask = MaskType(0)
 
-divide_tolerance = np.finfo(float).tiny
 np.seterr(all='ignore')
 
 def doc_note(note):
@@ -398,9 +397,14 @@
 #............................
 class _DomainSafeDivide:
     """Define a domain for safe division."""
-    def __init__ (self, tolerance=divide_tolerance):
+    def __init__ (self, tolerance=None):
         self.tolerance = tolerance
     def __call__ (self, a, b):
+        # Delay the selection of the tolerance to here in order to reduce numpy
+        # import times. The calculation of these parameters is a substantial
+        # component of numpy's import time.
+        if self.tolerance is None:
+            self.tolerance = np.finfo(float).tiny
         return umath.absolute(a) * self.tolerance >= umath.absolute(b)
 #............................
 class _DomainGreater:

Modified: trunk/numpy/ma/extras.py
===================================================================
--- trunk/numpy/ma/extras.py	2008-07-03 03:57:29 UTC (rev 5338)
+++ trunk/numpy/ma/extras.py	2008-07-03 06:15:14 UTC (rev 5339)
@@ -39,7 +39,7 @@
 from numpy import ndarray, array as nxarray
 import numpy.core.umath as umath
 from numpy.lib.index_tricks import AxisConcatenator
-from numpy.lib.polynomial import _lstsq, _single_eps, _double_eps
+from numpy.lib.polynomial import _lstsq
 
 #...............................................................................
 def issequence(seq):
@@ -866,10 +866,7 @@
         x[m] = y[m] = masked
     # Set rcond
     if rcond is None :
-        if x.dtype in (np.single, np.csingle):
-            rcond = len(x)*_single_eps
-        else :
-            rcond = len(x)*_double_eps
+        rcond = len(x)*np.finfo(x.dtype).eps
     # Scale x to improve condition number
     scale = abs(x).max()
     if scale != 0 :



More information about the Numpy-svn mailing list