Skip to content

Commit

Permalink
added get_log_basename to process API
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasWeise committed Nov 1, 2023
1 parent de1560d commit 943a6d3
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 2 deletions.
8 changes: 8 additions & 0 deletions moptipy/api/_process_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ def _after_init(self) -> None:
if self.__timer is not None:
self.__timer.start()

def get_log_basename(self) -> str | None:
lf: Final[str | None] = self.__log_file
if lf is None:
return None
lid = lf.rfind(".")
lis = lf.rfind("/")
return lf[:lid] if (lid > 0) and (lid > lis) else lf

def get_random(self) -> Generator:
return self.__random

Expand Down
4 changes: 3 additions & 1 deletion moptipy/api/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,7 @@ def execute(self) -> Process:
algorithm.solve(process) # apply the algorithm
except Exception as be:
# noinspection PyProtectedMember
process._caught = be
if process._caught is None:
# noinspection PyProtectedMember
process._caught = be
return process
18 changes: 18 additions & 0 deletions moptipy/api/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,24 @@ def add_log_section(self, title: str, text: str) -> None:
:param text: the text to log
"""

def get_log_basename(self) -> str | None:
"""
Get the basename of the log, if any.
If a log file is associated with this process, then this function
returns the name of the log file without the file suffix. If no log
file is associated with the process, then `None` is returned.
This can be used to store additional information during the run of
the optimization algorithm. However, treat this carefully, as some
files with the same base name may exist or be generated by other
modules.
:returns: the path to the log file without the file suffix if a log
file is associated with the process, or `None` otherwise
"""
return None

def initialize(self) -> None:
"""
Raise an error because this method shall never be called.
Expand Down
2 changes: 1 addition & 1 deletion moptipy/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from typing import Final

#: the version string of `moptipy`
__version__: Final[str] = "0.9.96"
__version__: Final[str] = "0.9.97"
2 changes: 2 additions & 0 deletions tests/api/test_mo_process_no_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def test_mo_process_no_ss_no_log() -> None:
.set_algorithm(algorithm)\
.set_max_fes(100)\
.execute() as process:
assert process.get_log_basename() is None
assert isinstance(process, MOProcess)
assert type_name_of(process) \
== "moptipy.api._mo_process_no_ss._MOProcessNoSS"
Expand Down Expand Up @@ -114,6 +115,7 @@ def test_mo_process_no_ss_log() -> None:
.set_log_file(tf)\
.execute() as process:
assert isinstance(process, MOProcess)
assert process.get_log_basename() is not None
assert type_name_of(process) \
== "moptipy.api._mo_process_no_ss._MOProcessNoSS"
assert str(process) == "MOProcessWithoutSearchSpace"
Expand Down
5 changes: 5 additions & 0 deletions tests/api/test_mo_process_no_ss_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def test_mo_process_no_ss_log() -> None:
assert exists(tf)
assert isfile(tf)

lid = tf.rfind(".")
lis = tf.rfind("/")
tfn: str = tf[:lid] if (lid > 0) and (lid > lis) else tf

