File size: 4,270 Bytes
3e3ee14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Plotting imports stay lazy; select the headless backend before importing pyplot.
# ruff: noqa: PLC0415

"""Ground-truth vs generated marginal plots for wandb logging."""

import torch
from torchmetrics import MetricCollection

from morph.utils.metrics.tensor import RG_RANGE


def plot_marginal_comparison(
    gen_hist: torch.Tensor,
    target_hist: torch.Tensor,
    labels: list[str] | None,
    title: str,
    xlabel: str,
    eps: float = 1e-8,
):
    """Compare ground-truth and generated marginal densities in a Figure.

    Both inputs are unnormalized histograms; they are normalized to densities here.
    """
    import matplotlib as mpl

    mpl.use("Agg", force=False)
    import matplotlib.pyplot as plt
    import numpy as np

    gen = gen_hist.detach().to("cpu", dtype=torch.float32)
    target = target_hist.detach().to("cpu", dtype=torch.float32)

    n = max(gen.numel(), target.numel())
    if gen.numel() < n:
        gen = torch.cat([gen, torch.zeros(n - gen.numel())])
    if target.numel() < n:
        target = torch.cat([target, torch.zeros(n - target.numel())])

    gen_sum = float(gen.sum())
    target_sum = float(target.sum())
    gen = gen / max(gen_sum, eps)
    target = target / max(target_sum, eps)

    if labels is None or len(labels) != n:
        labels = [str(i) for i in range(n)]

    x = np.arange(n)
    width = 0.4

    fig, ax = plt.subplots(figsize=(max(6.0, n * 0.35), 4.0))
    ax.bar(
        x - width / 2.0,
        target.numpy(),
        width,
        label="ground truth",
        color="steelblue",
        alpha=0.85,
    )
    ax.bar(
        x + width / 2.0,
        gen.numpy(),
        width,
        label="generated",
        color="darkorange",
        alpha=0.85,
    )
    ax.set_xticks(x)
    ax.set_xticklabels(labels, rotation=45 if n > 10 else 0, ha="right")
    ax.set_ylabel("probability")
    ax.set_xlabel(xlabel)
    ax.set_title(title)
    ax.legend()
    fig.tight_layout()
    return fig


# Per-distribution-metric plot captions, keyed by the collection key set in
# ``init_metrics``. Anything not listed is skipped (no caption == not plotted).
_DIST_PLOT_META = {
    "atom_count_dist_kl": ("Number of atoms: ground truth vs generated", "n_atoms"),
    "atom_type_dist_kl": ("Atom types: ground truth vs generated", "atom type"),
    "edge_type_dist_kl": (
        "Edge types (upper-tri pairs incl. NO_BOND): ground truth vs generated",
        "edge type",
    ),
    "charge_type_dist_kl": (
        "Formal charges: ground truth vs generated",
        "formal charge",
    ),
}


def build_marginal_plots(distribution_metrics: MetricCollection) -> dict:
    """One Figure per distribution metric with samples, keyed by a short name.

    Generic over the shared ``gen_hist`` / ``target`` / ``labels`` attribute
    contract of the tensor distribution metrics. Metrics that have not seen any
    samples are skipped. Caller closes the figures after logging.
    """
    plots: dict = {}
    for key, (title, xlabel) in _DIST_PLOT_META.items():
        if key not in distribution_metrics:
            continue
        m = distribution_metrics[key]
        if float(m.gen_hist.sum()) <= 0.0:
            continue
        plots[key.replace("_dist_kl", "")] = plot_marginal_comparison(
            gen_hist=m.gen_hist,
            target_hist=m.target,
            labels=m.labels,
            title=title,
            xlabel=xlabel,
        )
    return plots


def build_batch_marginal_plots(metrics, atom_tokens: list[str] | None = None):
    """GT-vs-generated plot for the geometric batch metrics that carry a
    histogram. Returns ``{wandb_key: matplotlib.Figure}``.

    Currently just radius of gyration — the 1D atom-count / atom-type marginals
    moved to the distribution collection (see ``build_marginal_plots``).
    """
    figs: dict = {}
    by_name = dict(metrics.items()) if hasattr(metrics, "items") else {}
    m = by_name.get("rog_l1")
    if m is not None and getattr(m, "gen_hist", None) is not None:
        figs["val/batch/plots/rog"] = plot_marginal_comparison(
            m.gen_hist,
            m.target_hist,
            None,
            "Radius of gyration",
            f"RoG bin (Å in [{RG_RANGE[0]}, {RG_RANGE[1]}])",
        )
    return figs