Skip to content

Commit 2bd91fd

Browse files
authored
Merge pull request #321 from drhodes/master
lazily import scipy to avoid import delay
2 parents 39e5472 + 55ee1ab commit 2bd91fd

File tree

3 files changed

+19
-8
lines changed

3 files changed

+19
-8
lines changed

mitxgraders/formulagrader/integralgrader.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import six
1010
from functools import wraps
11-
from scipy import integrate
1211
from numpy import real, imag
1312
from abc import abstractproperty
1413
from numbers import Number
@@ -517,6 +516,9 @@ def raw_integrand(x):
517516
suffixes=self.suffixes)
518517
return value
519518

519+
# lazy load this module for performance reasons
520+
from scipy import integrate
521+
520522
if self.config['complex_integrand']:
521523
integrand_re = lambda x: real(raw_integrand(x))
522524
integrand_im = lambda x: imag(raw_integrand(x))

mitxgraders/helpers/calc/mathfuncs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import six
1818
from numbers import Number
1919
import numpy as np
20-
import scipy.special as special
2120
from mitxgraders.helpers.calc.specify_domain import SpecifyDomain
2221
from mitxgraders.helpers.calc.exceptions import FunctionEvalError
2322
from mitxgraders.helpers.calc.math_array import MathArray
@@ -208,6 +207,9 @@ def factorial(z):
208207
"functions cannot be used at negative integer values.")
209208
raise FunctionEvalError(msg)
210209

210+
# lazy import this module for performance reasons
211+
import scipy.special as special
212+
211213
value = special.gamma(z+1)
212214
# value is a numpy array; If it's 0d, we can just get its item:
213215
try:

mitxgraders/matrixsampling.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ def rvs(self, dimension):
2929
raise NotImplementedError('This feature requires newer versions of numpy '
3030
'and scipy than are available.')
3131

32-
try:
33-
from scipy.stats import ortho_group, special_ortho_group, unitary_group
34-
except ImportError:
35-
ortho_group = Unavailable()
36-
special_ortho_group = Unavailable()
37-
unitary_group = Unavailable()
3832

3933
# Set the objects to be imported from this grader
4034
__all__ = [
@@ -825,6 +819,13 @@ def generate_sample(self):
825819
"""
826820
Generates an orthogonal matrix
827821
"""
822+
# lazy load this module for performance reasons
823+
try:
824+
from scipy.stats import ortho_group, special_ortho_group
825+
except ImportError:
826+
ortho_group = Unavailable()
827+
special_ortho_group = Unavailable()
828+
828829
# Generate the array
829830
if self.config['unitdet']:
830831
array = special_ortho_group.rvs(self.config['dimension'])
@@ -896,6 +897,12 @@ def generate_sample(self):
896897
"""
897898
Generates an orthogonal matrix as appropriate
898899
"""
900+
# lazy load this module for performance reasons
901+
try:
902+
from scipy.stats import unitary_group
903+
except ImportError:
904+
unitary_group = Unavailable()
905+
899906
# Generate the array
900907
array = unitary_group.rvs(self.config['dimension'])
901908
# Fix the determinant if need be

0 commit comments

Comments
 (0)