fitCentersPixelIVF.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. function [globalPar, cPars] = ...
  2. fitCentersPixelIVF(path, patientID, cax, cm, centers,sigma2)
  3. %path is the core directory
  4. %cax are the mid intervals in images
  5. %cm are the interval durations (1,n)
  6. %data are the images
  7. %nclass is number of typical responses sought
  8. npar=1;
  9. nglob=2;
  10. nclass=size(centers,1);
  11. cm2=ones(nclass,1)*cm';
  12. %w=sqrt(cm2.*centers)./sum(cm2.*centers,2);
  13. w=ones(nclass,1)*sqrt(cm)';
  14. %make sure that the function that is most similar to input function has
  15. %a high weight on maximum value, which comes as the second reading
  16. %abandonded, because decay time gets shortened and fitting of the slow
  17. %rise is given up in favor of meeting the high point through k1 value
  18. %[~,im]=max(centers(:,2));
  19. %w(im,2)=w(im,2)+0.1*(sqrt(max(cm))-w(im,2));
  20. %for individual fits
  21. valueGeneratorSettings1.type='loggaus';
  22. valueGeneratorSettings1.parameters=[-3,1];
  23. valueGeneratorSettings1.u=[0,Inf];
  24. ivar=2;
  25. valueGeneratorSettings1(ivar).type='flat';
  26. valueGeneratorSettings1(ivar).parameters=[0,1];
  27. valueGeneratorSettings1(ivar).u=[0,1];
  28. ivar=ivar+1;
  29. valueGeneratorSettings1(ivar).type='loggaus';
  30. valueGeneratorSettings1(ivar).parameters=[-7,2];
  31. valueGeneratorSettings1(ivar).u=[0,Inf];
  32. ivar=ivar+1;
  33. valueGeneratorSettings1(ivar).type='gaus';
  34. valueGeneratorSettings1(ivar).parameters=[10,5];
  35. valueGeneratorSettings1(ivar).u=[0,Inf];
  36. options = optimoptions(@lsqnonlin);
  37. options.Display='off';
  38. %[options.OptimalityTolerance,options.FunctionTolerance,options.StepTolerance];
  39. %holds best estimate
  40. n1=40;
  41. nFitIndividual=4;
  42. qfit=zeros(1,nFitIndividual*nclass);
  43. %use input functions estimate from center fits
  44. p=[readFitParameters(path,patientID,10); ...
  45. readFitParameters(path,patientID,20); ...
  46. readFitParameters(path,patientID,30)];
  47. fp=median(p);%4 values, const, fall & rise time, delay
  48. fprintf('Global fit: (%f,%f,%f,%f)\n',fp(1),fp(2),fp(3),fp(4));
  49. chiSum=0;
  50. %now fit every curve individually with the known input function
  51. for j=1:nclass
  52. cay=centers(j,:)';
  53. w=sqrt(cm);
  54. fun1=@(par)evaluateSingleDiff(cax,cay,w,fp,par);
  55. qpar=zeros(1,4);
  56. qmin=1e30;
  57. for k=1:n1
  58. [par0,ul1,ub1]=generateInitialValues(valueGeneratorSettings1);
  59. [par1,resnorm,residual,exitflag,output,lambda,jacobian] = ...
  60. lsqnonlin(fun1,par0,ul1,ub1,options);
  61. fchi2=sqrt(residual'*residual);
  62. if fchi2<qmin
  63. qmin=fchi2;
  64. qpar=par1;
  65. end
  66. end
  67. qfit((j-1)*nFitIndividual+1:j*nFitIndividual)=qpar;
  68. fprintf('Center: %d (%f %f %f %f)/(%f %f %f %f)\n',j,...
  69. par0(1),par0(2),par0(3),par0(4),...
  70. par1(1),par1(2),par1(3),par1(4));
  71. %residual
  72. chiSum=chiSum+qmin;
  73. end
  74. %writematrix(ci,fullfile(path,sprintf('CI%d.txt',i)),'Delimiter','tab');
  75. %output
  76. chi2=chiSum;
  77. fprintf('Done %f \n',chi2);
  78. %this is the result that will be used by k1 calculator
  79. sigmaCode=sprintf('%.2f',sigma2);
  80. sigmaCode=strrep(sigmaCode,'.','p');
  81. code=sprintf('%s_%d_%s_PixelIVF',patientID,nclass,sigmaCode);
  82. ofile=fullfile(path,patientID,sprintf('%s_fitParFinal.txt',code));
  83. try
  84. writematrix([chi2 fp qfit],ofile,'Delimiter','tab');
  85. catch ME
  86. dlmwrite(ofile,[chi2 fp qfit],'\t');
  87. end
  88. %ofile=fullfile(path,sprintf('CIFinal_%d_%d.txt',nclass,realizationId));
  89. %writematrix(fci,ofile,'Delimiter','tab');
  90. globalPar=[fp(1:3) 0 0 0];
  91. cPars=reshape(qfit,[],nclass)';
  92. %reset k1 for those where BVF is nearly one
  93. %cPars(cPars(:,2)>0.4,1)=0;
  94. %reset k1 for those where k2 is abnormally large
  95. %cPars(cPars(:,3)>1,1)=0;
  96. opath=fullfile(path,patientID);
  97. plotFunctions(opath,code,globalPar,cPars,cax,centers);
  98. end