Skip to content

Commit 40313aa

Browse files
Remove SciPy conditional logic
1 parent 2c91b5a commit 40313aa

File tree

17 files changed

+82
-370
lines changed

17 files changed

+82
-370
lines changed

aesara/sandbox/linalg/ops.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from aesara.tensor.math import pow as aet_pow
1818
from aesara.tensor.math import prod
1919
from aesara.tensor.nlinalg import MatrixInverse, det, matrix_inverse, trace
20-
from aesara.tensor.slinalg import Cholesky, Solve, cholesky, imported_scipy, solve
20+
from aesara.tensor.slinalg import Cholesky, Solve, cholesky, solve
2121

2222

2323
logger = logging.getLogger(__name__)
@@ -229,8 +229,6 @@ def transinv_to_invtrans(fgraph, node):
229229
@register_stabilize
230230
@local_optimizer([Dot, Dot22])
231231
def inv_as_solve(fgraph, node):
232-
if not imported_scipy:
233-
return False
234232
if isinstance(node.op, (Dot, Dot22)):
235233
l, r = node.inputs
236234
if l.owner and l.owner.op == matrix_inverse:

aesara/scalar/math.py

Lines changed: 23 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import os
88

99
import numpy as np
10+
import scipy.special
11+
import scipy.stats
1012

1113
from aesara.configdefaults import config
1214
from aesara.gradient import grad_not_implemented
@@ -25,26 +27,11 @@
2527
)
2628

2729

28-
imported_scipy_special = False
29-
try:
30-
import scipy.special
31-
import scipy.stats
32-
33-
imported_scipy_special = True
34-
# Importing scipy.special may raise ValueError.
35-
# See http://projects.scipy.org/scipy/ticket/1739
36-
except (ImportError, ValueError):
37-
pass
38-
39-
4030
class Erf(UnaryScalarOp):
4131
nfunc_spec = ("scipy.special.erf", 1, 1)
4232

4333
def impl(self, x):
44-
if imported_scipy_special:
45-
return scipy.special.erf(x)
46-
else:
47-
super().impl(x)
34+
return scipy.special.erf(x)
4835

4936
def L_op(self, inputs, outputs, grads):
5037
(x,) = inputs
@@ -78,10 +65,7 @@ class Erfc(UnaryScalarOp):
7865
nfunc_spec = ("scipy.special.erfc", 1, 1)
7966

8067
def impl(self, x):
81-
if imported_scipy_special:
82-
return scipy.special.erfc(x)
83-
else:
84-
super().impl(x)
68+
return scipy.special.erfc(x)
8569

8670
def L_op(self, inputs, outputs, grads):
8771
(x,) = inputs
@@ -130,10 +114,7 @@ class Erfcx(UnaryScalarOp):
130114
nfunc_spec = ("scipy.special.erfcx", 1, 1)
131115

132116
def impl(self, x):
133-
if imported_scipy_special:
134-
return scipy.special.erfcx(x)
135-
else:
136-
super().impl(x)
117+
return scipy.special.erfcx(x)
137118

138119
def L_op(self, inputs, outputs, grads):
139120
(x,) = inputs
@@ -195,10 +176,7 @@ class Erfinv(UnaryScalarOp):
195176
nfunc_spec = ("scipy.special.erfinv", 1, 1)
196177

197178
def impl(self, x):
198-
if imported_scipy_special:
199-
return scipy.special.erfinv(x)
200-
else:
201-
super().impl(x)
179+
return scipy.special.erfinv(x)
202180

203181
def L_op(self, inputs, outputs, grads):
204182
(x,) = inputs
@@ -232,10 +210,7 @@ class Erfcinv(UnaryScalarOp):
232210
nfunc_spec = ("scipy.special.erfcinv", 1, 1)
233211

234212
def impl(self, x):
235-
if imported_scipy_special:
236-
return scipy.special.erfcinv(x)
237-
else:
238-
super().impl(x)
213+
return scipy.special.erfcinv(x)
239214

240215
def L_op(self, inputs, outputs, grads):
241216
(x,) = inputs
@@ -273,10 +248,7 @@ def st_impl(x):
273248
return scipy.special.gamma(x)
274249

275250
def impl(self, x):
276-
if imported_scipy_special:
277-
return Gamma.st_impl(x)
278-
else:
279-
super().impl(x)
251+
return Gamma.st_impl(x)
280252

