TomographicImaging / CIL

A versatile python framework for tomographic imaging
https://tomographicimaging.github.io/CIL/
Apache License 2.0
93 stars 41 forks source link

callbacks not a package and limited doc available #1794

Open KrisThielemans opened 4 months ago

KrisThielemans commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

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.