Spaces:
Sleeping
Sleeping
MilesCranmer
commited on
Commit
•
118c5f6
1
Parent(s):
9a5df63
Generate table with scientific notation for floats
Browse files- pysr/export_latex.py +1 -0
- pysr/sr.py +10 -4
- test/test.py +11 -11
pysr/export_latex.py
CHANGED
@@ -5,6 +5,7 @@ from sympy.printing.latex import LatexPrinter
|
|
5 |
|
6 |
class PreciseLatexPrinter(LatexPrinter):
|
7 |
"""Modified SymPy printer with custom float precision."""
|
|
|
8 |
def __init__(self, settings=None, prec=3):
|
9 |
super().__init__(settings)
|
10 |
self.prec = prec
|
|
|
5 |
|
6 |
class PreciseLatexPrinter(LatexPrinter):
|
7 |
"""Modified SymPy printer with custom float precision."""
|
8 |
+
|
9 |
def __init__(self, settings=None, prec=3):
|
10 |
super().__init__(settings)
|
11 |
self.prec = prec
|
pysr/sr.py
CHANGED
@@ -2039,14 +2039,20 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
|
|
2039 |
# Also convert these to reduced precision:
|
2040 |
# loss = self.equations_.iloc[i]["loss"]
|
2041 |
# score = self.equations_.iloc[i]["score"]
|
2042 |
-
complexity =
|
2043 |
-
loss =
|
2044 |
-
|
|
|
|
|
|
|
|
|
2045 |
|
2046 |
-
row_pieces = [
|
2047 |
if include_score:
|
2048 |
row_pieces.append(score)
|
2049 |
|
|
|
|
|
2050 |
latex_table_content.append(
|
2051 |
" & ".join(row_pieces) + r" \\",
|
2052 |
)
|
|
|
2039 |
# Also convert these to reduced precision:
|
2040 |
# loss = self.equations_.iloc[i]["loss"]
|
2041 |
# score = self.equations_.iloc[i]["score"]
|
2042 |
+
complexity = str(self.equations_.iloc[i]["complexity"])
|
2043 |
+
loss = to_latex(
|
2044 |
+
sympy.Float(self.equations_.iloc[i]["loss"]), prec=precision
|
2045 |
+
)
|
2046 |
+
score = to_latex(
|
2047 |
+
sympy.Float(self.equations_.iloc[i]["score"]), prec=precision
|
2048 |
+
)
|
2049 |
|
2050 |
+
row_pieces = [equation, complexity, loss]
|
2051 |
if include_score:
|
2052 |
row_pieces.append(score)
|
2053 |
|
2054 |
+
row_pieces = ["$" + piece + "$" for piece in row_pieces]
|
2055 |
+
|
2056 |
latex_table_content.append(
|
2057 |
" & ".join(row_pieces) + r" \\",
|
2058 |
)
|
test/test.py
CHANGED
@@ -540,9 +540,9 @@ class TestLaTeXTable(unittest.TestCase):
|
|
540 |
# Regular table:
|
541 |
latex_table_str = model.latex_table()
|
542 |
middle_part = r"""
|
543 |
-
$x_{0}$ & 1 & 1.05 \\
|
544 |
-
$\cos{\left(x_{0} \right)}$ & 2 & 0.0232 \\
|
545 |
-
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & 8 & 1.
|
546 |
"""
|
547 |
true_latex_table_str = self.create_true_latex(middle_part)
|
548 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
@@ -550,9 +550,9 @@ class TestLaTeXTable(unittest.TestCase):
|
|
550 |
# Different precision:
|
551 |
latex_table_str = model.latex_table(precision=5)
|
552 |
middle_part = r"""
|
553 |
-
$x_{0}$ & 1 & 1.052 \\
|
554 |
-
$\cos{\left(x_{0} \right)}$ & 2 & 0.02315 \\
|
555 |
-
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & 8 & 1.
|
556 |
"""
|
557 |
true_latex_table_str = self.create_true_latex(middle_part)
|
558 |
self.assertEqual(latex_table_str, self.create_true_latex(middle_part))
|
@@ -560,9 +560,9 @@ class TestLaTeXTable(unittest.TestCase):
|
|
560 |
# Including score:
|
561 |
latex_table_str = model.latex_table(include_score=True)
|
562 |
middle_part = r"""
|
563 |
-
$x_{0}$ & 1 & 1.05 & 0 \\
|
564 |
-
$\cos{\left(x_{0} \right)}$ & 2 & 0.0232 & 3.82 \\
|
565 |
-
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & 8 & 1.
|
566 |
"""
|
567 |
true_latex_table_str = self.create_true_latex(middle_part, include_score=True)
|
568 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
@@ -570,7 +570,7 @@ class TestLaTeXTable(unittest.TestCase):
|
|
570 |
# Only last equation:
|
571 |
latex_table_str = model.latex_table(indices=[2])
|
572 |
middle_part = r"""
|
573 |
-
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & 8 & 1.
|
574 |
"""
|
575 |
true_latex_table_str = self.create_true_latex(middle_part)
|
576 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
@@ -595,4 +595,4 @@ class TestLaTeXTable(unittest.TestCase):
|
|
595 |
)
|
596 |
self.assertEqual(
|
597 |
to_latex(expr, prec=8), "3232.3249 x - 1.4857485 \cdot 10^{-10}"
|
598 |
-
)
|
|
|
540 |
# Regular table:
|
541 |
latex_table_str = model.latex_table()
|
542 |
middle_part = r"""
|
543 |
+
$x_{0}$ & $1$ & $1.05$ \\
|
544 |
+
$\cos{\left(x_{0} \right)}$ & $2$ & $0.0232$ \\
|
545 |
+
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & $8$ & $1.12 \cdot 10^{-15}$ \\
|
546 |
"""
|
547 |
true_latex_table_str = self.create_true_latex(middle_part)
|
548 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
|
|
550 |
# Different precision:
|
551 |
latex_table_str = model.latex_table(precision=5)
|
552 |
middle_part = r"""
|
553 |
+
$x_{0}$ & $1$ & $1.052$ \\
|
554 |
+
$\cos{\left(x_{0} \right)}$ & $2$ & $0.02315$ \\
|
555 |
+
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & $8$ & $1.1235 \cdot 10^{-15}$ \\
|
556 |
"""
|
557 |
true_latex_table_str = self.create_true_latex(middle_part)
|
558 |
self.assertEqual(latex_table_str, self.create_true_latex(middle_part))
|
|
|
560 |
# Including score:
|
561 |
latex_table_str = model.latex_table(include_score=True)
|
562 |
middle_part = r"""
|
563 |
+
$x_{0}$ & $1$ & $1.05$ & $0.0$ \\
|
564 |
+
$\cos{\left(x_{0} \right)}$ & $2$ & $0.0232$ & $3.82$ \\
|
565 |
+
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & $8$ & $1.12 \cdot 10^{-15}$ & $5.11$ \\
|
566 |
"""
|
567 |
true_latex_table_str = self.create_true_latex(middle_part, include_score=True)
|
568 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
|
|
570 |
# Only last equation:
|
571 |
latex_table_str = model.latex_table(indices=[2])
|
572 |
middle_part = r"""
|
573 |
+
$x_{0} + x_{1} - \cos{\left(x_{0} x_{1} \right)}$ & $8$ & $1.12 \cdot 10^{-15}$ \\
|
574 |
"""
|
575 |
true_latex_table_str = self.create_true_latex(middle_part)
|
576 |
self.assertEqual(latex_table_str, true_latex_table_str)
|
|
|
595 |
)
|
596 |
self.assertEqual(
|
597 |
to_latex(expr, prec=8), "3232.3249 x - 1.4857485 \cdot 10^{-10}"
|
598 |
+
)
|