Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #549 stderr formatting #580

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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