Skip to content

Commit 46e2e67

Browse files
Change load and run interface (#51)
- Change run and load interface to run_simulation and load_from_file - Update Formatting and docstrings - Move delete_file to H5 class --------- Co-authored-by: Johannes Schmölder <[email protected]>
1 parent 93c4ebe commit 46e2e67

9 files changed

+172
-70
lines changed

cadet/cadet.py

+97-45
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ def is_dll(path: os.PathLike) -> bool:
3131
return suffix in {'.so', '.dll'}
3232

3333

34-
def install_path_to_cadet_paths(
35-
install_path: Optional[os.PathLike],
34+
def resolve_cadet_paths(
35+
install_path: Optional[os.PathLike]
3636
) -> tuple[Optional[Path], Optional[Path], Optional[Path], Optional[Path]]:
3737
"""
38-
Get the correct paths (root_path, cadet_cli_path, cadet_dll_path, cadet_create_lwe_path)
39-
from the installation path of CADET. This is extracted into a function to be used
40-
in both the meta class and the cadet class itself.
38+
Resolve paths from the installation path of CADET.
4139
4240
Parameters
4341
----------
@@ -48,7 +46,7 @@ def install_path_to_cadet_paths(
4846
Returns
4947
-------
5048
tuple[Optional[Path], Optional[Path], Optional[Path], Optional[Path]]
51-
Tuple with CADET installation paths
49+
tuple with CADET installation paths
5250
(root_path, cadet_cli_path, cadet_dll_path, cadet_create_lwe_path)
5351
"""
5452
if install_path is None:
@@ -75,16 +73,16 @@ def install_path_to_cadet_paths(
7573
lwe_executable += '.exe'
7674

7775
cadet_cli_path = cadet_root / 'bin' / cli_executable
78-
if cadet_cli_path.is_file():
79-
cadet_cli_path = cadet_cli_path
80-
else:
76+
if not cadet_cli_path.is_file():
8177
raise FileNotFoundError(
82-
"CADET could not be found. Please check the path."
78+
"CADET CLI could not be found. Please check the path."
8379
)
8480

8581
cadet_create_lwe_path = cadet_root / 'bin' / lwe_executable
86-
if cadet_create_lwe_path.is_file():
87-
cadet_create_lwe_path = cadet_create_lwe_path.as_posix()
82+
if not cadet_create_lwe_path.is_file():
83+
raise FileNotFoundError(
84+
"CADET createLWE could not be found. Please check the path."
85+
)
8886

8987
if platform.system() == 'Windows':
9088
dll_path = cadet_root / 'bin' / 'cadet.dll'
@@ -97,10 +95,7 @@ def install_path_to_cadet_paths(
9795
if not dll_path.is_file() and dll_debug_path.is_file():
9896
dll_path = dll_debug_path
9997

100-
if dll_path.is_file():
101-
cadet_dll_path = dll_path
102-
else:
103-
cadet_dll_path = None
98+
cadet_dll_path = dll_path if dll_path.is_file() else None
10499

105100
return root_path, cadet_cli_path, cadet_dll_path, cadet_create_lwe_path
106101

@@ -112,6 +107,7 @@ class CadetMeta(type):
112107
This meta class allows setting the `cadet_path` attribute for all instances of the
113108
`Cadet` class.
114109
"""
110+
115111
use_dll = False
116112
cadet_cli_path = None
117113
cadet_dll_path = None
@@ -166,9 +162,10 @@ def cadet_path(cls, cadet_path: Optional[os.PathLike]) -> None:
166162

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

169-
_install_path, cadet_cli_path, cadet_dll_path, cadet_create_lwe_path = install_path_to_cadet_paths(cadet_path)
165+
install_path, cadet_cli_path, cadet_dll_path, cadet_create_lwe_path = \
166+
resolve_cadet_paths(cadet_path)
170167

171-
cls._install_path = _install_path
168+
cls._install_path = install_path
172169
cls.cadet_create_lwe_path = cadet_create_lwe_path
173170
cls.cadet_cli_path = cadet_cli_path
174171
cls.cadet_dll_path = cadet_dll_path
@@ -178,8 +175,9 @@ class Cadet(H5, metaclass=CadetMeta):
178175
"""
179176
CADET interface class.
180177
181-
This class manages the CADET runner, whether it's based on a CLI executable or a DLL,
182-
and provides methods for running simulations and loading results.
178+
This class manages the CADET runner, whether it's based on the CLI executable or
179+
the in-memory interface and provides methods for running simulations and loading
180+
results.
183181
184182
Attributes
185183
----------
@@ -195,9 +193,15 @@ class Cadet(H5, metaclass=CadetMeta):
195193
Stores the information returned after a simulation run.
196194
"""
197195

198-
def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, *data):
196+
def __init__(
197+
self,
198+
install_path: Optional[Path] = None,
199+
use_dll: bool = False,
200+
*data
201+
) -> None:
199202
"""
200203
Initialize a new instance of the Cadet class.
204+
201205
Priority order of install_paths is:
202206
1. install_path set in __init__ args
203207
2. install_path set in CadetMeta
@@ -216,13 +220,14 @@ def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, *
216220
self._cadet_cli_runner: Optional[CadetCLIRunner] = None
217221
self._cadet_dll_runner: Optional[CadetDLLRunner] = None
218222

219-
# Regardless of settings in the Meta Class, if we get an install_path, we respect the install_path
223+
# Regardless of settings in the Meta Class, if we get an install_path, we
224+
# respect the install_path
220225
if install_path is not None:
221226
self.use_dll = use_dll
222-
self.install_path = install_path # This will set _cadet_dll_runner and _cadet_cli_runner
227+
self.install_path = install_path # This will automatically set the runners.
223228
return
224229

225-
# If _cadet_cli_runner_class has been set in the Meta Class, use them, else instantiate Nones
230+
# Use CLIRunner of the Meta class, if provided.
226231
if hasattr(self, "cadet_cli_path") and self.cadet_cli_path is not None:
227232
self._cadet_cli_runner: Optional[CadetCLIRunner] = CadetCLIRunner(
228233
self.cadet_cli_path
@@ -231,6 +236,7 @@ def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, *
231236
self._cadet_cli_runner: Optional[CadetCLIRunner] = None
232237
self.use_dll = use_dll
233238

239+
# Use DLLRunner of the Meta class, if provided.
234240
if hasattr(self, "cadet_dll_path") and self.cadet_dll_path is not None:
235241
try:
236242
self._cadet_dll_runner: Optional[CadetDLLRunner] = CadetDLLRunner(
@@ -244,11 +250,10 @@ def __init__(self, install_path: Optional[Path] = None, use_dll: bool = False, *
244250
self._cadet_dll_runner: Optional[CadetCLIRunner] = None
245251
self.use_dll = use_dll
246252

247-
# If any runner has been set in the Meta Class, don't auto-detect Cadet, just return
248253
if self._cadet_cli_runner is not None or self._cadet_dll_runner is not None:
249254
return
250255

251-
# If neither Meta Class nor install_path are given: auto-detect Cadet
256+
# Auto-detect Cadet if neither Meta Class nor install_path are given.
252257
self.install_path = self.autodetect_cadet()
253258

254259
@property
@@ -271,7 +276,7 @@ def install_path(self, install_path: Optional[os.PathLike]) -> None:
271276
Parameters
272277
----------
273278
install_path : Optional[os.PathLike]
274-
Path to the root of the CADET installation or the executable file 'cadet-cli'.
279+
Path to the root of the CADET installation or the 'cadet-cli' executable.
275280
If a file path is provided, the root directory will be inferred.
276281
"""
277282
if install_path is None:
@@ -281,7 +286,8 @@ def install_path(self, install_path: Optional[os.PathLike]) -> None:
281286
self.cadet_create_lwe_path = None
282287
return
283288

284-
root_path, cadet_cli_path, cadet_dll_path, create_lwe_path = install_path_to_cadet_paths(install_path)
289+
root_path, cadet_cli_path, cadet_dll_path, create_lwe_path = \
290+
resolve_cadet_paths(install_path)
285291

286292
self._install_path = root_path
287293
self.cadet_create_lwe_path = create_lwe_path
@@ -376,7 +382,6 @@ def cadet_runner(self) -> CadetRunnerBase:
376382
Optional[CadetRunnerBase]
377383
The current runner instance, either a DLL or file-based runner.
378384
"""
379-
380385
if self.use_dll and self.found_dll:
381386
return self._cadet_dll_runner
382387

@@ -422,10 +427,10 @@ def create_lwe(self, file_path=None):
422427

423428
self.filename = file_path
424429

425-
self.load()
430+
self.load_from_file()
426431

427432
if file_path_input is None:
428-
os.remove(file_path)
433+
self.delete_file()
429434

430435
return self
431436

@@ -487,20 +492,53 @@ def run_load(
487492
timeout : Optional[int]
488493
Maximum time allowed for the simulation to run, in seconds.
489494
clear : bool
490-
If True, clear previous results after loading new ones.
495+
If True, clear the simulation results from the current runner instance.
491496
492497
Returns
493498
-------
494499
ReturnInformation
495500
Information about the simulation run.
496501
"""
497-
return_information = self.run(timeout)
502+
warnings.warn(
503+
"Cadet.run_load() will be removed in a future release. "
504+
"Please use Cadet.run_simulation()",
505+
category=FutureWarning
506+
)
507+
return_information = self.run_simulation(timeout=timeout, clear=clear)
508+
509+
return return_information
510+
511+
def run_simulation(
512+
self,
513+
timeout: Optional[int] = None,
514+
clear: bool = True
515+
) -> ReturnInformation:
516+
"""
517+
Run the CADET simulation and load the results.
518+
519+
Parameters
520+
----------
521+
timeout : Optional[int]
522+
Maximum time allowed for the simulation to run, in seconds.
523+
clear : bool
524+
If True, clear the simulation results from the current runner instance.
525+
526+
Returns
527+
-------
528+
ReturnInformation
529+
Information about the simulation run.
530+
"""
531+
return_information = self.cadet_runner.run(
532+
simulation=self,
533+
timeout=timeout
534+
)
498535

499536
if return_information.return_code == 0:
500-
self.load_results()
537+
self.cadet_runner.load_results(self)
501538

502539
if clear:
503540
self.clear()
541+
504542
return return_information
505543

506544
def run(
@@ -520,6 +558,11 @@ def run(
520558
ReturnInformation
521559
Information about the simulation run.
522560
"""
561+
warnings.warn(
562+
"Cadet.run() will be removed in a future release. \n"
563+
"Please use Cadet.run_simulation()",
564+
category=FutureWarning
565+
)
523566
return_information = self.cadet_runner.run(
524567
self,
525568
timeout=timeout,
@@ -529,23 +572,32 @@ def run(
529572

530573
def load_results(self) -> None:
531574
"""Load the results of the last simulation run into the current instance."""
532-
runner = self.cadet_runner
533-
if runner is not None:
534-
runner.load_results(self)
575+
warnings.warn(
576+
"Cadet.load_results() will be removed in a future release. \n"
577+
"Please use Cadet.load_from_file() to load results from a file "
578+
"or use Cadet.run_simulation() to run a simulation and directly load the "
579+
"simulation results.",
580+
category=FutureWarning
581+
)
582+
self.load_from_file()
583+
584+
def load(self) -> None:
585+
"""Load the results of the last simulation run into the current instance."""
586+
warnings.warn(
587+
"Cadet.load() will be removed in a future release. \n"
588+
"Please use Cadet.load_from_file() to load results from a file or use "
589+
"Cadet.run_simulation() to run a simulation and directly load the "
590+
"simulation results.",
591+
category=FutureWarning
592+
)
593+
self.load_from_file()
535594

536595
def clear(self) -> None:
537-
"""Clear the loaded results from the current instance."""
596+
"""Clear the simulation results from the current runner instance."""
538597
runner = self.cadet_runner
539598
if runner is not None:
540599
runner.clear()
541600

542-
def delete_file(self) -> None:
543-
if self.filename is not None:
544-
try:
545-
os.remove(self.filename)
546-
except FileNotFoundError:
547-
pass
548-
549601
def __del__(self):
550602
self.clear()
551603
del self._cadet_dll_runner

cadet/cadet_dll.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1710,9 +1710,9 @@ def __setstate__(self, state):
17101710

17111711
def clear(self) -> None:
17121712
"""
1713-
Clear the current simulation state.
1713+
Clear the simulation results from the current instance.
17141714
1715-
This method deletes the current simulation results and resets the driver.
1715+
Note, this method deletes also resets the driver.
17161716
"""
17171717
if hasattr(self, "res"):
17181718
del self.res

0 commit comments

Comments
 (0)