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

Use get_records_with_cache to cache to_records calls #286

Merged
merged 32 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
8f8c4d9
try storing qcportal cache on clients
ntBre Jun 26, 2024
4c649b2
generate a filename not just directory
ntBre Jun 26, 2024
6d44b85
Revert "generate a filename not just directory"
ntBre Jul 9, 2024
6f3db65
Revert "try storing qcportal cache on clients"
ntBre Jul 9, 2024
02e8484
Merge branch 'main' into get_records_with_cache
ntBre Jul 9, 2024
da52d5e
add outline of CachedPortalClient with signatures from qcportal
ntBre Jul 9, 2024
6555c20
default to using the cache
ntBre Jul 9, 2024
2a9ec5b
fill in todos, ensure record_ids are sequences
ntBre Jul 9, 2024
05e1d9b
if given a single id, these methods have to return a single record
ntBre Jul 9, 2024
fccfb9c
comment out non-functional get_molecules, add note about root issue
ntBre Jul 9, 2024
ce64b0f
ensure unpack has a value
ntBre Jul 9, 2024
859ec44
delete get_molecules after ben's confirmation at the meeting
ntBre Jul 9, 2024
09f76c3
factor out default cache dir
ntBre Jul 9, 2024
f8ae094
break the tests to find where updates are needed
ntBre Jul 11, 2024
e408f6d
re-export CachedPortalClient
ntBre Jul 11, 2024
b119fcd
fix get_optimizations and filter test
ntBre Jul 11, 2024
3041e13
tests passing again
ntBre Jul 11, 2024
af7b6f7
unused import
ntBre Jul 11, 2024
c4507c6
add private _no_session manager for CachedPortalClient
ntBre Jul 11, 2024
be7babe
check that the cache works for optimizations and singlepoints
ntBre Jul 11, 2024
a740add
start CachedPortalClient docs, copy init signature from qcportal
ntBre Jul 12, 2024
89ba82f
override __repr__
ntBre Jul 12, 2024
9850fa3
copy over and update docs from qcportal
ntBre Jul 12, 2024
0f7c65e
actually add CachedPortalClient to the docs page
ntBre Jul 12, 2024
136912b
fix link (I hope)
ntBre Jul 12, 2024
9503c21
test that different clients can share a cache_dir
ntBre Jul 16, 2024
fb57acb
restore PortalClient to default, remove now-unused default cache dir
ntBre Jul 16, 2024
bfa7e95
make CachedPortalClient private
ntBre Jul 16, 2024
938bba9
update warning message to be more explicit
ntBre Jul 16, 2024
5f3ff4e
fix unused
ntBre Jul 16, 2024
c63daab
add a warning to portal_client_manager
ntBre Jul 17, 2024
9ab6ed6
move warning up
ntBre Jul 17, 2024
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
3 changes: 2 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ Utilities
:nosignatures:
:toctree: api/generated/

_CachedPortalClient
portal_client_manager

Exceptions
Expand Down Expand Up @@ -262,4 +263,4 @@ Exceptions
PCMSettingError
InvalidDatasetError
DatasetRegisterError
RecordTypeError
RecordTypeError
8 changes: 7 additions & 1 deletion openff/qcsubmit/_tests/results/test_filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import logging
from tempfile import TemporaryDirectory

import numpy
import pytest
Expand Down Expand Up @@ -31,6 +32,7 @@
SMILESFilter,
UnperceivableStereoFilter,
)
from openff.qcsubmit.utils import _CachedPortalClient, portal_client_manager

from . import RecordStatusEnum, SinglepointRecord

Expand Down Expand Up @@ -545,5 +547,9 @@ def test_unperceivable_stereo_filter(toolkits, n_expected, public_client):
)
assert collection.n_results == 1

filtered = collection.filter(UnperceivableStereoFilter(toolkits=toolkits))
with (
TemporaryDirectory() as d,
portal_client_manager(lambda a: _CachedPortalClient(a, cache_dir=d)),
):
filtered = collection.filter(UnperceivableStereoFilter(toolkits=toolkits))
assert filtered.n_results == n_expected
40 changes: 36 additions & 4 deletions openff/qcsubmit/_tests/results/test_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import datetime
from tempfile import TemporaryDirectory

import pytest
from openff.toolkit.topology import Molecule
Expand All @@ -26,6 +27,7 @@
)
from openff.qcsubmit.results.filters import ResultFilter
from openff.qcsubmit.results.results import TorsionDriveResult, _BaseResultCollection
from openff.qcsubmit.utils import _CachedPortalClient, portal_client_manager

