Skip to content
Merged
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
25 changes: 12 additions & 13 deletions src/easyreflectometry/summary/summary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,18 @@
_TOOLTIP_SCHEME = 'nametooltip'


def _format_error(error: float) -> str:
"""Format a parameter error the same way the Analysis table does.
def _format_value(value: float, sig_figs: int) -> str:

Check warning on line 27 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L27

Added line #L27 was not covered by tests
"""Format a numeric value for summary display.

Mirrors the JS ``formatError`` in Fittables.qml:
2 significant figures; fall back to 1-decimal exponential for long strings.
Zero (no fit run yet) is shown as '0.0'.
*sig_figs* significant figures; fall back to 1-decimal exponential when
the formatted string is too long. Zero is shown as '0.0'.
"""
if error == 0.0:
if value == 0.0:

Check warning on line 33 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L33

Added line #L33 was not covered by tests
return '0.0'
s = f'{error:.2g}'
if len(s) <= 6:
s = f'{value:.{sig_figs}g}'
if len(s) <= sig_figs + 4:

Check warning on line 36 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L35-L36

Added lines #L35 - L36 were not covered by tests
return s
return f'{error:.1e}'
return f'{value:.1e}'

Check warning on line 38 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L38

Added line #L38 was not covered by tests


def _truncate_name(name: str, max_len: int = _NAME_MAX_LEN) -> str:
Expand Down Expand Up @@ -174,9 +173,9 @@

html_parameter = HTML_PARAMETER_TEMPLATE
html_parameter = html_parameter.replace('parameter_name', f'{name}')
html_parameter = html_parameter.replace('parameter_value', f'{value}')
html_parameter = html_parameter.replace('parameter_value', _format_value(value, 3))

Check warning on line 176 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L176

Added line #L176 was not covered by tests
html_parameter = html_parameter.replace('parameter_unit', f'{unit}')
error_str = _format_error(error)
error_str = _format_value(error, 2)

Check warning on line 178 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L178

Added line #L178 was not covered by tests
html_parameter = html_parameter.replace('parameter_error', error_str)
html_parameters.append(html_parameter)

Expand All @@ -200,8 +199,8 @@
range_units = 'Å⁻¹'
html_experiment = HTML_DATA_COLLECTION_TEMPLATE
html_experiment = html_experiment.replace('experiment_name', _truncate_name(experiment_name))
html_experiment = html_experiment.replace('range_min', f'{range_min}')
html_experiment = html_experiment.replace('range_max', f'{range_max}')
html_experiment = html_experiment.replace('range_min', _format_value(range_min, 2))
html_experiment = html_experiment.replace('range_max', _format_value(range_max, 2))

Check warning on line 203 in src/easyreflectometry/summary/summary.py

View check run for this annotation

Codecov / codecov/patch

src/easyreflectometry/summary/summary.py#L202-L203

Added lines #L202 - L203 were not covered by tests
html_experiment = html_experiment.replace('range_units', f'{range_units}')
html_experiment = html_experiment.replace('num_data_points', f'{num_data_points}')
html_experiment = html_experiment.replace('resolution_function', f'{resolution_function}')
Expand Down
Loading