archive_len: int
with MOExecution() \
.set_solution_space(space) \
Expand All @@ -57,6 +61,7 @@ def test_mo_process_no_ss_log() -> None:
.set_log_file(tf)\
.set_log_improvements(True)\
.execute() as process:
assert process.get_log_basename() == tfn
assert isinstance(process, MOProcess)
assert type_name_of(process) \
== "moptipy.api._mo_process_no_ss_log._MOProcessNoSSLog"
Expand Down
2 changes: 2 additions & 0 deletions tests/api/test_mo_process_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_mo_process_mo_ss_no_log() -> None:
.set_max_fes(100)\
.set_archive_max_size(ams)\
.execute() as process:
assert process.get_log_basename() is None
assert type_name_of(process) \
== "moptipy.api._mo_process_ss._MOProcessSS"
assert str(process) == "MOProcessWithSearchSpace"
Expand Down Expand Up @@ -121,6 +122,7 @@ def test_mo_process_ss_log() -> None:
+ ((f1.upper_bound() + 1) * (1 + f0.upper_bound()))
assert 0 < process.get_consumed_fes() <= 100
assert process.has_log()
assert process.get_log_basename() is not None
archive: list[MORecord] = process.get_archive()
for rec in archive:
assert f0.lower_bound() <= rec.fs[0] <= f0.upper_bound()
Expand Down
4 changes: 4 additions & 0 deletions tests/api/test_mo_process_ss_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ def test_process_ss_log_all() -> None:
with TempFile.create(suffix=FILE_SUFFIX) as tf:
assert exists(tf)
assert isfile(tf)
lid = tf.rfind(".")
lis = tf.rfind("/")
tfn: str = tf[:lid] if (lid > 0) and (lid > lis) else tf
archive_len: int
with MOExecution()\
.set_search_space(search_space)\
Expand All @@ -173,6 +176,7 @@ def test_process_ss_log_all() -> None:
.set_log_file(tf)\
.set_log_all_fes(True)\
.execute() as process:
assert process.get_log_basename() == tfn
assert type_name_of(process) \
== "moptipy.api._mo_process_ss_log._MOProcessSSLog"
assert str(process) == "MOLoggingProcessWithSearchSpace"
Expand Down
2 changes: 2 additions & 0 deletions tests/api/test_process_no_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_process_no_ss_no_log() -> None:
.set_algorithm(algorithm)\
.set_max_fes(100)\
.execute() as process:
assert process.get_log_basename() is None
assert type_name_of(process) \
== "moptipy.api._process_no_ss._ProcessNoSS"
assert str(process) == "ProcessWithoutSearchSpace"
Expand Down Expand Up @@ -73,6 +74,7 @@ def test_process_no_ss_log() -> None:
.set_max_time_millis(20)\
.set_log_file(tf)\
.execute() as process:
assert process.get_log_basename() is not None
assert type_name_of(process) \
== "moptipy.api._process_no_ss._ProcessNoSS"
assert str(process) == "ProcessWithoutSearchSpace"
Expand Down
5 changes: 5 additions & 0 deletions tests/api/test_process_no_ss_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def test_process_no_ss_log_log() -> None:
assert exists(tf)
assert isfile(tf)

lid = tf.rfind(".")
lis = tf.rfind("/")
tfn: str = tf[:lid] if (lid > 0) and (lid > lis) else tf

with Execution()\
.set_solution_space(space)\
.set_objective(objective)\
Expand All @@ -45,6 +49,7 @@ def test_process_no_ss_log_log() -> None:
.set_log_file(tf)\
.set_log_improvements(True) \
.execute() as process:
assert process.get_log_basename() == tfn
assert type_name_of(process) \
== "moptipy.api._process_no_ss_log._ProcessNoSSLog"
assert str(process) == "LoggingProcessWithoutSearchSpace"
Expand Down
1 change: 1 addition & 0 deletions tests/api/test_process_ss.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_process_ss_no_log() -> None:
== "moptipy.api._process_ss._ProcessSS"
assert str(process) == "ProcessWithSearchSpace"
assert process.has_best()
assert process.get_log_basename() is None
assert not process.has_log()
assert process.get_max_fes() == 100
assert process.get_max_time_millis() is None
Expand Down
5 changes: 5 additions & 0 deletions tests/api/test_process_ss_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def test_process_ss_log_log() -> None:
with TempFile.create() as tf:
assert exists(tf)
assert isfile(tf)
lid = tf.rfind(".")
lis = tf.rfind("/")
tfn: str = tf[:lid] if (lid > 0) and (lid > lis) else tf

with Execution()\
.set_search_space(search_space)\
.set_solution_space(solution_space)\
Expand All @@ -57,6 +61,7 @@ def test_process_ss_log_log() -> None:
.set_log_file(tf)\
.set_log_improvements(True)\
.execute() as process:
assert process.get_log_basename() == tfn
assert type_name_of(process) \
== "moptipy.api._process_ss_log._ProcessSSLog"
assert str(process) == "LoggingProcessWithSearchSpace"
Expand Down

0 comments on commit 943a6d3

Please sign in to comment.