NCscurImCF_3parfor.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. %% CALLED BY NCfitImCFparforFailGbl2.m %%
  2. function [resMatStd, resMat, selTimesStd, selIntensStd, FiltTimesELr, NormIntensELr] =...
  3. NCscurImCF_3parfor(dataMatrix, AUCfinalTime, currSpotArea, sols, bl, minTime)
  4. % Preallocate
  5. resMatStd=zeros(1,27);
  6. resMat=zeros(1,27);
  7. % Set internal variables sent to matlab fit function
  8. me=200;
  9. meL=750;
  10. mi=25;
  11. miL=250;
  12. rmsStg1=0;
  13. rmsStg1I(1)=0;
  14. slps=1;
  15. filterTimes=[];
  16. normIntens=[];
  17. nn=1;
  18. numFitTpts=0;
  19. % Build filterTimes and normIntens from spot dataMatrix selection codes produced in filter section
  20. for n=1:size(dataMatrix,2)
  21. if (((dataMatrix(3,n)==1))||(dataMatrix(3,n)==3)||(dataMatrix(3,n)==2)...
  22. ||(dataMatrix(3,n)==0))
  23. filterTimes(nn)=dataMatrix(1,n);
  24. normIntens(nn)=dataMatrix(4,n);
  25. nn=nn+1;
  26. end
  27. end
  28. filterTimes=filterTimes';
  29. selTimesStd=filterTimes;
  30. normIntens=normIntens';
  31. selIntensStd=normIntens;
  32. lastTptUsed=1;
  33. lastIntensUsed=1;
  34. thresGT2TmStd=0;
  35. try
  36. lastTptUsed=max(filterTimes);
  37. lastIntensUsed=normIntens(length(normIntens));
  38. lastIntensUsedStd=lastIntensUsed;
  39. lastTptUsedStd=lastTptUsed;
  40. Tpt1Std=filterTimes(1);
  41. numFitTptsStd=nnz((normIntens(:)>=0)==1);
  42. thresGT2=find(((normIntens(:)>2)==1), 1);
  43. if isempty(thresGT2)
  44. thresGT2TmStd=0;
  45. else
  46. thresGT2TmStd=filterTimes(thresGT2);
  47. end
  48. numTptsGT2Std=nnz((normIntens(:)>=2)==1); % nnz(filterTimes(find(filterTimes>=thresGT2Tm)));
  49. K_Guess=max(normIntens);
  50. numTimePts=length(filterTimes);
  51. opts=fitoptions('Method','Nonlinear','Robust','On','DiffMinChange',1.0E-11,'DiffMaxChange',0.001,...
  52. 'MaxFunEvals',me, 'MaxIter', mi, 'TolFun', 1.0E-12, 'TolX', 1.0E-10, 'Lower', [K_Guess*0.5,0,0],...
  53. 'StartPoint', [K_Guess,filterTimes(floor(numTimePts/2)),0.30], 'Upper', [K_Guess*2.0,max(filterTimes),1.0],'Display','off');
  54. ftype=fittype('K / (1 + exp(-r* (t - l )))','independent','t','dependent',['K','r','l'],'options',opts);
  55. % Carry out the curve fitting process
  56. [fitObject, errObj]=fit(filterTimes,normIntens,ftype);
  57. coeffsArray=coeffvalues(fitObject);
  58. rmsStg1=errObj.rsquare;
  59. rmsStg1I(slps)=errObj.rsquare;
  60. sDat(slps,1)=errObj.rsquare;
  61. K=coeffsArray(1); sDat(slps,2)=coeffsArray(1); % Carrying Capacity
  62. l=coeffsArray(2); sDat(slps,3)=coeffsArray(2); % lag time
  63. r=coeffsArray(3); sDat(slps,4)=coeffsArray(3); % rateS
  64. % Integrate (from first to last time point)
  65. numVals=size(filterTimes);
  66. numVals=numVals(1);
  67. t_begin=0;
  68. t_end=AUCfinalTime;
  69. AUC=(K/r*log(1+exp(-r*(t_end-l)))-K/r*log(exp(-r*(t_end-l)))) - (K/r*log(1+exp(-r*(t_begin-l)))-K/r*log(exp(-r*(t_begin-l))));
  70. MSR=r;
  71. rsquare=errObj.rsquare;
  72. confObj=confint(fitObject,0.9); % get the 90% confidence
  73. NANcond=0; stdNANcond=0; % stdNANcond added to relay not to attempt ELr as there is no curve to find critical point
  74. confObj_filtered=confObj;
  75. Klow=confObj(1,1); sDat(slps,5)=confObj(1,1);
  76. Kup=confObj(2,1); sDat(slps,6)=confObj(2,1);
  77. llow=confObj(1,2); sDat(slps,7)=confObj(1,2);
  78. lup=confObj(2,2); sDat(slps,8)=confObj(2,2);
  79. rlow=confObj(1,3); sDat(slps,9)=confObj(1,3);
  80. rup=confObj(2,3); sDat(slps,10)=confObj(2,3);
  81. if(isnan(Klow)||isnan(Kup)||isnan(llow)||isnan(lup)||isnan(rlow)||isnan(rup))
  82. NANcond=1; stdNANcond=1; % stdNANcond added to relay not to attempt ELr as there is no curve to find critical point
  83. end
  84. catch
  85. % if no data is given, return zeros
  86. AUC=0;MSR=0;K=0;r=0;l=0;rsquare=0;Klow=0;Kup=0;
  87. rlow=0;rup=0;lup=0;llow=0;
  88. NANcond=1; stdNANcond=1; %stdNANcond added to relay not to attempt ELr as there is no curve to find critical point
  89. end
  90. if (exist('K','var')&& exist('r','var') && exist('l','var'))
  91. t=(0:1:200);
  92. Growth=K ./ (1 + exp(-r.* (t - l )));
  93. fitblStd=min(Growth);
  94. end
  95. cutTm(1:4)=1000; %-1 means cuts not used or NA
  96. % Preserve for ResultsStd
  97. resMatStd(1)=AUC;
  98. resMatStd(2)=MSR;
  99. resMatStd(3)=K;
  100. resMatStd(4)=r;
  101. resMatStd(5)=l;
  102. resMatStd(6)=rsquare;
  103. resMatStd(7)=Klow;
  104. resMatStd(8)=Kup;
  105. resMatStd(9)=rlow;
  106. resMatStd(10)=rup;
  107. resMatStd(11)=llow;
  108. resMatStd(12)=lup;
  109. resMatStd(13)=currSpotArea;
  110. resMatStd(14)=lastIntensUsedStd; % filtNormIntens(length(filtNormIntens));
  111. maxRateTime=0; %[]; %Std shows []; ELr shows 0; %parfor
  112. resMatStd(15)=0; %maxRateTimestdMeth;
  113. resMatStd(16)=lastTptUsedStd;
  114. if isempty(Tpt1Std)
  115. Tpt1Std=777;
  116. end
  117. resMatStd(17)=Tpt1Std;
  118. resMatStd(18)=bl; % perform in the filter section of NCfitImCFparfor
  119. resMatStd(19)=fitblStd; % taken from NCfil... and not affected by NCscur...changes
  120. resMatStd(20)=minTime; % not affected by changes made in NCscur...for refined 'r'
  121. resMatStd(21)=thresGT2TmStd;
  122. resMatStd(22)=numFitTptsStd;
  123. resMatStd(23)=numTptsGT2Std;
  124. resMatStd(24)=999; % yhe Standard method has no cuts .:.no cutTm
  125. resMatStd(25)=999;
  126. resMatStd(26)=999;
  127. resMatStd(27)=999;
  128. % ELr New Experimental data through L+deltaS Logistic fit for 'Improved r' Fitting
  129. FiltTimesELr=[]; %{ii}=filterTimes;
  130. NormIntensELr=[]; %{ii}=normIntens;
  131. normIntens=selIntensStd;
  132. filterTimes=selTimesStd;
  133. stdIntens=selIntensStd;
  134. tmpIntens=selIntensStd;
  135. stdTimes=selTimesStd;
  136. if stdNANcond==0
  137. % Determine critical points and offsets for selecting Core Data based on
  138. % Standard curve fit run. Put diff into NImStartupImCF02.m calling source
  139. % to reduce repeated execution since it doesn't change.
  140. % fd4=diff(sym('K / (1 + exp(-r* (t - l )))'),4);
  141. % sols=solve(fd4);
  142. tc1=eval(sols(2));
  143. tc2=eval(sols(3));
  144. LL=l; %eval(sols(1)); %exactly the same as 'l' from std. fit method-Save time
  145. rsTmStd=LL-tc1; %%riseTime (first critical point to L)
  146. deltS=rsTmStd/2;
  147. tc1Early=tc1-deltS; %AKA- tc1AdjTm %2*tc1 -LL
  148. L_Late=LL+deltS;
  149. tc1EdatPt=find(filterTimes>(tc1Early),1,'first');
  150. cutTm(1)=filterTimes(2);
  151. cutDatNum(1)=2;
  152. cutTm(2)=tc1Early;
  153. cutDatNum(2)=tc1EdatPt-1;
  154. L_LDatPt=find(filterTimes< L_Late,1,'last');
  155. tc2LdatPt=find(filterTimes< tc2+rsTmStd,1,'last');
  156. cutTm(3)=L_Late;
  157. cutDatNum(3)=L_LDatPt;
  158. % Select Core Data Set (Remove Early data before critical point)
  159. ints=[];
  160. ints(1:L_LDatPt-tc1EdatPt+2)=(tmpIntens(L_LDatPt));
  161. ints(2:end)=tmpIntens(tc1EdatPt:L_LDatPt);
  162. ints(1)=tmpIntens(1);
  163. tms=[];
  164. tms(1:L_LDatPt-tc1EdatPt+2)=(stdTimes(L_LDatPt));
  165. tms(2:end)=stdTimes(tc1EdatPt:L_LDatPt);
  166. tms(1)=stdTimes(1);
  167. % Include/Keep late data that define K
  168. if length(tmpIntens(tc2LdatPt:end))> 4
  169. KlastInts=tmpIntens(tc2LdatPt:end);
  170. KlastTms=stdTimes(tc2LdatPt:end);
  171. lengthKlast=length(tmpIntens(tc2LdatPt:end));
  172. ints(end:(end+ lengthKlast-1))=KlastInts;
  173. tms(end:(end+ lengthKlast-1 ))=KlastTms;
  174. cutTm(4)=tc2+rsTmStd;
  175. cutDatNum(4)=tc2LdatPt-1;
  176. else
  177. lengthKlast=length(tmpIntens(tc2LdatPt-1:end));
  178. if lengthKlast>1
  179. KlastInts=tmpIntens(end-(lengthKlast-1):end);
  180. KlastTms=stdTimes(end-(lengthKlast-1):end);
  181. ints(end:(end+ lengthKlast-1 ))=KlastInts;
  182. tms(end:(end+ lengthKlast-1 ))=KlastTms;
  183. end
  184. cutTm(4)=stdTimes(tc2LdatPt-1);
  185. cutDatNum(4)=tc2LdatPt-2; %length(stdTimes(end-(lengthKlast-1):end));
  186. end
  187. Ints=[];
  188. Tms=[];
  189. Ints=ints';
  190. Tms=tms';
  191. try
  192. filterTimes=Tms; filterTimes4=Tms;
  193. normIntens=Ints; normIntens4=Ints;
  194. % Classic symmetric logistic curve fit setup restated as COMMENTS for reference convenience
  195. % opts=fitoptions is the same as for Std and so is redundant
  196. % opts=fitoptions('Method','Nonlinear','Robust','On',...
  197. % 'DiffMinChange',1.0E-11,'DiffMaxChange',0.001,...
  198. % 'MaxFunEvals',me, 'MaxIter', mi, 'TolFun', 1.0E-12, 'TolX', 1.0E-10, 'Lower', [K_Guess*0.5,0,0], 'StartPoint', [K_Guess,filterTimes(floor(numTimePts/2)),0.30], 'Upper', [K_Guess*2.0,max(filterTimes),1.0]);
  199. ftype=fittype('K / (1 + exp(-r* (t - l )))','independent','t','dependent',['K','l','r'],'options',opts);
  200. fitObject=[]; errObj=[];
  201. % carry out the curve fitting process
  202. [fitObject, errObj]=fit(Tms,Ints,ftype);
  203. coeffsArray=coeffvalues(fitObject);
  204. r3=coeffsArray(3); % sDat(slps,4)=coeffsArray(3); % rateS
  205. if (exist('K','var')&& exist('r','var') && exist('l','var'))
  206. t=(0:1:200);
  207. GrowthELr=K ./ (1 + exp(-r.* (t - l )));
  208. fitblELr=min(GrowthELr); %jh diag
  209. end
  210. catch
  211. % if no data is given, return zeros
  212. AUC=0;MSR=0;K=0;r=0;l=0;rsquare=0;Klow=0;Kup=0;
  213. rlow=0;rup=0;lup=0;llow=0; %normIntens=[];
  214. end
  215. end
  216. % Update values if r is better(higher) with removal of early data
  217. try
  218. if r3>r && stdNANcond==0
  219. r=r3; sDat(slps,4)=sDat(slps,4); % rateS
  220. K=coeffsArray(1); sDat(slps,2)=coeffsArray(1); % Carrying Capacity
  221. l=coeffsArray(2); sDat(slps,3)=coeffsArray(2); % lag time
  222. coeffsArray=coeffvalues(fitObject);
  223. rmsStg1=errObj.rsquare;
  224. rmsStg1I(slps)=errObj.rsquare;
  225. sDat(slps,1)=errObj.rsquare;
  226. % JH diagnostics
  227. numFitTpts=nnz((normIntens(:)>=0)==1);
  228. thresGT2=find(((normIntens(:)>2)==1), 1);
  229. thresGT2Tm=filterTimes(thresGT2);
  230. numTptsGT2=nnz((normIntens(:)>=2)==1);
  231. numTimePts=length(filterTimes);
  232. AUC=(K/r*log(1+exp(-r*(t_end-l)))-K/r*log(exp(-r*(t_end-l)))) - (K/r*log(1+exp(-r*(t_begin-l)))-K/r*log(exp(-r*(t_begin-l))));
  233. MSR=r3;
  234. rsquare=errObj.rsquare;
  235. confObj=confint(fitObject,0.9); % get the 90% confidence
  236. NANcond=0;
  237. confObj_filtered=confObj;
  238. Klow=confObj(1,1); sDat(slps,5)=confObj(1,1);
  239. Kup=confObj(2,1); sDat(slps,6)=confObj(2,1);
  240. llow=confObj(1,2); sDat(slps,7)=confObj(1,2);
  241. lup=confObj(2,2); sDat(slps,8)=confObj(2,2);
  242. rlow=confObj(1,3); sDat(slps,9)=confObj(1,3);
  243. rup=confObj(2,3); sDat(slps,10)=confObj(2,3);
  244. if(isnan(Klow)||isnan(Kup)||isnan(llow)||isnan(lup)||isnan(rlow)||isnan(rup))
  245. NANcond=1;
  246. end
  247. filterTimes=Tms;
  248. normIntens=Ints;
  249. resMat(17)=.00002;
  250. resMat(18)=bl;
  251. resMat(19)=fitblELr;
  252. resMat(20)=minTime;
  253. else % r is better than r3 so use the Std data in the ELr result sheet
  254. filterTimes=selTimesStd;
  255. normIntens=selIntensStd;
  256. lastTptUsed=lastTptUsedStd; % Reinstall Std values for jh diags
  257. Tpt1=filterTimes(1);
  258. try
  259. if isempty(Tpt1)
  260. Tpt1=0.00002; %777;
  261. end
  262. catch
  263. Tpt1=0.00002; %777;
  264. end
  265. resMat(17)=Tpt1;
  266. numFitTpts=numFitTptsStd;
  267. numTptsGT2=numTptsGT2Std;
  268. thresGT2Tm=thresGT2TmStd;
  269. cutTm(1:4)=1000; % 1 means cuts not used or NA
  270. resMat(18)=bl; % only applicable to Std curve Fit; ELr superceeds and makes meaningless
  271. resMat(19)=fitblStd; % only applicable to Std curve Fit; ELr superceeds and makes meaningless
  272. resMat(20)=minTime; % only applicable to Std curve Fit; ELr superceeds and makes meaningless
  273. end % if r3>r1
  274. catch
  275. % if no data is given, return zeros
  276. AUC=0;MSR=0;K=0;r=0;l=0;rsquare=0;Klow=0;Kup=0;
  277. rlow=0;rup=0;lup=0;llow=0; % normIntens=[];
  278. end
  279. resMat(1)=AUC;
  280. resMat(2)=MSR;
  281. resMat(3)=K;
  282. resMat(4)=r;
  283. resMat(5)=l;
  284. resMat(6)=rsquare;
  285. resMat(7)=Klow;
  286. resMat(8)=Kup;
  287. resMat(9)=rlow;
  288. resMat(10)=rup;
  289. resMat(11)=llow;
  290. resMat(12)=lup;
  291. resMat(13)=currSpotArea;
  292. resMat(14)=lastIntensUsed; %filtNormIntens(length(filtNormIntens));
  293. % spline fit unneccessary and removed therfor no max spline rate time->set 0
  294. maxRateTime=0; % ELr will show 0; Std will show []
  295. resMat(15)=maxRateTime;
  296. resMat(16)=lastTptUsed; % filterTimes(length(filterTimes));
  297. try % if Std fit used no cuts .:. no cutTm
  298. resMat(24)=cutTm(1);
  299. resMat(25)=cutTm(2);
  300. resMat(26)=cutTm(3);
  301. resMat(27)=cutTm(4);
  302. catch
  303. resMat(24)=999; % if Std fit used no cuts .:. no cutTm
  304. resMat(25)=999;
  305. resMat(26)=999;
  306. resMat(27)=999;
  307. end
  308. FiltTimesELr=filterTimes;
  309. NormIntensELr=normIntens;
  310. lastTptUsed=max(filterTimes);
  311. lastIntensUsed=normIntens(length(normIntens));
  312. if (exist('K','var')&& exist('r','var') && exist('l','var'))
  313. t=(0:1:200);
  314. Growth=K ./ (1 + exp(-r.* (t - l )));
  315. fitbl=min(Growth); % jh diag
  316. end
  317. try % jh diag
  318. if isempty(thresGT2Tm)
  319. thresGT2Tm=0
  320. end
  321. catch
  322. thresGT2Tm=0;
  323. numTptsGT2=0;
  324. end
  325. resMat(21)=thresGT2Tm;
  326. resMat(22)=numFitTpts;
  327. resMat(23)=numTptsGT2;
  328. end