Skip to content

Commit f6bf9e9

Browse files
authored
Adds progress bar which uses the progress event (#62)
1 parent 05acde5 commit f6bf9e9

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

RATapi/run.py

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,53 @@
11
import time
22

3+
from tqdm import tqdm
4+
35
import RATapi.rat_core
46
from RATapi.inputs import make_input
57
from RATapi.outputs import make_results
68
from RATapi.utils.enums import Display
79

810

11+
class ProgressBar:
12+
"""Creates a progress bar that gets updates from the progress event during a
13+
calculation
14+
15+
Parameters
16+
----------
17+
display : bool, default: True
18+
Indicates if displaying is allowed
19+
20+
"""
21+
22+
def __init__(self, display=True):
23+
self.display = display
24+
25+
def __enter__(self):
26+
if self.display:
27+
RATapi.events.register(RATapi.events.EventTypes.Progress, self.updateProgress)
28+
self.pbar = tqdm(total=100, desc="", delay=1, bar_format="{l_bar}{bar}", ncols=90, disable=not self.display)
29+
self.pbar.delay = 0
30+
return self.pbar
31+
32+
def updateProgress(self, event):
33+
"""Callback for the progress event.
34+
35+
Parameters
36+
----------
37+
event: ProgressEventData
38+
The progress event data.
39+
"""
40+
41+
value = event.percent * 100
42+
self.pbar.desc = event.message
43+
self.pbar.update(value - self.pbar.n)
44+
45+
def __exit__(self, _exc_type, _exc_val, _traceback):
46+
self.pbar.leave = False
47+
if self.display:
48+
RATapi.events.clear(RATapi.events.EventTypes.Progress, self.updateProgress)
49+
50+
951
def run(project, controls):
1052
"""Run RAT for the given project and controls inputs."""
1153
parameter_field = {
@@ -19,23 +61,24 @@ def run(project, controls):
1961
}
2062

2163
horizontal_line = "\u2500" * 107 + "\n"
22-
64+
display_on = controls.display != Display.Off
2365
problem_definition, cells, limits, priors, cpp_controls = make_input(project, controls)
2466

25-
if controls.display != Display.Off:
67+
if display_on:
2668
print("Starting RAT " + horizontal_line)
2769

2870
start = time.time()
29-
problem_definition, output_results, bayes_results = RATapi.rat_core.RATMain(
30-
problem_definition,
31-
cells,
32-
limits,
33-
cpp_controls,
34-
priors,
35-
)
71+
with ProgressBar(display=display_on):
72+
problem_definition, output_results, bayes_results = RATapi.rat_core.RATMain(
73+
problem_definition,
74+
cells,
75+
limits,
76+
cpp_controls,
77+
priors,
78+
)
3679
end = time.time()
3780

38-
if controls.display != Display.Off:
81+
if display_on:
3982
print(f"Elapsed time is {end-start:.3f} seconds\n")
4083

4184
results = make_results(controls.procedure, output_results, bayes_results)
@@ -45,7 +88,7 @@ def run(project, controls):
4588
for index, value in enumerate(getattr(problem_definition, parameter_field[class_list])):
4689
getattr(project, class_list)[index].value = value
4790

48-
if controls.display != Display.Off:
91+
if display_on:
4992
print("Finished RAT " + horizontal_line)
5093

5194
return project, results

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ pytest-cov >= 4.1.0
77
matplotlib >= 3.8.3
88
StrEnum >= 0.4.15; python_version < '3.11'
99
ruff >= 0.4.10
10-
scipy >= 1.13.1
10+
scipy >= 1.13.1
11+
tqdm >= 4.66.5

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def build_libraries(self, libraries):
171171
"pydantic >= 2.7.2",
172172
"matplotlib >= 3.8.3",
173173
"scipy >= 1.13.1",
174+
"tqdm>=4.66.5",
174175
],
175176
extras_require={
176177
':python_version < "3.11"': ["StrEnum >= 0.4.15"],

tests/test_run.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import RATapi
1212
import RATapi.outputs
1313
import RATapi.rat_core
14+
from RATapi.events import EventTypes, ProgressEventData, notify
15+
from RATapi.run import ProgressBar
1416
from RATapi.utils.enums import Calculations, Geometries, LayerModels, Procedures
1517
from tests.utils import check_results_equal
1618

@@ -320,3 +322,20 @@ def test_run(test_procedure, test_output_problem, test_output_results, test_baye
320322
project, results = RATapi.run(input_project, RATapi.Controls(procedure=test_procedure))
321323

322324
check_results_equal(test_results, results)
325+
326+
327+
def test_progress_bar() -> None:
328+
event = ProgressEventData()
329+
event.message = "TESTING"
330+
event.percent = 0.2
331+
with ProgressBar() as bar:
332+
assert bar.n == 0
333+
notify(EventTypes.Progress, event)
334+
assert bar.desc == "TESTING"
335+
assert bar.n == 20
336+
337+
event.message = "AGAIN"
338+
event.percent = 0.6
339+
notify(EventTypes.Progress, event)
340+
assert bar.desc == "AGAIN"
341+
assert bar.n == 60

0 commit comments

Comments
 (0)