Sinogram tools for research

Hello everyone,

For a research project on castor (it is about uncertainty quantification in reconstructions)
we have to do some exotic treatement of our sinograms (reconstructions on segmented domain,
adding noise to counts, fuse (sum) of two sinograms) and it feels that castor is missing
some simple functionality to work with sinogram data. Each time I want to do some small
treatment of data - I need to write C++ code even if the functionality I implement is not a
tool for everyday use - but simple/one-time preprocessing step.

For example, just for fast reconstructions in our first approach we decided to remove TOF data
from histogram datafile. There are some parts of the source code which allow to do
this more-or-less automatically (one can set some flag to ingore TOF to ‘true’ and then
each time event is queried - it is automaticall aggregated), but I met a structural issue.
The child class that implements histogram event for PET seems to be compiled as
static, so whenever I alter some parameter in the current event, it acts not on a particular
instance of a class, but globally. To give an example - when I remove TOF from the event read from the input sinogram (event is read into RAM), I want to reset number of TOF channels to 1.
But when I do that - if I query the following event from the input, it will be automatically regaraded as event having 1 TOF channel, when in reality it is not. So I have to do this dirty reset TOF size to 1 → write to output → reset TOF size back to how it was.

It could be just aesthetic issue, but it appeared that I must also change in the class for datafile (not event) the property that it contains also only one TOF channel. And this is impossible to do
as the corresponding field in the class is set as private and there is no method to alter it (it is for a reason, but still - I had to add my custom method to alter this variable, which is kind of dirty/scary because potentially it can break things for future developers).

To say, I see that castor design is very flexible (and code is indeed very well ogranized), but
implemented methods follow mostly “read-only” style, than “write” new data. In general, it would be very good to have tools to open/alter sinogram data (maybe with Python API frontend) and
write it back so you can play easily with data and also with castor itself. But I think then it needs to add extra functionality in implemented classes (make them less static, or simply implement extra constructors to allow to create copies easliy). I wonder if this is of any interest for developer community of castor - as it needs time and resources…

On my side, possibly I would be happy to work on it, but I need to have an opinion and support of “old-school” developers who have more global view of the Castor project.

Hello @fedor-goncharov,

We faced somewhat similar issues as what you described. More specifically, we wanted to alter CASToR datafiles to add or remove some fields (such as normalization and random correction coefficients), but also wanted to have the ability to edit some events (for instance, set the value of some field to 0 if some condition based on the crystal IDs is satisfied).

We created a small collection of Python scripts that allows us to perform such tasks. The scripts are not designed to be extremely performant nor elegant, but they do the job. They only support PET datafiles (as we are only working on PET modality) and might be slightly skewed towards our use cases, but they might be of interest to you anyway. (Also, I’m not sure whether operations on TOF are properly supported…)

Just to give you an example, using our script, doubling the normalization factors contained in a CASToR datafile is as easy as

def double_normalization(row):
  row[CASToRCDFField.NORMALIZATION] *= 2
  return row

update_castor_datafile(cdh_path, output_cdh, output_cdf, double_normalization)

If you are interested, I can look into how to make those scripts public (and also perhaps roughly document how they work!). I had also started to design a better interface for those scripts, but never completed it.

Hope my comment is not completely off-topic! Let me know what you think.

Hi @acoussat,

Thanks, yes, we should get in contact and I would be interested to see the scripts.
In fact, we finally managed to develop tools in C++ that allow to do what we want
with sinograms - remove TOF data and fields, introduce noise in sinograms,
project image to a sinogram and fusion of two sinograms. But this necessited,
unfortunately, change of the source code of the CASToR itself. That is why I was
saying, that it is worth to have maybe a side project to develop tools that allow easy
access and change (which is not always the case) of castor data files.

I can also share with my codes if you want.

P. S. I wait then of your message, to get in touch on your scripts :slight_smile: