MilesCranmer commited on
Commit
38bbf68
1 Parent(s): 4f97146

Make latex table work for multiple outputs

Browse files
Files changed (1) hide show
  1. pysr/sr.py +57 -37
pysr/sr.py CHANGED
@@ -2005,10 +2005,11 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
2005
 
2006
  Parameters
2007
  ----------
2008
- indices : list[int], default=None
2009
  If you wish to select a particular subset of equations from
2010
  `self.equations_`, give the row numbers here. By default,
2011
- all equations will be used.
 
2012
  precision : int, default=3
2013
  The number of significant figures shown in the LaTeX
2014
  representations.
@@ -2020,53 +2021,72 @@ class PySRRegressor(MultiOutputMixin, RegressorMixin, BaseEstimator):
2020
  latex_table_str : str
2021
  A string that will render a table in LaTeX of the equations.
2022
  """
2023
- if self.nout_ > 1:
2024
- raise NotImplementedError(
2025
- "LaTeX tables are not implemented for multiple outputs."
2026
- )
2027
- if indices is None:
2028
- indices = range(len(self.equations_))
2029
 
2030
  columns = ["Equation", "Complexity", "Loss"]
2031
  if include_score:
2032
  columns.append("Score")
2033
 
 
 
 
 
 
 
 
 
 
2034
  latex_table_top = generate_top_of_latex_table(columns)
 
2035
 
2036
- latex_table_content = []
2037
- for i in indices:
2038
- equation = self.latex(i, precision=precision)
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
- )
2059
 
2060
- latex_table_bottom = generate_bottom_of_latex_table()
 
 
2061
 
2062
- latex_table_str = "\n".join(
2063
- [
2064
- latex_table_top,
2065
- *latex_table_content,
2066
- latex_table_bottom,
2067
- ]
2068
- )
2069
- return latex_table_str
 
 
 
2070
 
2071
 
2072
  def _denoise(X, y, Xresampled=None, random_state=None):
 
2005
 
2006
  Parameters
2007
  ----------
2008
+ indices : list[int] | list[list[int]], default=None
2009
  If you wish to select a particular subset of equations from
2010
  `self.equations_`, give the row numbers here. By default,
2011
+ all equations will be used. If there are multiple output
2012
+ features, then pass a list of lists.
2013
  precision : int, default=3
2014
  The number of significant figures shown in the LaTeX
2015
  representations.
 
2021
  latex_table_str : str
2022
  A string that will render a table in LaTeX of the equations.
2023
  """
2024
+ self.refresh()
 
 
 
 
 
2025
 
2026
  columns = ["Equation", "Complexity", "Loss"]
2027
  if include_score:
2028
  columns.append("Score")
2029
 
2030
+ # All indices:
2031
+ if indices is None:
2032
+ if self.nout_ > 1:
2033
+ indices = [
2034
+ list(range(len(out_equations))) for out_equations in self.equations_
2035
+ ]
2036
+ else:
2037
+ indices = list(range(len(self.equations_)))
2038
+
2039
  latex_table_top = generate_top_of_latex_table(columns)
2040
+ latex_table_bottom = generate_bottom_of_latex_table()
2041
 
2042
+ equations = self.equations_
 
 
 
 
 
 
 
 
 
 
 
 
2043
 
2044
+ if isinstance(indices[0], int):
2045
+ indices = [indices]
2046
+ equations = [equations]
2047
+
2048
+ latex_equations = [
2049
+ [to_latex(eq, prec=precision) for eq in equation_set["sympy_format"]]
2050
+ for equation_set in equations
2051
+ ]
2052
+
2053
+ all_latex_table_str = []
2054
+
2055
+ for output_feature, index_set in enumerate(indices):
2056
+ latex_table_content = []
2057
+ for i in index_set:
2058
+ latex_equation = latex_equations[output_feature][i]
2059
+ complexity = str(equations[output_feature].iloc[i]["complexity"])
2060
+ loss = to_latex(
2061
+ sympy.Float(equations[output_feature].iloc[i]["loss"]),
2062
+ prec=precision,
2063
+ )
2064
+ score = to_latex(
2065
+ sympy.Float(equations[output_feature].iloc[i]["score"]),
2066
+ prec=precision,
2067
+ )
2068
 
2069
+ row_pieces = [latex_equation, complexity, loss]
2070
+ if include_score:
2071
+ row_pieces.append(score)
2072
 
2073
+ row_pieces = ["$" + piece + "$" for piece in row_pieces]
 
 
2074
 
2075
+ latex_table_content.append(
2076
+ " & ".join(row_pieces) + r" \\",
2077
+ )
2078
 
2079
+ all_latex_table_str.append(
2080
+ "\n".join(
2081
+ [
2082
+ latex_table_top,
2083
+ *latex_table_content,
2084
+ latex_table_bottom,
2085
+ ]
2086
+ )
2087
+ )
2088
+
2089
+ return "\n\n".join(all_latex_table_str)
2090
 
2091
 
2092
  def _denoise(X, y, Xresampled=None, random_state=None):