281253
def L_op(self, inputs, outputs, gout):
282254
(x,) = inputs
@@ -315,10 +287,7 @@ def st_impl(x):
315287
return scipy.special.gammaln(x)
316288

317289
def impl(self, x):
318-
if imported_scipy_special:
319-
return GammaLn.st_impl(x)
320-
else:
321-
super().impl(x)
290+
return GammaLn.st_impl(x)
322291

323292
def L_op(self, inputs, outputs, grads):
324293
(x,) = inputs
@@ -362,10 +331,7 @@ def st_impl(x):
362331
return scipy.special.psi(x)
363332

364333
def impl(self, x):
365-
if imported_scipy_special:
366-
return Psi.st_impl(x)
367-
else:
368-
super().impl(x)
334+
return Psi.st_impl(x)
369335

370336
def L_op(self, inputs, outputs, grads):
371337
(x,) = inputs
@@ -456,10 +422,7 @@ def st_impl(x):
456422
return scipy.special.polygamma(1, x)
457423

458424
def impl(self, x):
459-
if imported_scipy_special:
460-
return TriGamma.st_impl(x)
461-
else:
462-
super().impl(x)
425+
return TriGamma.st_impl(x)
463426

464427
def grad(self, inputs, outputs_gradients):
465428
raise NotImplementedError()
@@ -545,10 +508,7 @@ def st_impl(x, k):
545508
return scipy.stats.chi2.sf(x, k)
546509

547510
def impl(self, x, k):
548-
if imported_scipy_special:
549-
return Chi2SF.st_impl(x, k)
550-
else:
551-
super().impl(x, k)
511+
return Chi2SF.st_impl(x, k)
552512

553513
def c_support_code(self, **kwargs):
554514
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
@@ -589,10 +549,7 @@ def st_impl(k, x):
589549
return scipy.special.gammainc(k, x)
590550

591551
def impl(self, k, x):
592-
if imported_scipy_special:
593-
return GammaInc.st_impl(k, x)
594-
else:
595-
super().impl(k, x)
552+
return GammaInc.st_impl(k, x)
596553

597554
def c_support_code(self, **kwargs):
598555
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
@@ -633,10 +590,7 @@ def st_impl(k, x):
633590
return scipy.special.gammaincc(x, k)
634591

635592
def impl(self, k, x):
636-
if imported_scipy_special:
637-
return GammaIncC.st_impl(k, x)
638-
else:
639-
super().impl(k, x)
593+
return GammaIncC.st_impl(k, x)
640594

641595
def c_support_code(self, **kwargs):
642596
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
@@ -677,10 +631,7 @@ def st_impl(k, x):
677631
return scipy.special.gammaincc(k, x) * scipy.special.gamma(k)
678632

679633
def impl(self, k, x):
680-
if imported_scipy_special:
681-
return GammaU.st_impl(k, x)
682-
else:
683-
super().impl(k, x)
634+
return GammaU.st_impl(k, x)
684635

685636
def c_support_code(self, **kwargs):
686637
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
@@ -721,10 +672,7 @@ def st_impl(k, x):
721672
return scipy.special.gammainc(k, x) * scipy.special.gamma(k)
722673

723674
def impl(self, k, x):
724-
if imported_scipy_special:
725-
return GammaL.st_impl(k, x)
726-
else:
727-
super().impl(k, x)
675+
return GammaL.st_impl(k, x)
728676

729677
def c_support_code(self, **kwargs):
730678
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
@@ -765,10 +713,7 @@ def st_impl(v, x):
765713
return scipy.special.jv(v, x)
766714

767715
def impl(self, v, x):
768-
if imported_scipy_special:
769-
return self.st_impl(v, x)
770-
else:
771-
super().impl(v, x)
716+
return self.st_impl(v, x)
772717

773718
def grad(self, inputs, grads):
774719
v, x = inputs
@@ -794,10 +739,7 @@ def st_impl(x):
794739
return scipy.special.j1(x)
795740

796741
def impl(self, x):
797-
if imported_scipy_special:
798-
return self.st_impl(x)
799-
else:
800-
super().impl(x)
742+
return self.st_impl(x)
801743

802744
def grad(self, inputs, grads):
803745
(x,) = inputs
@@ -828,10 +770,7 @@ def st_impl(x):
828770
return scipy.special.j0(x)
829771

830772
def impl(self, x):
831-
if imported_scipy_special:
832-
return self.st_impl(x)
833-
else:
834-
super().impl(x)
773+
return self.st_impl(x)
835774

