Skip to content

Commit 3c9b499

Browse files
committed
Remove os.path in scalar/math.py
1 parent cccd8d7 commit 3c9b499

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

pytensor/scalar/math.py

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
As SciPy is not always available, we treat them separately.
55
"""
66

7-
import os
87
from functools import reduce
8+
from pathlib import Path
99
from textwrap import dedent
1010

1111
import numpy as np
@@ -47,6 +47,9 @@
4747
from pytensor.scalar.loop import ScalarLoop
4848

4949

50+
C_CODE_PATH = Path(__file__).parent / "c_code"
51+
52+
5053
class Erf(UnaryScalarOp):
5154
nfunc_spec = ("scipy.special.erf", 1, 1)
5255

@@ -154,19 +157,12 @@ def L_op(self, inputs, outputs, grads):
154157

155158
def c_header_dirs(self, **kwargs):
156159
# Using the Faddeeva.hh (c++) header for Faddeevva.cc
157-
res = [
158-
*super().c_header_dirs(**kwargs),
159-
os.path.join(os.path.dirname(__file__), "c_code"),
160-
]
160+
res = [*super().c_header_dirs(**kwargs), C_CODE_PATH]
161161
return res
162162

163163
def c_support_code(self, **kwargs):
164164
# Using Faddeeva.cc source file from: http://ab-initio.mit.edu/wiki/index.php/Faddeeva_Package
165-
with open(
166-
os.path.join(os.path.dirname(__file__), "c_code", "Faddeeva.cc")
167-
) as f:
168-
raw = f.read()
169-
return raw
165+
return (C_CODE_PATH / "Faddeeva.cc").read_text(encoding="utf-8")
170166

171167
def c_code(self, node, name, inp, out, sub):
172168
(x,) = inp
@@ -612,9 +608,7 @@ def impl(self, x, k):
612608
return Chi2SF.st_impl(x, k)
613609

614610
def c_support_code(self, **kwargs):
615-
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
616-
raw = f.read()
617-
return raw
611+
return (C_CODE_PATH / "gamma.c").read_text(encoding="utf-8")
618612

619613
def c_code(self, node, name, inp, out, sub):
620614
x, k = inp
@@ -665,9 +659,7 @@ def grad(self, inputs, grads):
665659
]
666660

667661
def c_support_code(self, **kwargs):
668-
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
669-
raw = f.read()
670-
return raw
662+
return (C_CODE_PATH / "gamma.c").read_text(encoding="utf-8")
671663

672664
def c_code(self, node, name, inp, out, sub):
673665
k, x = inp
@@ -718,9 +710,7 @@ def grad(self, inputs, grads):
718710
]
719711

720712
def c_support_code(self, **kwargs):
721-
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
722-
raw = f.read()
723-
return raw
713+
return (C_CODE_PATH / "gamma.c").read_text(encoding="utf-8")
724714

725715
def c_code(self, node, name, inp, out, sub):
726716
k, x = inp
@@ -1031,9 +1021,7 @@ def impl(self, k, x):
10311021
return GammaU.st_impl(k, x)
10321022

10331023
def c_support_code(self, **kwargs):
1034-
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
1035-
raw = f.read()
1036-
return raw
1024+
return (C_CODE_PATH / "gamma.c").read_text(encoding="utf-8")
10371025

10381026
def c_code(self, node, name, inp, out, sub):
10391027
k, x = inp
@@ -1069,9 +1057,7 @@ def impl(self, k, x):
10691057
return GammaL.st_impl(k, x)
10701058

10711059
def c_support_code(self, **kwargs):
1072-
with open(os.path.join(os.path.dirname(__file__), "c_code", "gamma.c")) as f:
1073-
raw = f.read()
1074-
return raw
1060+
return (C_CODE_PATH / "gamma.c").read_text(encoding="utf-8")
10751061

10761062
def c_code(self, node, name, inp, out, sub):
10771063
k, x = inp

0 commit comments

Comments
 (0)