diff options
author | Robert Kern <robert.kern@gmail.com> | 2008-07-03 06:15:14 +0000 |
---|---|---|
committer | Robert Kern <robert.kern@gmail.com> | 2008-07-03 06:15:14 +0000 |
commit | 102cdc22b12df8a44be644d39a277229e5324028 (patch) | |
tree | 104f86c4355fd18c9bd7d7648b120a0d0c288299 /numpy | |
parent | 590babe4646a3435f8a709d6230d05c10f085be1 (diff) | |
download | numpy-102cdc22b12df8a44be644d39a277229e5324028.tar.gz |
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.
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/lib/_datasource.py | 7 | ||||
-rw-r--r-- | numpy/lib/getlimits.py | 14 | ||||
-rw-r--r-- | numpy/lib/polynomial.py | 8 | ||||
-rw-r--r-- | numpy/lib/tests/test__datasource.py | 3 | ||||
-rw-r--r-- | numpy/lib/utils.py | 13 | ||||
-rw-r--r-- | numpy/ma/core.py | 8 | ||||
-rw-r--r-- | numpy/ma/extras.py | 7 |
7 files changed, 34 insertions, 26 deletions
diff --git a/numpy/lib/_datasource.py b/numpy/lib/_datasource.py index 20a6cc391..bd69b45f2 100644 --- a/numpy/lib/_datasource.py +++ b/numpy/lib/_datasource.py @@ -37,7 +37,6 @@ __docformat__ = "restructuredtext en" 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 @@ class DataSource (object): 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 @@ class DataSource (object): 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): diff --git a/numpy/lib/getlimits.py b/numpy/lib/getlimits.py index 89b40203f..99016de2d 100644 --- a/numpy/lib/getlimits.py +++ b/numpy/lib/getlimits.py @@ -21,14 +21,16 @@ _convert_to_float = { } class finfo(object): - """Machine limits for floating point types. + """ Machine limits for floating point types. - :Parameters: - dtype : floating point type or instance - - :SeeAlso: - - numpy.lib.machar.MachAr + Parameters + ---------- + dtype : floating point type, dtype, or instance + The kind of floating point data type to get information about. + See Also + -------- + numpy.lib.machar.MachAr """ _finfo_cache = {} diff --git a/numpy/lib/polynomial.py b/numpy/lib/polynomial.py index 141e85e25..303cdb13c 100644 --- a/numpy/lib/polynomial.py +++ b/numpy/lib/polynomial.py @@ -17,8 +17,6 @@ from numpy.lib.shape_base import hstack, atleast_1d 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 @@ def polyfit(x, y, deg, rcond=None, full=False): # 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() diff --git a/numpy/lib/tests/test__datasource.py b/numpy/lib/tests/test__datasource.py index abb58701d..5102237d6 100644 --- a/numpy/lib/tests/test__datasource.py +++ b/numpy/lib/tests/test__datasource.py @@ -5,6 +5,7 @@ import struct 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 @@ def urlopen_stub(url, data=None): 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. diff --git a/numpy/lib/utils.py b/numpy/lib/utils.py index 8a119722b..7df342e02 100644 --- a/numpy/lib/utils.py +++ b/numpy/lib/utils.py @@ -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 @@ def info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): 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 info(object=None,maxwidth=76,output=sys.stdout,toplevel='numpy'): 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 @@ def _lookfor_generate_cache(module, import_modules, regenerate): """ global _lookfor_caches + # Local import to speed up numpy's import time. + import inspect if module is None: module = "numpy" @@ -751,6 +754,8 @@ def safe_eval(source): ... SyntaxError: Unknown name: dict """ + # Local import to speed up numpy's import time. + import compiler walker = SafeEval() try: ast = compiler.parse(source, "eval") diff --git a/numpy/ma/core.py b/numpy/ma/core.py index 4c8291a5f..368abd11d 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -77,7 +77,6 @@ import warnings 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 _DomainTan: #............................ 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: diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 2ab2eae93..f369180f2 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -39,7 +39,7 @@ import numpy as np 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 @@ def polyfit(x, y, deg, rcond=None, full=False): 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 : |