from . import (
OptimizationRecord,
Expand Down Expand Up @@ -315,7 +317,33 @@ def test_to_records(
public_client, collection_name, spec_name=spec_name
)
assert collection.n_molecules == expected_n_mols
records_and_molecules = collection.to_records()

def disconnected_client(addr, cache_dir):
ret = _CachedPortalClient(addr, cache_dir)
ret._req_session = None
return ret

with TemporaryDirectory() as d:
client = _CachedPortalClient(public_client.address, d)
with portal_client_manager(lambda _: client):
with (
client._no_session(),
pytest.raises(Exception, match="no attribute 'prepare_request'"),
):
collection.to_records()
records_and_molecules = collection.to_records()
# TorsionDriveResultCollection.to_records requires fetching
# molecules, which cannot currently be cached
j-wags marked this conversation as resolved.
Show resolved Hide resolved
if collection_type is not TorsionDriveResultCollection:
with client._no_session():
assert len(collection.to_records()) == len(records_and_molecules)
# the previous checks show that the *same* client can access
# its cache without making new requests. disconnected_client
# instead shows that a newly-constructed client pointing at the
# same cache_dir can still access the cache
with portal_client_manager(lambda addr: disconnected_client(addr, d)):
assert len(collection.to_records()) == len(records_and_molecules)

assert len(records_and_molecules) == expected_n_recs
record, molecule = records_and_molecules[0]

Expand Down Expand Up @@ -351,9 +379,13 @@ def test_optimization_to_basic_result_collection(public_client):
optimization_result_collection = OptimizationResultCollection.from_server(
public_client, ["OpenFF Gen 2 Opt Set 3 Pfizer Discrepancy"]
)
basic_collection = optimization_result_collection.to_basic_result_collection(
"hessian"
)
with (
TemporaryDirectory() as d,
portal_client_manager(lambda a: _CachedPortalClient(a, d)),
):
basic_collection = optimization_result_collection.to_basic_result_collection(
"hessian"
)
assert basic_collection.n_results == 197
assert basic_collection.n_molecules == 49

Expand Down
16 changes: 13 additions & 3 deletions openff/qcsubmit/_tests/test_submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Here we use the qcfractal snowflake fixture to set up the database.
"""

from tempfile import TemporaryDirectory

import pytest
from openff.toolkit.topology import Molecule
from qcelemental.models.procedures import OptimizationProtocols
Expand Down Expand Up @@ -37,7 +39,7 @@
OptimizationResultCollection,
TorsionDriveResultCollection,
)
from openff.qcsubmit.utils import get_data
from openff.qcsubmit.utils import _CachedPortalClient, get_data, portal_client_manager


def await_results(client, timeout=120, check_fn=PortalClient.get_singlepoints, ids=[1]):
Expand Down Expand Up @@ -1408,7 +1410,11 @@ def test_invalid_cmiles(fulltest_client, factory_type, result_collection_type):
assert ds.specifications.keys() == {"default"}
results = result_collection_type.from_datasets(datasets=ds)
assert results.n_molecules == 1
records = results.to_records()
with (
TemporaryDirectory() as d,
portal_client_manager(lambda a: _CachedPortalClient(a, d)),
):
records = results.to_records()
assert len(records) == 1
# Single points and optimizations look here
fulltest_client.modify_molecule(
Expand All @@ -1427,6 +1433,10 @@ def test_invalid_cmiles(fulltest_client, factory_type, result_collection_type):
ds._cache_data.update_entries(entries)
results = result_collection_type.from_datasets(datasets=ds)
assert results.n_molecules == 1
with pytest.warns(UserWarning, match="invalid CMILES"):
with (
pytest.warns(UserWarning, match="invalid CMILES"),
TemporaryDirectory() as d,
portal_client_manager(lambda a: _CachedPortalClient(a, d)),
):
records = results.to_records()
assert len(records) == 0
2 changes: 2 additions & 0 deletions openff/qcsubmit/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from openff.qcsubmit.utils.utils import (
_CachedPortalClient,
check_missing_stereo,
chunk_generator,
clean_strings,
Expand All @@ -22,4 +23,5 @@
"get_symmetry_classes",
"get_symmetry_group",
"portal_client_manager",
"_CachedPortalClient",
]
Loading
Loading