MonitorCollection

class jaxrens.postprocess.collection.MonitorCollection(monitors)[source]

Bases: object

Aggregate multiple Monitor objects (e.g. a pressure sweep cohort).

Parameters:

monitors (list[Monitor]) – Initial list of Monitor objects.

classmethod from_directories(paths, labels=None, *, prefix='ns')[source]

Build a collection by loading one Monitor per directory.

Parameters:
  • paths (list[Path | str]) – Per-run directories.

  • labels (list[str] | None) – Display labels, one per path. Defaults to directory names.

  • prefix (str) – Checkpoint file prefix.

Returns:

Populated MonitorCollection.

Return type:

MonitorCollection

classmethod from_multi_run_directory(path, *, prefix=None, prefer_final=True, labels=None, config=None)[source]

Build a collection from a single multi-run output directory.

Multi-run NS (run_ns_parallel / run_ns_multi_gpu) writes one combined checkpoint with leading batch shape (n_runs,) or (G, P) plus per-replica <prefix>.runNN.energies files. This constructor flattens the leading batch axes, reads each per-replica energy log, and produces one Monitor per replica.

Convention: stored energies are treated as the full thermal variable (dead_volumes = live_volumes = None). When the run used an EnsembleTerm, the stored energy is the NPT enthalpy H = U + P*V, so leaving volumes None yields log Z_NPT / Cp / Gibbs G directly from the standard thermodynamics functions (passing the bare cell volume column would double-count the P*V term). The bare cell-volume column from each .energies log is stashed on each Monitor as volume_trace so callers can compute <V>(T) separately.

For single-run checkpoints (no batch axis) this builds a one-element collection using the same convention as the multi-run path (reading the <prefix>.energies log, dead_volumes=None, bare volume stashed as volume_trace) rather than delegating to Monitor.from_directory, so single- and multi-replica collections behave identically.

Config auto-discovery: when config is None the constructor looks for config.yaml in path and then in path.parent (the standard experiment_dir/output/ layout). A found config supplies the run prefix (output.out_file_prefix) and — for an NPT pressure sweep whose pressure-list length matches the replica count — per-replica labels and pressure_gpa / pressure_eva3 attributes attached to each Monitor. Pass prefix= / labels= explicitly to override the inferred values, or hand a specific path via config= to skip auto-discovery. When no config is found we fall back to prefix='ns' and runNN labels.

Parameters:
  • path (Path | str) – Directory produced by jaxrens run on a multi-run config.

  • prefix (str | None) – File prefix matching output.out_file_prefix. If None (default), inferred from the config when present; otherwise falls back to "ns".

  • prefer_final (bool) – Prefer <prefix>.final.checkpoint.h5 over the periodic <prefix>.checkpoint.h5; falls back further to <prefix>.initial.checkpoint.h5 when the run is still in flight. Zero-byte files (mid-write) are skipped.

  • labels (list[str] | None) – Per-replica display labels, length n_total. If None, inferred from the config (e.g. NPT pressures); otherwise falls back to f"run{i:02d}".

  • config (Path | str | None) – Explicit path to the run’s YAML config. When None, auto-discovery looks in path then path.parent.

Returns:

Populated MonitorCollection.

Raises:
  • FileNotFoundError – If no usable checkpoint is found, or if any <prefix>.runNN.energies file is missing.

  • ValueError – If labels is supplied with the wrong length, or if the checkpoint’s leading batch shape is unrecognised.

Return type:

MonitorCollection

add(monitor)[source]

Append a Monitor to the collection.

Parameters:

monitor (Monitor) – Monitor to add.

Return type:

None

remove(label)[source]

Remove the first Monitor whose label matches.

Parameters:

label (str) – Label to match.

Raises:

KeyError – If no monitor with that label exists.

Return type:

None

plot_energy_trace(*, ax=None, **kwargs)[source]

Overlay energy traces for all monitors on a single axes.

Parameters:
  • ax – Existing axes. Created if None.

  • **kwargs – Forwarded to each plot_energy_trace call.

Returns:

The Axes object.

plot_heat_capacity(T, *, ax=None, **kwargs)[source]

Overlay C_v curves for all monitors.

