CIL icon indicating copy to clipboard operation
CIL copied to clipboard

New callback to save a list of iterates every set number of iterations

Open MargaretDuff opened this issue 1 year ago • 2 comments

Changes

New callback to save a list of iterates every set number of iterations

Testing you performed

Please add any demo scripts to https://github.com/TomographicImaging/CIL-Demos/tree/main/misc

Related issues/links

Closes #1909

Checklist

  • [x] I have performed a self-review of my code
  • [x] I have added docstrings in line with the guidance in the developer guide
  • [x] I have updated the relevant documentation
  • [x] I have implemented unit tests that cover any new or modified functionality
  • [ ] CHANGELOG.md has been updated with any functionality change
  • [x] Request review from all relevant developers
  • [x] Change pull request label to 'Waiting for review'

Contribution Notes

Please read and adhere to the developer guide and local patterns and conventions.

  • [x] The content of this Pull Request (the Contribution) is intentionally submitted for inclusion in CIL (the Work) under the terms and conditions of the Apache-2.0 License
  • [x] I confirm that the contribution does not violate any intellectual property rights of third parties

MargaretDuff avatar Aug 29 '24 08:08 MargaretDuff

might want to check:

  • https://github.com/TomographicImaging/Hackathon-000-Stochastic-QualityMetrics/blob/main/img_quality_cil_stir/image_quality_callback.py
  • https://github.com/SyneRBI/PETRIC/blob/main/petric.py

In the Petric challenge they have the callback:

class SaveIters(Callback):
    """Saves `algo.x` as "iter_{algo.iteration:04d}.hv" and `algo.loss` in `csv_file`"""
    def __init__(self, outdir=OUTDIR, csv_file='objectives.csv', **kwargs):
        super().__init__(**kwargs)
        self.outdir = Path(outdir)
        self.outdir.mkdir(parents=True, exist_ok=True)
        self.csv = csv.writer((self.outdir / csv_file).open("w", buffering=1))
        self.csv.writerow(("iter", "objective"))

    def __call__(self, algo: Algorithm):
        if not self.skip_iteration(algo):
            log.debug("saving iter %d...", algo.iteration)
            algo.x.write(str(self.outdir / f'iter_{algo.iteration:04d}.hv'))
            self.csv.writerow((algo.iteration, algo.get_last_loss()))
            log.debug("...saved")
        if algo.iteration == algo.max_iteration:
            algo.x.write(str(self.outdir / 'iter_final.hv'))

This also saves the loss in a CSV file. I like that the file name it has the iteration number. I am not sure we can do this with our current TiffWriter?

MargaretDuff avatar Aug 29 '24 12:08 MargaretDuff

I'll open a follow-up PR to fix this (#1913) as well as #1892 later. Fine for now.

Thanks @casperdcl - is it the naming that you want to change?

MargaretDuff avatar Sep 03 '24 14:09 MargaretDuff