NCfitImCFparforFailGbl2.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. %% CALLED BY par4GblFnc8c.m %%
  2. function [par4scanselIntensStd,par4scanselTimesStd,par4scanTimesELr,par4scanIntensELr,par4scanCFparameters,par4scanCFdate,outC,outCstd]= ...
  3. NCfitImCFparforFailGbl2(parMat,times, values, timeOffsets, fileSuffix, AUCfinalTime, ~, spotAreas, printResultsDir, ~,~, sols, ~) %,scan)
  4. % Preallocation for parfor loop
  5. st(1,1:size(times,2))=1111;
  6. resMat(1,1:27)=0;
  7. resMatStd=resMat;
  8. outC=zeros(384,27);
  9. outCstd=zeros(384,27);
  10. for m=1:384
  11. pa{m}=st;
  12. par4scanCFparameters{m}=parMat;
  13. par4scanCFdate{m}=datestr((now),31);
  14. end
  15. par4scanselTimesStd=pa;
  16. par4scanselIntensStd=pa;
  17. par4scanTimesELr=pa;
  18. par4scanIntensELr=pa;
  19. par4resMat=zeros(384,27);
  20. par4resMatStd=zeros(384,27);
  21. % Spot (cultures) loop
  22. for ii=1:384 % startSpot:numCultures
  23. ii; % db print out the culture number
  24. timepts=[];
  25. currValues=[];
  26. currSpotAreas=[];
  27. currSpotArea=[];
  28. dataMatrix=[];
  29. selTimesStd=[]; % 191024 parfor
  30. selIntensStd=[]; % 191024 parfor
  31. FiltTimesELr=[]; % 191024 parfor
  32. NormIntensELr=[]; % 191024 parfor
  33. % add offset...1 offset PER PLATE
  34. timepts=times + timeOffsets; % (floor((ii-1)/arrayFormat) + 1);
  35. currValues=values(ii,:); % change values(spotNum,:);
  36. % get spot areas for this culture
  37. currSpotArea=spotAreas(:,ii);
  38. % just use the area at the last time point
  39. % currSpotArea=currSpotAreas(1);
  40. % Preallocate to accomodate parfor loop
  41. resMatStd=zeros(1,27);
  42. resMat=zeros(1,27);
  43. currNormIntens=currValues/currSpotArea;
  44. tmpx=find(currNormIntens>5); %15jh % 2.3);
  45. validSpot=true;
  46. if(isempty(tmpx) || length(tmpx)<3)
  47. validSpot=false;
  48. normIntens=currNormIntens;
  49. filterTimes=timepts; %filterTimes; %currTimes;
  50. selTimesStd=timepts;
  51. selIntensStd=currNormIntens;
  52. FiltTimesELr=timepts;
  53. NormIntensELr=currNormIntens;
  54. else
  55. % NCfilImCF.m
  56. % Preallocate incase something bails in NCscurImCFparfor
  57. resMatStd=zeros(1,27);
  58. resMat=zeros(1,27);
  59. hold off;
  60. dataMatrix=[];
  61. K=0;r=0;l=0;Klow=0;Kup=0;rlow=0;rup=0;llow=0;lup=0;AUC=0;MSR=0;rsquare=0;
  62. bl=0;
  63. Tpt1=0;numFitTpts=0;thresGT2=0;minTime=0;fitbl=0; % diagnostic outputs only
  64. timepts=timepts; % timepts=currTimes; parfor
  65. normIntens=currNormIntens;
  66. dataMatrix=[]; % arfor move clear from NCfitImCF...m
  67. loIntensThres=parMat(4);
  68. stdLoIntLim=parMat(5);
  69. % Basic filtering
  70. % [loInten Thres,stdbased Trim before Scurve start, and dropout detection]
  71. if(max(normIntens) > 2.29)
  72. threshold=loIntensThres; %1.9; %Increase this value to reduce low data point (flag=2)
  73. else
  74. threshold=0;
  75. end
  76. dropThreshold=-0.0001*max(normIntens);
  77. % Initialize dataMatrix
  78. dataMatrix(1,:)=timepts;
  79. dataMatrix(2,:)=normIntens;
  80. dataMatrix(3,:)=ones;
  81. dataMatrix(4,:)=normIntens;
  82. % Determine a mean Intensity Index point and assoc'd TimePt
  83. a=min(normIntens(normIntens>=0)); %(find(normIntens>=0)));
  84. b=max(normIntens(normIntens>=0)); %(find(normIntens>=0)));
  85. c=0.5*(b-a);
  86. d=b-c;
  87. meanIntIndPt=find(normIntens>d,1);
  88. meanInt=normIntens(meanIntIndPt);
  89. meanTime=times(meanIntIndPt);
  90. % NCLoIntstdTrim
  91. % NCLoSstdTrim.m called by NCfilImCF and NCfil.m
  92. flg1=0;
  93. loScurvLim=stdLoIntLim;
  94. loStimeN=1;
  95. last2n=1;
  96. stdDev=[];
  97. nrmIntens0=normIntens;
  98. for n=1:meanIntIndPt
  99. if nrmIntens0(n)<=0
  100. nrmIntens0(n)=0;
  101. end
  102. if(nrmIntens0(n)<threshold)
  103. if (loStimeN-2)>0
  104. dataMatrix(3,1:(n-2))=2; % add to lowIntensity cull flags, the pre S cull data
  105. else
  106. dataMatrix(3,1:n)=2;
  107. end
  108. dataMatrix(3,1:(n-2))=2;
  109. last2n=n;
  110. end
  111. if n<(length(nrmIntens0)-3)
  112. x=nrmIntens0(n:(n+3));
  113. stdDev(n)=std(x);
  114. if (stdDev(n)<loScurvLim && flg1~=1)
  115. loStime=timepts(n);
  116. loStimeN=n;
  117. end
  118. if stdDev(n)>6
  119. flg1=1;
  120. end
  121. end
  122. end
  123. % TODO repetitive code
  124. if (loStimeN-2)>0
  125. dataMatrix(3,1:(loStimeN-2))=2; % add to lowIntensity cull flags, the pre S cull data
  126. else
  127. dataMatrix(3,1:(loStimeN-2))=2
  128. end
  129. qcutoff=2;
  130. qind=find(normIntens>2); %,:,'first');
  131. if ~isempty(qind(3))
  132. qcutoff=qind(3);
  133. end
  134. [minInt,I]=min(normIntens(2:qcutoff));
  135. bl=minInt;
  136. minTime=dataMatrix(1,I);
  137. if (length(qind)>5)&&I>1
  138. dataMatrix(3,1:(I-1))=5;
  139. end
  140. tGT2Flg=0;
  141. for n=1:length(normIntens)
  142. dataMatrix(4,n)=normIntens(n)-bl;
  143. if n>I && dataMatrix(4,n)>=2 && tGT2Flg==0
  144. thresGT2=n;thresGT2Tm=dataMatrix(1,n);tGT2Flg=1;
  145. end
  146. end
  147. resMat(18)=bl;
  148. resMatStd(18)=bl;
  149. resMatStd(20)=minTime;
  150. resMat(20)=minTime;
  151. % DropOut cull section (single drop points)
  152. DropOutStartPt=length(normIntens);
  153. for n=1:length(normIntens)
  154. if(n>1)
  155. if(((normIntens(n)- normIntens(n-1))< dropThreshold)) && ...
  156. (n > max(meanIntIndPt,thresGT2) )
  157. dataMatrix(3,n)=6;
  158. end
  159. end
  160. end
  161. % TODO should/could this be removed as recreated in%NCscurImCF_3parfor.m
  162. % Post Stdtest cull for low intensities inclusion of additional low value points
  163. % selTimes=[--,--] %don't know size before as it is a filtered output
  164. tmpIndx=0;
  165. for n=1:length(normIntens)
  166. if (dataMatrix(3,n)==1)
  167. tmpIndx=tmpIndx+1;
  168. selTimes(tmpIndx)=dataMatrix(1,n); % selTimes(nn)=dataMatrix(1,n);
  169. selIntens(tmpIndx)=dataMatrix(4,n); %s elIntens(nn)=dataMatrix(4,n);
  170. end
  171. end
  172. selTimes=selTimes';
  173. selIntens=selIntens';
  174. filtNormIntens=normIntens;
  175. dataMatrix0=dataMatrix;
  176. filterTimes=timepts; % parfor
  177. %NCscurImCF
  178. %NCscurImCF_1
  179. %NCscurImCF_2
  180. %NCscurImCF_3
  181. %NCscurImCF_3parfor
  182. %NCscurImCF_3parfor(dataMatrix, AUCfinalTime)
  183. %dataMatrix %debugging parfor gbl ok 85.7145; 126.4579,6, 124.5264 37tPt
  184. %adsfj %debugging parfor gbl
  185. [resMatStd, resMat, selTimesStd, selIntensStd, FiltTimesELr, NormIntensELr] =...
  186. NCscurImCF_3parfor(dataMatrix0, AUCfinalTime, currSpotArea, sols, bl, minTime);
  187. end
  188. par4scanselTimesStd{ii}=selTimesStd %timepts'; %filterTimes';
  189. par4scanselIntensStd{ii}=selIntensStd; %normIntens';
  190. par4scanTimesELr{ii}=FiltTimesELr; % 19_1021 preserve for CurveDisplay and EZview
  191. par4scanIntensELr{ii}=NormIntensELr; % 19_1021 preserve for CurveDisplay and EZview
  192. outC(ii,:)=resMat; %{ii, par4resMat};
  193. outCstd(ii,:)=resMatStd; %{ii, par4resMatStd};
  194. end
  195. % To accomodate parfor
  196. % Copy par4scan thru global p4 functions inside of parfor loop --then outside to par4Gbl_Main8b.m
  197. fileExt='.txt';
  198. filePrefix='FitResultsComplete_';
  199. fileNamePlate=[filePrefix fileSuffix fileExt];
  200. fileName=fullfile(printResultsDir, fileNamePlate); %[printResultsDir fileNamePlate];
  201. fid=fopen(fileName,'w');
  202. fprintf(fid, 'Fit Results Complete\n');
  203. %fprintf(fid, 'Num.\tAUC\tMSR\tK\tr\tl\tR-squared\tK-lower\tK-upper\tr-lower\tr-upper\tl-upper\tl-lower\tArea\tLastInten\tSpineMaxRateTimePt\tLastFitTimePt\n');
  204. fclose(fid);
  205. end