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

👌 Improve ParameterGroup markdown rendering #918

Merged
merged 3 commits into from
Dec 1, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions glotaran/parameter/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Keys:
MAX = "max"
MIN = "min"
NON_NEG = "non-negative"
STD_ERR = "standard-error"
VARY = "vary"


Expand Down Expand Up @@ -238,6 +239,8 @@ def _set_options_from_dict(self, options: dict[str, Any]):
self.minimum = options[Keys.MIN]
if Keys.VARY in options:
self.vary = options[Keys.VARY]
if Keys.STD_ERR in options:
self.standard_error = options[Keys.STD_ERR]

@property
def label(self) -> str | None:
Expand Down
25 changes: 17 additions & 8 deletions glotaran/parameter/parameter_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,11 +579,16 @@ def update_parameter_expression(self):
)
parameter.value = value

def markdown(self) -> MarkdownStr:
def markdown(self, float_format: str = ".3e") -> MarkdownStr:
"""Format the :class:`ParameterGroup` as markdown string.

This is done by recursing the nested :class:`ParameterGroup` tree.

Parameters
----------
float_format: str
Format string for floating point numbers, by default ".3e"

Returns
-------
MarkdownStr :
Expand All @@ -594,12 +599,12 @@ def markdown(self) -> MarkdownStr:
table_header = [
"_Label_",
"_Value_",
"_StdErr_",
"_Min_",
"_Max_",
"_Standard Error_",
"_Minimum_",
"_Maximum_",
"_Vary_",
"_Non-Negative_",
"_Expr_",
"_Expression_",
]
if self.label is not None:
return_string += f"{node_indentation}* __{self.label}__:\n"
Expand All @@ -613,19 +618,23 @@ def markdown(self) -> MarkdownStr:
parameter.maximum,
parameter.vary,
parameter.non_negative,
parameter.expression,
f"`{parameter.expression}`",
]
for _, parameter in self._parameters.items()
]
parameter_table = indent(
tabulate(
parameter_rows, headers=table_header, tablefmt="github", missingval="None"
parameter_rows,
headers=table_header,
tablefmt="github",
missingval="None",
floatfmt=float_format,
),
f" {node_indentation}",
)
return_string += f"\n{parameter_table}\n\n"
for _, child_group in sorted(self.items()):
return_string += f"{child_group.__str__()}"
return_string += f"{child_group.markdown(float_format=float_format)}"
return MarkdownStr(return_string)

def _repr_markdown_(self) -> str:
Expand Down
49 changes: 32 additions & 17 deletions glotaran/parameter/test/test_parameter_group_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,40 @@
PARAMETERS_3C_KINETIC = """\
kinetic:
- ["1", 300e-3]
- ["2", 500e-4]
- ["3", 700e-5]
- ["2", 500e-4, {standard-error: 0.000012345678}]
- ["3", {expr: $kinetic.1 + $kinetic.2}]
"""

RENDERED_MARKDOWN = """\
* __irf__:

| _Label_ | _Value_ | _StdErr_ | _Min_ | _Max_ | _Vary_ | _Non-Negative_ | _Expr_ |
|-----------|-----------|------------|---------|---------|----------|------------------|----------|
| center | 1.3 | nan | -inf | inf | True | False | None |
| width | 7.8 | nan | -inf | inf | True | False | None |
| _Label_ | _Value_ | _Standard Error_ | _Minimum_ | _Maximum_ | _Vary_ | _Non-Negative_ | _Expression_ |
|-----------|-----------|--------------------|-------------|-------------|----------|------------------|----------------|
| center | 1.300e+00 | nan | -inf | inf | True | False | `None` |
| width | 7.800e+00 | nan | -inf | inf | True | False | `None` |

* __j__:

| _Label_ | _Value_ | _StdErr_ | _Min_ | _Max_ | _Vary_ | _Non-Negative_ | _Expr_ |
|-----------|-----------|------------|---------|---------|----------|------------------|----------|
| 1 | 1 | nan | -inf | inf | False | False | None |
| _Label_ | _Value_ | _Standard Error_ | _Minimum_ | _Maximum_ | _Vary_ | _Non-Negative_ | _Expression_ |
|-----------|-----------|--------------------|-------------|-------------|----------|------------------|----------------|
| 1 | 1.000e+00 | nan | -inf | inf | False | False | `None` |

* __kinetic__:

| _Label_ | _Value_ | _StdErr_ | _Min_ | _Max_ | _Vary_ | _Non-Negative_ | _Expr_ |
|-----------|-----------|------------|---------|---------|----------|------------------|----------|
| 1 | 0.3 | nan | -inf | inf | True | False | None |
| 2 | 0.05 | nan | -inf | inf | True | False | None |
| 3 | 0.007 | nan | -inf | inf | True | False | None |
| _Label_ | _Value_ | _Standard Error_ | _Minimum_ | _Maximum_ | _Vary_ | _Non-Negative_ | _Expression_ |
|-----------|-----------|--------------------|-------------|-------------|----------|------------------|---------------------------|
| 1 | 3.000e-01 | nan | -inf | inf | True | False | `None` |
| 2 | 5.000e-02 | 1.235e-05 | -inf | inf | True | False | `None` |
| 3 | 3.500e-01 | nan | -inf | inf | False | False | `$kinetic.1 + $kinetic.2` |

""" # noqa: E501

RENDERED_MARKDOWN_E5_PRECISION = """\
* __irf__:

| _Label_ | _Value_ | _Standard Error_ | _Minimum_ | _Maximum_ | _Vary_ | _Non-Negative_ | _Expression_ |
|-----------|-------------|--------------------|-------------|-------------|----------|------------------|----------------|
| center | 1.30000e+00 | 1.23457e-05 | -inf | inf | True | False | `None` |

""" # noqa: E501

Expand All @@ -52,9 +61,9 @@ def test_param_group_markdown_is_order_independent():
{
"j": [["1", 1, {"vary": False, "non-negative": False}]],
"kinetic": [
["1", 300e-3],
["2", 500e-4],
["3", 700e-5],
["1", 0.3],
["2", 500e-4, {"standard-error": 0.000012345678}],
["3", 700e-5, {"expr": "$kinetic.1 + $kinetic.2"}],
],
"irf": [["center", 1.3], ["width", 7.8]],
}
Expand All @@ -67,6 +76,12 @@ def test_param_group_markdown_is_order_independent():
assert str(initial_parameters2.markdown()) == RENDERED_MARKDOWN
assert str(initial_parameters_ref.markdown()) == RENDERED_MARKDOWN

minimal_params = ParameterGroup.from_dict(
{"irf": [["center", 1.3, {"standard-error": 0.000012345678}]]}
)

assert str(minimal_params.markdown(float_format=".5e")) == RENDERED_MARKDOWN_E5_PRECISION


def test_param_group_repr():
"""Repr creates code to recreate the object with from_dict."""
Expand Down