J-PET Framework: Software platform for PET tomography data reconstruction and analysis

J-PET Framework is an open-source software platform for data analysis, written in C++ and based on the ROOT package. It provides a common environment for implementation of reconstruction, calibration and filtering procedures, as well as for user-level analyses of Positron Emission Tomography data. The library contains a set of building blocks that can be combined by users with even little programming experience, into chains of processing tasks through a convenient, simple and well-documented API. The generic input-output interface allows processing the data from various sources: low-level data from the tomography acquisition system or from diagnostic setups such as digital oscilloscopes, as well as high-level tomography structures e.g. sinograms or a list of lines-of-response. Moreover, the environment can be interfaced with Monte Carlo simulation packages such as GEANT and GATE, which are commonly used in the medical scientific community.


Motivation and significance
Positron Emission Tomography is one of the most popular methods for tomographic imaging used in nuclear medicine. In contrast to other techniques such as Computed Tomography that can detect anatomical changes, PET provides information about metabolic processes in the patient's body even at the cell level [3]. This allows detection of pathological symptoms that usually precede the anatomical changes. PET tomography has a wide range of research and clinical applications e.g. it is commonly used for diagnosis of cancer, neurological disorders, heart diseases and many others [4].
The J-PET collaboration aims at providing a low-cost, modular, whole-body PET scanner based on detection of photon interactions in plastic scintillators [14][15][16] with a view to its application in both medical diagnostics [16,17] and in proton therapy monitoring [18]. The J-PET prototype is a research device which not only demonstrates the new operating principle for its use in standard PET tomography but also explores new imaging modalities such as spatially-resolved determination of properties of positronium atoms produced in a patient's body [19][20][21][22].
The exploratory nature of the J-PET device results in its operation with much more flexible data registration conditions than used in commercial PET solutions. In order to allow for classical PET imaging without discrimination of signals, which may be used in the novel diagnostic methods, J-PET operates in a triggerless data acquisition mode [23], resulting in a volume of recorded data unprecedented in medical imaging technologies.
From the software point of view, development and testing of novel PET modalities and tomography methods become challenging as the standard approaches must be either extended or entirely replaced by new algorithms. Moreover, at the prototyping stage, multiple elements of the detector, its geometrical setup and the data acquisition chain are subject to change and various reconstruction procedures may be tested in parallel. The software framework used to analyze data from evolving prototypes and to implement and test new reconstruction algorithms must follow these changes dynamically. At the same time, however, the need to efficiently process the data stream from trigger-less acquisition requires that the performance may not be compromised when asserting flexibility.
The J-PET Framework package has been developed as an answer to the aforementioned challenges, providing a dynamically adjustable environment for development and efficient implementation of new algorithms. The basic idea is to provide a set of generic building blocks, allowing a quick implementation of data processing chains to be used by analysts with even little programming experience through a convenient and simple API. The J-PET Framework is used for analysis of data recorded by the tomograph prototype from the level of raw data saved by its data acquisition system, through assembly of higher-level data structures representing the logic needed for reconstruction of the physical properties of electron-positron annihilation into photons, up to the level of medical image reconstruction and statistical analysis of the data.
Notably, the data acquisition system of J-PET is based on the TRB3 hardware platform [24,25] which is widely used by experimental setups both in the fields of medical imaging and particle physics experiments [26]. Consequently, the J-PET Framework can be easily adopted for data analysis in other TRB3-based experiments.
From the point of view of the full data reconstruction flow, the usage scope of the J-PET Framework is different compared with the existing tomography image software packages such as STIR [27], CASTOR [28] or QETIR [29], since it also allows to implement low-level reconstruction and calibration algorithms which operate before the formation of Line-of-Responses (LORs), while typical input data for image algorithms consists of higherlevel structures such as sinograms or list of LORs. At the same time, J-PET environment provides tools for the implementation of typical image reconstruction algorithms and effectively such procedures e.g. Time-of-Flight Filtered-Backprojection, have been implemented within the Framework. However, the aim of the J-PET Framework package is not to replace the existing image tomography toolkits, which offer well tested and proven solutions, but rather to provide a possibility for passing the transformed data to the external packages.
While an early version of the J-PET Framework is described in Ref. [30], this article is intended to present its architecture and functionality available in its current mature form, which allows to extend the scope of its usage beyond the J-PETproject. Therefore, we focus on the properties of the core J-PET Framework library [31] rather than on the particular J-PET-specific reconstruction algorithms developed using the framework which are available in a separate repository [32].

