-
Notifications
You must be signed in to change notification settings - Fork 0
/
featurizers.py
449 lines (302 loc) · 15 KB
/
featurizers.py
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
442
443
444
445
446
447
448
"""
Module to create features from Pymatgen Material objects (e.g. composition-based & structure-based)
Author: Son Gyo Jung
Email: sgj13@cam.ac.uk
"""
import os
from xmlrpc.client import boolean
import pandas as pd
import joblib
import pathlib
import numpy as np
from pymatgen.core import Composition
from matminer.featurizers.base import MultipleFeaturizer
from matminer.featurizers.conversions import StructureToComposition
######## Featurizer based on the Composition object
# Element
from matminer.featurizers.composition import ElementFraction, TMetalFraction, Stoichiometry, BandCenter
# Composition
from matminer.featurizers.composition import ElementProperty, Meredig
# Ion
from matminer.featurizers.composition import OxidationStates, IonProperty, ElectronAffinity, ElectronegativityDiff
# Alloy
from matminer.featurizers.composition.alloy import WenAlloys, Miedema, YangSolidSolution
# Orbital
from matminer.featurizers.composition.orbital import AtomicOrbitals, ValenceOrbital
# Packing
from matminer.featurizers.composition.packing import AtomicPackingEfficiency
# Thermo
from matminer.featurizers.composition.thermo import CohesiveEnergy, CohesiveEnergyMP
######## Featurizer based on the Structure object
# Matrix
from matminer.featurizers.structure.matrix import OrbitalFieldMatrix
# Misc
from matminer.featurizers.structure.misc import XRDPowderPattern
# Order
from matminer.featurizers.structure.order import DensityFeatures, ChemicalOrdering, MaximumPackingEfficiency, StructuralComplexity
# RDF
from matminer.featurizers.structure.rdf import ElectronicRadialDistributionFunction
# Site
from matminer.featurizers.structure.sites import SiteStatsFingerprint
# Symmetry
from matminer.featurizers.structure.symmetry import GlobalSymmetryFeatures, Dimensionality
# Composition
from matminer.featurizers.structure.composite import JarvisCFID
class prepare_to_featurize():
"""
Class to generate features
args:
(1) df (pandas.Dataframe) - dataframe with chemical information
return:
(1) pandas.Dataframe of features (pkl and/or csv)
"""
def __init__(self, df):
self.df = df
self.df = self.df.fillna(0)
self.cur_dir = pathlib.Path().resolve()
def movecol(self, cols_to_move = [], ref_col = '', place = 'after'):
"""
Function to rearrange columns
arg:
(a) cols_to_move (list) - list of columns to move
(b) ref_col (type:str) - reference column
(c) place (type:str) - whether to move the specified columns 'before' or 'after' the reference column (set to 'after' as default)
return:
(a) pandas.Dataframe
"""
cols = self.df.columns.tolist()
if place == 'after':
s1 = cols[:list(cols).index(ref_col) + 1]
s2 = cols_to_move
if place == 'before':
s1 = cols[:list(cols).index(ref_col)]
s2 = cols_to_move + [ref_col]
s1 = [i for i in s1 if i not in s2]
s3 = [i for i in cols if i not in s1 + s2]
return self.df[s1 + s2 + s3]
def OHE(self, *args, **kwargs):
"""
One-hot-encoding of categorical columns
return:
(a) pandas.Dataframe
"""
col = kwargs.get('col')
categorical_cols = [col for col, dt in self.df.dtypes.items()
if dt == object
and col != 'formula'
and col != 'structure'
and col != 'mpid'
]
if col is not None:
categorical_cols.remove(col)
print('No. of categorical features:', len(categorical_cols))
self.df = pd.get_dummies(data = self.df, columns = categorical_cols, prefix_sep = '_ohe_', drop_first = False)
self.ohe_cols = [i for i in self.df.columns if '_ohe_' in i]
return self.df, self.ohe_cols
def drop(self, cols=[]):
'''
Function to drop unwanted columns
return:
(a) pandas.Dataframe
'''
self.df = self.df.drop(cols, axis=1)
return self.df
def generate_oxid_composition_features(self):
'''
Generate features using the Composition object. Additional featurizers by uncommenting those excluded.
arg:
(a) oxidation_features (boolean) - generate ion featuresgenerate_composition_features
return:
(a) pandas.Dataframe
'''
oxid_composition_featurizer = MultipleFeaturizer([
OxidationStates(), # Ion
IonProperty(), # Ion
ElectronAffinity(), # Ion
ElectronegativityDiff(), # Ion
])
self.df = oxid_composition_featurizer.featurize_dataframe(self.df, 'oxidation_composition', ignore_errors=True)
feature_len = len(oxid_composition_featurizer.feature_labels())
print('Total no. of features generated:', feature_len)
# Inf to NaN
self.df.replace([np.inf, -np.inf], np.nan, inplace=True)
# NaN to zero
self.df = self.df.fillna(0)
return self.df
def generate_oxid_composition_features_with_composition(self):
'''
Generate features using the Composition object. Additional featurizers by uncommenting those excluded.
arg:
(a) oxidation_features (boolean) - generate ion features
return:
(a) pandas.Dataframe
'''
oxid_composition_featurizer = MultipleFeaturizer([
OxidationStates(), # Ion
IonProperty(), # Ion
ElectronAffinity(), # Ion
ElectronegativityDiff(), # Ion
])
self.df = oxid_composition_featurizer.featurize_dataframe(self.df, 'composition', ignore_errors=True)
feature_len = len(oxid_composition_featurizer.feature_labels())
print('Total no. of features generated:', feature_len)
# Inf to NaN
self.df.replace([np.inf, -np.inf], np.nan, inplace=True)
# NaN to zero
self.df = self.df.fillna(0)
return self.df
def generate_composition_features(self):
'''
Generate features using the Composition object. Additional featurizers by uncommenting those excluded.
arg:
(a) oxidation_features (boolean) - generate ion features
return:
(a) pandas.Dataframe
'''
composition_featurizer = MultipleFeaturizer([
ElementFraction(), # Element
TMetalFraction(), # Element
Stoichiometry(), # Element
BandCenter(), # Element
ElementProperty.from_preset('magpie'), # Composition
ElementProperty.from_preset('matminer'), # Composition
ElementProperty.from_preset('deml'), # Composition
ElementProperty.from_preset('megnet_el'), # Composition
Meredig(), # Composition
YangSolidSolution(), # Alloy
AtomicOrbitals(), # Orbital
ValenceOrbital(), # Orbital
AtomicPackingEfficiency() # Packing
### Additional featurizers:
# WenAlloys(), # Alloy
# Miedema(), # Alloy
# CohesiveEnergy(mapi_key='lZCh9ke4qRxMQO16'), # Thermo
# CohesiveEnergyMP(mapi_key='lZCh9ke4qRxMQO16') # Thermo
])
self.df = composition_featurizer.featurize_dataframe(self.df, 'composition', ignore_errors=True)
feature_len = len(composition_featurizer.feature_labels())
print('Total no. of features generated:', feature_len)
# Inf to NaN
self.df.replace([np.inf, -np.inf], np.nan, inplace=True)
# NaN to zero
self.df = self.df.fillna(0)
return self.df
def generate_structural_features(self):
'''
Generate features using the Composition object. See the list below.
arg:
(a) oxidation_features (boolean) - generate ion features
return:
(a) pandas.Dataframe
'''
structural_featurizer = MultipleFeaturizer([
OrbitalFieldMatrix(), # Matrix
XRDPowderPattern(), # Misc
DensityFeatures(), # Order
ChemicalOrdering(), # Order
MaximumPackingEfficiency(), # Order
StructuralComplexity(), # Order
SiteStatsFingerprint.from_preset("CoordinationNumber_ward-prb-2017"), # Site
SiteStatsFingerprint.from_preset("LocalPropertyDifference_ward-prb-2017"), # Site
GlobalSymmetryFeatures(), # Symmetry
Dimensionality() # Symmetry
])
self.df = structural_featurizer.featurize_dataframe(self.df, 'structure', ignore_errors=True)
feature_len = len(structural_featurizer.feature_labels())
print('Total no. of features generated:', feature_len)
# Inf to NaN
self.df.replace([np.inf, -np.inf], np.nan, inplace=True)
# NaN to zero
self.df = self.df.fillna(0)
return self.df
def generate_oxid_structural_features(self):
'''
Generate Electronic Radial Distribution Function features using the Composition object.
arg:
(a) oxidation_features (boolean) - generate ion features
return:
(a) pandas.Dataframe
'''
structural_featurizer = MultipleFeaturizer([
ElectronicRadialDistributionFunction(), # RDF
])
self.df = structural_featurizer.featurize_dataframe(self.df, 'oxidation_structure', ignore_errors=True)
feature_len = len(structural_featurizer.feature_labels())
print('Total no. of features generated:', feature_len)
# Inf to NaN
self.df.replace([np.inf, -np.inf], np.nan, inplace=True)
# NaN to zero
self.df = self.df.fillna(0)
return self.df
def generate_JarvisCFID_features(self):
'''
Generate Jarvis CFID features using the Composition object.
arg:
(a) oxidation_features (boolean) - generate ion features
return:
(a) pandas.Dataframe
'''
structural_featurizer = MultipleFeaturizer([
JarvisCFID(), # Composition
])
self.df = structural_featurizer.featurize_dataframe(self.df, 'structure', ignore_errors=True)
feature_len = len(structural_featurizer.feature_labels())
print('Total no. of features generated:', feature_len)
# Inf to NaN
self.df.replace([np.inf, -np.inf], np.nan, inplace=True)
# NaN to zero
self.df = self.df.fillna(0)
return self.df
def custom_features(self, composition_col_exist):
'''
Create custom features which includes:
(a) weight,
(b) total electrons,
(c) electronegativity,
(d) noble_gas,
(e) transition_metal,
(f) post_transition_metal,
(g) rare_earth_metal,
(h) metal, metalloid,
(i) alkali,
(j) alkaline,
(k) halogen,
(l) chalcogen,
(m) lanthanoid,
(n) actinoid,
(o) quadrupolar,
(p) s-block,
(q) p-block,
(r) d-block,
(s) f-block,
'''
# Check if Composition contains any elements matching a given category
category = [
'noble_gas', 'transition_metal', 'post_transition_metal', 'rare_earth_metal', 'metal', 'metalloid', \
'alkali', 'alkaline', 'halogen', 'chalcogen', 'lanthanoid', 'actinoid', 'quadrupolar', 's-block', 'p-block', \
'd-block', 'f-block'
]
if composition_col_exist is False:
cf = StructureToComposition(target_col_id='composition')
self.df = cf.featurize_dataframe(self.df, 'structure', ignore_errors=True)
# Generate total molecular weight of Composition
self.df['weight'] = self.df['composition']
self.df['weight'] = self.df['weight'].map(lambda x: x.weight)
# Generate total electrons
self.df['total_e'] = self.df['composition']
self.df['total_e'] = self.df['total_e'].map(lambda x: x.total_electrons)
# Generate average electronegativity of the composition
self.df['avg_electroneg'] = self.df['composition']
self.df['avg_electroneg'] = self.df['avg_electroneg'].map(lambda x: x.average_electroneg)
for c in category:
self.df[c] = self.df['composition']
self.df[c] = self.df[c].map(lambda x: x.contains_element_type(c))
self.df[c] = self.df[c].astype(int)
return self.df
def save(self, name, csv=False):
#Save data as csv
joblib.dump(self.df, os.path.join(self.cur_dir, str(name) + '.pkl'))
print('Data saved as:', str(name) + '.pkl')
if csv == True:
self.df.to_csv(os.path.join(self.cur_dir, str(name) + + '.csv'))
print('Data saved as:', str(name) + '.csv')