Parameters:
  • T (ndarray) – Temperature array.

  • ax – Existing axes. Created if None.

  • **kwargs – Forwarded to each plot_heat_capacity call.

Returns:

The Axes object.

plot_partition_function(T, *, ax=None, **kwargs)[source]

Overlay log Z curves for all monitors.

Parameters:
  • T (ndarray) – Temperature array.

  • ax – Existing axes. Created if None.

  • **kwargs – Forwarded to each plot_partition_function call.

Returns:

The Axes object.

plot_free_energy(T, *, ax=None, **kwargs)[source]

Overlay free energy curves for all monitors.

Parameters:
  • T (ndarray) – Temperature array.

  • ax – Existing axes. Created if None.

  • **kwargs – Forwarded to each plot_free_energy call.

Returns:

The Axes object.

plot_log_evidence_trace(*, ax=None, **kwargs)[source]

Overlay cumulative log Z traces for all monitors.

Parameters:
  • ax – Existing axes. Created if None.

  • **kwargs – Forwarded to each plot_log_evidence_trace call.

Returns:

The Axes object.

plot_step_sizes(*, ax=None, per_run=False, **kwargs)[source]

Plot per-move step-size adaptation trace for the cohort.

Uses the collection-level self.adaptation_trace populated by from_multi_run_directory (one shared .adaptation.h5 for the whole cohort, indexed along an n_runs axis). Falls back to per-monitor traces when the cohort-level trace is unavailable (e.g. collections built via from_directories).

Parameters:
  • ax – Existing axes. Created if None.

  • per_run (bool) – When True, draws one line per (move, replica) combination; when False, draws mean ± std across replicas.

  • **kwargs – Forwarded to the underlying plotting function.

Returns:

The Axes object.

plot_heatmap(T, observable='heat_capacity', *, ax=None, cmap='viridis', fmt='PT', vmin=None, vmax=None, do_colorbar=True, cbar_label=None, T_label='T', P_label=None, pressure_attr='auto', **obs_kwargs)[source]

Render a pressure-temperature heatmap of an observable.

Each monitor in the collection contributes one row of the (n_P, n_T) grid. Pressure values are taken from the pressure_gpa or pressure_eva3 attribute set by from_multi_run_directory when a config is found; when neither is present we fall back to ordinal replica indices (the heatmap then functions purely as a 2D overlay). Monitors are sorted by pressure before plotting so pcolormesh sees a monotonic axis.

Parameters:
  • T (ndarray) – Temperature grid, shape (n_T,). Passed to each monitor’s observable method.

  • observable (str | Callable[[...], ndarray]) – One of "heat_capacity", "free_energy", "log_partition_function"/"log_Z", "volume", or a callable (monitor, T, **obs_kwargs) -> array (n_T,).

  • ax – Existing axes to plot into. Created if None.

  • cmap (str) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • fmt (str) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • vmin (float | None) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • vmax (float | None) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • do_colorbar (bool) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • cbar_label (str | None) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • T_label (str) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • P_label (str | None) – Forwarded to plot_heatmap(). P_label defaults to a unit-aware string when pressure_attr is resolved to a named field.

  • pressure_attr (str) – "auto" (default) prefers pressure_gpa then pressure_eva3 then ordinal indices; pass an explicit attribute name to force a choice, or "index" for replica indices.

  • **obs_kwargs – Forwarded to the observable function (e.g. k_B=8.617e-5).

Returns:

The Axes object.

plot_acceptance_rates(*, ax=None, per_run=False, **kwargs)[source]

Plot per-move acceptance-rate trace for the cohort.

Uses the cohort-wide self.adaptation_trace (set by from_multi_run_directory) when present, falling back to per-monitor traces otherwise.

Parameters:
  • ax – Existing axes. Created if None.

  • per_run (bool) – When True, one line per (move, replica); when False, mean ± std across replicas.

  • **kwargs – Forwarded to the underlying plotting function.

Returns:

The Axes object.

summary()[source]

Return a dict summarising all monitors.

Returns:

Dict with keys 'labels', 'n_dead', 'log_evidence', 'is_npt', each mapping to a list of length len(self).

Return type:

dict