8 #include "gVariables.hh" 10 #include "sOutputManager.hh" 13 #include "oImageSpace.hh" 14 #include "oDynamicModelManager.hh" 15 #include "sRandomNumberGenerator.hh" 16 #include "sChronoManager.hh" 26 cout <<
"Usage: castor-imageDynamicTools -i path_to_ifile.root"<< endl;
27 cout <<
" -fout name"<< endl;
28 cout <<
" -OR- "<< endl;
29 cout <<
" -dout name "<< endl;
30 cout <<
" -dynamic-model param" << endl;
33 cout <<
"[Main settings]:" << endl;
34 cout <<
" -i path_to_in_image : give an input dynamic interfile image" << endl;
35 cout <<
" -fout name : Give the root name for all output files. All output files will be written as 'name_suffix.ext'." << endl;
36 cout <<
" So the provided name should not end with '.' or '/' character. (no default, alternative to -dout)" << endl;
37 cout <<
" -dout name : Give the name of the output directory where all output files will be written. All files will also" << endl;
38 cout <<
" be prefixed by the name of the directory. The provided name should not end with '.' or '/' character." << endl;
39 cout <<
" (no default, alternative to -fout)" << endl;
40 cout <<
" -dynamic-model param : Dynamic model applied to either the frames of a dynamic acquisition, respiratory-gated frames, cardiac-gated frames, or simultaneously between these datasets." << endl;
41 cout <<
" Select the dynamic model to be used, along with a configuration file (model:file) or the list of parameters associated to the model (model_name,param1,param2,...)." << endl;
43 cout <<
" -delay value : Delay condition in seconds to exclude early frames from the application of the dynamic model, if series of images are provided as input." << endl;
45 cout <<
" -im param : Provide an image-based deformation model to be used for involuntary motion correction (for testing purposes), along with a configuration file (deformation:file)" << endl;
46 cout <<
" or the list of parameters associated to the projector (deformation,param1,param2,...)." << endl;
49 cout <<
"[Optional settings]:" << endl;
50 cout <<
" -it nb : Number of iterations for both parameters estimation and subsequent dynamic image estimation steps of the dynamic model (default = 1)." << endl;
51 cout <<
" -omd : (M)erge (D)ynamic images. Indicate if a dynamic serie of 3D images should be written on disk in one file" << endl;
52 cout <<
" instead of a serie of 3D images associated with an interfile metaheader" << endl;
53 cout <<
" -odi : Output dynamic images. Generate the serie of dynamic images estimated from the parametric images of the dynamic model. By default, only the parametric images are generated" << endl;
55 cout <<
" -onbp prec : By default, numbers are displayed using scientific format. This option allows to customize the format and precision" << endl;
56 cout <<
" : The format is format,precision. f is the format (s=scientific, f=fixed), p is the precision" << endl;
57 cout <<
" eg: -onbp f,5 --> fixed with 5 digits precision, -onbp --> scientifici with max precision." << endl;
60 cout <<
" -th param : Set the number of threads for parallel computation (default: 1). If 0 is given, then the maximum number of available threads is automatically determined." << endl;
61 cout <<
" Can also give two parameters separated by a comma (e.g. 16,4), to distinguish between the number of threads for projection and image operations respectively." << endl;
64 cout <<
"[Miscellaneous settings]:" << endl;
65 cout <<
" -vb : give the verbosity level, from 0 (no verbose) to above 5 (at the event level) (default: 1)." << endl;
76 int main(
int argc,
char** argv)
85 MPI_Init(&argc, &argv);
86 MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
87 MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
88 if (mpi_rank!=0)
return 0;
99 string input_file =
"";
100 vector<string> path_to_input_image;
103 string path_fout =
"";
104 string path_dout =
"";
105 string dynamic_model_options =
"";
106 string motion_options =
"";
109 string frame_list =
"";
113 int nb_FramesToSkip =0;
115 int nb_resp_gates = 1;
117 int nb_card_gates = 1;
119 uint32_t nb_ite_dyn = 1;
121 string nb_threads =
"1";
123 bool merge_dynamic_imgs_flag =
false;
125 bool output_dynamic_imgs_from_model =
false;
127 string onb_prec =
"s,0";
135 for (
int i=1; i<argc; i++)
137 string option = (string)argv[i];
139 if (option==
"-h" || option==
"--help" || option==
"-help")
ShowHelp(0);
144 if (path_to_input_image.size() > 0)
146 Cerr(
"***** castor-imageDynamicTools :: the following file names have already been provided (-i/-il options must be used ONCE): " << endl);
147 for (
size_t i=0 ; i<path_to_input_image.size() ; i++)
148 Cout(
" Selected input image file: " << path_to_input_image[i] << endl);
153 if (argv[i+1] == NULL)
155 Cerr(
"***** castor-imageDynamicTools :: argument missing for option: " << option << endl);
160 input_file = argv[i+1];
161 path_to_input_image.push_back(input_file);
168 else if (option==
"-dout")
172 Cerr(
"***** castor-imageDynamicTools() -> Argument missing for option: " << option << endl);
175 path_dout = argv[i+1];
179 else if (option==
"-fout")
183 Cerr(
"***** castor-imageDynamicTools() -> Argument missing for option: " << option << endl);
186 path_fout = argv[i+1];
191 else if (option==
"-omd")
193 merge_dynamic_imgs_flag =
true;
196 else if (option==
"-odi")
198 output_dynamic_imgs_from_model =
true;
202 else if (option==
"-th")
206 Cerr(
"***** castor-imageDynamicTools() -> Argument missing for option: " << option << endl);
209 nb_threads = (string)argv[i+1];
215 else if (option==
"-dynamic-model")
219 Cerr(
"***** castor-imageDynamicTools() -> Argument missing for option: " << option << endl);
222 dynamic_model_options = (string)argv[i+1];
226 else if (option==
"-delay")
230 Cerr(
"***** castor-imageDynamicTools :: Argument missing for option: " << option << endl);
233 delayInSec = atoi(argv[i+1]);
237 else if (option==
"-it")
241 Cerr(
"***** castor-imageDynamicTools() -> Argument missing for option: " << option << endl);
247 Cerr(
"***** castor-imageDynamicTools() -> Exception when trying to read provided number iterations for the dynamic model (must be strictly positive integer) '" << nb_ite_dyn <<
" for option: " << option << endl);
254 else if (option==
"-im")
258 Cerr(
"***** castor-recon() -> Argument missing for option: " << option << endl);
261 motion_options = (string)argv[i+1];
266 else if (option==
"-vb")
270 Cerr(
"***** castor-imageDynamicTools :: Argument missing for option: " << option << endl);
273 vb = atoi(argv[i+1]);
277 else if (option==
"-onbp")
281 Cerr(
"***** castor-recon() -> Argument missing for option: " << option << endl);
284 onb_prec = argv[i+1];
289 Cerr(
"***** castor-imageDynamicTools :: Unknown option '" << option <<
"' !" << endl);
302 if (path_to_input_image.empty() )
304 Cerr(
"***** castor-imageDynamicTools :: Please provide at least one data filename (-i)" << endl);
312 Cout(
" Selected root data-file(s) to convert: " << endl);
313 for (
size_t i=0 ; i<path_to_input_image.size() ; i++)
314 Cout(path_to_input_image[i] << endl);
319 if (path_fout.empty() && path_dout.empty())
321 Cerr(
"***** castor-imageDynamicTools() -> Please provide an output option for output files (-fout or -dout) !" << endl);
325 if (!path_fout.empty() && !path_dout.empty())
327 Cerr(
"***** castor-imageDynamicTools() -> Please provide either output option -fout or -dout but not both !" << endl);
332 if (dynamic_model_options !=
"" 333 && motion_options !=
"")
335 Cerr(
"***** castor-imageDynamicTools() -> Please provide either output option -dynamic-model or -im but not both !" << endl);
363 Cerr(
"***** castor-imageDynamicTools() -> A problem occurred while checking for the config directory path !" << endl);
369 Cerr(
"***** castor-imageDynamicTools() -> A problem occurred while initializing output directory !" << endl);
375 Cerr(
"***** castor-imageDynamicTools() -> A problem occurred while logging command line arguments !" << endl);
390 Cerr(
"***** castor-imageDynamicTools() -> An error occurred while trying to read the interfile header of file reading file " << path_to_input_image[i] <<
" !" << endl);
395 vector <string> image_filenames;
397 if (
IntfIsMHD(path_to_input_image[i], image_filenames) < 0 )
399 Cerr(
"***** castor-imageDynamicTools() -> Error : while trying to read the interfile header '"<< path_to_input_image[i] <<
"' !" << endl);
407 if(image_filenames.size() > 1)
416 Cerr(
"***** castor-imageDynamicTools() -> An error occurred while trying to read the interfile header of file reading file " << path_to_input_image[i] <<
" !" << endl);
421 stringstream ss_duration, ss_startTime;
426 string frame_duration_str = ss_duration.str();
427 string frame_start_str = ss_startTime.str();
430 if (stoi(frame_start_str)<delayInSec) nb_FramesToSkip ++;
434 frame_list.append(frame_start_str);
435 frame_list.append(
":");
436 frame_list.append(frame_duration_str);
440 frame_list.append(
",");
449 Cerr(
"***** castor-imageDynamicTools() -> Interfile reading error of the input image :"<<endl);
451 <<
"' does not match the actual number of time frames * respiratory gates * cardiac gates ) '"<< IF.
nb_time_frames <<
"' !" << endl);
469 frame_list.append(ss.str());
479 frame_list.append(
":").append(ss.str());
482 frame_list.append(
",").append(ss.str());
496 frame_list.append(
",").append(ss.str());
504 frame_list.append(
":").append(ss.str());
511 if(vb >= 2)
Cout (
"----- Number of frames to skip from model fit: " << nb_FramesToSkip << endl);
512 if(vb >= 2)
Cout (
"Frame list is : " << frame_list << endl);
546 Cerr(
"***** castor-imageDynamicTools :: A problem occurred while checking image dimensions parameters !" << endl);
551 Cerr(
"***** castor-imageDynamicTools :: A problem occurred while initializing image dimensions !" << endl);
558 Cerr(
"***** castor-imageDynamicTools :: A problem occurred while initializing Dynamic data manager's class !" << endl);
569 if (vb>=5)
Cout(
"----- Dynamic model initialization (if any) ... -----" << endl);
574 p_DynamicModelManager->
SetOptions(dynamic_model_options);
579 Cerr(
"***** castor-imageDynamicTools() -> A problem occurred while checking dynamic model manager's parameters !" << endl);
585 Cerr(
"***** castor-imageDynamicTools() -> A problem occurred while initializing dynamic model manager !" << endl);
590 if (vb>=5)
Cout(
"----- Dynamic model initialization OK -----" << endl);
608 p_ImageSpace->
InitImage(path_to_input_image[i], 0);
616 if (vb>=5)
Cout(
"----- Image deformation initialization (if any) ... -----" << endl);
624 if (motion_options !=
"")
626 p_DeformationManager->
SetOptions(motion_options);
637 Cerr(
"***** castor-recon() -> A problem occurred while checking image deformation manager's parameters !" << endl);
643 Cerr(
"***** castor-recon() -> A problem occurred while initializing image deformation manager !" << endl);
647 if (vb >=5)
Cout(
"----- Image deformation initialization OK -----" << endl);
650 clock_t clock_start = clock();
651 time_t time_start = time(NULL);
656 if(dynamic_model_options !=
"")
658 for(uint32_t it=0; it<nb_ite_dyn ; it++)
660 Cout(
" --- Apply Dynamic Model, iteration" << it+1 <<
" --- " << endl);
663 Cerr(
"***** castor-imageDynamicTools -> A problem occurred while applying dynamic model to current estimate images !" << endl);
671 Cerr(
"***** castor-imageDynamicTools -> A problem occurred while saving parametric images related to the dynamic model !" << endl);
675 if(output_dynamic_imgs_from_model)
682 Cerr(
"***** castor-imageDynamicTools -> A problem occurred while saving output image !" << endl);
687 Cout(endl <<
" --- Parametric images generated at: " << ((path_fout.empty()) ? path_dout : path_fout) <<
" --- " << endl);
694 if(motion_options !=
"")
699 Cerr(
"***** castor-imageDynamicTools -> A problem occurred while applying dynamic model to current estimate images !" << endl);
708 Cerr(
"***** castor-imageDynamicTools -> A problem occurred while saving output image !" << endl);
712 Cout(endl <<
" --- Parametric images generated at: " << ((path_fout.empty()) ? path_dout : path_fout) <<
" --- " << endl);
717 clock_t clock_stop = clock();
718 time_t time_stop = time(NULL);
724 if (vb>=2)
Cout(
"oIterativeAlgorithm::IterateCPU() -> Total time spent | User: " << (
FLTNB)(time_stop-time_start)
725 <<
" sec | CPU: " << (clock_stop-clock_start)/((
FLTNB)CLOCKS_PER_SEC) <<
" sec" << endl);
732 if (vb>=2)
Cout(
" Deallocating objects ..." << endl);
740 if(p_ImageSpace)
delete p_ImageSpace;
741 if(p_ID)
delete p_ID;
743 Cout(
" --- END --- " << endl << endl);
void SetNbCardGates(int a_nbCardGates)
void SetVerbose(int a_verbose)
void DeallocateImage()
Free memory for the main image matrices.
void SetFOVSizeX(FLTNB a_fovSizeX)
int IntfIsMHD(string a_pathToFile, vector< string > &ap_lPathToImgs)
Check if the string in argument contains the path to a Interfile metaheader.
void SetVoxSizeY(FLTNB a_voxSizeY)
void SetnbFramesToSkip(int a_FramesToSkip)
void SetOffsetZ(FLTNB a_offsetZ)
void InstantiateImage()
Allocate memory for the main image matrices.
void SetVoxSizeX(FLTNB a_voxSizeX)
void SetNbRespGates(int a_nbRespGates)
void SetNbVoxY(INTNB a_nbVoxY)
int Initialize()
Set the dynamic model flag and instanciate/initialize model objects through the ParseOptionsAndInitia...
static sOutputManager * GetInstance()
Instanciate the singleton object and Initialize member variables if not already done, return a pointer to this object otherwise.
int InitImage(const string &a_pathToInitialImage, FLTNB a_value)
int SetNbThreads(const string &a_nbThreads)
int SetOutNbPrec(string a_format)
int LogCommandLine(int argc, char **argv)
vector< FLTNB > image_start_time
void SetFOVSizeY(FLTNB a_fovSizeY)
void SetNbVoxZ(INTNB a_nbVoxZ)
int CheckConfigDir(const string &a_path)
void SetOffsetY(FLTNB a_offsetY)
int CheckParameters()
A function used to check the parameters settings.
int Initialize()
A function used to initialize all that is needed.
Singleton class that manages output writing on disk (images, sinograms, etc). It also manages loggi...
This class is designed to manage the use of dynamic model in the reconstruction.
void SetFOVSizeZ(FLTNB a_fovSizeZ)
int SaveOutputImage(int a_iteration, int a_subset=-1)
void SetVerbose(int a_verboseLevel)
void ComputeOutputImage()
void SetImageDimensionsAndQuantification(oImageDimensionsAndQuantification *ap_ImageDimensionsAndQuantification)
int SaveParametricImages(int a_iteration, int a_subset=-1)
void SetMergeDynImagesFlag(bool a_flag)
int InitDynamicData(string a_pathTo4DDataSplittingFile, int a_respMotionCorrectionFlag, int a_cardMotionCorrectionFlag, int a_invMotionCorrectionFlag, int a_nbRespGates, int a_nbCardGates)
void InstantiateForwardImage()
Allocate memory for the forward image matrices.
int CheckParameters()
This function is used to check parameters after the latter have been all set using Set functions...
void IntfKeyInitFields(Intf_fields *ap_IF)
Init the file of an Interfile fields structure passed in parameter to their default values...
void SetFOVOutMasking(FLTNB a_fovOutPercent, INTNB a_nbSliceOutMask)
void SetNbBeds(int a_nbBeds)
void SetVoxSizeZ(FLTNB a_voxSizeZ)
void InstantiateBackwardImageFromDynamicBins()
Allocate memory for the backward image matrices and initialize them.
void DeallocateOutputImage()
Free memory for the Image matrices dedicated to output writing on disk.
Interfile fields. This structure contains all the Interfile keys currently managed by CASToR Decl...
void GetUserEndianness()
Check user/host computer endianness and write it to the global variable User_Endianness.
This class holds all the matrices in the image domain that can be used in the algorithm: image...
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'.
vector< FLTNB > image_duration
void SetNbVoxX(INTNB a_nbVoxX)
void SetOffsetX(FLTNB a_offsetX)
This class is designed to manage all dimensions and quantification related stuff. ...
void SetOptions(const string &a_options)
void SetFrames(const string &a_frameList)
int IntfReadHeader(const string &a_pathToHeaderFile, Intf_fields *ap_IntfFields, int vb)
Read an Interfile header.
void SetVerbose(int a_verbose)
void SetImageDimensionsAndQuantification(oImageDimensionsAndQuantification *ap_ImageDimensionsAndQuantification)
void DeallocateBackwardImageFromDynamicBins()
Free memory of the backward image matrices.
void DeallocateForwardImage()
Free memory for the forward image matrices.
int ApplyDynamicModel(oImageSpace *ap_ImageS, int a_iteration, int a_subset)
void SetVerbose(int a_verboseLevel)
void InstantiateOutputImage()
Instanciate Image matrices dedicated to output writing on disk.