andreamena commited on
Commit
71741d2
·
verified ·
1 Parent(s): f966fb2

Create evaluation.py

Browse files
Files changed (1) hide show
  1. evaluation.py +119 -0
evaluation.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import numpy as np
3
+ import itertools
4
+ import shutil
5
+ from collections import Counter
6
+ from pymatgen.core import Structure
7
+ from smact import element_dictionary, metals, neutral_ratios
8
+ from smact.screening import pauling_test
9
+ from pymatgen.analysis.structure_matcher import StructureMatcher
10
+ from tqdm import tqdm
11
+
12
+ # --- FIX: Use relative paths for Hugging Face ---
13
+ input_folder = "generation_cifs"
14
+ output_folder = "valid_generation_cifs"
15
+ unique_folder = "unique_structures"
16
+ novel_folder = "novel_structures"
17
+ # ------------------------------------------------
18
+
19
+ def get_safe_space_group_number(structure):
20
+ try:
21
+ spg_info = structure.get_space_group_info()
22
+ return spg_info[1] if spg_info else None
23
+ except Exception:
24
+ return None
25
+
26
+ def structure_validity(structure, cutoff=0.5):
27
+ dist_mat = structure.distance_matrix
28
+ dist_mat = dist_mat + np.diag(np.ones(dist_mat.shape[0]) * (cutoff + 10.))
29
+ if dist_mat.min() < cutoff or structure.volume < 0.1:
30
+ return False
31
+ spg_number = get_safe_space_group_number(structure)
32
+ if spg_number is None:
33
+ return False
34
+ return True
35
+
36
+ def smact_validity(atom_types, use_pauling_test=True, include_alloys=True):
37
+ elem_counter = Counter(atom_types)
38
+ elems = [(elem, elem_counter[elem]) for elem in sorted(elem_counter.keys())]
39
+ comp, elem_counts = list(zip(*elems))
40
+ elem_counts = np.array(elem_counts)
41
+ elem_counts = elem_counts / np.gcd.reduce(elem_counts)
42
+ count = tuple(elem_counts.astype("int").tolist())
43
+
44
+ elem_symbols = tuple(comp)
45
+ try:
46
+ space = element_dictionary(elem_symbols)
47
+ except:
48
+ return False # Handle elements not in SMACT
49
+
50
+ smact_elems = [e[1] for e in space.items()]
51
+ electronegs = [e.pauling_eneg for e in smact_elems]
52
+ ox_combos = [e.oxidation_states for e in smact_elems]
53
+
54
+ if len(set(elem_symbols)) == 1:
55
+ return True
56
+ if include_alloys:
57
+ is_metal_list = [elem_s in metals for elem_s in elem_symbols]
58
+ if all(is_metal_list):
59
+ return True
60
+
61
+ threshold = np.max(count)
62
+ oxn = 1
63
+ for oxc in ox_combos:
64
+ oxn *= len(oxc)
65
+ if oxn > 1e7:
66
+ return False
67
+
68
+ for ox_states in itertools.product(*ox_combos):
69
+ stoichs = [(c,) for c in count]
70
+ cn_e, cn_r = neutral_ratios(ox_states, stoichs=stoichs, threshold=threshold)
71
+ if cn_e:
72
+ if use_pauling_test:
73
+ electroneg_OK = pauling_test(ox_states, electronegs)
74
+ else:
75
+ electroneg_OK = True
76
+ if electroneg_OK:
77
+ return True
78
+ return False
79
+
80
+ def get_atom_types_from_structure(structure):
81
+ return [str(element) for element in structure.species]
82
+
83
+ # Create directories
84
+ os.makedirs(output_folder, exist_ok=True)
85
+ os.makedirs(unique_folder, exist_ok=True)
86
+ os.makedirs(novel_folder, exist_ok=True)
87
+
88
+ total = 0
89
+ structure_valid_count = 0
90
+ composition_valid_count = 0
91
+ both_valid_count = 0
92
+
93
+ if os.path.exists(input_folder):
94
+ for filename in os.listdir(input_folder):
95
+ if filename.endswith(".cif"):
96
+ total += 1
97
+ full_path = os.path.join(input_folder, filename)
98
+ try:
99
+ structure = Structure.from_file(full_path)
100
+ atom_types = get_atom_types_from_structure(structure)
101
+
102
+ struct_valid = structure_validity(structure)
103
+ comp_valid = smact_validity(atom_types)
104
+
105
+ if struct_valid:
106
+ structure_valid_count += 1
107
+ if comp_valid:
108
+ composition_valid_count += 1
109
+ if struct_valid and comp_valid:
110
+ both_valid_count += 1
111
+ shutil.copy(full_path, os.path.join(output_folder, filename))
112
+ except Exception as e:
113
+ print(f"Error processing {filename}: {e}")
114
+
115
+ print(f"\n=== Validity Summary ===")
116
+ print(f"Total CIFs checked: {total}")
117
+ print(f"Structural validity passed: {structure_valid_count}")
118
+ print(f"Compositional validity passed: {composition_valid_count}")
119
+ print(f"Both valid (saved): {both_valid_count}")