-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'test/saving-loading-states' into feat/reintegrate-workflow
# Conflicts: # .pre-commit-config.yaml # pyproject.toml # tests/test_serializer.py
- Loading branch information
Showing
11 changed files
with
626 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from hypothesis import Verbosity, settings | ||
|
||
settings.register_profile("ci", max_examples=1000) | ||
settings.register_profile("debug", max_examples=10, verbosity=Verbosity.verbose) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import yaml | ||
|
||
|
||
def dump(data, file): | ||
yaml.dump(data, file, Dumper=yaml.Dumper) | ||
return | ||
|
||
|
||
def load(file): | ||
data = yaml.load(file, Loader=yaml.Loader) | ||
return data | ||
|
||
|
||
def dumps(data): | ||
string = yaml.dump(data, Dumper=yaml.Dumper) | ||
return string | ||
|
||
|
||
def loads(string): | ||
data = yaml.load(string, Loader=yaml.Loader) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,86 @@ | ||
import importlib | ||
import logging | ||
import pathlib | ||
import tempfile | ||
import uuid | ||
from collections import namedtuple | ||
|
||
from hypothesis import Verbosity, given, settings | ||
from hypothesis import strategies as st | ||
import hypothesis.strategies as st | ||
|
||
from autora.serializer import SerializersSupported, dump_state, load_state | ||
from autora.state import StandardState | ||
logger = logging.getLogger(__name__) | ||
|
||
_SUPPORTED_SERIALIZERS = [ | ||
("pickle", "b"), | ||
("dill", "b"), | ||
("autora.serializer.yaml_", ""), | ||
] | ||
_SERIALIZER_DEF = namedtuple("_SERIALIZER_DEF", ["name", "module", "file_type"]) | ||
_AVAILABLE_SERIALIZERS = [] | ||
|
||
@given( | ||
st.builds(StandardState, st.text(), st.text(), st.text(), st.lists(st.integers())), | ||
st.sampled_from(SerializersSupported), | ||
) | ||
@settings(verbosity=Verbosity.verbose) | ||
def test_load_inverts_dump(s, serializer): | ||
"""Test that each serializer can be used to serialize and deserialize a state object.""" | ||
with tempfile.TemporaryDirectory() as dir: | ||
path = pathlib.Path(dir, f"{str(uuid.uuid4())}") | ||
print(path, s) | ||
for module_name, file_type in _SUPPORTED_SERIALIZERS: | ||
try: | ||
module = importlib.import_module(module_name) | ||
except ImportError: | ||
logger.info(f"serializer {module} not available") | ||
continue | ||
_AVAILABLE_SERIALIZERS.append(_SERIALIZER_DEF(module_name, module, file_type)) | ||
|
||
dump_state(s, path, dumper=serializer) | ||
assert load_state(path, loader=serializer) == s | ||
AVAILABLE_SERIALIZERS = st.sampled_from(_AVAILABLE_SERIALIZERS) | ||
|
||
|
||
@st.composite | ||
def serializer_loads_dumps_strategy(draw): | ||
serializer = draw(AVAILABLE_SERIALIZERS) | ||
loads, dumps = serializer.module.loads, serializer.module.dumps | ||
return loads, dumps | ||
|
||
|
||
@st.composite | ||
def serializer_dump_load_string_strategy(draw): | ||
"""Strategy returns a function which dumps an object and reloads it via a bytestream.""" | ||
serializer = draw(AVAILABLE_SERIALIZERS) | ||
loads, dumps = serializer.module.loads, serializer.module.dumps | ||
|
||
def _load_dump_via_string(o): | ||
logger.info(f"load dump via string using {serializer.module=}") | ||
return loads(dumps(o)) | ||
|
||
return _load_dump_via_string | ||
|
||
|
||
@st.composite | ||
def serializer_dump_load_binary_file_strategy(draw): | ||
"""Strategy returns a function which dumps an object reloads it via a temporary binary file.""" | ||
serializer = draw(AVAILABLE_SERIALIZERS) | ||
load, dump = serializer.module.load, serializer.module.dump | ||
|
||
def _load_dump_via_disk(o): | ||
logger.info(f"load dump via disk using {serializer.module=}") | ||
with tempfile.TemporaryDirectory() as tempdir: | ||
filename = str(uuid.uuid1()) | ||
with open(pathlib.Path(tempdir, filename), f"w{serializer.file_type}") as f: | ||
dump(o, f) | ||
with open(pathlib.Path(tempdir, filename), f"r{serializer.file_type}") as f: | ||
o_loaded = load(f) | ||
return o_loaded | ||
|
||
return _load_dump_via_disk | ||
|
||
|
||
@st.composite | ||
def serializer_dump_load_strategy(draw): | ||
"""Strategy returns a function which dumps an object and reloads it via a supported method.""" | ||
_dump_load = draw( | ||
st.one_of( | ||
serializer_dump_load_string_strategy(), | ||
serializer_dump_load_binary_file_strategy(), | ||
) | ||
) | ||
return _dump_load | ||
|
||
|
||
if __name__ == "__main__": | ||
o = list("abcde") | ||
loader_dumper_disk = serializer_dump_load_strategy().example() | ||
o_loaded = loader_dumper_disk(o) | ||
print(o, o_loaded) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import logging | ||
|
||
import pandas as pd | ||
from hypothesis import HealthCheck, given, settings | ||
|
||
from autora.state import StandardStateDataClass | ||
|
||
from .test_serializer import serializer_dump_load_strategy | ||
from .test_strategies import standard_state_dataclass_strategy | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@given(standard_state_dataclass_strategy(), serializer_dump_load_strategy()) | ||
@settings(suppress_health_check={HealthCheck.too_slow}, deadline=500) | ||
def test_state_serialize_deserialize(o: StandardStateDataClass, dump_load): | ||
o_loaded = dump_load(o) | ||
assert o.variables == o_loaded.variables | ||
assert pd.DataFrame.equals(o.conditions, o_loaded.conditions) | ||
assert pd.DataFrame.equals(o.experiment_data, o_loaded.experiment_data) |
Oops, something went wrong.