File size: 17,077 Bytes
e94bdab | 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 | """Integrate experimental Li solid-electrolyte conductivity data.
Two separate, independently curated databases are supported:
1. **Hargreaves et al. 2023** — npj Computational Materials
~820 entries, 403 compositions, 214 sources
https://doi.org/10.1038/s41524-023-01137-3
2. **OBELiX (Therrien et al. 2025, NRC-Mila)**
~599 entries, curated with leakage-resistant splits
pip install obelix-data
https://github.com/nrc-mila/OBELiX
These are complementary — not duplicates — and are tracked as two separate
provenance sources with distinct citations.
Usage:
# Hargreaves 2023
python scripts/integrate_experimental_data.py --ransom-path path/to/ransom2023.csv
# OBELiX via pip package
python scripts/integrate_experimental_data.py --obelix
# Both
python scripts/integrate_experimental_data.py --ransom-path ... --obelix
# Dry run
python scripts/integrate_experimental_data.py --dry-run
"""
import json, os, sys, time, argparse, csv, io, re, subprocess
from pathlib import Path
from collections import defaultdict
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
WIDTH = 60
RANSOM_URLS = [
"https://raw.githubusercontent.com/nrc-cnrc/ransom2023-conductivity/main/data/conductivity_database.csv",
]
HARGREAVES_DOI = "https://doi.org/10.1038/s41524-022-00951-z"
OBELIX_DOI = "https://github.com/nrc-mila/OBELiX"
def parse_formula(formula):
parts = re.findall(r'([A-Z][a-z]*)(\d*\.?\d*)', formula)
return {el: float(cnt) if cnt else 1.0 for el, cnt in parts}
def formula_similarity(f1, f2):
d1 = parse_formula(f1)
d2 = parse_formula(f2)
if set(d1.keys()) != set(d2.keys()):
return False
total1, total2 = sum(d1.values()), sum(d2.values())
for el in d1:
r1 = d1[el] / total1
r2 = d2[el] / total2
if abs(r1 - r2) > 0.05:
return False
return True
def try_fetch_ransom():
"""Try to download Hargreaves 2023 database."""
import urllib.request
for url in RANSOM_URLS:
try:
req = urllib.request.Request(url, headers={"User-Agent": "Scandium-Labs/1.0"})
with urllib.request.urlopen(req, timeout=30) as resp:
data = resp.read().decode("utf-8")
print(f" Downloaded {len(data):,} bytes")
return data
except Exception as e:
print(f" Failed: {str(e)[:80]}")
return None
def try_fetch_obelix_package():
"""Try to install obelix-data package and load data."""
try:
import obelix
ob = obelix.OBELiX(data_path="/tmp/obelix_rawdata", no_cifs=True)
n = len(ob.dataframe)
print(f" OBELiX package loaded: {n} entries")
return ob
except ImportError:
print(" obelix-data not installed. Attempting pip install...")
result = subprocess.run(
[sys.executable, "-m", "pip", "install", "obelix-data"],
capture_output=True, text=True, timeout=60
)
if result.returncode == 0:
try:
import obelix
ob = obelix.OBELiX(data_path="/tmp/obelix_rawdata", no_cifs=True)
n = len(ob.dataframe)
print(f" OBELiX installed and loaded: {n} entries")
return ob
except Exception as e:
print(f" Load failed after install: {e}")
return None
else:
print(f" Install failed: {result.stderr[-200:]}")
return None
def parse_ransom_csv(csv_data):
"""Parse Hargreaves 2023 CSV into entry dicts."""
reader = csv.DictReader(io.StringIO(csv_data))
entries = []
for i, row in enumerate(reader):
entry = {
"source": "Hargreaves2023",
"source_id": f"Hargreaves2023-{i:04d}",
"is_experimental": True,
"experimental_database": "Hargreaves2023",
"provenance": {
"source": "Hargreaves2023",
"source_id": f"Hargreaves2023-{i:04d}",
"doi": HARGREAVES_DOI,
"integrated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
},
}
formula = row.get("Formula", row.get("formula", "")).strip()
if formula:
entry["formula"] = formula
entry["structured_formula"] = formula
entry["elements"] = list(parse_formula(formula).keys())
entry["carrier_elements"] = ["Li"]
for field in ["Conductivity_S_cm", "conductivity_S_cm", "Conductivity (S/cm)"]:
val = row.get(field, "").strip()
if val:
try:
entry["conductivity_S_cm"] = float(val)
except ValueError:
pass
for field in ["Ea_eV", "activation_energy_eV", "Activation energy (eV)"]:
val = row.get(field, "").strip()
if val:
try:
entry["activation_energy_eV"] = float(val)
except ValueError:
pass
for field in ["Temperature_K", "temperature_K", "Temperature (K)"]:
val = row.get(field, "").strip()
if val:
try:
entry["temperature_K"] = float(val)
except ValueError:
pass
ref = row.get("Reference", row.get("reference", "")).strip()
if ref:
entry["reference"] = ref
entry["provenance"]["experimental_reference"] = ref
entries.append(entry)
return entries
def parse_obelix_via_package(obelix_obj):
"""Parse OBELiX data via pandas DataFrame."""
entries = []
try:
df = obelix_obj.dataframe
for idx, row in df.iterrows():
formula = str(row.get("Reduced Composition", ""))
true_comp = str(row.get("True Composition", ""))
conductivity = row.get("Ionic conductivity (S cm-1)")
doi = str(row.get("DOI", ""))
family = str(row.get("Family", ""))
icsd = row.get("ICSD ID")
sg = str(row.get("Space group", ""))
entry = {
"source": "OBELiX",
"source_id": f"OBELiX-{idx}",
"is_experimental": True,
"experimental_database": "OBELiX_Therrien2025",
"formula": formula,
"structured_formula": true_comp if (true_comp and true_comp != "nan") else formula,
"elements": list(parse_formula(formula).keys()) if formula else [],
"carrier_elements": ["Li"],
"conductivity_S_cm": float(conductivity) if pd.notna(conductivity) else None,
"space_group": sg if sg != "nan" else "",
"sse_family": family if family != "nan" else "",
"reference": doi if doi != "nan" else "",
"provenance": {
"source": "OBELiX_Therrien2025",
"source_id": f"OBELiX-{idx}",
"doi": "https://github.com/nrc-mila/OBELiX",
"icsd_id": str(icsd) if pd.notna(icsd) else "",
"integrated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
},
}
entries.append(entry)
except Exception as e:
print(f" OBELiX DataFrame parse error: {e}")
return entries
def cross_reference_and_add(exp_entries, all_dataset_entries):
"""Cross-reference experimental entries with the existing dataset."""
formula_index = defaultdict(list)
for e in all_dataset_entries:
sf = e.get("structured_formula", e.get("formula", ""))
formula_index[sf].append(e)
matched = 0
unmatched = 0
conductivity_added = 0
new_entries = []
for exp_e in exp_entries:
exp_formula = exp_e.get("formula", "")
matched_entries = formula_index.get(exp_formula, [])
if not matched_entries:
for sf, existing in formula_index.items():
if formula_similarity(exp_formula, sf):
matched_entries = existing
break
db_name = exp_e.get("experimental_database", "unknown")
if matched_entries:
matched += 1
for existing_e in matched_entries:
if "ssb_screening" not in existing_e:
existing_e["ssb_screening"] = {}
cond = exp_e.get("conductivity_S_cm")
ea = exp_e.get("activation_energy_eV")
if cond is not None:
existing_e["ssb_screening"]["estimated_ionic_conductivity_S_cm"] = cond
existing_e["ssb_screening"]["conductivity_source"] = f"experimental_{db_name}"
conductivity_added += 1
if ea is not None:
existing_e["ssb_screening"]["experimental_activation_energy_eV"] = ea
existing_e["is_experimental"] = True
if "provenance" not in existing_e:
existing_e["provenance"] = {}
existing_e["provenance"]["experimental_confirmed"] = True
existing_e["provenance"]["experimental_database"] = db_name
existing_e["provenance"]["experimental_reference"] = exp_e.get("reference", "")
else:
unmatched += 1
new_entry = {
"source": exp_e.get("source", "experimental"),
"source_id": exp_e.get("source_id", f"exp-{unmatched}"),
"formula": exp_formula,
"structured_formula": exp_formula,
"elements": exp_e.get("elements", []),
"nsites": len(exp_e.get("elements", [])),
"band_gap": None,
"formation_energy_per_atom": None,
"energy_above_hull": None,
"is_experimental": True,
"families": ["experimental_SSE"],
"sse_family": "experimental",
"mobile_ion": "Li",
"carrier_elements": ["Li"],
"tier": "experimental_gold",
"quality_score": 95,
"quality_flags": ["experimental_data", "has_conductivity"],
"ssb_screening": {
"estimated_ionic_conductivity_S_cm": exp_e.get("conductivity_S_cm"),
"conductivity_source": f"experimental_{db_name}",
"experimental_activation_energy_eV": exp_e.get("activation_energy_eV"),
"measurement_temperature_K": exp_e.get("temperature_K"),
"mobile_ion": "Li",
"sse_family": "experimental",
"gates_passed": ["experimental"],
"sse_candidate_score": 100,
},
"provenance": exp_e.get("provenance", {}),
"license": "CC-BY-4.0",
}
new_entries.append(new_entry)
return matched, unmatched, conductivity_added, new_entries
def main():
parser = argparse.ArgumentParser(description="Integrate experimental conductivity data")
parser.add_argument("--ransom-path", type=str, default=None,
help="Path to Hargreaves 2023 CSV file")
parser.add_argument("--obelix", action="store_true",
help="Try to load OBELiX via obelix-data package")
parser.add_argument("--dry-run", action="store_true")
parser.add_argument("--cross-ref-only", action="store_true")
args = parser.parse_args()
if not args.ransom_path and not args.obelix:
print("Specify at least one data source:")
print(" --ransom-path <file.csv> Hargreaves et al. 2023 database")
print(" --obelix OBELiX via obelix-data package")
sys.exit(1)
BASE_DIR = Path(__file__).resolve().parent.parent
DATASET_PATH = BASE_DIR / "dataset"
print("=" * WIDTH)
print(" EXPERIMENTAL DATA INTEGRATION")
print("=" * WIDTH)
all_experimental = []
# --- Hargreaves 2023 ---
if args.ransom_path:
source_label = "Hargreaves et al. 2023 (npj Comput. Mater.)"
print(f"\n [{source_label}]")
ransom_data = None
path = Path(args.ransom_path)
if path.exists():
with open(path) as f:
ransom_data = f.read()
print(f" Loaded from {path}")
else:
print(f" File not found: {path}")
print(" Attempting download...")
ransom_data = try_fetch_ransom()
if ransom_data:
entries = parse_ransom_csv(ransom_data)
print(f" Parsed {len(entries):,} entries")
for e in entries:
e["experimental_database"] = "Hargreaves2023"
all_experimental.extend(entries)
with_cond = sum(1 for e in entries if e.get("conductivity_S_cm") is not None)
with_ea = sum(1 for e in entries if e.get("activation_energy_eV") is not None)
print(f" With conductivity: {with_cond}")
print(f" With activation energy: {with_ea}")
else:
print(f" Could not load Hargreaves 2023 data.")
print(f" Download manually from: {HARGREAVES_DOI}")
# --- OBELiX Therrien 2025 ---
if args.obelix:
source_label = "OBELiX (Therrien et al. 2025, NRC-Mila)"
print(f"\n [{source_label}]")
print(" Attempting obelix-data package...")
ob_data = try_fetch_obelix_package()
if ob_data is not None:
entries = parse_obelix_via_package(ob_data)
print(f" Parsed {len(entries):,} entries")
for e in entries:
e["experimental_database"] = "OBELiX_Therrien2025"
all_experimental.extend(entries)
with_cond = sum(1 for e in entries if e.get("conductivity_S_cm") is not None)
with_ea = sum(1 for e in entries if e.get("activation_energy_eV") is not None)
print(f" With conductivity: {with_cond}")
print(f" With activation energy: {with_ea}")
else:
print(f" Could not load OBELiX via package.")
print(f" Try: pip install obelix-data")
print(f" Or: https://github.com/nrc-mila/OBELiX")
if not all_experimental:
print("\n No experimental data loaded. Nothing to integrate.")
sys.exit(1)
# --- Cross-reference with existing dataset ---
print(f"\n Loading Scandium-Dataset...")
t0 = time.time()
with open(DATASET_PATH / "entries_final_v3.json") as f:
all_entries = json.load(f)
print(f" {len(all_entries):,} entries ({time.time()-t0:.1f}s)")
print(f"\n{'─' * WIDTH}")
print(" Cross-referencing...")
print(f"{'─' * WIDTH}")
matched, unmatched, conductivity_added, new_entries = cross_reference_and_add(
all_experimental, all_entries
)
print(f"\n Results:")
print(f" Matched existing entries: {matched}")
print(f" Unmatched (new compositions): {unmatched}")
print(f" Conductivity labels added: {conductivity_added}")
print(f" New experimental entries: {len(new_entries)}")
if new_entries:
cond_entries = [(e.get("formula", "?"),
e.get("ssb_screening", {}).get("estimated_ionic_conductivity_S_cm"))
for e in new_entries
if e.get("ssb_screening", {}).get("estimated_ionic_conductivity_S_cm")]
for formula, cond in sorted(cond_entries, key=lambda x: -abs(x[1] or 0))[:5]:
if cond:
print(f" {formula:30s} σ={cond:.2e} S/cm")
if not args.dry_run:
if new_entries:
all_entries.extend(new_entries)
print(f"\n Added {len(new_entries):,} experimental entries")
output_path = DATASET_PATH / "entries_final_v3.json"
print(f" Writing to {output_path}...")
t_write = time.time()
with open(output_path, "w") as f:
json.dump(all_entries, f)
print(f" Done ({time.time()-t_write:.1f}s)")
experimental_count = sum(1 for e in all_entries if e.get("is_experimental"))
with_conductivity_total = sum(
1 for e in all_entries
if e.get("ssb_screening", {}).get("estimated_ionic_conductivity_S_cm")
)
print(f"\n{'─' * WIDTH}")
print(" INTEGRATION SUMMARY")
print(f"{'─' * WIDTH}")
db_sources = set(e.get("experimental_database", "unknown") for e in all_experimental)
for db in sorted(db_sources):
count = sum(1 for e in all_experimental if e.get("experimental_database") == db)
print(f" {db}: {count} entries")
print(f" Total experimental entries in dataset: {experimental_count}")
print(f" Entries with conductivity labels: {with_conductivity_total}")
else:
print(f"\n (dry-run)")
print("=" * WIDTH)
if __name__ == "__main__":
main()
|