fitFromClusters.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. function [globalPar, cPars, xa, U] = ...
  2. fitFromClusters(path, cax, cm, data, nclass, realizationId)
  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=3;
  9. nglob=3;
  10. %correct plotFunctions and evaluateCenterDiff
  11. c1=size(cax,1);
  12. A=reshape(data,[],c1);
  13. %find most common responses
  14. %nclass=10;
  15. [centers,U]=fcm(A,nclass);
  16. %data(ix,iy,iz,:)=A((iz-1)*i1*i2+(iy-1)*i1+ix,:)
  17. cm2=ones(size(centers,1),1)*cm';
  18. %w=sqrt(cm2.*centers)./sum(cm2.*centers,2);
  19. w=ones(size(centers,1),1)*sqrt(cm)';
  20. pars=cax(1:2)';
  21. fun=@(x)evaluateCenterDiff(cax,centers,w,pars,x(1:nglob),...
  22. reshape(x(nglob+1:nglob+npar*nclass),[],nclass)');
  23. %cax are the delays
  24. %c
  25. valueGeneratorSettings.type='poisson';
  26. valueGeneratorSettings.parameters=[25];%mu
  27. valueGeneratorSettings.u=[0,Inf];
  28. ivar=2;
  29. valueGeneratorSettings(ivar).type='loggaus';
  30. valueGeneratorSettings(ivar).parameters=[1.5,1];%log10(mean),log10(std)
  31. valueGeneratorSettings(ivar).u=[0,Inf];
  32. ivar=ivar+1;
  33. valueGeneratorSettings(ivar).type='loggaus';
  34. valueGeneratorSettings(ivar).parameters=[-1,1];%log10(mean),log10(std)
  35. valueGeneratorSettings(ivar).u=[0,Inf];
  36. ivar=ivar+1;
  37. for j=1:nclass
  38. valueGeneratorSettings(ivar).type='loggaus';
  39. valueGeneratorSettings(ivar).parameters=[-3,1];
  40. valueGeneratorSettings(ivar).u=[0,Inf];
  41. ivar=ivar+1;
  42. valueGeneratorSettings(ivar).type='flat';
  43. valueGeneratorSettings(ivar).parameters=[0,1];
  44. valueGeneratorSettings(ivar).u=[0,1];
  45. ivar=ivar+1;
  46. valueGeneratorSettings(ivar).type='loggaus';
  47. valueGeneratorSettings(ivar).parameters=[-7,2];
  48. valueGeneratorSettings(ivar).u=[0,Inf];
  49. ivar=ivar+1;
  50. end
  51. %for individual fits
  52. valueGeneratorSettings1.type='loggaus';
  53. valueGeneratorSettings1.parameters=[-3,1];
  54. valueGeneratorSettings1.u=[0,Inf];
  55. ivar=2;
  56. valueGeneratorSettings1(ivar).type='flat';
  57. valueGeneratorSettings1(ivar).parameters=[0,1];
  58. valueGeneratorSettings1(ivar).u=[0,1];
  59. ivar=ivar+1;
  60. valueGeneratorSettings1(ivar).type='loggaus';
  61. valueGeneratorSettings1(ivar).parameters=[-7,2];
  62. valueGeneratorSettings1(ivar).u=[0,Inf];
  63. [x0,ul,ub]=generateInitialValues(valueGeneratorSettings);
  64. options = optimoptions(@lsqnonlin);
  65. options.Display='off';
  66. %[options.OptimalityTolerance,options.FunctionTolerance,options.StepTolerance];
  67. %holds best estimate
  68. x=x0;
  69. fmin=1e30;
  70. n=20;
  71. n1=20;
  72. imin=-1;
  73. for i=1:n
  74. %find best fit for nclass responses.
  75. %total number of points that are fitted is 20*nclass, total number of
  76. %parameters estimated is 4+3*nclass
  77. [x1,resnorm,residual,exitflag,output,lambda,jacobian] = lsqnonlin(fun,x0,ul,ub,options);
  78. %this gets tau and A
  79. %now fit every curve individually with the known input function
  80. for j=1:nclass
  81. cay=centers(j,:)';
  82. w=ones(size(cay));
  83. fun1=@(par)evaluateSingleDiff(cax,cay,w,pars,x1,par);
  84. qpar=zeros(1,3);
  85. qmin=1e30;
  86. for k=1:n1
  87. [par0,ul1,ub1]=generateInitialValues(valueGeneratorSettings1);
  88. [par1,resnorm,residual,exitflag,output,lambda,jacobian] = ...
  89. lsqnonlin(fun1,par0,ul1,ub1,options);
  90. fchi2=sqrt(residual*residual');
  91. if fchi2<qmin
  92. qmin=fchi2;
  93. qpar=par1;
  94. end
  95. end
  96. x1(nglob+(j-1)*3+1:nglob+j*3)=qpar;
  97. fprintf('Center: %d (%f %f %f)/(%f %f %f)\n',j,...
  98. par0(1),par0(2),par0(3),...
  99. par1(1),par1(2),par1(3));
  100. %residual
  101. end
  102. %writematrix(ci,fullfile(path,sprintf('CI%d.txt',i)),'Delimiter','tab');
  103. %output
  104. chi2=fun(x1);
  105. chi2=chi2'*chi2;
  106. if chi2<fmin
  107. fmin=chi2;
  108. x=x1;
  109. imin=i;
  110. %fci=ci;
  111. end
  112. %writematrix([chi2 x1],fullfile(path,sprintf('fitPar%d.txt',i)),'Delimiter','tab');
  113. fprintf('Done %d %f (%.2f,%.2f,%.2f)/(%.2f,%.2f,%.2f)\n',...
  114. i,chi2,x0(1),x0(2),x0(3),x1(1),x1(2),x1(3));
  115. [x0,ul,ub]=generateInitialValues(valueGeneratorSettings);
  116. end
  117. ofile=fullfile(path,sprintf('fitParFinal_%d_%d.txt',nclass,realizationId));
  118. try
  119. writematrix([fmin x],ofile,'Delimiter','tab');
  120. catch ME
  121. dlmwrite(ofile,[fmin x],'\t');
  122. end
  123. %ofile=fullfile(path,sprintf('CIFinal_%d_%d.txt',nclass,realizationId));
  124. %writematrix(fci,ofile,'Delimiter','tab');
  125. disp(sprintf('Found min at %d',imin));
  126. globalPar=[x(1:3) 0 cax(1:2)'];
  127. cPars=reshape(x(nglob+1:nglob+npar*nclass),[],nclass)';
  128. %reset k1 for those where BVF is nearly one
  129. %cPars(cPars(:,2)>0.4,1)=0;
  130. %reset k1 for those where k2 is abnormally large
  131. %cPars(cPars(:,3)>1,1)=0;
  132. plotFunctions(path,globalPar,cPars,cax,centers);
  133. dt=size(data);
  134. k1v=U'*cPars(:,1);
  135. xa(:,:,:,1)=reshape(k1v,dt(1:3));
  136. vbfv=U'*cPars(:,2);
  137. xa(:,:,:,2)=reshape(vbfv,dt(1:3));
  138. k2v=U'*cPars(:,3);
  139. xa(:,:,:,3)=reshape(k2v,dt(1:3));
  140. end