|
| 1 | +"""This submodule contains the `Matminer2024FastFeaturizer` class. """ |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import modnet.featurizers |
| 5 | +import contextlib |
| 6 | + |
| 7 | + |
| 8 | +class Matminer2024FastFeaturizer(modnet.featurizers.MODFeaturizer): |
| 9 | + """A set of efficient featurizers for features implemented in matminer |
| 10 | + at time of creation (matminer v0.9.2 from 2024). |
| 11 | +
|
| 12 | + Removes featurizers that are known to be slow (i.e., orders of magnitude |
| 13 | + more intensive to compute than the rest of the featurizers). |
| 14 | +
|
| 15 | + """ |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + fast_oxid: bool = True, |
| 20 | + continuous_only: bool = True, |
| 21 | + ): |
| 22 | + """Creates the featurizer and imports all featurizer functions. |
| 23 | +
|
| 24 | + Parameters: |
| 25 | + fast_oxid: Whether to use the accelerated oxidation state parameters within |
| 26 | + pymatgen when constructing features that constrain oxidation states such |
| 27 | + that all sites with the same species in a structure will have the same |
| 28 | + oxidation state (recommended if featurizing any structure |
| 29 | + with large unit cells). |
| 30 | + continuous_only: Whether to keep only the features that are continuous |
| 31 | + with respect to the composition (only for composition featurizers). |
| 32 | + Discontinuous features may lead to discontinuities in the model predictions. |
| 33 | +
|
| 34 | + """ |
| 35 | + |
| 36 | + super().__init__() |
| 37 | + self.drop_allnan = False |
| 38 | + self.fast_oxid = fast_oxid |
| 39 | + self.continuous_only = continuous_only |
| 40 | + self.load_featurizers() |
| 41 | + |
| 42 | + def load_featurizers(self): |
| 43 | + with contextlib.redirect_stdout(None): |
| 44 | + from matminer.featurizers.composition import ( |
| 45 | + BandCenter, |
| 46 | + ElementFraction, |
| 47 | + ElementProperty, |
| 48 | + Stoichiometry, |
| 49 | + TMetalFraction, |
| 50 | + ValenceOrbital, |
| 51 | + ) |
| 52 | + from matminer.featurizers.structure import ( |
| 53 | + DensityFeatures, |
| 54 | + EwaldEnergy, |
| 55 | + GlobalSymmetryFeatures, |
| 56 | + StructuralComplexity, |
| 57 | + ) |
| 58 | + from matminer.utils.data import ( |
| 59 | + DemlData, |
| 60 | + PymatgenData, |
| 61 | + ) |
| 62 | + |
| 63 | + pymatgen_features = [ |
| 64 | + "block", |
| 65 | + "mendeleev_no", |
| 66 | + "electrical_resistivity", |
| 67 | + "velocity_of_sound", |
| 68 | + "thermal_conductivity", |
| 69 | + "bulk_modulus", |
| 70 | + "coefficient_of_linear_thermal_expansion", |
| 71 | + ] |
| 72 | + |
| 73 | + deml_features = [ |
| 74 | + "atom_radius", |
| 75 | + "molar_vol", |
| 76 | + "heat_fusion", |
| 77 | + "boiling_point", |
| 78 | + "heat_cap", |
| 79 | + "first_ioniz", |
| 80 | + "electric_pol", |
| 81 | + "GGAU_Etot", |
| 82 | + "mus_fere", |
| 83 | + "FERE correction", |
| 84 | + ] |
| 85 | + |
| 86 | + magpie_featurizer = ElementProperty.from_preset("magpie") |
| 87 | + magpie_featurizer.stats = ["mean", "avg_dev"] |
| 88 | + |
| 89 | + pymatgen_featurizer = ElementProperty( |
| 90 | + data_source=PymatgenData(), |
| 91 | + stats=["mean", "avg_dev"], |
| 92 | + features=pymatgen_features, |
| 93 | + ) |
| 94 | + |
| 95 | + deml_featurizer = ElementProperty( |
| 96 | + data_source=DemlData(), |
| 97 | + stats=["mean", "avg_dev"], |
| 98 | + features=deml_features, |
| 99 | + ) |
| 100 | + |
| 101 | + self.composition_featurizers = ( |
| 102 | + BandCenter(), |
| 103 | + ElementFraction(), |
| 104 | + magpie_featurizer, |
| 105 | + pymatgen_featurizer, |
| 106 | + deml_featurizer, |
| 107 | + Stoichiometry(p_list=[2, 3, 5, 7, 10]), |
| 108 | + TMetalFraction(), |
| 109 | + ValenceOrbital(props=["frac"]), |
| 110 | + ) |
| 111 | + |
| 112 | + self.oxid_composition_featurizers = [] |
| 113 | + |
| 114 | + self.structure_featurizers = ( |
| 115 | + DensityFeatures(), |
| 116 | + EwaldEnergy(), |
| 117 | + GlobalSymmetryFeatures(), |
| 118 | + StructuralComplexity(), |
| 119 | + ) |
| 120 | + |
| 121 | + self.site_featurizers = [] |
| 122 | + |
| 123 | + def featurize_composition(self, df): |
| 124 | + """Applies the preset composition featurizers to the input dataframe, |
| 125 | + renames some fields and cleans the output dataframe. |
| 126 | +
|
| 127 | + """ |
| 128 | + from pymatgen.core.periodic_table import Element |
| 129 | + |
| 130 | + df = super().featurize_composition(df) |
| 131 | + |
| 132 | + if self.composition_featurizers and not self.continuous_only: |
| 133 | + _orbitals = {"s": 1, "p": 2, "d": 3, "f": 4} |
| 134 | + df["AtomicOrbitals|HOMO_character"] = df[ |
| 135 | + "AtomicOrbitals|HOMO_character" |
| 136 | + ].map(_orbitals) |
| 137 | + df["AtomicOrbitals|LUMO_character"] = df[ |
| 138 | + "AtomicOrbitals|LUMO_character" |
| 139 | + ].map(_orbitals) |
| 140 | + |
| 141 | + df["AtomicOrbitals|HOMO_element"] = df["AtomicOrbitals|HOMO_element"].apply( |
| 142 | + lambda x: -1 if not isinstance(x, str) else Element(x).Z |
| 143 | + ) |
| 144 | + df["AtomicOrbitals|LUMO_element"] = df["AtomicOrbitals|LUMO_element"].apply( |
| 145 | + lambda x: -1 if not isinstance(x, str) else Element(x).Z |
| 146 | + ) |
| 147 | + |
| 148 | + if self.continuous_only: |
| 149 | + # These are additional features that have shown discontinuities in my tests. |
| 150 | + # Hopefully, I got them all... |
| 151 | + df.drop( |
| 152 | + columns=[ |
| 153 | + "ElementProperty|DemlData mean electric_pol", |
| 154 | + "ElementProperty|DemlData mean FERE correction", |
| 155 | + "ElementProperty|DemlData mean GGAU_Etot", |
| 156 | + "ElementProperty|DemlData mean heat_fusion", |
| 157 | + "ElementProperty|DemlData mean mus_fere", |
| 158 | + ], |
| 159 | + inplace=True, |
| 160 | + errors="ignore", |
| 161 | + ) |
| 162 | + |
| 163 | + if self.oxid_composition_featurizers: |
| 164 | + df.drop(columns=["IonProperty|max ionic char"], inplace=True) |
| 165 | + |
| 166 | + return modnet.featurizers.clean_df(df, drop_allnan=self.drop_allnan) |
| 167 | + |
| 168 | + def featurize_structure(self, df): |
| 169 | + """Applies the preset structural featurizers to the input dataframe, |
| 170 | + renames some fields and cleans the output dataframe. |
| 171 | +
|
| 172 | + """ |
| 173 | + |
| 174 | + if self.structure_featurizers: |
| 175 | + df = super().featurize_structure(df) |
| 176 | + |
| 177 | + _crystal_system = { |
| 178 | + "cubic": 1, |
| 179 | + "tetragonal": 2, |
| 180 | + "orthorombic": 3, |
| 181 | + "hexagonal": 4, |
| 182 | + "trigonal": 5, |
| 183 | + "monoclinic": 6, |
| 184 | + "triclinic": 7, |
| 185 | + } |
| 186 | + |
| 187 | + def _int_map(x): |
| 188 | + if x == np.nan: |
| 189 | + return 0 |
| 190 | + elif x: |
| 191 | + return 1 |
| 192 | + else: |
| 193 | + return 0 |
| 194 | + |
| 195 | + df["GlobalSymmetryFeatures|crystal_system"] = df[ |
| 196 | + "GlobalSymmetryFeatures|crystal_system" |
| 197 | + ].map(_crystal_system) |
| 198 | + df["GlobalSymmetryFeatures|is_centrosymmetric"] = df[ |
| 199 | + "GlobalSymmetryFeatures|is_centrosymmetric" |
| 200 | + ].map(_int_map) |
| 201 | + |
| 202 | + return modnet.featurizers.clean_df(df, drop_allnan=self.drop_allnan) |
| 203 | + |
| 204 | + def featurize_site(self, df): |
| 205 | + """Applies the preset site featurizers to the input dataframe, |
| 206 | + renames some fields and cleans the output dataframe. |
| 207 | +
|
| 208 | + """ |
| 209 | + |
| 210 | + # rename some features for backwards compatibility with pretrained models |
| 211 | + aliases = { |
| 212 | + "GeneralizedRadialDistributionFunction": "GeneralizedRDF", |
| 213 | + "AGNIFingerprints": "AGNIFingerPrint", |
| 214 | + "BondOrientationalParameter": "BondOrientationParameter", |
| 215 | + } |
| 216 | + df = super().featurize_site(df, aliases=aliases) |
| 217 | + df = df.loc[:, (df != 0).any(axis=0)] |
| 218 | + |
| 219 | + return modnet.featurizers.clean_df(df, drop_allnan=self.drop_allnan) |
0 commit comments