Skip to content

Commit

Permalink
fix #549
Browse files Browse the repository at this point in the history
- add na_rep to _table_formatter_stderr
- use stderr formatter in fit_report
  • Loading branch information
dbrakenhoff committed Mar 23, 2023
1 parent 1379345 commit 19d2986
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions pastas/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Internal Pastas
from pastas.decorators import get_stressmodel
from pastas.io.base import _load_model, dump
from pastas.modelplots import Plotting
from pastas.modelplots import Plotting, _table_formatter_stderr
from pastas.modelstats import Statistics
from pastas.noisemodels import NoiseModel
from pastas.solver import LeastSquares
Expand Down Expand Up @@ -795,7 +795,7 @@ def solve(
if solver is not None:
self.fit = solver
self.fit.set_model(self)
# Create the default solver is None is provided or already present
# Create the default solver if None is provided or already present
elif self.fit is None:
self.fit = LeastSquares()
self.fit.set_model(self)
Expand Down Expand Up @@ -1741,7 +1741,9 @@ def fit_report(

parameters = self.parameters.loc[:, ["optimal", "stderr", "initial", "vary"]]
stderr = parameters.loc[:, "stderr"] / parameters.loc[:, "optimal"]
parameters.loc[:, "stderr"] = stderr.abs().apply("±{:.2%}".format)
parameters.loc[:, "stderr"] = "±" + stderr.abs().apply(
_table_formatter_stderr, na_rep="nan"
)

# Determine the width of the fit_report based on the parameters
width = len(parameters.to_string().split("\n")[1])
Expand Down
8 changes: 4 additions & 4 deletions pastas/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ def plot_track_solve_history(self, fig: Optional[Figure] = None) -> List[Axes]:
return fig.axes


def _table_formatter_params(s: float) -> str:
def _table_formatter_params(s: float, na_rep: str = "") -> str:
"""Internal method for formatting parameters in tables in Pastas plots.
Parameters
Expand All @@ -938,7 +938,7 @@ def _table_formatter_params(s: float) -> str:
float formatted as str.
"""
if np.isnan(s):
return ""
return na_rep
elif np.floor(np.log10(np.abs(s))) <= -2:
return f"{s:.2e}"
elif np.floor(np.log10(np.abs(s))) > 5:
Expand All @@ -947,7 +947,7 @@ def _table_formatter_params(s: float) -> str:
return f"{s:.2f}"


def _table_formatter_stderr(s: float) -> str:
def _table_formatter_stderr(s: float, na_rep: str = "") -> str:
"""Internal method for formatting stderrs in tables in Pastas plots.
Parameters
Expand All @@ -961,7 +961,7 @@ def _table_formatter_stderr(s: float) -> str:
float formatted as str.
"""
if np.isnan(s):
return ""
return na_rep
elif np.floor(np.log10(np.abs(s))) <= -4:
return f"{s * 100.:.2e}%"
elif np.floor(np.log10(np.abs(s))) > 3:
Expand Down

0 comments on commit 19d2986

Please sign in to comment.