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

Fix parallelization test #37

Merged
merged 9 commits into from
Dec 4, 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
35 changes: 28 additions & 7 deletions cadet/cadet.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ class CadetMeta(type):
This meta class allows setting the `cadet_path` attribute for all instances of the
`Cadet` class.
"""
use_dll = False
cadet_cli_path = None
cadet_dll_path = None
cadet_create_lwe_path = None

@property
def cadet_path(cls) -> Optional[Path]:
Expand All @@ -131,7 +135,7 @@ def cadet_path(cls) -> Optional[Path]:
return None

@cadet_path.setter
def cadet_path(cls, cadet_path: os.PathLike) -> None:
def cadet_path(cls, cadet_path: Optional[os.PathLike]) -> None:
"""
Set the CADET path and initialize the appropriate runner.

Expand All @@ -145,13 +149,20 @@ def cadet_path(cls, cadet_path: os.PathLike) -> None:
If the path is a DLL, a `CadetDLLRunner` runner is used.
Otherwise, a `CadetFileRunner` runner is used.
"""
cadet_path = Path(cadet_path)

warnings.warn(
"Support for setting cadet.cadet_path will be removed in a future version. "
"TODO: What alternative should be used?",
"Support for setting Cadet.cadet_path will be removed in a future version. "
"Please set the `install_path` on instance level.",
DeprecationWarning
)
if cadet_path is None:
cls.use_dll = False
cls._install_path = None
cls.cadet_cli_path = None
cls.cadet_dll_path = None
cls.cadet_create_lwe_path = None
return

cadet_path = Path(cadet_path)

cls.use_dll = cadet_path.suffix in [".dll", ".so"]

Expand Down Expand Up @@ -202,6 +213,9 @@ def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, *
self.cadet_create_lwe_path: Optional[Path] = None
self.return_information: Optional[dict] = None

self._cadet_cli_runner: Optional[CadetCLIRunner] = None
self._cadet_dll_runner: Optional[CadetDLLRunner] = None

# Regardless of settings in the Meta Class, if we get an install_path, we respect the install_path
if install_path is not None:
self.use_dll = use_dll
Expand Down Expand Up @@ -314,9 +328,9 @@ def cadet_path(self, cadet_path: os.PathLike) -> None:
Otherwise, a `CadetFileRunner` runner is used.
"""
cadet_path = Path(cadet_path)

warnings.warn(
"Deprecation warning: Support for setting cadet.cadet_path will be removed in a future version.",
"Deprecation warning: Support for setting cadet.cadet_path will be removed "
" in a future version. Use `install_path` instead.",
FutureWarning
)
self.install_path = cadet_path
Expand Down Expand Up @@ -523,6 +537,13 @@ def clear(self) -> None:
if runner is not None:
runner.clear()

def delete_file(self) -> None:
if self.filename is not None:
try:
os.remove(self.filename)
except FileNotFoundError:
pass

def __del__(self):
self.clear()
del self._cadet_dll_runner
Expand Down
6 changes: 0 additions & 6 deletions tests/test_autodetection.py

This file was deleted.

4 changes: 2 additions & 2 deletions tests/test_install_path_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_install_path():
def test_dll_runner_attrs():
cadet = Cadet(full_path_dll.parent.parent)
cadet_runner = cadet._cadet_dll_runner
assert re.match("\d\.\d\.\d", cadet_runner.cadet_version)
assert re.match(r"\d\.\d\.\d", cadet_runner.cadet_version)
assert isinstance(cadet_runner.cadet_branch, str)
assert isinstance(cadet_runner.cadet_build_type, str | None)
assert isinstance(cadet_runner.cadet_commit_hash, str)
Expand All @@ -58,7 +58,7 @@ def test_dll_runner_attrs():
def test_cli_runner_attrs():
cadet = Cadet(full_path_dll.parent.parent)
cadet_runner = cadet._cadet_cli_runner
assert re.match("\d\.\d\.\d", cadet_runner.cadet_version)
assert re.match(r"\d\.\d\.\d", cadet_runner.cadet_version)
assert isinstance(cadet_runner.cadet_branch, str)
assert isinstance(cadet_runner.cadet_build_type, str | None)
assert isinstance(cadet_runner.cadet_commit_hash, str)
Expand Down
9 changes: 8 additions & 1 deletion tests/test_meta_class_install_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ def test_meta_class():
assert sim.use_dll
assert sim.install_path == full_path_dll.parent.parent
assert sim.cadet_dll_path == full_path_dll
assert sim.cadet_cli_path.parent == full_path_dll.parent
assert sim.cadet_cli_path.parent.parent == full_path_dll.parent.parent

# With an install path given, the sim instance should use the given install path
sim = Cadet(install_path=install_path_conda)
assert sim.install_path == install_path_conda
assert sim.cadet_dll_path.parent.parent == install_path_conda
assert sim.cadet_cli_path.parent.parent == install_path_conda

# Reset Path
Cadet.cadet_path = None


if __name__ == "__main__":
pytest.main([__file__])
6 changes: 6 additions & 0 deletions tests/test_parallelization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
def run_simulation(model):
model.save()
data = model.run_load()
model.delete_file()

return data


Expand Down Expand Up @@ -38,3 +40,7 @@ def test_parallelization_simulation():
delayed(run_simulation)(model, ) for model in models
)
assert results_sequential == results_parallel


if __name__ == "__main__":
pytest.main([__file__])
Loading