torchref.utils.stats module

Statistics Utilities for torchref.

Provides verbosity-aware statistics reporting across all refinement components.

Verbosity Levels

  • VERBOSITY_ESSENTIAL (0): Major weights (ADP, GEOM, Xray targets), R-factors

  • VERBOSITY_STANDARD (1): Component weights and component losses

  • VERBOSITY_DETAILED (2): Detailed stats (RMSDs, per-restraint statistics)

  • VERBOSITY_DEBUG (3): All internal parameters for debugging

Usage

from torchref.utils.stats import stat, filter_stats, VERBOSITY_STANDARD
stats = {
    'rwork': stat(0.20, VERBOSITY_ESSENTIAL),
    'bond_rmsd': stat(0.015, VERBOSITY_DETAILED),
}
filter_stats(stats, VERBOSITY_ESSENTIAL)
# {'rwork': 0.20}

# StatEntry is JSON serializable - just use json.dumps directly:
import json
json.dumps(stats, cls=StatEntryEncoder)
# '{"rwork": 0.2, "bond_rmsd": 0.015}'
class torchref.utils.stats.StatEntry(value, verbosity=1)[source]

Bases: object

A statistics entry with value and verbosity level.

JSON serializable - when serialized, only the value is written.

value

The statistic value.

Type:

Any

verbosity

Verbosity level required to show this stat.

Type:

int

value: Any
verbosity: int = 1
__json__()[source]

Return JSON-serializable representation (just the value).

__init__(value, verbosity=1)
torchref.utils.stats.stat(value, verbosity=1)[source]

Create a StatEntry with given value and verbosity.

Parameters:
  • value (Any) – The statistic value.

  • verbosity (int, optional) – Verbosity level. Default is VERBOSITY_STANDARD.

Returns:

A statistics entry object.

Return type:

StatEntry

torchref.utils.stats.filter_stats(stats, max_verbosity)[source]

Filter stats dictionary to only include entries at or below max_verbosity.

Parameters:
  • stats (dict) – Stats dictionary with StatEntry values or nested dicts.

  • max_verbosity (int) – Maximum verbosity level to include.

Returns:

Filtered stats with raw values (StatEntry unwrapped).

Return type:

dict

torchref.utils.stats.flatten_stats(stats, prefix='')[source]

Flatten nested stats dict into flat dict with dotted keys.

Parameters:
  • stats (dict) – Nested stats dictionary.

  • prefix (str, optional) – Prefix for keys. Default is ‘’.

Returns:

Flattened dictionary with dotted keys.

Return type:

dict

torchref.utils.stats.format_stats_table(stats, title='', indent=2)[source]

Format stats dictionary as a printable table.

Parameters:
  • stats (dict) – Stats dictionary (already filtered by verbosity).

  • title (str, optional) – Title for the table.

  • indent (int, optional) – Indentation spaces. Default is 2.

Returns:

Formatted table string.

Return type:

str