19
19
20
20
from pymatgen .core import Element , get_el_sp
21
21
22
-
23
- def test_yaml ():
24
- with open ("periodic_table.yaml" ) as file :
25
- data = yaml .load (file )
26
- print (data )
27
-
28
-
29
- def test_json ():
30
- with open ("periodic_table.json" ) as file :
31
- data = json .load (file )
32
- print (data )
22
+ ptable_yaml_path = "periodic_table.yaml"
33
23
34
24
35
25
def parse_oxi_state ():
36
- with open ("periodic_table.yaml" ) as file :
37
- data = yaml .load (file )
26
+ data = loadfn (ptable_yaml_path )
38
27
with open ("oxidation_states.txt" ) as file :
39
28
oxi_data = file .read ()
40
29
oxi_data = re .sub ("[\n \r ]" , "" , oxi_data )
@@ -72,10 +61,9 @@ def parse_oxi_state():
72
61
73
62
74
63
def parse_ionic_radii ():
75
- with open ("periodic_table.yaml" ) as f :
76
- data = yaml .load (f )
77
- with open ("ionic_radii.csv" ) as f :
78
- radii_data = f .read ()
64
+ data = loadfn (ptable_yaml_path )
65
+ with open ("ionic_radii.csv" ) as file :
66
+ radii_data = file .read ()
79
67
radii_data = radii_data .split ("\r " )
80
68
header = radii_data [0 ].split ("," )
81
69
for idx in range (1 , len (radii_data )):
@@ -99,15 +87,14 @@ def parse_ionic_radii():
99
87
data [el ]["Ionic_radii" ] = ionic_radii
100
88
else :
101
89
print (el )
102
- with open ("periodic_table2.yaml" , "w" ) as f :
103
- yaml .dump (data , f )
90
+ with open ("periodic_table2.yaml" , "w" ) as file :
91
+ yaml .dump (data , file )
104
92
105
93
106
94
def parse_radii ():
107
- with open ("periodic_table.yaml" ) as f :
108
- data = yaml .load (f )
109
- with open ("radii.csv" ) as f :
110
- radii_data = f .read ()
95
+ data = loadfn (ptable_yaml_path )
96
+ with open ("radii.csv" ) as file :
97
+ radii_data = file .read ()
111
98
radii_data = radii_data .split ("\r " )
112
99
113
100
for line in radii_data :
@@ -134,15 +121,14 @@ def parse_radii():
134
121
data [el ]["Van der waals radius" ] = vdw_radii
135
122
else :
136
123
print (el )
137
- with open ("periodic_table2.yaml" , "w" ) as f :
138
- yaml .dump (data , f )
139
- with open ("periodic_table.json" , "w" ) as f :
140
- json .dump (data , f )
124
+ with open ("periodic_table2.yaml" , "w" ) as file :
125
+ yaml .dump (data , file )
126
+ with open ("../pymatgen/core/ periodic_table.json" , "w" ) as file :
127
+ json .dump (data , file )
141
128
142
129
143
130
def update_ionic_radii ():
144
- with open ("periodic_table.yaml" ) as f :
145
- data = yaml .load (f )
131
+ data = loadfn (ptable_yaml_path )
146
132
147
133
for d in data .values ():
148
134
if "Ionic_radii" in d :
@@ -154,15 +140,14 @@ def update_ionic_radii():
154
140
if "Ionic_radii_ls" in d :
155
141
d ["Ionic radii ls" ] = {k : v / 100 for k , v in d ["Ionic_radii_ls" ].items ()}
156
142
del d ["Ionic_radii_ls" ]
157
- with open ("periodic_table2.yaml" , "w" ) as f :
158
- yaml .dump (data , f )
159
- with open ("periodic_table.json" , "w" ) as f :
160
- json .dump (data , f )
143
+ with open ("periodic_table2.yaml" , "w" ) as file :
144
+ yaml .dump (data , file )
145
+ with open ("../pymatgen/core/ periodic_table.json" , "w" ) as file :
146
+ json .dump (data , file )
161
147
162
148
163
149
def parse_shannon_radii ():
164
- with open ("periodic_table.yaml" ) as f :
165
- data = yaml .load (f )
150
+ data = loadfn (ptable_yaml_path )
166
151
167
152
from openpyxl import load_workbook
168
153
@@ -194,22 +179,20 @@ def parse_shannon_radii():
194
179
if el in data :
195
180
data [el ]["Shannon radii" ] = dict (radii [el ])
196
181
197
- with open ("periodic_table.yaml" , "w" ) as f :
198
- yaml .safe_dump (data , f )
199
- with open ("periodic_table.json" , "w" ) as f :
200
- json .dump (data , f )
182
+ dumpfn (data , ptable_yaml_path )
183
+ with open ("../pymatgen/core/periodic_table.json" , "w" ) as file :
184
+ json .dump (data , file )
201
185
202
186
203
187
def gen_periodic_table ():
204
- with open ("periodic_table.yaml" ) as f :
205
- data = yaml .load (f )
188
+ data = loadfn (ptable_yaml_path )
206
189
207
- with open ("periodic_table.json" , "w" ) as f :
208
- json .dump (data , f )
190
+ with open ("../pymatgen/core/ periodic_table.json" , "w" ) as file :
191
+ json .dump (data , file )
209
192
210
193
211
194
def gen_iupac_ordering ():
212
- periodic_table = loadfn ("periodic_table.json" )
195
+ periodic_table = loadfn ("../pymatgen/core/ periodic_table.json" )
213
196
order = [
214
197
([18 ], range (6 , 0 , - 1 )), # noble gasses
215
198
([1 ], range (7 , 1 , - 1 )), # alkali metals
@@ -265,26 +248,38 @@ def add_electron_affinities():
265
248
row .append (td .get_text ().strip ())
266
249
data .append (row )
267
250
data .pop (0 )
268
- ea = {int (r [0 ]): float (re .sub (r"[\s\(\)]" , "" , r [3 ].strip ("()[]" ))) for r in data }
269
- assert set (ea ).issuperset (range (1 , 93 )) # Ensure that we have data for up to U.
251
+
252
+ ea = {}
253
+ max_Z = max (Element (element ).Z for element in Element .__members__ )
254
+ for r in data :
255
+ # don't want superheavy elements or less common isotopes
256
+ if int (r [0 ]) > max_Z or r [2 ] in ea :
257
+ continue
258
+ temp_str = re .sub (r"[\s\(\)]" , "" , r [3 ].strip ("()[]" ))
259
+ # hyphen-like characters used that can't be parsed by .float
260
+ bytes_rep = temp_str .encode ("unicode_escape" ).replace (b"\\ u2212" , b"-" )
261
+ ea [r [2 ]] = float (bytes_rep .decode ("unicode_escape" ))
262
+
263
+ Z_set = {Element .from_name (element ).Z for element in ea }
264
+ assert Z_set .issuperset (range (1 , 93 )) # Ensure that we have data for up to U.
270
265
print (ea )
271
266
pt = loadfn ("../pymatgen/core/periodic_table.json" )
272
- for k , v in pt .items ():
273
- v ["Electron affinity" ] = ea .get (Element (k ). Z )
267
+ for key , val in pt .items ():
268
+ val ["Electron affinity" ] = ea .get (Element (key ). long_name )
274
269
dumpfn (pt , "../pymatgen/core/periodic_table.json" )
275
270
276
271
277
272
def add_ionization_energies ():
278
273
"""Update the periodic table data file with ground level and ionization energies from NIST."""
279
274
280
- with open ("NIST Atomic Ionization Energies Output.html" ) as f :
281
- soup = BeautifulSoup (f .read (), "html.parser" )
282
- for t in soup .find_all ("table" ):
283
- if "Hydrogen" in t .text :
275
+ with open ("NIST Atomic Ionization Energies Output.html" ) as file :
276
+ soup = BeautifulSoup (file .read (), "html.parser" )
277
+ for table in soup .find_all ("table" ):
278
+ if "Hydrogen" in table .text :
284
279
break
285
280
data = collections .defaultdict (list )
286
- for tr in t .find_all ("tr" ):
287
- row = [td .get_text ().strip () for td in tr .find_all ("td" )]
281
+ for row in table .find_all ("tr" ):
282
+ row = [td .get_text ().strip () for td in row .find_all ("td" )]
288
283
if row :
289
284
Z = int (row [0 ])
290
285
val = re .sub (r"\s" , "" , row [8 ].strip ("()[]" ))
@@ -294,9 +289,9 @@ def add_ionization_energies():
294
289
print (data [51 ])
295
290
assert set (data ).issuperset (range (1 , 93 )) # Ensure that we have data for up to U.
296
291
pt = loadfn ("../pymatgen/core/periodic_table.json" )
297
- for k , v in pt .items ():
298
- del v ["Ionization energy" ]
299
- v ["Ionization energies" ] = data .get (Element (k ). Z , [])
292
+ for key , val in pt .items ():
293
+ del val ["Ionization energy" ]
294
+ val ["Ionization energies" ] = data .get (Element (key ). long_name , [])
300
295
dumpfn (pt , "../pymatgen/core/periodic_table.json" )
301
296
302
297
0 commit comments