Skip to content

Commit

Permalink
chore: make more imports optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Maik Jablonka committed Dec 17, 2022
1 parent 99f042b commit 8d57d52
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
3 changes: 1 addition & 2 deletions examples/build_model_using_mofdscribe.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@
"from pymatviz.parity import density_scatter_with_hist\n",
"\n",
"from mofdscribe.featurizers.chemistry import APRDF, RACS, AMD\n",
"from mofdscribe.featurizers.topology.ph_hist import PHHist\n",
"from mofdscribe.featurizers.base import MOFMultipleFeaturizer\n",
"from mofdscribe.datasets.thermal_stability_dataset import ThermalStabilityDataset\n",
"from mofdscribe.datasets.structuredataset import FrameDataset\n",
Expand Down Expand Up @@ -192,7 +191,7 @@
"metadata": {},
"outputs": [],
"source": [
"featurizer = MOFMultipleFeaturizer([APRDF(), PHHist(), RACS(), AMD()])"
"featurizer = MOFMultipleFeaturizer([APRDF(), RACS(), AMD()])"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions src/mofdscribe/featurizers/bu/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,6 @@
import numpy as np
from loguru import logger
from numpy.typing import ArrayLike

from pymatgen.core import Molecule
from rdkit import Chem
from rdkit.Chem import BondType
Expand Down Expand Up @@ -452,7 +451,7 @@
}


def pymatgen_2_babel_atom_idx_map(pmg_mol: Molecule, ob_mol: "ob.OBMol") -> Dict[int, int]:
def pymatgen_2_babel_atom_idx_map(pmg_mol: Molecule, ob_mol) -> Dict[int, int]:
"""
Create an atom index mapping between pymatgen mol and openbabel mol.
Expand All @@ -471,6 +470,7 @@ def pymatgen_2_babel_atom_idx_map(pmg_mol: Molecule, ob_mol: "ob.OBMol") -> Dict
RuntimeError: if mapping is not possible.
"""
from openbabel import openbabel as ob

pmg_coords = pmg_mol.cart_coords
ob_coords = [[a.GetX(), a.GetY(), a.GetZ()] for a in ob.OBMolAtomIter(ob_mol)]
ob_index = [a.GetIdx() for a in ob.OBMolAtomIter(ob_mol)]
Expand Down Expand Up @@ -644,6 +644,7 @@ def create_rdkit_mol_from_mol_graph(
"""
from openbabel import openbabel as ob
from pymatgen.io.babel import BabelMolAdaptor

pymatgen_mol = mol_graph.molecule
species = [str(s) for s in pymatgen_mol.species]
coords = pymatgen_mol.cart_coords
Expand Down
10 changes: 7 additions & 3 deletions src/mofdscribe/featurizers/topology/_tda_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import numpy as np
from element_coder import encode_many
from loguru import logger
from moleculetda.construct_pd import construct_pds
from moleculetda.read_file import make_supercell
from moleculetda.vectorize_pds import diagrams_to_arrays, get_images
from pymatgen.core import Structure
from pymatgen.transformations.advanced_transformations import CubicSupercellTransformation

Expand All @@ -18,6 +15,8 @@

# @np_cache
def construct_pds_cached(coords, periodic=False, weights: Optional[Collection] = None):
from moleculetda.construct_pd import construct_pds

return construct_pds(coords, periodic=periodic, weights=weights)


Expand All @@ -28,6 +27,7 @@ def _coords_for_structure(
no_supercell: bool = False,
weighting: Optional[str] = None,
) -> Tuple[np.ndarray, np.ndarray]:
from moleculetda.read_file import make_supercell

if no_supercell:
if weighting is not None:
Expand Down Expand Up @@ -64,6 +64,8 @@ def _coords_for_structure(
def _pd_arrays_from_coords(
coords, periodic: bool = False, bd_arrays: bool = False, weights: Optional[np.ndarray] = None
):
from moleculetda.vectorize_pds import diagrams_to_arrays

pds = construct_pds_cached(coords, periodic=periodic, weights=weights)
if bd_arrays:
pd = diagrams_to_bd_arrays(pds)
Expand Down Expand Up @@ -118,6 +120,8 @@ def get_persistent_images_for_structure(
persistent_images (dict): dictionary of persistent images and their
barcode representations
"""
from moleculetda.vectorize_pds import get_images

element_images: Dict[dict] = defaultdict(dict)
specs = []
for mb, mp in zip(max_b, max_p):
Expand Down
3 changes: 2 additions & 1 deletion src/mofdscribe/featurizers/utils/eqeq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from typing import List, Tuple

from ase.io.cif import write_cif
from pyeqeq import run_on_cif
from pymatgen.core import IStructure
from pymatgen.io.ase import AseAtomsAdaptor


@lru_cache(maxsize=32)
def get_eqeq_charges(structure: IStructure) -> Tuple[str, List[float]]:
from pyeqeq import run_on_cif

with open(os.devnull, "w") as devnull, contextlib.redirect_stdout(devnull), NamedTemporaryFile(
"w", suffix=".cif"
) as f:
Expand Down

0 comments on commit 8d57d52

Please sign in to comment.