Software description
The design of the J-PET Framework originated from the necessity of performing reconstruction and analysis of PET data from a prototype tomography scanner. It has become a more generalized environment for execution of tasks which could be adapted to multi-step analysis of various kinds of data. All the features naturally followed the implementation: managing input/output, incorporating palette of configurations, adapting data and parameter structures, user interface and task handling.
The core of the J-PET Framework is constituted by a dynamic library that can be linked to user applications. The library provides tools for loading, analyzing and saving transformed data as well as for implementation of transformation algorithms that can be further connected in chains and finally executed.
The J-PET environment can be used to e.g. develop a reconstruction chain for the real data collected by a PET scanner or to implement a calibration procedure, an image reconstruction method or any kind of a multi-step analysis. Other typical applications consist of comparative studies of prototype PET scanners performance based on the Monte Carlo (MC) simulations.

User application programming interface
The core library of the J-PET Framework provides the users with an Application Programming Interface (API) presented schematically in Fig. 1. The API is concentrated on giving the user access to data (structured as a stream of subsequent ''events'') read from various sources as well to parameters of the experimental setup (read from external configuration files) in order to combine the event data and setup details in user-defined algorithms assembling higher-level data structures and filtering them based on custom conditions (Listing 2 presents an simple example of such procedure). The API further allows for grouping of such user-provided logic into analysis modules which can then be chained to constitute a complete analysis workflow (as demonstrated in Listing 1).

Software architecture
The library is written in C++ using object-oriented paradigm. The core components are implemented as classes with welldefined responsibilities e.g. computing task execution, input/output operations, logging, option parsing, option validation. Moreover, the package contains a set of classes representing physical entities e.g. part of the scanner or PET-specialized data structures such Line-of-Response, which form a language that can be used to express the domain-specific concepts (see more details in Section 2.3.4).
The basic concept of the J-PET Framework is the decomposition of a data processing chain into a series of standardized modular blocks. Each module corresponds to a particular computing task, e.g. a reconstruction algorithm or a calibration procedure, with well-defined input and output. The processing chain is built by registration of chosen modules in the JPetManager, responsible for synchronization of the data flow between the modules (see Fig. 2). This approach permits to quickly interchange modules and to create processing chains for different experimental setups. Scheme of the Application Programming Interface exposed to the user for creation of data reconstruction and analysis workflows. Elements of the workflow exposed to and modified by the user are contained in the middle gray region whereas the elements outside it are handled by the framework transparently to the user. Main functionalities of the API exposed to the user are marked with blue arrows and blue text. (For interpretation of the references to color in this figure legend, the reader is referred to the web version of this article.)

Software functionalities
J-PET Framework provides a set of functionalities that helps in rapid data reconstruction and analysis prototyping. In this section, we list the most useful features and present selected usage examples. More applications can be found in the repository [32].

Handling of multiple data sources
The Framework provides a generic input-output mechanism that through simple extensions allow for processing of data from various sources e.g. low-level data in a binary format from a tomographic data acquisition system, textual representations of complete photomultiplier (PMT) signal waveforms collected using a serial data analyzer at detector testing stages, as well as high-level tomography-specific structures e.g. sinograms or linesof-response. Other extensions of the input interface feature using results of Monte Carlo simulations in place of data as described in Section 2.3.10. Fig. 3 presents how inputs from various data sources are unified at higher analysis levels so that more abstract steps of reconstruction can act on data independently of their origin. PMT signals recorded with a serial data analyzer, for example, correspond to PMT signal representations already assembled from single Time-to-Digital Converter (TDC) signals in case of the TRB3-based data acquisition system and are thus injected to the analysis chain at the corresponding level, i.e. before a module pairing PMT signals from the same detection module to identify photon interactions.

Input/output mechanisms
Besides the source-specific data formats handled by dedicated wrapper modules, the J-PET Framework relies on binary internally-compressed data format provided by the ROOT package, widely adopted in both particle physics and nuclear medicine research. The framework provides automatic handling of input and output files for standard data analysis modules, abstracting the actual storage away from the analysis or reconstruction logic. User code is only responsible for deciding whether an entry processed by a module should be preserved or discarded. Depending on the option chosen by the user, output from every analysis module is either saved to a separate file in the ROOT format or directly fed as input to the subsequent module in the chain. While the former is useful at the stages of testing the analysis, the latter approach allows to create a pipeline of analysis modules minimizing I/O load as the only output saved to disk is the one produced by the last module in the chain which typically corresponds to the most filtered data stream where the data volume is reduced by 1-2 orders of magnitude with respect to the raw input. This is particularly important when multiple analysis processes are operating on the same disk space, which is a common use case in data-driven parallel computing specific to particle physics and low-level PET tomography event filtering and reconstruction.