836775
def grad(self, inp, grads):
837776
(x,) = inp
@@ -862,10 +801,7 @@ def st_impl(v, x):
862801
return scipy.special.iv(v, x)
863802

864803
def impl(self, v, x):
865-
if imported_scipy_special:
866-
return self.st_impl(v, x)
867-
else:
868-
super().impl(v, x)
804+
return self.st_impl(v, x)
869805

870806
def grad(self, inputs, grads):
871807
v, x = inputs
@@ -891,10 +827,7 @@ def st_impl(x):
891827
return scipy.special.i1(x)
892828

893829
def impl(self, x):
894-
if imported_scipy_special:
895-
return self.st_impl(x)
896-
else:
897-
super().impl(x)
830+
return self.st_impl(x)
898831

899832
def grad(self, inputs, grads):
900833
(x,) = inputs
@@ -917,10 +850,7 @@ def st_impl(x):
917850
return scipy.special.i0(x)
918851

919852
def impl(self, x):
920-
if imported_scipy_special:
921-
return self.st_impl(x)
922-
else:
923-
super().impl(x)
853+
return self.st_impl(x)
924854

925855
def grad(self, inp, grads):
926856
(x,) = inp
@@ -939,10 +869,7 @@ class Sigmoid(UnaryScalarOp):
939869
nfunc_spec = ("scipy.special.expit", 1, 1)
940870

941871
def impl(self, x):
942-
if imported_scipy_special:
943-
return scipy.special.expit(x)
944-
else:
945-
super().impl(x)
872+
return scipy.special.expit(x)
946873

947874
def grad(self, inp, grads):
948875
(x,) = inp

aesara/sparse/type.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
import numpy as np
2-
3-
4-
try:
5-
import scipy.sparse
6-
7-
imported_scipy = True
8-
except ImportError:
9-
imported_scipy = False
10-
2+
import scipy.sparse
113

124
import aesara
135
from aesara.graph.type import Type
@@ -54,12 +46,11 @@ class SparseType(Type):
5446
5547
"""
5648

57-
if imported_scipy:
58-
format_cls = {
59-
"csr": scipy.sparse.csr_matrix,
60-
"csc": scipy.sparse.csc_matrix,
61-
"bsr": scipy.sparse.bsr_matrix,
62-
}
49+
format_cls = {
50+
"csr": scipy.sparse.csr_matrix,
51+
"csc": scipy.sparse.csc_matrix,
52+
"bsr": scipy.sparse.bsr_matrix,
53+
}
6354
dtype_set = {
6455
"int8",
6556
"int16",
@@ -81,10 +72,6 @@ class SparseType(Type):
8172
Constant = None
8273

8374
def __init__(self, format, dtype):
84-
if not imported_scipy:
85-
raise Exception(
86-
"You can't make SparseType object as SciPy" " is not available."
87-
)
8875
dtype = str(dtype)
8976
if dtype in self.dtype_set:
9077
self.dtype = dtype

aesara/tensor/nnet/abstract_conv.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
import warnings
1616

1717
import numpy as np
18+
from scipy.signal.signaltools import _bvalfromboundary, _valfrommode, convolve
19+
from scipy.signal.sigtools import _convolve2d
1820

1921
import aesara
2022
from aesara import tensor as aet
@@ -31,15 +33,6 @@
3133
from aesara.tensor.var import TensorConstant, TensorVariable
3234

3335

34-
try:
35-
from scipy.signal.signaltools import _bvalfromboundary, _valfrommode, convolve
36-
from scipy.signal.sigtools import _convolve2d
37-
38-
imported_scipy_signal = True
39-
except ImportError:
40-
imported_scipy_signal = False
41-
42-
4336
__docformat__ = "restructuredtext en"
4437
_logger = logging.getLogger("aesara.tensor.nnet.abstract_conv")
4538

@@ -2342,11 +2335,6 @@ def conv(
23422335
"""
23432336
Basic slow Python 2D or 3D convolution for DebugMode
23442337
"""
2345-
if not imported_scipy_signal:
2346-
raise NotImplementedError(
2347-
"AbstractConv perform requires the python package"
2348-
" for scipy.signal to be installed."
2349-
)
23502338
if not (mode in ("valid", "full")):
23512339
raise ValueError(
23522340
"invalid mode {}, which must be either "

0 commit comments

Comments
 (0)