9 #include "gVariables.hh" 10 #include "gOptions.hh" 11 #include "oImageDimensionsAndQuantification.hh" 12 #include "vDataFile.hh" 13 #include "iDataFilePET.hh" 14 #include "iDataFileSPECT.hh" 15 #include "iDataFileCT.hh" 16 #include "iEventHistoPET.hh" 17 #include "iEventHistoSPECT.hh" 18 #include "iEventHistoCT.hh" 19 #include "iEventListPET.hh" 20 #include "iEventListSPECT.hh" 21 #include "iEventListCT.hh" 22 #include "sOutputManager.hh" 23 #include "sRandomNumberGenerator.hh" 42 cout <<
"Usage: castor-datafilePosteriorBootstrap -df datafile.cdh -(f/d)out output" << endl;
44 cout <<
"This program can be used to create a resampled histogram datafile for the posterior bootstrap reconstruction." << endl;
45 cout <<
"See the related paper 'Reconstruction, analysis and interpretation of posterior probability distributions of PET images, using the posterior bootstrap' by Marina Filipovic et al., PMB 2021." << endl;
47 cout <<
"[Mandatory parameters]:" << endl;
48 cout <<
" -df datafile.cdh : Give a CASToR histogram datafile." << endl;
49 cout <<
" -fout name : Give the root name for all output files (no default, alternative to -dout)" << endl;
50 cout <<
" -dout name : Give the name of the output directory where all output files will be written (no default, alternative to -fout)" << endl;
52 cout <<
"[Options]:" << endl;
53 cout <<
" -rng : Give a seed for the random number generator (should be >=0)" << endl;
54 cout <<
" --norm-w : Set an additional normalization step to maintain the same number of counts: implies more RAM usage and forces the random weights to form a probability distribution" << endl;
55 cout <<
" -> by default set to false" << endl;
56 cout <<
" -vb value : Give the verbosity level, from 0 (no verbose) to 2 (default: 1)" << endl;
57 cout <<
" --help,-h,-help : Print out this help page." << endl;
63 cout <<
" Build date: " << BUILD_DATE << endl;
67 cout <<
" This program is part of the CASToR release version " <<
CASTOR_VERSION <<
"." << endl;
80 int main(
int argc,
char** argv)
88 MPI_Init(&argc, &argv);
89 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
90 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
91 if (mpi_rank!=0)
return 0;
110 string datafile =
"";
117 string path_dout =
"";
119 string path_fout =
"";
128 int64_t random_generator_seed = -1;
138 for (
int i=1; i<argc; i++)
141 string option = (string)argv[i];
148 if (option==
"-h" || option==
"--help" || option==
"-help")
154 else if (option==
"-rng")
158 Cerr(
"***** castor-datafileBootstrap() -> Argument missing for option: " << option << endl);
163 Cerr(
"***** castor-datafileBootstrap() -> Exception when trying to read provided number '" << random_generator_seed <<
" for option: " << option << endl);
169 else if (option==
"--norm-w")
175 else if (option==
"-vb")
179 Cerr(
"***** castor-datafileBootstrap() -> Argument missing for option: " << option << endl);
184 Cerr(
"***** castor-datafileBootstrap() -> Exception when trying to read provided verbosity level '" << verbose <<
" for option: " << option << endl);
195 else if (option==
"-df")
199 Cerr(
"***** castor-datafileBootstrap() -> Argument missing for option: " << option << endl);
202 datafile = (string)argv[i+1];
211 else if (option==
"-dout")
215 Cerr(
"***** castor-datafileBootstrap() -> Argument missing for option: " << option << endl);
218 path_dout = argv[i+1];
222 else if (option==
"-fout")
226 Cerr(
"***** castor-datafileBootstrap() -> Argument missing for option: " << option << endl);
229 path_fout = argv[i+1];
238 Cerr(
"***** castor-datafileBootstrap() -> Unknown option '" << option <<
"' !" << endl);
250 Cerr(
"***** castor-datafileBootstrap() -> Please provide an input datafile !" << endl);
254 if (path_fout.empty() && path_dout.empty())
256 Cerr(
"***** castor-datafileBootstrap() -> Please provide an output option for output files (-fout or -dout) !" << endl);
260 if (!path_fout.empty() && !path_dout.empty())
262 Cerr(
"***** castor-datafileBootstrap() -> Please provide either output option -fout or -dout but not both !" << endl);
277 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while initializing output directory !" << endl);
283 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while logging command line arguments !" << endl);
296 if (random_generator_seed>=0) p_RandomNumberGenerator->
Initialize(random_generator_seed, nb_threads, extra_gen);
297 else p_RandomNumberGenerator->
Initialize(nb_threads, extra_gen);
305 string scanner_name =
"";
308 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while trying to find the system name in the datafile header !" << endl);
313 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while searching for scanner system !" << endl);
318 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred during scanner object construction ! !" << endl);
323 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while creating Scanner object !" << endl);
328 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while retrieving scanner fields from the datafile header !" << endl);
333 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while generating/reading the LUT !" << endl);
352 Cerr(
"***** castor-datafileBootstrap() -> Unknown scanner type (" << p_ScannerManager->
GetScannerType() <<
") for datafile construction ! Abort." << endl);
359 bool do_not_affect_quantification =
false;
362 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred during datafile header reading ! Abort." << endl);
367 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred while checking datafile parameters ! Abort." << endl);
372 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred in datafile initialization ! Abort." << endl);
377 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred in datafile initialization ! Abort." << endl);
382 Cerr(
"***** castor-datafileBootstrap() -> A problem occurred in datafile preparation ! Abort." << endl);
388 Cerr(
"***** castor-datafileBootstrap() -> The input datafile is not a histogram, this program is only suitable to histogram files !" << endl);
404 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while setting parameters of output file from input file !" << endl);
410 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while opening file for writing !" << endl);
419 int64_t nb_events = p_DataFile->
GetSize();
421 if (verbose>=1)
Cout(
"castor-datafileBootstrap() -> Draw posterior bootstrap values for all the " << nb_events <<
" events" << endl);
433 if (norm_w) vals =
new HPFLTNB*[nb_events];
434 int64_t printing_index = 0;
436 for (i=0; i<nb_events; i++)
441 if (printing_index%5000==0)
443 int percent = ((int)( ((
FLTNB)(i)) * 100. / ((
FLTNB)(nb_events/nb_threads)) ));
444 cout <<
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b " 445 << percent <<
" % " << flush;
454 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while getting the event from index " << i <<
" !" << endl);
456 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while closing file during writing !" << endl);
460 if (norm_w) vals[i] =
new HPFLTNB[
event->GetNbValueBins()];
462 for (
int b=0; b<
event->GetNbValueBins(); b++)
466 FLTNB val =
event->GetEventValue(b);
467 if (norm_w) vals[i][b] = val;
470 nb_counts_old += val;
472 gamma_distribution<FLTNB> gamma_distrib(val, 1.);
474 val = gamma_distrib(rgen);
475 nb_counts_new += val;
477 if (norm_w) vals[i][b] = val;
478 else event->SetEventValue(b, (
FLTNBDATA)val);
482 if (!norm_w) p_OutputDataFile->
WriteEvent(event);
490 norm_factor = nb_counts_old/nb_counts_new;
492 for (i=0; i<nb_events; i++)
497 if (printing_index%5000==0)
499 int percent = ((int)( ((
FLTNB)(i)) * 100. / ((
FLTNB)(nb_events/nb_threads)) ));
500 cout <<
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b " 501 << percent <<
" % " << flush;
510 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while getting the event from index " << i <<
" !" << endl);
512 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while closing file during writing !" << endl);
517 for (
int b=0; b<
event->GetNbValueBins(); b++)
522 vals[i][b] *= norm_factor;
523 nb_counts_new += vals[i][b];
525 event->SetEventValue(b, (
FLTNBDATA)(vals[i][b]));
540 if (verbose>=2) cout <<
"\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" 541 <<
" --> 100 % " << endl;
542 cout<<
"number of counts (original histogram) "<<nb_counts_old<<endl;
543 cout<<
"number of counts (resampled histogram) "<<nb_counts_new<<endl;
544 if (norm_w) cout<<
"renormalization factor "<<norm_factor<<endl;
548 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while closing file after writing !" << endl);
556 Cerr(
"***** castor-datafileBootstrap() -> An error occurred while writing output header file !" << endl);
This class is designed to be a mother virtual class for DataFile.
static sScannerManager * GetInstance()
Instanciate the singleton object and Initialize member variables if not already done, return a pointer to this object otherwise.
static sRandomNumberGenerator * GetInstance()
Instanciate the singleton object and Initialize member variables if not already done, return a pointer to this object otherwise.
void SetVerbose(int a_verbose)
int CheckParameters()
Check the initialization of member variables Call the CheckSpecificParameters() function implemente...
virtual int WriteHeader()=0
This function is implemented in child classes. Generate a header file according to the data output ...
int FindScannerSystem(string a_scannerName)
int BuildScannerObject()
Instantiate the specific scanner object related to the modality, and set verbosity of scanner object...
int SetParametersFrom(vDataFile *ap_DataFile)
virtual int WriteEvent(vEvent *ap_Event, int a_th=0)=0
int ReadInfoInHeader(bool a_affectQuantificationFlag=true)
void SetVerbose(int a_verboseLevel)
virtual int ComputeSizeEvent()=0
This function is implemented in child classes Computation of the size of each event according to th...
void SetVerbose(int a_verboseLevel)
Set verbosity level.
static sOutputManager * GetInstance()
Instanciate the singleton object and Initialize member variables if not already done, return a pointer to this object otherwise.
int InitializeMappedFile()
Check the datafile existency, map it to memory and get the raw char* pointer. .
int InstantiateScanner()
Instantiate scanner using the related function in the scanner classes.
int LogCommandLine(int argc, char **argv)
vEvent * GetEvent(int64_t a_eventIndex, int a_th=0)
int CloseFile()
Close as many binary file stream for writing.
#define SCANNER_SPECT_CONVERGENT
int BuildLUT()
Call the eponym function of the scanner class.
Singleton class that manages output writing on disk (images, sinograms, etc). It also manages loggi...
Engine & GetGenerator()
Get the random generator for the current thread.
Singleton class that Instantiate and initialize the scanner object.
int Initialize(int a_nbThreads, int a_nbExtra)
Instantiate pseudo random number generators, one per thread by default, and additional extra ones if ...
virtual int PrepareDataFile()=0
This function is implemented in child classes Store different kind of information inside arrays (da...
Inherit from vDataFile. Class that manages the reading of a SPECT input file (header + data)...
void SetHeaderDataFileName(const string &a_headerFileName)
int OpenFileForWriting(string a_suffix="")
void SetMPIRank(int a_mpiRank)
#define KEYWORD_MANDATORY
Singleton class that generate a thread-safe random generator number for openMP As singleton...
void SetNbEvents(int64_t a_value)
void SetBedIndex(int a_bedIndex)
Mother class for the Event objects.
int InitOutputDirectory(const string &a_pathFout, const string &a_pathDout)
int ConvertFromString(const string &a_str, string *a_result)
Copy the 'a_str' string in the position pointed by 'a_result'.
Inherit from vDataFile. Class that manages the reading of a CT input file (header + data)...
This class is designed to manage all dimensions and quantification related stuff. ...
void SetVerbose(int a_verboseLevel)
int main(int argc, char **argv)
void SetDefault()
A function used to set number of threads and MPI instances to 1 and bypass the CheckParameters() func...
void SetVerbose(int a_verbose)
int ReadDataASCIIFile(const string &a_file, const string &a_keyword, T *ap_return, int a_nbElts, bool a_mandatoryFlag)
Look for "a_nbElts" elts in the "a_file" file matching the "a_keyword" string passed as parameter a...
void SetImageDimensionsAndQuantification(oImageDimensionsAndQuantification *ap_ImageDimensionsAndQuantification)
Inherit from vDataFile. Class that manages the reading of a PET input file (header + data)...
int GetGeometricInfoFromDataFile(string a_pathToDataFilename)