Options
The library provides multiple manners of loading optional information for any custom processing task. These collections of various parameters would be required for a successful reconstruction of PET data, giving i.e. descriptions of a experimental setup, measurement conditions, necessary calibrations or desired form of the output. The Framework provides the following interfaces for dynamic configuration: • command line options (e.g. input file, configuration files, progress display) • JSON file with the description of experimental setupparametrization of objects, that serve as data schema, • JSON file with user-provided options -any custom settings to be used during execution of tasks, passed as named parameters of elementary C++ types All the provided options are parsed and validated before execution of chain of tasks and are accessible during its processing.

Data and parameter structures
The library includes classes representing abstract entities common for analysis of data from J-PET measurements. Parameter Objects represent hardware parts of the detector, along with their working parameters, in-setup placement and connections with other parts, e.g. a single object per each plastic scintillator strip with its location in the detector or a photomultiplier coupled to a given scintillator. Data Objects are structures representing subsequent stages of reconstructed data -from elementary ones containing only TDC time and data acquisition channel number, to a detailed reconstruction of a physical event or a line-of-response.
Data Objects refer to particular elements of the setup encapsulated in Parameter Objects where the physical signals have originated. Moreover, mapping of connections between such components imposes relations between Parameter Objects themselves. These relations are implemented using persistent object references (TRef ) provided by the ROOT libraries [33] which ensure O(1) lookup of corresponding elements as well as persistence of the relations across file storage.
On the user side, encapsulation of data and setup properties into abstract objects allows for definition of reconstruction and analysis logic even by users without programming proficiency which is one of the objectives of the J-PET Framework. Listing 2 demonstrates the interplay between Data (JPetHit and JPetEvent) and Parameter Objects (Scintillator and BarrelSlot) in a simple task.

Setup description
Since experimental setup and its conditions can change from one measurement to another, the set of parameters describing it must be generated dynamically. The library provides dedicated tools to handle a setup representation in a form of a configuration JSON file. Based on its content, the Framework generates the collection of Parameter Objects (see Section 2.3.4) together with relations between them expressed in a standardized format. Once this file is parsed, a Parameter Bank encapsulating the latter is embedded in all output data files to allow for their further stand-alone analysis.

Processing control and logging
Each execution instance of any application based on the Framework environment, generates a log file with a unique name. By default all input parameters and options are stored in the log file, moreover each task can produce custom messages with one of appropriate tags: INFO, DEBUG, ERROR.
User Tasks classes can use tools for creating control histograms. All such objects are then automatically stored in the output file. An usage example can be found in code snippet 3.

Compressed input files
It is also possible to provide a raw data file in a compressed format; in that case a task is added by default, that simply decompresses the input file before any other procedures begin.

Handling of binary data format
The library can read raw data input files provided from the scanner data acquisition or from digital oscilloscope measurements. Binary data is transformed with dedicated tasks in the ROOT format, making it available for further processing by the consecutive tasks in the stream.

Iterative tasks
The structure of task chain allows the implementation of iterative tasks schemes, in which a module can be executed in a loop till a given condition is fulfilled. The stopping condition can be based on desired number of consecutive iterations or on the return value of the function defined by the user. This functionality is especially useful for optimization goals, i.e. refining detector calibration constants or estimation of event classification parameters.

Interfaces to Monte Carlo simulation packages
Testing and debugging of data analysis modules is often supported by using Monte Carlo-simulated events in place of actual data. To this end, the J-PET Framework offers interfaces to two Monte Carlo simulation packages: the custom J-PET MC simulation software [34] based on the Geant4 toolkit [35] as well as the GATE package for simulation of PET and SPECT tomography [36].
MC-simulated events are wrapped into the same data structures as data so that analysis modules intended to process experimental data can be applied transparently to the simulation results. At the same time, all MC-specific event information is preserved and accessible on demand.

Event display
J-PET Event Display [37] is a visualization tool based on the J-PET library. It can load files with the Framework data structures to visualize the reconstructed PET data in an event-by-event manner at different phases of the processing. Information on input geometry of the detector is provided by the same configuration files in the JSON format used for reconstruction of data described in Section 2.3.5. A usage example of the Event Display is shown in Fig. 5.

