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

Cache generate_conformers with hash_molecule_args_and_kwargs approach #251

Merged
merged 3 commits into from
Mar 8, 2022
Merged
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
12 changes: 6 additions & 6 deletions src/smirnoff_hack.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ def at_cached_assign_partial_charges(self, molecule, *args, **kwargs):
# cache the OE generate_conformers function (save 15s)
OE_TOOLKIT_CACHE_molecule_conformers = {}
oe_original_generate_conformers = OpenEyeToolkitWrapper.generate_conformers
def oe_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=None, clear_existing=True):
cache_key = hash((hash_molecule(molecule), n_conformers, str(rms_cutoff), clear_existing))
def oe_cached_generate_conformers(self, molecule, *args, **kwargs):
cache_key = hash_molecule_args_and_kwargs(molecule, args, kwargs)
if cache_key not in OE_TOOLKIT_CACHE_molecule_conformers:
oe_original_generate_conformers(self, molecule, n_conformers=n_conformers, rms_cutoff=rms_cutoff, clear_existing=clear_existing)
oe_original_generate_conformers(self, molecule, *args, **kwargs)
OE_TOOLKIT_CACHE_molecule_conformers[cache_key] = molecule._conformers
molecule._conformers = OE_TOOLKIT_CACHE_molecule_conformers[cache_key]
OpenEyeToolkitWrapper.generate_conformers = oe_cached_generate_conformers
Expand All @@ -137,10 +137,10 @@ def oe_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=Non
# cache the RDKit generate_conformers function
RDK_TOOLKIT_CACHE_molecule_conformers = {}
rdk_original_generate_conformers = RDKitToolkitWrapper.generate_conformers
def rdk_cached_generate_conformers(self, molecule, n_conformers=1, rms_cutoff=None, clear_existing=True):
cache_key = hash((hash_molecule(molecule), n_conformers, str(rms_cutoff), clear_existing))
def rdk_cached_generate_conformers(self, molecule, *args, **kwargs):
cache_key = hash_molecule_args_and_kwargs(molecule, args, kwargs)
if cache_key not in RDK_TOOLKIT_CACHE_molecule_conformers:
rdk_original_generate_conformers(self, molecule, n_conformers=n_conformers, rms_cutoff=rms_cutoff, clear_existing=clear_existing)
rdk_original_generate_conformers(self, molecule, *args, **kwargs)
RDK_TOOLKIT_CACHE_molecule_conformers[cache_key] = molecule._conformers
molecule._conformers = RDK_TOOLKIT_CACHE_molecule_conformers[cache_key]
RDKitToolkitWrapper.generate_conformers = rdk_cached_generate_conformers
Expand Down