Skip to content

Commit ae7bdd0

Browse files
tensorboard handling
1 parent e0cc650 commit ae7bdd0

4 files changed

Lines changed: 77 additions & 5 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
77

88
[project]
99
name = "spotpython"
10-
version = "0.14.63"
10+
version = "0.14.64"
1111
authors = [
1212
{ name="T. Bartz-Beielstein", email="tbb@bartzundbartz.de" }
1313
]

src/spotPython/utils/init.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def fun_control_init(
7474
target_column=None,
7575
target_type=None,
7676
task=None,
77+
tensorboard_start=False,
78+
tensorboard_stop=False,
7779
test=None,
7880
test_seed=1234,
7981
test_size=0.4,
@@ -228,6 +230,10 @@ def fun_control_init(
228230
Default is None.
229231
TENSORBOARD_CLEAN (bool):
230232
Whether to clean (delete) the tensorboard folder or not. Default is False.
233+
tensorboard_start (bool):
234+
Whether to start the tensorboard or not. Default is False.
235+
tensorboard_stop (bool):
236+
Whether to stop the tensorboard or not. Default is False.
231237
test (object):
232238
The test data set for spotRiver. Default is None.
233239
test_seed (int):
@@ -340,6 +346,7 @@ def fun_control_init(
340346
"DATASET_PATH": DATASET_PATH,
341347
"RESULTS_PATH": RESULTS_PATH,
342348
"TENSORBOARD_PATH": TENSORBOARD_PATH,
349+
"TENSORBOARD_CLEAN": TENSORBOARD_CLEAN,
343350
"_L_in": _L_in,
344351
"_L_out": _L_out,
345352
"_torchmetric": _torchmetric,
@@ -403,6 +410,8 @@ def fun_control_init(
403410
"target_column": target_column,
404411
"target_type": target_type,
405412
"task": task,
413+
"tensorboard_start": tensorboard_start,
414+
"tensorboard_stop": tensorboard_stop,
406415
"test": test,
407416
"test_seed": test_seed,
408417
"test_size": test_size,

src/spotPython/utils/tensorboard.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def start_tensorboard() -> subprocess.Popen:
88
process: The process of the tensorboard server.
99
1010
Examples:
11+
>>> from spotPython.utils.tensorboard import start_tensorboard
1112
>>> process = start_tensorboard()
1213
1314
"""
@@ -17,17 +18,22 @@ def start_tensorboard() -> subprocess.Popen:
1718

1819

1920
def stop_tensorboard(process) -> None:
20-
"""Stops a tensorboard server.
21+
"""
22+
Stops a tensorboard server if the process exists.
2123
2224
Args:
23-
process (subprocess.Popen):
24-
The process of the tensorboard server.
25+
process (subprocess.Popen): The process of the tensorboard server.
2526
2627
Returns:
2728
None
2829
2930
Examples:
31+
>>> from spotPython.utils.tensorboard import start_tensorboard, stop_tensorboard
3032
>>> process = start_tensorboard()
3133
>>> stop_tensorboard(process)
3234
"""
33-
process.terminate()
35+
if process is not None and process.poll() is None:
36+
process.terminate()
37+
process.wait() # Ensure the process has terminated
38+
else:
39+
print("No active tensorboard process found or the process is already terminated.")
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from spotPython.utils.tensorboard import start_tensorboard, stop_tensorboard
2+
3+
import subprocess
4+
from unittest.mock import patch, MagicMock
5+
import pytest
6+
7+
8+
@patch("subprocess.Popen")
9+
def test_start_tensorboard(mock_popen):
10+
# Arrange
11+
mock_process = MagicMock()
12+
mock_popen.return_value = mock_process
13+
14+
# Act
15+
process = start_tensorboard()
16+
17+
# Assert
18+
mock_popen.assert_called_once_with(["tensorboard", "--logdir=./runs"])
19+
assert process == mock_process
20+
21+
22+
@patch("subprocess.Popen")
23+
def test_stop_tensorboard_active_process(mock_popen):
24+
# Arrange
25+
mock_process = MagicMock()
26+
mock_process.poll.return_value = None # Simulate that the process is still running
27+
28+
# Act
29+
stop_tensorboard(mock_process)
30+
31+
# Assert
32+
mock_process.terminate.assert_called_once()
33+
mock_process.wait.assert_called_once()
34+
35+
36+
@patch("subprocess.Popen")
37+
def test_stop_tensorboard_no_process(mock_popen):
38+
# Arrange
39+
mock_process = MagicMock()
40+
mock_process.poll.return_value = 0 # Simulate that the process is already terminated
41+
42+
# Act
43+
stop_tensorboard(mock_process)
44+
45+
# Assert
46+
mock_process.terminate.assert_not_called()
47+
mock_process.wait.assert_not_called()
48+
49+
50+
@patch("subprocess.Popen")
51+
def test_stop_tensorboard_no_process_provided(mock_popen):
52+
# Act
53+
stop_tensorboard(None)
54+
55+
# Assert
56+
# Nothing to assert because the function should just print a message, but ensuring no errors
57+
assert True

0 commit comments

Comments
 (0)