@@ -1313,13 +1313,6 @@ def has_errors(self):
1313
1313
def check (self , structure : Structure ) -> str | None :
1314
1314
"""Check whether a structure constructed from CIF passes sanity checks.
1315
1315
1316
- Args:
1317
- structure (Structure) : structure created from CIF
1318
-
1319
- Returns:
1320
- str | None: If any check fails, on output, returns a human-readable str for the
1321
- reason why (e.g., which elements are missing). Returns None if all checks pass.
1322
-
1323
1316
Checks:
1324
1317
- Composition from CIF is valid
1325
1318
- CIF composition contains only valid elements
@@ -1329,19 +1322,30 @@ def check(self, structure: Structure) -> str | None:
1329
1322
- CIF and structure have same relative stoichiometry. Thus
1330
1323
if CIF reports stoichiometry LiFeO, and the structure has
1331
1324
composition (LiFeO)4, this check passes.
1325
+
1326
+ Args:
1327
+ structure (Structure) : structure created from CIF
1328
+
1329
+ Returns:
1330
+ str | None: If any check fails, on output, returns a human-readable str for the
1331
+ reason why (e.g., which elements are missing). Returns None if all checks pass.
1332
1332
"""
1333
1333
failure_reason = None
1334
1334
1335
1335
cif_as_dict = self .as_dict ()
1336
1336
head_key = next (iter (cif_as_dict ))
1337
1337
1338
1338
cif_formula = None
1339
+ check_stoichiometry = True
1339
1340
for key in ("_chemical_formula_sum" , "_chemical_formula_structural" ):
1340
1341
if cif_as_dict [head_key ].get (key ):
1341
1342
cif_formula = cif_as_dict [head_key ][key ]
1342
1343
break
1343
1344
1345
+ # In case of missing CIF formula keys, get non-stoichiometric formula from
1346
+ # unique sites and skip relative stoichiometry check (added in gh-3628)
1344
1347
if cif_formula is None and cif_as_dict [head_key ].get ("_atom_site_type_symbol" ):
1348
+ check_stoichiometry = False
1345
1349
cif_formula = " " .join (cif_as_dict [head_key ]["_atom_site_type_symbol" ])
1346
1350
1347
1351
try :
@@ -1370,17 +1374,20 @@ def check(self, structure: Structure) -> str | None:
1370
1374
failure_reason = f"Missing elements { missing_str } { addendum } "
1371
1375
1372
1376
elif not all (struct_comp [elt ] - orig_comp [elt ] == 0 for elt in orig_comp ):
1373
- # Check that stoichiometry is same, i.e., same relative ratios of elements
1374
- ratios = {elt : struct_comp [elt ] / orig_comp [elt ] for elt in orig_comp_elts }
1375
-
1376
- same_stoich = all (
1377
- abs (ratios [elt_a ] - ratios [elt_b ]) < self .comp_tol
1378
- for elt_a in orig_comp_elts
1379
- for elt_b in orig_comp_elts
1380
- )
1377
+ if check_stoichiometry :
1378
+ # Check that CIF/PMG stoichiometry has same relative ratios of elements
1379
+ ratios = {elt : struct_comp [elt ] / orig_comp [elt ] for elt in orig_comp_elts }
1380
+
1381
+ same_stoich = all (
1382
+ abs (ratios [elt_a ] - ratios [elt_b ]) < self .comp_tol
1383
+ for elt_a in orig_comp_elts
1384
+ for elt_b in orig_comp_elts
1385
+ )
1381
1386
1382
- if not same_stoich :
1383
- failure_reason = f"Incorrect stoichiometry:\n CIF={ orig_comp } \n PMG={ struct_comp } \n { ratios = } "
1387
+ if not same_stoich :
1388
+ failure_reason = f"Incorrect stoichiometry:\n CIF={ orig_comp } \n PMG={ struct_comp } \n { ratios = } "
1389
+ else :
1390
+ self .warnings += ["Skipping relative stoichiometry check because CIF does not contain formula keys." ]
1384
1391
1385
1392
return failure_reason
1386
1393
0 commit comments