Skip to content

Commit

Permalink
Defer MPRester import to allow it to act more like an optional depe…
Browse files Browse the repository at this point in the history
…ndency (#906)
  • Loading branch information
ml-evs committed Jul 24, 2023
1 parent 20fc201 commit 926d1a2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion matminer/featurizers/composition/thermo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Composition featurizers for thermodynamic properties.
"""

from pymatgen.ext.matproj import MPRester

from matminer.featurizers.base import BaseFeaturizer
from matminer.utils.data import CohesiveEnergyData
Expand Down Expand Up @@ -40,6 +39,8 @@ def featurize(self, comp, formation_energy_per_atom=None):
formation_energy_per_atom = formation_energy_per_atom or None

if not formation_energy_per_atom:
from pymatgen.ext.matproj import MPRester

# Get formation energy of most stable structure from MP
if self.mapi_key:
struct_lst = MPRester(self.mapi_key).get_data(comp.reduced_formula)
Expand Down Expand Up @@ -98,6 +99,7 @@ def featurize(self, comp):
Args:
comp: (str) compound composition, eg: "NaCl"
"""
from pymatgen.ext.matproj import MPRester

# Get formation energy of most stable structure from MP
with MPRester(self.mapi_key) if self.mapi_key else MPRester() as mpr:
Expand Down
4 changes: 3 additions & 1 deletion matminer/featurizers/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from monty.json import MontyDecoder
from pymatgen.core.composition import Composition
from pymatgen.core.structure import IStructure
from pymatgen.ext.matproj import MPRester
from pymatgen.io.ase import AseAtomsAdaptor

from matminer.featurizers.base import BaseFeaturizer
Expand Down Expand Up @@ -566,6 +565,9 @@ class CompositionToStructureFromMP(ConversionFeaturizer):
"""

def __init__(self, target_col_id="structure", overwrite_data=False, mapi_key=None):

from pymatgen.ext.matproj import MPRester

super().__init__(target_col_id, overwrite_data)
if mapi_key:
self.mpr = MPRester(mapi_key)
Expand Down

0 comments on commit 926d1a2

Please sign in to comment.