Development philosophy, testing and continuous integration
The J-PET Framework developer community is trying to consistently adopt good coding rules and practices in the development routine to assure the quality of the software. In particular, any new code before being merged into official repository must be reviewed and accepted by at least one person not being the author. Moreover, it must pass a set of unit and integration tests defined for the platform. The contributors are strongly encouraged to add unit tests together with new classes and to format the code consistently using the clang-format tool.
The Continuous Integration process is integrated with the project Github repository. Any new pull request launches automatic set of tests based on the Travis [38] and Jenkins [39] services. The unit tests are operated by the Travis system, while larger integration tests, which typically require some input data, are run by a dedicated Jenkins server. Both services deliver a detailed report about possible failures. The testing system is fully automatized on the servers and can be launched manually for local testing.
Issue and bug tracking is performed with the Redmine service which also serves also as a user support forum. The reference guide is automatically generated from the code using the Doxygen tool and is available online [40]. Additionally, an analysis user guide is provided in the repository and it is being updated with every new version of the Framework.

Sample code snippets analysis
Listing 1 presents the instance of JPetManager registering user tasks to form a chain of procedures. With the following useTask method, the user is specifying the input and output data format of each tasks. In the example, the output of the first task serves as input for the second one. The processing of all algorithms with the provided arguments is triggered by the run method. This simple construction allows to create an analysis from custom building blocks even for a user with little programming experience.

Illustrative examples
In this section we present two examples developed with the Framework library. The first application can be used to perform tests of a prototype PET scanner based on the Monte Carlo simulations of various phantoms. The simulated scanner was built from a cylindrical layer (radius of 42.5, length of 50 cm) of 384 plastic strips (more details about the MC simulations of the J-PETscanners can be found in [16]).
The program loads the data sample generated by the GATE Monte Carlo simulation package and transforms it by smearing the measured observables such as time, energy and position based on the parametrization of experimental uncertainties determined for a given prototype scanner. This procedures mimics the real measurement effects. Next, the data is reconstructed and finally transformed to a sinogram, which serves as an input to the image reconstruction task implementing the Time-of-Flight Filtered-Backprojection algorithm (see Fig. 4) or can be send to an external image reconstruction package such as STIR [27], CASTOR [28] or QETIR [29]. All operations are implemented as consecutive tasks executed by the framework.
The second program implements a full reconstruction chain for the real data collected by the 3-layer J-PET scanner. The example reconstruction and analysis is based on the test measurement with the radioactive source placed in the center of the scanner. The reconstructed results are visualized with the J-PET Event Display tool (see Fig. 5).

Impact
Flexibility and robustness of the J-PET Framework library allowed it to be adopted as the main software platform of the J-PET project. The software and applications constructed based on this package have been used for many kinds of scientific studies involving data analyses from the J-PET tomography scanner and will be utilized for future analyses in the fundamental research and in the development of various PET scanners prototypes.
The Framework software platform is currently used by scientists from the Jagiellonian University in Kraków, National Centre for Nuclear Research in Warsaw and INFN Laboratori Nazionali di Frascati and has been successfully deployed on different scales starting from laptops and personal PC-s, through mid-size computing clusters to HPC Swierk cluster.

Conclusions
In this article we presented the features and range of possible applications of the J-PET Framework, a C++ based library for data processing and analysis for PET tomography and for fundamental searches. The Framework provides tools for the implementation of a wide range of data reconstruction and calibration procedures as well as user-level data analyses and preparation of input for higher-level medical imaging software. The platform is focused on flexibility in adjusting to dynamically changing prototyping environments and asserting ease of implementation of the required logic by users without programming proficiency while maintaining high processing performance.
Currently, use cases of the J-PET Framework span among various data analyses and imaging application of the first J-PET device. In the near future, a new generation light-weight modular  [36], and further processed by the Framework-based parser which applies the experimental parametrizations to fully imitate a measurement of the scanner. J-PET scanner with fully digital readout and high mobility will be commissioned along with sibling devices such as a similartechnology-based mammography scanner. The software platform is currently being extended with modules specific to the new devices, which will allow for reusing the higher-level analysis steps with data from new hardware setups.
Despite having originated solely for the purpose of analysis of data from a single setup, the recent expansion of the scope of its usage in the context of J-PET demonstrates that the flexibility of its architecture allows for use in a wider range of experiments related to nuclear medicine and fundamental studies. .

Declaration of competing interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.