From f39992bda89913124c72b4252b4aac75b4e3158e Mon Sep 17 00:00:00 2001 From: Matthew Evans <7916000+ml-evs@users.noreply.github.com> Date: Sat, 24 Jun 2023 19:15:03 +0100 Subject: [PATCH] Defer `MPRester` import to allow it to act more like an optional dependency (#906) --- matminer/featurizers/composition/thermo.py | 4 +++- matminer/featurizers/conversions.py | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/matminer/featurizers/composition/thermo.py b/matminer/featurizers/composition/thermo.py index 46ce4564a..c1dc2a358 100644 --- a/matminer/featurizers/composition/thermo.py +++ b/matminer/featurizers/composition/thermo.py @@ -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 @@ -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) @@ -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: diff --git a/matminer/featurizers/conversions.py b/matminer/featurizers/conversions.py index f61135722..4d9699c0f 100644 --- a/matminer/featurizers/conversions.py +++ b/matminer/featurizers/conversions.py @@ -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 @@ -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)