CIL icon indicating copy to clipboard operation
CIL copied to clipboard

callbacks not a package and limited doc available

Open KrisThielemans opened this issue 1 year ago • 3 comments

Description

import cil.optimisation.utilities.callbacks.Callback as Callback

leads to

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[62], line 1
----> 1 import cil.optimisation.utilities.callbacks.Callback as Callback

ModuleNotFoundError: No module named 'cil.optimisation.utilities.callbacks.Callback'; 'cil.optimisation.utilities.callbacks' is not a package
help(cil.optimisation.utilities.callbacks.Callback)

Is this expected?

Also, the docstring for this class gives no idea how this should work, e.g. what the signature is for the __call__ method.

KrisThielemans avatar Apr 27 '24 22:04 KrisThielemans

From looking at Algorithm.py, I found out I have to do

from cil.optimisation.utilities.callbacks import Callback, ProgressCallback

Obvious? If so, sorry for the noise!

KrisThielemans avatar Apr 27 '24 22:04 KrisThielemans

By looking at existing code (but not documentation), I was able to construct a callback that saves images

class SaveCallback(Callback):
    def __init__(self, run_every=1):
        super().__init__()
        self.run_every = run_every
  
    def __call__(self, algorithm):
         if not hasattr(algorithm, 'saved_images'):
             algorithm.saved_images = []
         if (algorithm.iteration % self.run_every == 0):
             algorithm.saved_images.append((algorithm.iteration, algorithm.get_output().clone()))

Seems to work ok.

KrisThielemans avatar Apr 27 '24 22:04 KrisThielemans

import cil.optimisation.utilities.callbacks.Callback as Callback

where did you see this @KrisThielemans? It should indeed be:

from cil.optimisation.utilities.callbacks import Callback

FYI the docs are at https://tomographicimaging.github.io/CIL/nightly/optimisation/#callbacks.

casperdcl avatar Apr 23 '25 08:04 casperdcl