123 if(
m_verbose>=2)
Cout(
"vDynamicModel::CheckParameters ..."<< endl);
128 Cerr(
"***** vDynamicModel::CheckParameters() -> No image dimensions provided !" << endl);
135 Cerr(
"***** vDynamicModel::CheckParameters() -> Wrong verbosity level provided !" << endl);
142 Cerr(
"***** vDynamicModel::CheckParameters() -> Basis functions number has not been initialized !" << endl);
149 Cerr(
"***** vDynamicModel::CheckParameters() -> Number of model parameter has not been initialized !" << endl);
156 Cerr(
"***** vDynamicModel::CheckParameters() -> Number of model parameter has not been initialized !" << endl);
163 Cerr(
"***** vDynamicModel::CheckParameters() -> An error occurred while checking parameters of the child dynamic class !" << endl);
189 if(
m_verbose >=2)
Cout(
"vDynamicModel::Initialize ..."<< endl);
194 Cerr(
"***** vDynamicModel::Initialize() -> Must call CheckParameters functions before Initialize() !" << endl);
201 if(
m_verbose >=2)
Cout(
"vDynamicModel::Initialize Arterial Input Curve"<< endl);
211 Cerr(
"***** vDynamicModel::Initialize() -> Error while initializing Arterial Input Curve " << endl);
217 Cerr(
"***** vDynamicModel::Initialize() -> Error while checking Arterial Input Curve parameters " << endl);
220 if(
m_verbose >=3)
Cout(
"vDynamicModel::Interpolating Arterial Input Curve ..."<< endl);
235 Cerr(
"***** vDynamicModel::Initialize() -> Wrong initial values in mask (must be either 0 or 1) !" << endl);
248 Cerr(
"***** vDynamicModel::Initialize() -> A problem occurred while initializing stuff specific to the dynamic model !" << endl);
267 if(
m_verbose >=2)
Cout(
"vDynamicModel::ComputeOutputParImage ..." <<endl);
279 if(
m_verbose >=3)
Cout(
" Setting parametric image #: " << p+1 << endl );
309 Cout(
"vDynamicModel::ApplyOutputFOVMaskingOnParametricImages() -> Mask output image" << endl);
325 squared_radius_x *= squared_radius_x;
328 squared_radius_y *= squared_radius_y;
336 #pragma omp parallel for private(x) schedule(guided) 341 squared_distance_x *= squared_distance_x;
347 squared_distance_y *= squared_distance_y;
349 if ( squared_distance_x/squared_radius_x + squared_distance_y/squared_radius_y <= 1. )
continue;
373 for (
int z=0; z<removed_slices; z++)
380 INTNB index = base_z_first + i;
388 INTNB index = base_z_last + i;
416 if(
m_verbose >= 2)
Cout(
"vDynamicModel::EstimateModel ... " <<endl);
421 Cerr(
"***** vDynamicModel::EstimateModel() -> Called while not initialized !" << endl);
429 Cerr(
"***** vDynamicModel::EstimateModel() -> A problem occurred while estimating dynamic image series with model parameters !" << endl);
454 if(
m_verbose >= 2)
Cout(
"vDynamicModel::EstimateImage ... " <<endl);
459 Cerr(
"***** vDynamicModel::EstimateImage() -> Called while not initialized !" << endl);
467 Cerr(
"***** vDynamicModel::EstimateImage() -> A problem occurred while applying dynamic model to current estimate images !" << endl);
492 if(
m_verbose >=3)
Cout(
"vDynamicModel::ReadAndCheckConfigurationFile ..."<< endl);
494 ifstream in_file(a_fileOptions.c_str(), ios::in);
499 string dtag =
"No_image_update";
507 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '"<< dtag <<
"' flag in " << a_fileOptions << endl);
513 dtag =
"No_parameters_update";
521 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '"<< dtag <<
"' flag in " << a_fileOptions << endl);
527 dtag =
"Number_of_iterations_before_image_update";
535 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '"<< dtag <<
"' flag in " << a_fileOptions << endl);
541 dtag =
"Save_parametric_images";
549 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '"<< dtag <<
"' flag in " << a_fileOptions << endl);
555 dtag =
"Save_blacklisted_voxels_images";
563 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '"<< dtag <<
"' flag in " << a_fileOptions << endl);
568 string path_to_mask =
"";
573 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '"<< dtag <<
"' flag in " <<
m_fileOptions << endl);
577 if(!path_to_mask.empty())
587 Cerr(
"***** ivDynamicModel::ReadAndCheckConfigurationFile()-> Error reading Interfile : " << path_to_mask <<
" !" << endl);
592 Cout(
"vDynamicModel::ReadAndCheckConfigurationFile() -> The following mask will be used for the 2nd model parameters estimation: " << path_to_mask << endl);
604 dtag =
"AIC_input_file";
612 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error while trying to read '" << dtag<<
"' flag in " << a_fileOptions << endl);
626 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile() -> Error occurred when trying to process the configuration file from a dynamic model child class " << endl);
632 Cerr(
"***** vDynamicModel::ReadAndCheckConfigurationFile -> Error while trying to read configuration file at: " << a_fileOptions << endl);
656 if(
m_verbose >=3)
Cout(
"vDynamicModel::SaveParametricImages ..." <<endl);
671 if (a_iteration >= 0)
673 stringstream ss; ss << a_iteration + 1;
674 path_to_image.append(
"_it").append(ss.str());
680 stringstream ss; ss << a_subset + 1;
681 path_to_image.append(
"_ss").append(ss.str());
690 Cerr(
"***** iLinearModel::SaveParametricImages()-> Error writing Interfile of output image !" << endl);
694 if(
m_verbose >=3)
Cout(
"vDynamicModel::SaveBlackListedVoxelsMask ..." <<endl);
699 if (a_iteration >= 0)
702 ss << a_iteration + 1;
703 path_to_image.append(
"_it").append(ss.str());
709 path_to_image.append(
"_ss").append(ss.str());
716 Cerr(
"***** iLinearModel::SaveParametricImages()-> Error writing image of blacklisted voxels !" 732 if (a_iteration >= 0)
734 stringstream ss; ss << a_iteration + 1;
735 path_to_image.append(
"parRGmodel_it").append(ss.str());
741 stringstream ss; ss << a_subset + 1;
742 path_to_image.append(
"_ss").append(ss.str());
751 Cerr(
"***** iLinearModel::SaveParametricImages()-> Error writing Interfile of output image !" << endl);
763 if (a_iteration >= 0)
765 stringstream ss; ss << a_iteration + 1;
766 path_to_image.append(
"parCGmodel_it").append(ss.str());
772 stringstream ss; ss << a_subset + 1;
773 path_to_image.append(
"_ss").append(ss.str());
782 Cerr(
"***** iLinearModel::SaveParametricImages()-> Error writing Interfile of output image !" << endl);
846 int pfeas, iz, jz, iz1, iz2, npp1, *index;
847 FLTNB d1, d2, sm, up, ss, *w, *zz;
848 int iter, k, j=0, l, itmax, izmax=0, nsetp, ii, jj=0, ip;
849 FLTNB temp, wmax, t, alpha, asave, dummy, unorm, ztest, cc;
852 if(m<=0 || n<=0 || A==NULL || B==NULL || X==NULL)
854 Cerr(
"***** vDynamicModel::NNLS()-> Incorrect input parameters !" << endl);
859 if(wp!=NULL) w=wp;
else w=(
FLTNB*)calloc(n,
sizeof(
FLTNB));
860 if(zzp!=NULL) zz=zzp;
else zz=(
FLTNB*)calloc(m,
sizeof(
FLTNB));
861 if(indexp!=NULL) index=indexp;
else index=(
int*)calloc(n,
sizeof(
int));
863 if(w==NULL || zz==NULL || index==NULL)
865 Cerr(
"***** vDynamicModel::NNLS()-> Incorrect memory allocation on working space !" << endl);
876 iz2=n-1; iz1=0; nsetp=0; npp1=0;
887 while(iz1<=iz2 && nsetp<m)
890 for(iz=iz1; iz<=iz2; iz++)
893 for(l=npp1; l<m; l++)
902 for(wmax=0., iz=iz1; iz<=iz2; iz++)
916 iz=izmax; j=index[iz];
921 asave=A[j][npp1]; up=0.0;
923 NNLS_LSS_H12(1, npp1, npp1+1, m, &A[j][0], 1, &up, &dummy, 1, 1, 0);
927 for(l=0; l<nsetp; l++)
933 d2 = unorm + (d1=A[j][npp1], fabs(d1)) * 0.01;
941 NNLS_LSS_H12(2, npp1, npp1+1, m, &A[j][0], 1, &up, zz, 1, 1, 1);
943 ztest = zz[npp1]/A[j][npp1];
962 index[iz]=index[iz1]; index[iz1]=j; iz1++; nsetp=npp1+1; npp1++;
964 if(iz1<=iz2)
for(jz=iz1; jz<=iz2; jz++)
967 NNLS_LSS_H12(2, nsetp-1, npp1, m, &A[j][0], 1, &up, &A[jj][0], 1, m, 1);
970 if(nsetp!=m)
for(l=npp1; l<m; l++) A[j][l]=0.;
974 for(l=0; l<nsetp; l++)
978 for(ii=0; ii<=ip; ii++)
979 zz[ii] -= A[jj][ii] * zz[ip+1];
985 while( ++iter<itmax )
988 for(alpha=2.0, ip=0; ip<nsetp; ip++)
993 t = -X[l] / (zz[ip]-X[l]);
1004 if(alpha==2.0)
break;
1007 for(ip=0; ip<nsetp; ip++)
1010 X[l] += alpha*(zz[ip]-X[l]);
1015 k=index[jj+1]; pfeas=1;
1022 for(j=jj+1; j<nsetp; j++)
1024 ii=index[j]; index[j-1]=ii;
1026 NNLS_LSS_G1(A[ii][j-1], A[ii][j], &cc, &ss, &A[ii][j-1]);
1028 for(A[ii][j]=0., l=0; l<n; l++)
if(l!=ii)
1032 A[l][j-1] = cc*temp+ss*A[l][j];
1033 A[l][j] = -ss*temp+cc*A[l][j];
1036 temp=B[j-1]; B[j-1]=cc*temp+ss*B[j]; B[j]=-ss*temp+cc*B[j];
1039 npp1=nsetp-1; nsetp--; iz1--; index[iz1]=k;
1045 for(jj=0, pfeas=1; jj<nsetp; jj++)
1060 for(l=0; l<nsetp; l++)
1064 for(ii=0; ii<=ip; ii++)
1065 zz[ii] -= A[jj][ii]*zz[ip+1];
1067 zz[ip] /= A[jj][ip];
1076 for(ip=0; ip<nsetp; ip++)
1091 for(k=npp1; k<m; k++)
1104 if(wp==NULL) free(w);
1105 if(zzp==NULL) free(zz);
1106 if(indexp==NULL) free(index);
1165 FLTNB d1, b, clinv, cl, sm;
1169 if(mode!=1 && mode!=2)
return(1);
1170 if(m<1 || u==NULL || u_dim1<1 || cm==NULL)
return(1);
1171 if(lpivot<0 || lpivot>=l1 || l1>m)
return(1);
1174 cl = fabs(u[lpivot*u_dim1]);
1178 if(cl<=0.)
return(0);
1182 for(j=l1; j<m; j++) {
1183 cl = fmax(fabs(u[j*u_dim1]), cl);
1186 if(cl<=0.)
return(0);
1191 d1=u[lpivot*u_dim1]*clinv;
1193 for(j=l1; j<m; j++) {
1194 d1=u[j*u_dim1]*clinv;
1199 if(u[lpivot*u_dim1] > 0.) cl=-cl;
1200 *up = u[lpivot*u_dim1] - cl;
1201 u[lpivot*u_dim1]=cl;
1205 b=(*up)*u[lpivot*u_dim1];
1208 if(b>=0.0)
return(0);
1211 for(j=0; j<ncv; j++) {
1213 sm = cm[ lpivot*ice + j*icv ] * (*up);
1214 for(k=l1; k<m; k++) sm += cm[ k * ice + j*icv ] * u[ k*u_dim1 ];
1218 cm[ lpivot * ice + j*icv] += sm*(*up);
1220 for(k=l1; k<m; k++) cm[ k*ice + j*icv] += u[k * u_dim1]*sm;
1257 xr=b/a; d1=xr; yr=sqrt(d1*d1 + 1.); d1=1./yr;
1258 *cterm=(a>=0.0 ? fabs(d1) : -fabs(d1));
1259 *sterm=(*cterm)*xr; *sig=fabs(a)*yr;
1263 xr=a/b; d1=xr; yr=sqrt(d1*d1 + 1.); d1=1./yr;
1264 *sterm=(b>=0.0 ? fabs(d1) : -fabs(d1));
1265 *cterm=(*sterm)*xr; *sig=fabs(b)*yr;
1269 *sig=0.; *cterm=0.; *sterm=1.;
virtual int CheckSpecificParameters()=0
This function is used to check the parameters of the child functions before initialization if require...
#define INTF_LERP_DISABLED
void NNLS_LSS_G1(FLTNB a, FLTNB b, FLTNB *cterm, FLTNB *sterm, FLTNB *sig)
This function is used by the NNLS function() Construction and/or application of a single Householder ...
int NNLS(FLTNB **A, int m, int n, FLTNB *B, FLTNB *X, FLTNB *rnorm, FLTNB *wp, FLTNB *zzp, int *indexp)
Implementation of NNLS (non-negative least squares) algorithm Derived from Turku PET center libraries...
virtual int CheckParameters()
This function is used to check parameters after the latter have been all set using Set functions...
int IntfWriteImgDynCoeffFile(const string &a_pathToImg, FLTNB **a2p_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int a_nbParImgs, int vb, bool a_mergeDynImgFlag=false)
virtual int InitializeSpecific()=0
A private function used to initialize everything specific to the child model.
FLTNB GetVoxSizeX()
Get the voxel's size along the X axis, in mm.
FLTNB GetFOVOutPercent()
Get the percentage of transaxial unmasked FOV.
oArterialInputCurve * mp_ArterialInputCurve
int ReadAndCheckConfigurationFile(string a_fileOptions)
This function is used to read options from a configuration file. It looks for the parameters implem...
int NNLS_LSS_H12(int mode, int lpivot, int l1, int m, FLTNB *u, int u_dim1, FLTNB *up, FLTNB *cm, int ice, int icv, int ncv)
oImageDimensionsAndQuantification * mp_ID
bool m_saveBlacklistedImageMaskFlag
virtual int ApplyOutputFOVMaskingOnParametricImages()
Mask the outside of the transaxial FOV based on the m_fovOutPercent.
FLTNB ** m2p_RGParametricImages
static sOutputManager * GetInstance()
Instanciate the singleton object and Initialize member variables if not already done, return a pointer to this object otherwise.
virtual int EstimateImage(oImageSpace *ap_Image, int a_ite, int a_sset)
FLTNB GetVoxSizeY()
Get the voxel's size along the Y axis, in mm.
const string & GetPathName()
virtual int EstimateModel(oImageSpace *ap_Image, int a_ite, int a_sset)
This function checks if the EstimateModelParameters() function (specific to each model) must be calle...
uint32_t * GetFramesTimeStopArray(int a_bed)
Singleton class that manages output writing on disk (images, sinograms, etc). It also manages loggi...
FLTNB ** m2p_outputParImages
FLTNB ** m2p_CGParametricImages
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...
bool m_ModelSpecificBasisFunctionsRequired
INTNB GetNbVoxXY()
Get the number of voxels in a slice.
virtual int ReadAndCheckConfigurationFileSpecific()=0
This function is used to read options from a configuration file. It is pure virtual so must be impl...
int Initialize()
A public function used to initialize the dynamic model.
FLTNB ** m2p_nestedModelTimeBasisFunctions
bool m_noParametersUpdateFlag
virtual ~vDynamicModel()
Destructor of vDynamicModel.
vDynamicModel()
Constructor of vDynamicModel. Simply set all data members to default values.
const string & GetBaseName()
uint32_t * GetFramesTimeStartsArray(int a_bed)
Get the array of frame start times for a bed in Ms at uint32_t.
FLTNB ** m2p_parametricImages
This class holds all the matrices in the image domain that can be used in the algorithm: image...
int GetNbTimeFrames()
Get the number of time frames.
INTNB GetNbVoxXYZ()
Get the total number of voxels.
virtual int EstimateImageWithModel(oImageSpace *ap_Image, int a_ite, int a_sset)=0
This function checks if the EstimateImageWithModel() function (specific to each model) must be called...
INTNB GetNbVoxX()
Get the number of voxels along the X axis.
INTNB GetNbVoxZ()
Get the number of voxels along the Z axis.
Declaration of class vDynamicModel.
virtual void ComputeOutputParImage()
Compute output image using the m2p_parametricImages matrix Store the result in the m2p_outputParImage...
virtual int EstimateModelParameters(oImageSpace *ap_Image, int a_ite, int a_sset)=0
This function is pure virtual so must be implemented by children. It can be used to estimate any te...
int SaveParametricImages(int a_iteration, int a_subset=-1)
This function is virtual it can be overloaded by children if required.
int IntfReadImage(const string &a_pathToHeaderFile, FLTNB *ap_ImgMatrix, oImageDimensionsAndQuantification *ap_ID, int vb, bool a_lerpFlag)
Main function dedicated to Interfile 3D image loading.
FLTNB * mp_blackListedvoxelsImage
int IntfWriteImgFile(const string &a_pathToImg, FLTNB *ap_ImgMatrix, const Intf_fields &ap_IntfF, int vb)
Main function dedicated to Interfile 3D image writing. Recover image information from a provided In...
INTNB GetNbSliceOutMask()
Get the number of extrem slices that will be masked at each side.
INTNB GetNbVoxY()
Get the number of voxels along the Y axis.