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/fix some minor import issues #1959

Merged
merged 2 commits into from
Nov 8, 2024
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
1 change: 1 addition & 0 deletions docs/releasehistory.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Releases follow the `major.minor.micro` scheme recommended by [PEP440](https://w
### Behavior changes

- [PR #1954](https://github.com/openforcefield/openff-toolkit/pull/1954): `Topology.from_openmm` no longer casts residue numbers to int.
- [PR #1959](https://github.com/openforcefield/openff-toolkit/pull/1959): Speeds up import times.

### Bugfixes

Expand Down
5 changes: 4 additions & 1 deletion openff/toolkit/topology/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import numpy as np
from numpy.typing import NDArray
from openff.units import ensure_quantity
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is in openff.units.openmm, which means importing it results in an import openmm call whenever anything in this module is imported. Obviously it's going to be in memory for many use cases, but it's unnecessary to import it here

from typing_extensions import TypeAlias

from openff.toolkit import Quantity, unit
Expand Down Expand Up @@ -618,6 +617,8 @@ def box_vectors(self, box_vectors):
return
if not hasattr(box_vectors, "units"):
if hasattr(box_vectors, "unit"):
from openff.units import ensure_quantity

# this is probably an openmm.unit.Quantity; we should gracefully import OpenMM but
# the chances of this being an object with the two previous conditions met is low
box_vectors = ensure_quantity(box_vectors, "openff")
Expand Down Expand Up @@ -1538,6 +1539,8 @@ def from_openmm(
topology.box_vectors = from_openmm(openmm_topology.getPeriodicBoxVectors())

if positions is not None:
from openff.units import ensure_quantity

topology.set_positions(ensure_quantity(positions, "openff"))

# TODO: How can we preserve metadata from the openMM topology when creating the OFF topology?
Expand Down
11 changes: 6 additions & 5 deletions openff/toolkit/utils/_viz.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import uuid
from io import StringIO
from typing import TYPE_CHECKING

from nglview.base_adaptor import Structure, Trajectory

from openff.toolkit import Molecule, Topology, unit
if TYPE_CHECKING:
from openff.toolkit import Molecule, Topology

MOLECULE_DEFAULT_REPS = [
dict(type="licorice", params=dict(radius=0.25, multipleBond=True))
]


class MoleculeNGLViewTrajectory(Structure, Trajectory):
"""
OpenFF Molecule adaptor.
Expand Down Expand Up @@ -40,7 +41,7 @@ class MoleculeNGLViewTrajectory(Structure, Trajectory):

def __init__(
self,
molecule: Molecule,
molecule: "Molecule",
ext: str = "MOL2",
):
if not molecule.conformers:
Expand All @@ -53,7 +54,7 @@ def __init__(
self.id = str(uuid.uuid4())

def get_coordinates(self, index: int = 0):
return self.molecule.conformers[index].m_as(unit.angstrom)
return self.molecule.conformers[index].m_as("angstrom")

@property
def n_frames(self):
Expand Down Expand Up @@ -91,7 +92,7 @@ class TopologyNGLViewStructure(Structure):

def __init__(
self,
topology: Topology,
topology: "Topology",
ext: str = "PDB",
):
self.topology = topology
Expand Down
3 changes: 2 additions & 1 deletion openff/toolkit/utils/ambertools_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from typing import TYPE_CHECKING, Optional

import numpy as np
from openff.utilities.provenance import get_ambertools_version

from openff.toolkit import Quantity, unit
from openff.toolkit.utils import base_wrapper, rdkit_wrapper
Expand Down Expand Up @@ -62,6 +61,8 @@ class AmberToolsToolkitWrapper(base_wrapper.ToolkitWrapper):
SUPPORTED_CHARGE_METHODS = _supported_charge_methods

def __init__(self):
from openff.utilities.provenance import get_ambertools_version

super().__init__()

if not self.is_available():
Expand Down
3 changes: 2 additions & 1 deletion openff/toolkit/utils/rdkit_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import numpy as np
from cachetools import LRUCache, cached
from numpy.typing import NDArray
from openff.units.elements import SYMBOLS

from openff.toolkit import Quantity, unit
Expand Down Expand Up @@ -1985,7 +1986,7 @@ def _elf_compute_electrostatic_energy(
return 0.5 * interaction_energies.sum()

@classmethod
def _elf_compute_rms_matrix(cls, molecule: "Molecule") -> np.ndarray:
def _elf_compute_rms_matrix(cls, molecule: "Molecule") -> NDArray:
"""Computes the symmetric RMS matrix of all conformers in a molecule taking
only heavy atoms into account.

Expand Down
9 changes: 2 additions & 7 deletions openff/toolkit/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import logging
from typing import TYPE_CHECKING, Any, Iterable, TypeVar, Union, overload

import numpy
import numpy as np
import pint
from numpy.typing import NDArray
from openff.units import Quantity, Unit, unit
from openff.utilities import requires_package

Expand Down Expand Up @@ -397,8 +397,6 @@ def serialize_numpy(np_array) -> tuple[bytes, tuple[int]]:
shape
The shape of the serialized array
"""
import numpy as np

bigendian_float = np.dtype(float).newbyteorder(">")
bigendian_array = np_array.astype(bigendian_float)
serialized = bigendian_array.tobytes()
Expand All @@ -409,7 +407,7 @@ def serialize_numpy(np_array) -> tuple[bytes, tuple[int]]:
def deserialize_numpy(
serialized_np: Union[bytes, list],
shape: tuple[int, ...],
) -> numpy.ndarray:
) -> NDArray:
"""
Deserializes a numpy array from a bytestring or list. The input, if a bytestring, is
assumed to be in big-endian byte order.
Expand All @@ -426,9 +424,6 @@ def deserialize_numpy(
np_array
The deserialized numpy array
"""

import numpy as np

if isinstance(serialized_np, list):
np_array = np.array(serialized_np)
if isinstance(serialized_np, bytes):
Expand Down
Loading