Skip to content

Commit

Permalink
Optimise convergence plotting (#2440)
Browse files Browse the repository at this point in the history
  • Loading branch information
JackEAllen authored Jan 6, 2025
2 parents a5ee3dc + 3f6d90d commit 2da9f72
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
4 changes: 3 additions & 1 deletion mantidimaging/core/reconstruct/cil_recon.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def __init__(self, verbose=1, progress: Progress | None = None) -> None:

def __call__(self, algo: Algorithm) -> None:
if self.progress:
extra_info = {'iterations': algo.iterations, 'losses': algo.loss}
extra_info = {}
if algo.iterations and algo.iterations[-1] == algo.iteration:
extra_info = {'iterations': algo.iterations[1:], 'losses': algo.loss[1:]}
if algo.last_residual and algo.last_residual[0] == algo.iteration:
extra_info["residual"] = algo.last_residual[1]
self.progress.update(
Expand Down
9 changes: 5 additions & 4 deletions mantidimaging/core/utility/progress_reporting/progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

from mantidimaging.core.utility.memory_usage import get_memory_usage_linux_str

ProgressHistory = NamedTuple('ProgressHistory', [('time', float), ('step', int), ('msg', str),
('extra_info', dict | None)])
ProgressHistory = NamedTuple('ProgressHistory', [('time', float), ('step', int), ('msg', str)])


class ProgressHandler:
Expand Down Expand Up @@ -59,6 +58,7 @@ def __init__(self, num_steps: int = 1, task_name: str = 'Task') -> None:
# List of tuples defining progress history
# (timestamp, step, message)
self.progress_history: list[ProgressHistory] = []
self.extra_info: dict | None = None

# Lock used to synchronise modifications to the progress state
self.lock = threading.Lock()
Expand Down Expand Up @@ -191,10 +191,11 @@ def update(self,
mean_time = self.calculate_mean_time(self.progress_history)
eta = mean_time * (self.end_step - self.current_step)

msg = f"{f'{msg}' if len(msg) > 0 else ''} | {self.current_step}/{self.end_step} | " \
msg = f"{msg} | {self.current_step}/{self.end_step} | " \
f"Time: {self._format_time(self.execution_time())}, ETA: {self._format_time(eta)}"
step_details = ProgressHistory(time.perf_counter(), self.current_step, msg, extra_info)
step_details = ProgressHistory(time.perf_counter(), self.current_step, msg)
self.progress_history.append(step_details)
self.extra_info = extra_info

# process progress callbacks
for cb in self.progress_handlers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,20 +257,20 @@ def test_format_time(self):
def test_calculate_mean_time(self):
progress_history = []

progress_history.append(ProgressHistory(100, 0, "", None))
progress_history.append(ProgressHistory(100, 0, ""))
self.assertEqual(Progress.calculate_mean_time(progress_history), 0)

# first step 5 seconds
progress_history.append(ProgressHistory(105, 1, "", None))
progress_history.append(ProgressHistory(105, 1, ""))
self.assertEqual(Progress.calculate_mean_time(progress_history), 5)

# second step 10 seconds
progress_history.append(ProgressHistory(115, 2, "", None))
progress_history.append(ProgressHistory(115, 2, ""))
self.assertEqual(Progress.calculate_mean_time(progress_history), 7.5)

for i in range(1, 50):
# add many 2 second steps
progress_history.append(ProgressHistory(115 + (i * 2), 2 + (i * 2), "", None))
progress_history.append(ProgressHistory(115 + (i * 2), 2 + (i * 2), ""))
self.assertEqual(Progress.calculate_mean_time(progress_history), 2)


Expand Down
3 changes: 1 addition & 2 deletions mantidimaging/gui/dialogs/async_task/presenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ def update_progress_plot(self, iterations: list, losses: list) -> None:

def progress_update(self) -> None:
msg = self.progress.last_status_message()
progress_info = self.progress.progress_history
extra_info = progress_info[-1].extra_info
extra_info = self.progress.extra_info
self.progress_updated.emit(self.progress.completion(), msg if msg is not None else '')

if extra_info:
Expand Down
13 changes: 7 additions & 6 deletions mantidimaging/gui/dialogs/async_task/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ def __init__(self, parent: QMainWindow):
self.progressBar.setMinimum(0)
self.progressBar.setMaximum(1000)

self.progress_plot = PlotWidget()
self.PlotVerticalLayout.addWidget(self.progress_plot)
self.progress_plot.hide()
self.progress_plot.setLogMode(y=True)
self.progress_plot.setMinimumHeight(300)

self.progress_plot: PlotWidget | None = None
self.residual_image_view: ImageView | None = None

self.show_timer = QTimer(self)
Expand Down Expand Up @@ -75,6 +70,12 @@ def set_progress(self, progress: float, message: str):
self.progressBar.setValue(int(progress * 1000))

def set_progress_plot(self, x: list, y: list):
if self.progress_plot is None:
self.progress_plot = PlotWidget()
self.PlotVerticalLayout.addWidget(self.progress_plot)
self.progress_plot.hide()
self.progress_plot.setLogMode(y=True)
self.progress_plot.setMinimumHeight(300)
self.progress_plot.show()
self.progress_plot.plotItem.plot(x, y)

Expand Down

0 comments on commit 2da9f72

Please sign in to comment.