Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer MPRester import to allow it to act more like an optional dep #906

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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