-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanifold_distance.py
More file actions
519 lines (431 loc) · 19.1 KB
/
manifold_distance.py
File metadata and controls
519 lines (431 loc) · 19.1 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
"""
Manifold Distance–Based Partitioning of Generated Structures
Classifies generated structures into four categories based on their position
relative to the reference (training) manifold in MLIP embedding space:
i) Dense interior — inside boundary, density ≥ 20th percentile, LOF ≥ -2.0
ii) Sparse interior — inside boundary, density < 20th percentile, LOF ≥ -2.0
iii) Near exterior — outside boundary, LOF ≥ -2.0
iv) Far exterior — LOF < -2.0 (Structural Hallucination)
Input is structure-level descriptors obtained by mean-pooling per-atom embeddings
from any MLIP extractor (e.g. MLIPEmbeddingExtractor from mlip_embedding_extractor.py).
Usage:
from manifold_distance import ManifoldClassifier
from mlip_embedding_extractor import MLIPEmbeddingExtractor
# 1. Extract embeddings
extractor = MLIPEmbeddingExtractor(model_type="mace", model_path="...", device="cuda")
ref_result = extractor.extract_from_csv("mp-20/train.csv")
gen_result = extractor.extract_from_csv("generated.csv")
# 2. Fit classifier on reference set
classifier = ManifoldClassifier(n_neighbors=10, lof_threshold=-1.5, sparse_percentile=20.0)
classifier.fit_from_embedding_result(ref_result)
# 3. Classify generated structures
results = classifier.classify_from_embedding_result(gen_result)
# 4. Inspect
classifier.print_summary(results)
df = classifier.to_dataframe(results)
"""
import warnings
from dataclasses import dataclass, field
from typing import Dict, List, Optional, Tuple
import numpy as np
from sklearn.decomposition import PCA
from sklearn.neighbors import LocalOutlierFactor, NearestNeighbors
from sklearn.preprocessing import StandardScaler
from sklearn.svm import OneClassSVM
try:
import pandas as pd
HAS_PANDAS = True
except ImportError:
HAS_PANDAS = False
# =============================================================================
# Category Definitions
# =============================================================================
CATEGORIES = {
"dense_interior": {
"description": "Inside boundary, dense region — well-supported candidate",
"color": "#2ecc71", # green
},
"sparse_interior": {
"description": "Inside boundary, sparse region — novel but locally consistent",
"color": "#3498db", # blue
},
"near_exterior": {
"description": "Outside boundary, LOF-consistent — exploration candidate",
"color": "#9b59b6", # purple
},
"far_exterior": {
"description": "LOF outlier — likely unphysical (Structural Hallucination)",
"color": "#e74c3c", # red
},
}
# =============================================================================
# Data Classes
# =============================================================================
@dataclass
class StructureMetrics:
"""Per-structure manifold metrics."""
# Identification
index: int
material_id: str
# Metric 1: manifold distance (z-score normalised mean kNN distance)
manifold_distance: float
# Metric 2: depth score (0 = centroid, 1 = periphery)
depth_score: float
# Metric 3: signed boundary distance (+ inside, – outside)
boundary_distance: float
# Metric 4: local density and its percentile vs. reference
local_density: float
density_percentile: float
# Metric 5: LOF score (≈ -1 normal, << -1 outlier)
lof_score: float
# Classification
category: str
nearest_reference_ids: List[str] = field(default_factory=list)
@dataclass
class ClassificationSummary:
"""Aggregate statistics for one category."""
category: str
description: str
count: int
percentage: float
material_ids: List[str]
# =============================================================================
# Utility: mean-pool atom embeddings → structure embeddings
# =============================================================================
def mean_pool(
atom_embeddings: List[np.ndarray],
material_ids: Optional[List[str]] = None,
) -> Tuple[np.ndarray, List[str]]:
"""
Collapse per-atom embeddings to one vector per structure via mean-pooling.
Args:
atom_embeddings: List of (n_atoms_i, embed_dim) arrays, one per structure.
Accepts the `.embeddings` field of EmbeddingResult directly.
material_ids: Optional list of IDs; auto-generated if None.
Returns:
structure_embeddings: (n_structures, embed_dim) float32 array
ids: list of material IDs
"""
pooled = []
valid_ids = []
ids = material_ids or [f"struct_{i}" for i in range(len(atom_embeddings))]
for i, emb in enumerate(atom_embeddings):
if emb is None or len(emb) == 0:
warnings.warn(f"Skipping structure {ids[i]}: empty embedding.")
continue
pooled.append(np.mean(emb, axis=0))
valid_ids.append(ids[i])
return np.array(pooled, dtype=np.float32), valid_ids
# =============================================================================
# Main Classifier
# =============================================================================
class ManifoldClassifier:
"""
Manifold distance–based classifier for generated crystal structures.
Fits a reference manifold from training-set embeddings and classifies
generated structures into four categories based on boundary position,
local density, and Local Outlier Factor.
Example:
>>> clf = ManifoldClassifier(n_neighbors=10)
>>> clf.fit(reference_embeddings, reference_ids)
>>> results = clf.classify(generated_embeddings, generated_ids)
>>> clf.print_summary(results)
"""
def __init__(
self,
n_neighbors: int = 30,
lof_threshold: float = -2.0,
sparse_percentile: float = 20.0,
svm_nu: float = 0.1,
):
"""
Args:
n_neighbors: k for kNN density / LOF computation.
Default matches ManifoldFishAnalyzer(n_neighbors=30).
lof_threshold: LOF scores below this → far_exterior / hallucination.
Default matches ManifoldFishAnalyzer(outlier_threshold=-2).
sparse_percentile: Density percentile below this → sparse region.
svm_nu: OneClassSVM nu parameter (expected outlier fraction).
"""
self.n_neighbors = n_neighbors
self.lof_threshold = lof_threshold
self.sparse_percentile = sparse_percentile
self.svm_nu = svm_nu
self._is_fitted = False
# -------------------------------------------------------------------------
# Fitting (reference manifold)
# -------------------------------------------------------------------------
def fit(
self,
reference_embeddings: np.ndarray,
reference_ids: Optional[List[str]] = None,
) -> "ManifoldClassifier":
"""
Fit all reference models.
Args:
reference_embeddings: (n_ref, embed_dim) structure-level descriptors.
reference_ids: Optional list of reference material IDs.
"""
ref = np.asarray(reference_embeddings, dtype=np.float32)
n_ref, dim = ref.shape
self._reference = ref
self._dim = dim
self._reference_ids = reference_ids or [f"ref_{i}" for i in range(n_ref)]
k = min(self.n_neighbors, n_ref - 1)
# kNN for distance / density
self._knn = NearestNeighbors(n_neighbors=k, metric="euclidean")
self._knn.fit(ref)
# Reference distance statistics (for z-score normalisation)
ref_dists, _ = self._knn.kneighbors(ref)
self._ref_mean_dist = float(np.mean(ref_dists[:, -1]))
self._ref_std_dist = float(np.std(ref_dists[:, -1])) + 1e-8
# Reference density distribution (for percentile ranking)
ref_densities = self._density_from_distances(ref_dists)
self._ref_densities_sorted = np.sort(ref_densities)
# Centroid / depth normalisation
self._centroid = ref.mean(axis=0)
self._max_centroid_dist = float(
np.max(np.linalg.norm(ref - self._centroid, axis=1))
) + 1e-8
# LOF (novelty mode — scores unseen points)
# random_state is not used by LOF itself (kNN is deterministic),
# but setting it future-proofs against sklearn version changes.
self._lof = LocalOutlierFactor(
n_neighbors=k,
novelty=True,
contamination=0.1,
)
self._lof.fit(ref) # fully deterministic given fixed data + n_neighbors
# Boundary model
# OneClassSVM works for any dimensionality.
# For dim ≤ 3 a Delaunay triangulation would suffice, but the typical
# embedding space (128–512 dims) always uses SVM.
self._svm_scaler = StandardScaler()
ref_scaled = self._svm_scaler.fit_transform(ref)
self._svm = OneClassSVM(kernel="rbf", gamma="auto", nu=self.svm_nu)
self._svm.fit(ref_scaled)
ref_decision = self._svm.decision_function(ref_scaled)
self._svm_decision_std = float(np.std(ref_decision)) + 1e-8
self._is_fitted = True
return self
def fit_from_embedding_result(
self,
embedding_result,
element_filter: Optional[int] = None,
) -> "ManifoldClassifier":
"""
Convenience wrapper: fit directly from an EmbeddingResult object
(output of MLIPEmbeddingExtractor).
Args:
embedding_result: EmbeddingResult with .embeddings and .material_ids fields.
element_filter: Optional atomic number; if set, mean-pools only atoms
of that element (useful for element-specific analysis).
"""
atom_embs = embedding_result.embeddings
ids = embedding_result.material_ids
if element_filter is not None:
atomic_nums = embedding_result.atomic_numbers
filtered = []
for emb, zs in zip(atom_embs, atomic_nums):
mask = np.asarray(zs) == element_filter
filtered.append(emb[mask] if mask.any() else emb)
atom_embs = filtered
struct_embs, valid_ids = mean_pool(atom_embs, ids)
return self.fit(struct_embs, valid_ids)
# -------------------------------------------------------------------------
# Classification (generated structures)
# -------------------------------------------------------------------------
def classify(
self,
generated_embeddings: np.ndarray,
material_ids: Optional[List[str]] = None,
) -> List[StructureMetrics]:
"""
Classify generated structures against the fitted reference manifold.
Args:
generated_embeddings: (n_gen, embed_dim) structure-level descriptors.
material_ids: Optional list of generated material IDs.
Returns:
List of StructureMetrics, one per generated structure.
"""
if not self._is_fitted:
raise RuntimeError("Call fit() or fit_from_embedding_result() first.")
gen = np.asarray(generated_embeddings, dtype=np.float32)
n_gen = len(gen)
ids = material_ids or [f"gen_{i}" for i in range(n_gen)]
# --- Metric 1: manifold distance (z-score) ---
dists, neighbor_idx = self._knn.kneighbors(gen)
mean_dists = dists.mean(axis=1)
manifold_distance = (mean_dists - self._ref_mean_dist) / self._ref_std_dist
# --- Metric 2: depth score ---
centroid_dists = np.linalg.norm(gen - self._centroid, axis=1)
depth_score = centroid_dists / self._max_centroid_dist
# --- Metric 3: boundary distance (SVM decision function, normalised) ---
gen_scaled = self._svm_scaler.transform(gen)
svm_decision = self._svm.decision_function(gen_scaled)
boundary_distance = svm_decision / self._svm_decision_std
# --- Metric 4: local density and density percentile ---
local_density = self._density_from_distances(dists)
positions = np.searchsorted(self._ref_densities_sorted, local_density, side="right")
density_percentile = 100.0 * positions / len(self._ref_densities_sorted)
# --- Metric 5: LOF score ---
lof_score = self._lof.score_samples(gen)
# --- Classify each structure ---
results = []
for i in range(n_gen):
category = self._classify_one(
boundary_distance=float(boundary_distance[i]),
density_percentile=float(density_percentile[i]),
lof_score=float(lof_score[i]),
)
nearest_ref_ids = [self._reference_ids[j] for j in neighbor_idx[i]]
results.append(StructureMetrics(
index=i,
material_id=ids[i],
manifold_distance=float(manifold_distance[i]),
depth_score=float(depth_score[i]),
boundary_distance=float(boundary_distance[i]),
local_density=float(local_density[i]),
density_percentile=float(density_percentile[i]),
lof_score=float(lof_score[i]),
category=category,
nearest_reference_ids=nearest_ref_ids,
))
return results
def classify_from_embedding_result(
self,
embedding_result,
element_filter: Optional[int] = None,
) -> List[StructureMetrics]:
"""
Convenience wrapper: classify directly from an EmbeddingResult object.
Args:
embedding_result: EmbeddingResult with .embeddings and .material_ids.
element_filter: Optional atomic number for element-specific pooling.
"""
atom_embs = embedding_result.embeddings
ids = embedding_result.material_ids
if element_filter is not None:
atomic_nums = embedding_result.atomic_numbers
filtered = []
for emb, zs in zip(atom_embs, atomic_nums):
mask = np.asarray(zs) == element_filter
filtered.append(emb[mask] if mask.any() else emb)
atom_embs = filtered
struct_embs, valid_ids = mean_pool(atom_embs, ids)
return self.classify(struct_embs, valid_ids)
# -------------------------------------------------------------------------
# Internal helpers
# -------------------------------------------------------------------------
@staticmethod
def _density_from_distances(dists: np.ndarray) -> np.ndarray:
"""Reciprocal of mean kNN distance as local density estimate."""
return 1.0 / (dists.mean(axis=1) + 1e-8)
def _classify_one(
self,
boundary_distance: float,
density_percentile: float,
lof_score: float,
) -> str:
"""
Decision tree for four-category classification.
Priority order:
1. LOF < lof_threshold → far_exterior (Structural Hallucination)
2. boundary_distance < 0 → near_exterior
3. density_percentile < sparse_percentile → sparse_interior
4. otherwise → dense_interior
"""
if lof_score < self.lof_threshold:
return "far_exterior"
if boundary_distance < 0:
return "near_exterior"
if density_percentile < self.sparse_percentile:
return "sparse_interior"
return "dense_interior"
# -------------------------------------------------------------------------
# Reporting
# -------------------------------------------------------------------------
def summarise(self, results: List[StructureMetrics]) -> Dict[str, ClassificationSummary]:
"""Return per-category counts and material IDs."""
from collections import defaultdict
buckets: Dict[str, List[str]] = defaultdict(list)
for r in results:
buckets[r.category].append(r.material_id)
n_total = len(results)
summary = {}
for cat in CATEGORIES:
ids = buckets.get(cat, [])
summary[cat] = ClassificationSummary(
category=cat,
description=CATEGORIES[cat]["description"],
count=len(ids),
percentage=100.0 * len(ids) / n_total if n_total else 0.0,
material_ids=ids,
)
return summary
def print_summary(self, results: List[StructureMetrics]) -> None:
"""Print a human-readable summary table."""
summary = self.summarise(results)
n_total = len(results)
print(f"\nManifold classification — {n_total} structures")
print(f"{'Category':<20} {'Count':>7} {'%':>7} Description")
print("-" * 72)
for cat, s in summary.items():
print(f"{cat:<20} {s.count:>7} {s.percentage:>6.1f}% {s.description}")
print()
def to_dataframe(self, results: List[StructureMetrics]):
"""Convert results to a pandas DataFrame (requires pandas)."""
if not HAS_PANDAS:
raise ImportError("pandas is required for to_dataframe(). pip install pandas")
rows = []
for r in results:
rows.append({
"material_id": r.material_id,
"category": r.category,
"manifold_distance": r.manifold_distance,
"depth_score": r.depth_score,
"boundary_distance": r.boundary_distance,
"local_density": r.local_density,
"density_percentile": r.density_percentile,
"lof_score": r.lof_score,
})
return pd.DataFrame(rows)
def get_ids_by_category(
self,
results: List[StructureMetrics],
category: str,
) -> List[str]:
"""Return material IDs for a given category."""
return [r.material_id for r in results if r.category == category]
# =============================================================================
# Convenience functions
# =============================================================================
def classify_structures(
reference_embeddings: np.ndarray,
generated_embeddings: np.ndarray,
reference_ids: Optional[List[str]] = None,
generated_ids: Optional[List[str]] = None,
n_neighbors: int = 30,
lof_threshold: float = -2.0,
sparse_percentile: float = 20.0,
) -> List[StructureMetrics]:
"""
One-shot classification of generated structures against a reference set.
Args:
reference_embeddings: (n_ref, embed_dim) structure-level descriptors.
generated_embeddings: (n_gen, embed_dim) structure-level descriptors.
reference_ids: Optional reference material IDs.
generated_ids: Optional generated material IDs.
n_neighbors: k for kNN / LOF.
lof_threshold: LOF cut-off for hallucination detection.
sparse_percentile: Density percentile threshold for sparse regions.
Returns:
List of StructureMetrics.
"""
clf = ManifoldClassifier(
n_neighbors=n_neighbors,
lof_threshold=lof_threshold,
sparse_percentile=sparse_percentile,
)
clf.fit(reference_embeddings, reference_ids)
return clf.classify(generated_embeddings, generated_ids)