Skip to content

Commit 14ca50e

Browse files
committed
deprecate Algorithm(update_objective_interval) -> Algorithm.run(update_objective_interval)
1 parent 8deeca5 commit 14ca50e

1 file changed

Lines changed: 16 additions & 19 deletions

File tree

Wrappers/Python/cil/optimisation/algorithms/Algorithm.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ class Algorithm:
2929
r"""Base class providing minimal infrastructure for iterative algorithms.
3030
3131
An iterative algorithm is designed to solve an optimization problem by repeatedly refining a solution. In CIL, we use iterative algorithms to minimize an objective function, often referred to as a loss. The process begins with an initial guess, and with each iteration, the algorithm updates the current solution based on the results of previous iterations (previous iterates). Iterative algorithms typically continue until a stopping criterion is met, indicating that an optimal or sufficiently good solution has been found. In CIL, stopping criteria can be implemented using a callback function (`cil.optimisation.utilities.callbacks`).
32-
32+
3333
The user is required to implement the :code:`set_up`, :code:`__init__`, :code:`update` and :code:`update_objective` methods.
3434
3535
The method :code:`run` is available to run :code:`n` iterations. The method accepts :code:`callbacks`: a list of callables, each of which receive the current Algorithm object (which in turn contains the iteration number and the actual objective value) and can be used to trigger print to screens and other user interactions. The :code:`run` method will stop when the stopping criterion is met or `StopIteration` is raised.
36-
37-
Parameters
38-
----------
39-
update_objective_interval: int, optional, default 1
40-
The objective (or loss) is calculated and saved every `update_objective_interval`. 1 means every iteration, 2 every 2 iterations and so forth. This is by default 1 and should be increased when evaluating the objective is computationally expensive.
4136
"""
4237

43-
def __init__(self, update_objective_interval=1, max_iteration=None, log_file=None):
44-
38+
def __init__(self, update_objective_interval=None, max_iteration=None, log_file=None):
39+
if update_objective_interval is None:
40+
update_objective_interval = 1
41+
else:
42+
warn("use `Algorithm.run(update_objective_interval)` instead of `update_objective_interval`", DeprecationWarning, stacklevel=2)
4543
self.iteration = -1
4644
self.__max_iteration = 1
4745
if max_iteration is not None:
@@ -227,27 +225,26 @@ def update_objective_interval(self, value):
227225
raise ValueError('interval must be an integer >= 0')
228226
self.__update_objective_interval = value
229227

230-
def run(self, iterations=None, callbacks: Optional[List[Callback]]=None, verbose=1, **kwargs):
231-
r"""run upto :code:`iterations` with callbacks/logging.
232-
228+
def run(self, iterations: int, update_objective_interval=None, callbacks: Optional[List[Callback]]=None, verbose=1, **kwargs):
229+
"""run upto :code:`iterations` with callbacks/logging.
230+
233231
For a demonstration of callbacks see https://github.com/TomographicImaging/CIL-Demos/blob/main/misc/callback_demonstration.ipynb
234232
235233
Parameters
236234
-----------
237-
iterations: int, default is None
235+
iterations: int
238236
Number of iterations to run. If a positive infinity is passed, the algorithm will run indefinitely until a callback raises `StopIteration`.
237+
update_objective_interval: int, optional, default 1
238+
The objective (or loss) is calculated and saved every `update_objective_interval`. 1 means every iteration, 2 every 2 iterations and so forth. This is by default 1 and should be increased when evaluating the objective is computationally expensive.
239239
callbacks: list of callables, default is Defaults to :code:`[ProgressCallback(verbose)]`
240240
List of callables which are passed the current Algorithm object each iteration. Defaults to :code:`[ProgressCallback(verbose)]`.
241241
verbose: 0=quiet, 1=info, 2=debug
242-
Passed to the default callback to determine the verbosity of the printed output.
242+
Passed to the default callback to determine the verbosity of the printed output.
243243
"""
244-
245-
if iterations is None:
246-
raise ValueError("`run()` missing number of `iterations`")
247-
244+
if update_objective_interval is not None:
245+
self.update_objective_interval = update_objective_interval
248246
if 'print_interval' in kwargs:
249-
warn("use `TextProgressCallback(miniters)` instead of `run(print_interval)`",
250-
DeprecationWarning, stacklevel=2)
247+
warn("use `TextProgressCallback(miniters)` instead of `run(print_interval)`", DeprecationWarning, stacklevel=2)
251248
if np.isposinf(iterations):
252249
if callbacks is None:
253250
raise ValueError("Infinite iterations require a callback with a stopping criterion that raises `StopIteration`")

0 commit comments

Comments
 (0)