File size: 16,531 Bytes
5c9a95b 29bbf09 5c9a95b 29bbf09 5c9a95b 29bbf09 5c9a95b | 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 | """
Turning a user's table + structures into something MEIDNet can learn from.
The steps are deliberately small and individually testable:
1. ``read_table`` โ CSV / Excel / JSON / Parquet โ DataFrame
2. ``parse_structure`` โ CIF text or file โ pymatgen Structure
3. ``align_to_prototype`` โ re-order atoms so site i matches prototype site i
4. ``featurize`` โ Structure โ fixed-length vector (the crystal modality)
5. ``load_dataset`` โ all of the above for every row, with a report of
what was kept, skipped and why.
"""
from __future__ import annotations
import os
import warnings
from collections import Counter
from dataclasses import dataclass, field
from itertools import product
import numpy as np
import pandas as pd
import torch
from pymatgen.core import Structure
from pymatgen.io.cif import CifParser
from torch.utils.data import Dataset
from meidnet.chem import ELEMENT_INDEX, NUM_SPECIES
warnings.filterwarnings("ignore", message="Issues encountered while parsing CIF")
warnings.filterwarnings("ignore", message="No\\sPauling\\selectronegativity")
# โโโโโโโโโโโโโโโโโโโโโโโโโ 1) tables โโโโโโโโโโโโโโโโโโโโโโโโโ
# Readers pandas needs an extra package for, and what to tell the user when it is missing.
_READER_HINTS = {
".xlsx": "Reading .xlsx files needs the 'openpyxl' package: pip install openpyxl",
".xls": "Reading old .xls files needs the 'xlrd' package: pip install xlrd (or save the sheet as .xlsx or CSV)",
".parquet": "Reading Parquet files needs the 'pyarrow' package: pip install pyarrow (or save the table as CSV)",
}
def read_table(path: str) -> pd.DataFrame:
ext = os.path.splitext(path)[1].lower()
try:
if ext in (".xlsx", ".xls"):
return pd.read_excel(path)
if ext == ".json":
return pd.read_json(path)
if ext == ".parquet":
return pd.read_parquet(path)
except ImportError as e:
raise ImportError(_READER_HINTS.get(ext, str(e))) from e
return pd.read_csv(path)
def normalise_id(value) -> str:
"""IDs are compared as strings; 12.0 and '12' both become '12' (as in MEIDNet v1)."""
try:
f = float(value)
if f.is_integer():
return str(int(f))
except (TypeError, ValueError):
pass
return str(value).strip()
# โโโโโโโโโโโโโโโโโโโโโโโโโ 2) structures โโโโโโโโโโโโโโโโโโโโโโโโโ
def parse_structure(cif_text: str | None = None, path: str | None = None) -> Structure:
parser = CifParser(path) if path is not None else CifParser.from_str(cif_text)
structs = parser.parse_structures(primitive=False)
if not structs:
raise ValueError("the CIF contains no structure")
s = structs[0]
while isinstance(s, list) and s:
s = s[0]
if not isinstance(s, Structure):
raise ValueError("could not read a single structure from the CIF")
return s
# โโโโโโโโโโโโโโโโโโโโโโโโโ 3) prototype alignment โโโโโโโโโโโโโโโโโโโโโโโโโ
class AlignmentError(ValueError):
pass
def _periodic_delta(a: np.ndarray) -> np.ndarray:
return a - np.round(a)
def align_to_prototype(struct: Structure, family, tol: float = 0.12) -> tuple[Structure, dict]:
"""
Re-order (and shift) the atoms of ``struct`` so that atom i sits on prototype
site i. Uses fractional coordinates in the structure's own cell: every origin
shift that puts some atom on some prototype site is tried, atoms are matched to
sites by the Hungarian algorithm, and the best match is kept. Fails (with a
reason) if the cell has a different number of atoms or any atom is farther than
``tol`` (fractional units) from its site.
"""
from scipy.optimize import linear_sum_assignment
proto = family.frac_coords
n = len(struct)
if n != len(proto):
raise AlignmentError(f"has {n} atoms but the {family.name} prototype has {len(proto)}")
frac = np.array(struct.frac_coords, dtype=np.float64)
groups = [g for g, _ in family.sites]
allowed = {g: set(family.groups[g].universe) for g in family.groups}
species = [site.specie.symbol for site in struct]
best = None
for i, k in product(range(n), range(n)):
shift = frac[i] - proto[k]
d = _periodic_delta((frac - shift)[:, None, :] - proto[None, :, :]) # (atom, site, 3)
cost = np.sqrt((d ** 2).sum(-1))
rows, cols = linear_sum_assignment(cost)
geo = cost[rows, cols]
# tie-breaker: prefer assignments that put elements in groups that list them
chem = sum(0 if (not allowed[groups[c]] or species[r] in allowed[groups[c]]) else 1 for r, c in zip(rows, cols))
key = (round(float(geo.max()), 3), chem, float(geo.sum()))
if best is None or key < best[0]:
best = (key, shift, rows.copy(), cols.copy(), d)
(gmax, chem, _), shift, rows, cols, d = best
if gmax > tol:
raise AlignmentError(f"atoms are up to {gmax:.2f} (fractional) away from the prototype sites")
order = np.empty(n, dtype=int)
order[cols] = rows # order[site] = atom index
new_frac = np.array([proto[s] + d[order[s], s] for s in range(n)])
aligned = Structure(struct.lattice, [species[order[s]] for s in range(n)], new_frac)
return aligned, {"max_offset": gmax, "elements_out_of_group": chem}
# โโโโโโโโโโโโโโโโโโโโโโโโโ 4) featurisation โโโโโโโโโโโโโโโโโโโโโโโโโ
def dense_length(max_sites: int) -> int:
return 6 + max_sites * max_sites + max_sites * NUM_SPECIES + max_sites * 3
def scale_lattice(a, b, c, alpha, beta, gamma) -> np.ndarray:
return np.array([a / 20.0, b / 20.0, c / 20.0, alpha / 180.0, beta / 180.0, gamma / 180.0], dtype=np.float32)
def featurize(struct: Structure, max_sites: int = 20, cutoff: float = 4.0) -> np.ndarray:
"""
Fixed-length vector for one structure:
[scaled lattice (6) | adjacency (NรN) | one-hot species (Nร118) | fractional coords (Nร3)],
padded with zeros up to ``max_sites`` atoms. Identical to MEIDNet v1.
"""
n = len(struct)
if n > max_sites:
raise ValueError(f"has {n} atoms, more than max_sites={max_sites}")
a, b, c = struct.lattice.abc
alpha, beta, gamma = struct.lattice.angles
lat = scale_lattice(a, b, c, alpha, beta, gamma)
adj = np.zeros((max_sites, max_sites), dtype=np.float32)
i0, i1, _, _ = struct.get_neighbor_list(r=cutoff)
for i, j in zip(i0, i1):
if i < max_sites and j < max_sites and i != j:
adj[i, j] = 1.0
spc = np.zeros((max_sites, NUM_SPECIES), dtype=np.float32)
crd = np.zeros((max_sites, 3), dtype=np.float32)
for i, site in enumerate(struct):
sym = site.specie.symbol
if sym not in ELEMENT_INDEX:
raise ValueError(f"contains '{sym}', which is not a plain element")
spc[i, ELEMENT_INDEX[sym]] = 1.0
crd[i] = site.frac_coords
return np.concatenate([lat.flatten(), adj.flatten(), spc.flatten(), crd.flatten()], axis=0)
def split_dense(x: torch.Tensor, max_sites: int) -> dict[str, torch.Tensor]:
B = x.size(0)
o = 0
lat = x[:, o:o + 6]; o += 6
adj = x[:, o:o + max_sites * max_sites].view(B, max_sites, max_sites); o += max_sites * max_sites
spc = x[:, o:o + max_sites * NUM_SPECIES].view(B, max_sites, NUM_SPECIES); o += max_sites * NUM_SPECIES
crd = x[:, o:o + max_sites * 3].view(B, max_sites, 3)
return {"lat": lat, "adj": adj, "species": spc, "coords": crd}
# โโโโโโโโโโโโโโโโโโโโโโโโโ 5) datasets โโโโโโโโโโโโโโโโโโโโโโโโโ
@dataclass
class PropertyStats:
columns: list[str]
mean: np.ndarray
std: np.ndarray
labels: list[str] = field(default_factory=list)
units: list[str] = field(default_factory=list)
minimum: np.ndarray | None = None
maximum: np.ndarray | None = None
def normalize(self, values: np.ndarray) -> np.ndarray:
return (values - self.mean) / self.std
def denormalize_tensor(self, t: torch.Tensor) -> torch.Tensor:
mean = torch.as_tensor(self.mean, dtype=t.dtype, device=t.device)
std = torch.as_tensor(self.std, dtype=t.dtype, device=t.device)
return t * std + mean
def index(self, column: str) -> int:
try:
return self.columns.index(column)
except ValueError:
raise KeyError(f"'{column}' is not one of the model's properties {self.columns}") from None
def to_dict(self) -> list[dict]:
out = []
for i, c in enumerate(self.columns):
out.append({
"column": c, "label": self.labels[i] if self.labels else c,
"unit": self.units[i] if self.units else "",
"mean": float(self.mean[i]), "std": float(self.std[i]),
"min": None if self.minimum is None else float(self.minimum[i]),
"max": None if self.maximum is None else float(self.maximum[i]),
})
return out
@classmethod
def from_dict(cls, items: list[dict]) -> "PropertyStats":
return cls(
columns=[d["column"] for d in items],
mean=np.array([d["mean"] for d in items], dtype=np.float64),
std=np.array([d["std"] for d in items], dtype=np.float64),
labels=[d.get("label", d["column"]) for d in items],
units=[d.get("unit", "") for d in items],
minimum=None if items[0].get("min") is None else np.array([d["min"] for d in items]),
maximum=None if items[0].get("max") is None else np.array([d["max"] for d in items]),
)
@dataclass
class Record:
material_id: str
dense: np.ndarray
properties: np.ndarray # raw (physical units)
formula: str
structure: object = None # pymatgen Structure, kept only with keep_structures=True
@dataclass
class DataReport:
"""What happened to every row - shown by `meidnet check` and in reports."""
source: str
rows: int = 0
kept: int = 0
skipped: Counter = field(default_factory=Counter)
examples: dict = field(default_factory=dict)
site_counts: Counter = field(default_factory=Counter)
elements: Counter = field(default_factory=Counter)
aligned: int = 0
def skip(self, reason: str, material_id: str):
self.skipped[reason] += 1
self.examples.setdefault(reason, [])
if len(self.examples[reason]) < 3:
self.examples[reason].append(material_id)
class MaterialsDataset(Dataset):
"""Torch dataset of (crystal vector, normalised property vector) pairs."""
def __init__(self, records: list[Record], stats: PropertyStats):
self.records = records
self.stats = stats
self._props = [stats.normalize(r.properties.astype(np.float64)).astype(np.float32) for r in records]
def __len__(self):
return len(self.records)
def __getitem__(self, idx):
r = self.records[idx]
return {
"crystal_vec": torch.from_numpy(r.dense).float(),
"props": torch.from_numpy(self._props[idx]),
"material_id": r.material_id,
}
def compute_stats(records: list[Record], columns, normalize_flags, labels=None, units=None) -> PropertyStats:
vals = np.stack([r.properties for r in records]).astype(np.float64)
mean = np.where(normalize_flags, vals.mean(0), 0.0)
std = np.where(normalize_flags, vals.std(0), 1.0)
std = np.where(std < 1e-12, 1.0, std)
return PropertyStats(list(columns), mean, std, list(labels or columns), list(units or [""] * len(columns)),
vals.min(0), vals.max(0))
def load_records(df: pd.DataFrame, data_cfg, base_resolve, family=None, source: str = "table",
report: DataReport | None = None, keep_structures: bool = False) -> tuple[list[Record], DataReport]:
report = report or DataReport(source=source)
cols = [p.column for p in data_cfg.properties]
missing_cols = [c for c in [data_cfg.id_column, *cols] if c not in df.columns]
if data_cfg.cif_column and data_cfg.cif_column not in df.columns and not data_cfg.structures_dir:
missing_cols.append(data_cfg.cif_column)
if missing_cols:
raise ValueError(
f"{source}: column(s) {missing_cols} not found. Available columns: {list(df.columns)}"
)
use_col = bool(data_cfg.cif_column) and data_cfg.cif_column in df.columns
sdir = base_resolve(data_cfg.structures_dir) if data_cfg.structures_dir else None
align = bool(family) and data_cfg.align_to_prototype
records = []
for _, row in df.iterrows():
report.rows += 1
mid = normalise_id(row[data_cfg.id_column])
try:
props = np.array([float(row[c]) for c in cols], dtype=np.float64)
except (TypeError, ValueError):
report.skip("non-numeric property value", mid)
continue
if not np.all(np.isfinite(props)):
report.skip("missing property value", mid)
continue
try:
if use_col and isinstance(row[data_cfg.cif_column], str):
s = parse_structure(cif_text=row[data_cfg.cif_column])
elif sdir:
if not mid or any(ch in mid for ch in "/\\:") or mid.startswith("."):
report.skip("id is not a valid file name", mid) # the file must be inside structures_dir
continue
path = os.path.join(sdir, f"{mid}.cif")
if not os.path.exists(path):
report.skip("no CIF file for this id", mid)
continue
s = parse_structure(path=path)
else:
report.skip("no structure given", mid)
continue
except Exception:
report.skip("CIF could not be read", mid)
continue
report.site_counts[len(s)] += 1
if len(s) > data_cfg.max_sites:
report.skip(f"more than max_sites={data_cfg.max_sites} atoms", mid)
continue
if not s.is_ordered: # doped / solid-solution CIFs: one element per site is needed
report.skip("partially occupied site (doped or solid solution): one element per site is needed", mid)
continue
try:
if align:
try:
s, _ = align_to_prototype(s, family, tol=data_cfg.prototype_tolerance)
report.aligned += 1
except AlignmentError:
report.skip(f"does not match the {family.name} prototype", mid)
continue
dense = featurize(s, data_cfg.max_sites, data_cfg.neighbor_cutoff)
except ValueError:
report.skip("unsupported species", mid)
continue
except Exception as e: # one odd structure must not stop the whole table
report.skip(f"structure could not be processed ({type(e).__name__})", mid)
continue
for el in s.composition.elements:
report.elements[str(el)] += 1
records.append(Record(mid, dense, props, s.composition.reduced_formula, s if keep_structures else None))
report.kept += 1
return records, report
def split_records(records: list[Record], val_fraction: float, seed: int) -> tuple[list[Record], list[Record]]:
if val_fraction <= 0 or len(records) < 10:
return records, []
rng = np.random.RandomState(seed)
idx = rng.permutation(len(records))
n_val = max(1, int(round(val_fraction * len(records))))
val = set(idx[:n_val].tolist())
return [r for i, r in enumerate(records) if i not in val], [r for i, r in enumerate(records) if i in val]
|