File size: 7,119 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
"""Molecular generation metrics; upstream attribution is in docs/development.md.

Package layout (split by dependency):
- ``tensor``      : geometric tensor metrics + bin edges/accumulators (no RDKit)
- ``distribution``: KL distribution metrics, batch-fed (no RDKit)
- ``chemistry``   : RDKit checks, valence-table checks, shared pool, PoseBusters
- ``plotting``    : GT-vs-generated marginal plots

Public surface re-exported here so ``from morph.utils.metrics import X``
keeps working unchanged.
"""

import torch
from torchmetrics import MetricCollection

from morph.utils.metrics.chemistry import (
    AtomStability,
    Diversity,
    MoleculeStability,
    Novelty,
    SanitizationValidity,
    Uniqueness,
    ValenceTableAtomStability,
    ValenceTableMoleculeStability,
    Validity,
    calc_atom_stabilities,
    calc_posebusters_metrics,
)
from morph.utils.metrics.distribution import (
    AtomCountKL,
    AtomTypeKL,
    ChargeTypeKL,
    EdgeTypeKL,
)
from morph.utils.metrics.plotting import build_marginal_plots
from morph.utils.metrics.tensor import build_batch_metrics

# Public surface consumed elsewhere via ``from morph.utils.metrics import X``.
__all__ = [
    "MetricCollection",
    "build_marginal_plots",
    "calc_atom_stabilities",
    "calc_metrics_",
    "init_metrics",
]


def init_metrics(
    train_smiles: list[str] | None = None,
    target_n_atoms_distribution: torch.Tensor | None = None,
    atom_type_distribution: torch.Tensor | None = None,
    edge_type_distribution: torch.Tensor | None = None,
    charge_type_distribution: torch.Tensor | None = None,
    atom_tokens: list[str] | None = None,
    edge_tokens: list[str] | None = None,
    charge_tokens: list[str] | None = None,
    allow_charged: bool = True,
    distributions=None,
    representation=None,
    train_smiles_are_canonical: bool = False,
):
    # Chemistry metrics consume the molecule population that validation has
    # already gathered onto rank zero. Their compute calls must therefore not
    # enter TorchMetrics collectives that the other ranks cannot join.
    chemistry_metric_kwargs = {"sync_on_compute": False}
    metrics = {
        "validity": Validity(allow_charged=allow_charged, **chemistry_metric_kwargs),
        "connected-sanitization-validity": SanitizationValidity(
            **chemistry_metric_kwargs
        ),
        "valence-table-atom-stability": ValenceTableAtomStability(
            **chemistry_metric_kwargs
        ),
        "valence-table-molecule-stability": ValenceTableMoleculeStability(
            **chemistry_metric_kwargs
        ),
        "uniqueness": Uniqueness(**chemistry_metric_kwargs),
        "diversity": Diversity(**chemistry_metric_kwargs),
        **(
            {
                "novelty": Novelty(
                    train_smiles,
                    already_canonical=train_smiles_are_canonical,
                    **chemistry_metric_kwargs,
                )
            }
            if train_smiles is not None
            else {}
        ),
    }

    # Distribution metrics: tensor-native KLs computed straight from the batch,
    # so they run in every representation. Kept in their own collection so they
    # accumulate across an entire validation epoch (rather than per batch) and
    # drive the ground-truth-vs-generated marginal plots. Edge/charge KLs are
    # only registered when the representation actually carries that field —
    # otherwise they are degenerate (all <NO_BOND> / neutral by projection) and
    # would log a meaningless 0.
    want_edges = representation is None or representation.requires_topology
    want_charges = representation is None or representation.requires_charges
    distribution_metrics: dict = {}
    tensor_metric_kwargs = {"sync_on_compute": False}
    if target_n_atoms_distribution is not None:
        distribution_metrics["atom_count_dist_kl"] = AtomCountKL(
            target_n_atoms_distribution, **tensor_metric_kwargs
        )
    if atom_type_distribution is not None and atom_tokens is not None:
        distribution_metrics["atom_type_dist_kl"] = AtomTypeKL(
            atom_type_distribution, labels=atom_tokens, **tensor_metric_kwargs
        )
    if edge_type_distribution is not None and edge_tokens is not None and want_edges:
        distribution_metrics["edge_type_dist_kl"] = EdgeTypeKL(
            edge_type_distribution, labels=edge_tokens, **tensor_metric_kwargs
        )
    if (
        charge_type_distribution is not None
        and charge_tokens is not None
        and want_charges
    ):
        distribution_metrics["charge_type_dist_kl"] = ChargeTypeKL(
            charge_type_distribution, labels=charge_tokens, **tensor_metric_kwargs
        )

    stability_metrics = {
        "rdkit-atom-stability": AtomStability(),
        "rdkit-molecule-stability": MoleculeStability(),
    }

    metrics = MetricCollection(metrics, compute_groups=False)
    stability_metrics = MetricCollection(stability_metrics, compute_groups=False)
    distribution_metrics = MetricCollection(distribution_metrics, compute_groups=False)

    # Geometric batch metrics — built from training-set target stats stored in
    # Distributions. Run in every representation. The collection may be empty if
    # Distributions lacks the geometric target stats.
    batch_metrics_dict: dict = {}
    if distributions is not None:
        batch_metrics_dict = build_batch_metrics(distributions)
    batch_metrics = MetricCollection(batch_metrics_dict, compute_groups=False)

    return metrics, stability_metrics, distribution_metrics, batch_metrics


def calc_metrics_(rdkit_mols, metrics, *, posebusters_contexts=None) -> dict:
    """Run an RDKit-mol metric collection over a list of mols and return the
    scalar results. Stability lives in its own collection with a different
    input contract (precomputed bools) — drive it separately. Atom-count KL
    runs tensor-side as ``AtomCountKL``.
    """
    # These molecules are gathered explicitly by validation. Compute each
    # metric without torchmetrics' implicit DDP synchronization: list-backed
    # states (uniqueness/diversity) cannot be reduced by torchmetrics, and a
    # rank-zero-only compute would otherwise deadlock on tensor-backed metrics.
    results = {}
    for name, metric in metrics.items():
        metric.reset()
        metric.update(rdkit_mols)
        value = metric.compute()
        results[name] = value.item() if isinstance(value, torch.Tensor) else value
    try:
        kwargs = (
            {"scaffold_contexts": posebusters_contexts}
            if posebusters_contexts is not None
            else {}
        )
        posebusters = calc_posebusters_metrics(rdkit_mols, **kwargs)
    except Exception as exc:
        # PoseBusters is diagnostic and must not discard the core chemistry
        # metrics or abort a long training run if one structural check fails.
        print(f"Error calculating PoseBusters metrics: {exc}")
    else:
        results.update(
            {f"posebusters/{name}": value for name, value in posebusters.items()}
        )
    return results