par4GblFnc8c.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. %% CALLED By p4loop8c.m %%
  2. function [p4L2, TmpexpScanIntens3,TmpFexpScanSpots3,TmpFexpScanBMtp3,TmpanlZoneRefs3,Tmpsbdg3]= ...
  3. par4GblFnc8c(parMat,tptLength,~,selScanNumLst,~,Fflg,PTmapPos,optCirMask,diaExt,doCircle,cirPixA,cirMask,~,...
  4. TmpexpScanIntens,TmpFexpScanSpots,TmpFexpScanBMtp,TmpanlZoneRefs,scCount,tifFileLstP4,~,ImParMat, ...
  5. numRows,numCols,scLst,easyResultsDir,projectScansDir,~, Tmpsbdg)
  6. global printResultsDir
  7. selScan=selScanNumLst(scCount);
  8. tptLength0=length(tifFileLstP4{scCount});
  9. % tptLength=numFiles;
  10. tifFileLst={tifFileLstP4(scCount)};
  11. % Extract the Imaging time stamps from selected tif files
  12. % clear('e','f'); %can't use clear inside a parfor loop. Preallocation
  13. % can be larger than useable .bmp files! Therefore must be small and
  14. % increased during for loop to maintain cell size integrity between e & f
  15. e=cell(1,2); f=cell(1,2); %(tptLength,2); f=cell(tptLength,2);
  16. nndx=0;
  17. for tPt=1:tptLength0 %size(tifFileLst,1) %length(tifFileLst)
  18. tifFileLstP4{scCount}(tPt)
  19. scLst;
  20. scLst(scCount)
  21. char(scLst(scCount))
  22. char(fullfile(projectScansDir,char(scLst(scCount))))
  23. projectScansDir;
  24. swCatch=0;
  25. nndx=nndx+1;
  26. tifFile=char(fullfile(projectScansDir,char(scLst(scCount)), tifFileLstP4{scCount}(tPt)));
  27. try
  28. info=imfinfo(tifFile); % D:\jwrDevel\DevelCurveFittingJWR\ImageScans\Scan2\020hr002.tif
  29. catch ME
  30. % Note: During parallel operation Matlab will not open file (fid)
  31. % etc. Therefore error message text will not be written.The only way
  32. % to get it out would be to pass variable through called function to
  33. % the ..Main8c script outside the parfor loop and print to file from
  34. % there. Consequently for now it only prints error to file when one
  35. % edits p4loop8c.m from 'parfor' to ordinary 'for' loop
  36. fFail=((fullfile(printResultsDir,'ImageFileReadFailure.txt')));
  37. fid=fopen(fFail,'a'); %create,open and append
  38. % fprintf(fid,'%s \n',char(tifFile))
  39. fclose(fid);
  40. nndx=nndx-1;
  41. swCatch=1;
  42. rep=getReport(ME, 'basic');
  43. rep=strcat('Read info failure for-',tifFile,' -', rep);
  44. % fprintf(fid,'%s \n',rep) %See Note:
  45. end
  46. tptLength=nndx;
  47. scTmNumeric=1; %initialize for parfor
  48. if swCatch==0
  49. scTmNumeric(nndx)=datenum(info.FileModDate);
  50. e(nndx,:)={tifFile, scTmNumeric(nndx)};
  51. % newtifFileLst(nndx)=tifFileLst(tPt);
  52. end
  53. end
  54. % Clear tifFileLst;
  55. tifFileLst=cell(nndx,1);
  56. f={sortrows(e,2)};
  57. tifFileLst=f{1,1}(:,1);
  58. areaOfIntensAboveBG=zeros(24,16,length(tifFileLst));
  59. % Calculate Time series for each Plate of Selected Scan
  60. lastPlateOnScan=1; %Ncode
  61. tSeriesv=[]; t0Seriesv=[];
  62. if tptLength>0 % added to jump over and fill data for invalid Sscan(plate runs
  63. scTmNumv=cell2mat(f{1,1}(:,2)); % (:,2))120613 fix for variant length data
  64. prtTmNumv=min(scTmNumv)-.000001;
  65. tSeriesv=((scTmNumv-prtTmNumv)*24);
  66. t0Seriesv=((scTmNumv-scTmNumv(1))*24);
  67. end
  68. % Added to jump over and fill data for invalid Sscan(plate runs
  69. if tptLength>=3
  70. % Create blank scan
  71. ImHeigth=2075;
  72. ImWidth=1400;
  73. Empsc=zeros(ImHeigth,ImWidth); %Ncode
  74. % Start scan loop
  75. lastPlateOnScan=1; %Ncode
  76. disp('Before call to NIscanIntens.....')
  77. % Execute Image conversion into Intensity Data
  78. [Tmpsbdg2, scanIntens, ~, ~, ~, TmpexpScanIntens2, TmpFexpScanSpots2, TmpFexpScanBMtp2, TmpanlZoneRefs2,areaOfIntensAboveBG]= ...
  79. NIscanIntensBGpar4GblFnc(Fflg,tifFileLst, ImParMat, PTmapPos,optCirMask,diaExt,doCircle,cirPixA,numRows,numCols,ImHeigth,ImWidth,cirMask, ...
  80. tptLength,selScan,Empsc,TmpexpScanIntens,TmpFexpScanSpots,TmpFexpScanBMtp,TmpanlZoneRefs,easyResultsDir, Tmpsbdg);
  81. TmpexpScanIntens3=TmpexpScanIntens2;
  82. TmpFexpScanSpots3=TmpFexpScanSpots2;
  83. TmpFexpScanBMtp3=TmpFexpScanBMtp2;
  84. TmpanlZoneRefs3=TmpanlZoneRefs2;
  85. Tmpsbdg3=Tmpsbdg2;
  86. % clear plate
  87. CFscanIntens=zeros(384,1); % zeros(16,24);
  88. plate=[];
  89. plate=zeros(24,16,tptLength);
  90. plate(:,:,:,1)=scanIntens(1:1:24,16:-1:1,:); % TmpexpScanIntens2(1:1:24,16:-1:1,:);%Ncode Dev3Vertical Similar to below
  91. % NIcheck: Check for bad scans at time points (swapped plates etc.)
  92. % Set up cell arrays for storing each plate in each Scan ..(scan,plate)
  93. SWprint=0;
  94. % Construct Legacy ...Intens.txt file
  95. if SWprint==1
  96. filename=fullfile(easyResultsDir,'PrintResults',(strcat('Plate',num2str(selScan),'_Intens.txt')));
  97. fid=fopen(filename,'w');
  98. end
  99. if length(size(plate))==2 % only two dims if only one image
  100. numOfImages=1;
  101. else
  102. numOfImages=size(plate,3);
  103. end
  104. CFscanIntens=zeros(384,numOfImages);
  105. locIndx=0; pl=1;
  106. for n=1:numCols % Ncode changed to 16 for Vert
  107. for m=1:numRows % Ncode change to 24 for Vert
  108. locIndx=locIndx+1;
  109. for k=1:numOfImages
  110. if SWprint==1
  111. if k==1 && numOfImages==1, fprintf(fid,'%.2f\n',plate(m,n,k,pl));end
  112. if k==1 && numOfImages~=1, fprintf(fid,'%.2f',plate(m,n,k,pl));end
  113. if k>1 && k<numOfImages, fprintf(fid,'\t%.2f',plate(m,n,k,pl));end %
  114. if k~=1 && k==numOfImages,fprintf(fid,'\t%.2f\n',plate(m,n,k,pl));end
  115. end
  116. CFscanIntens(locIndx,k)=plate(m,n,k,pl);
  117. end
  118. end
  119. end
  120. if SWprint==1
  121. fclose(fid);
  122. end
  123. % printGrwAreas
  124. % Area as the max number of pixels with intensities greater than the Background
  125. % grArea(row,col,tPt,Plate) Note that images r and c are 90 degrees shifted
  126. % and then mirror imaged. Thus data must be inverted and rotated to get
  127. % for the rows a-p and col 1-24 (and the EMS expected 1-384) orientation
  128. % clear grArea
  129. grArea=ones(24,16,tptLength);
  130. grArea(:,:,:,1)=areaOfIntensAboveBG(1:1:24,16:-1:1,:); %for Ncode dev3vert
  131. % for p=1:lastPlateOnScan
  132. if SWprint==1
  133. if SWgrowthArea==1
  134. filename=fullfile(printResultsDir,(strcat('Plate',num2str(selScan),'_Areas.txt')));
  135. else
  136. filename=fullfile(printResultsDir,(strcat('ALTgrowthA_Scan',num2str(selScan),'_Plate',num2str(1),'_Areas.txt')));
  137. filename=fullfile(printResultsDir,(strcat('ALTgrowthA_Plate',num2str(selScan),'_Plate','_Areas.txt')));
  138. end
  139. fid=fopen(filename,'w');
  140. end
  141. locIndx=0;
  142. Ag=zeros(1,384);
  143. for m=1:16 % Ncode change from 16 %Vert back to 16
  144. for n=1:24 % Ncode change from 24 %Vert back to 24
  145. growthArea=max(grArea(n,m,:,1));
  146. if SWprint==1
  147. fprintf(fid,'%3.f\n',growthArea);
  148. end
  149. locIndx=locIndx+1;
  150. Ag(locIndx)=growthArea;
  151. end
  152. end
  153. if SWprint==1
  154. fclose(fid);
  155. % fclose('all'); % TODO do we need this?
  156. end
  157. % AutoImCF curveFit kickoff setup
  158. autoImCFlag=1;
  159. if autoImCFlag==1, SWgrowthArea=1;end
  160. times=tSeriesv;
  161. scanAreas=Ag; % growthArea;
  162. offsets=0;
  163. suffix=strcat('Scan', num2str(selScan),'_Plate', num2str(1)); % char(QspLst(n));
  164. AUCfinalTime=96;
  165. arrayFormat=384;
  166. % Version compatability fixes
  167. %{
  168. v11a='7.12.0.635 (R2011a)';
  169. v14a='8.3.0.532 (R2014a)';
  170. %v14b='8.4.0.150421 (R2014b)';
  171. v18a='9.4.0.813654 (R2018a)';
  172. v18b='9.5.0.944444 (R2018b)';
  173. % Fourth order derivative of logistic equation
  174. if isequal(v18a,version)||isequal(v18b,version)
  175. syms t K r l;
  176. fd4=diff(K / (1 + exp(-r* (t - l ))),t,4);
  177. else
  178. fd4=diff(sym('K / (1 + exp(-r* (t - l )))'),4);
  179. end
  180. sols=solve(fd4);
  181. if isequal(v11a,version) || isequal(v14a,version)
  182. else
  183. tmpswap=sols(1);
  184. sols(1)=sols(3);
  185. sols(3)=tmpswap;
  186. end
  187. %}
  188. if verLessThan('matlab','8.3') % original work 23_0227 updated 23_0525 (8.4 changed to 8.3)
  189. fd4=diff(sym('K / (1 + exp(-r* (t - l )))'),4);
  190. sols=solve(fd4);
  191. else % accomodate new matlab changes after 2014a fix 2nd update 23_0227
  192. syms t K r l;
  193. fd4=diff(K / (1 + exp(-r* (t - l ))),t,4);
  194. sols=solve(fd4);
  195. tmpswap=sols(1);
  196. sols(1)=sols(3);
  197. sols(3)=tmpswap;
  198. end
  199. % CFscanIntens
  200. % Call CurveFit routine
  201. % NCfitImCF(times, CFscanIntens, offsets, suffix, AUCfinalTime, arrayFormat, scanAreas, printResultsDir, autoImCFlag, selScan,sols) %,scan)%, scPltList) outputDirectory;
  202. % [scanTmp2]=NCfitImCFparforFailGbl(times, CFscanIntens, offsets, suffix, AUCfinalTime, arrayFormat, scanAreas, printResultsDir, autoImCFlag, selScan, sols, scanTmp); %,scan)%, scPltList) outputDirectory;
  203. [par4scanselIntensStd,par4scanselTimesStd,par4scanTimesELr,par4scanIntensELr,par4scanCFparameters,par4scanCFdate,outC2,outCstd2]= ...
  204. NCfitImCFparforFailGbl2(parMat,times, CFscanIntens, offsets, suffix, AUCfinalTime, arrayFormat, scanAreas, printResultsDir, autoImCFlag, selScan, sols); %,scan)%, scPltList) outputDirectory;
  205. else %fill with default values when an invalid plate scan occurs
  206. CFscanIntens=zeros(16,24);
  207. Ag=zeros(1,384);
  208. % Preallocation for parfor loop
  209. times=tSeriesv;
  210. st(1,1:size(times,2))=1111;
  211. resMat(1,1:27)=0;
  212. resMatStd=resMat;
  213. outC2=zeros(384,27);
  214. outCstd2=zeros(384,27);
  215. for m=1:384
  216. pa{m}=st;
  217. par4scanCFparameters{m}=parMat;
  218. par4scanCFdate{m}=datestr((now),31);
  219. end
  220. par4scanselTimesStd=pa;
  221. par4scanselIntensStd=pa;
  222. par4scanTimesELr=pa;
  223. par4scanIntensELr=pa;
  224. par4resMat=zeros(384,27);
  225. par4resMatStd=zeros(384,27);
  226. %{
  227. TmpexpScanIntens00=cell(1); %cell(1,scanMax);
  228. TmpFexpScanSpots00=cell(1); %cell(1,scanMax);
  229. TmpFexpScanBMtp00=cell(1); %cell(1,scanMax);
  230. TmpanlZoneRefs00=cell(1); %cell(1,scanMax);
  231. Tmpsbdg00=cell(1);
  232. %}
  233. TmpexpScanIntens3=cell(1); %TmpexpScanIntens2;
  234. TmpFexpScanSpots3=cell(1); %TmpFexpScanSpots2;
  235. TmpFexpScanBMtp3=cell(1); %TmpFexpScanBMtp2;
  236. TmpanlZoneRefs3=cell(1); %TmpanlZoneRefs2;
  237. Tmpsbdg3=cell(1); %Tmpsbdg2;
  238. end %if tptLenth>=3 line19 20_0928
  239. tSeriesv; %debuggin parfor
  240. p4L1{1}=tSeriesv;
  241. p4L1{2}=t0Seriesv;
  242. p4L1{3}=datestr(prtTmNumv,31);
  243. p4L1{4}=CFscanIntens;
  244. locIndx=0;
  245. for n=1:numCols % Ncode changed to 16 for Vert
  246. for m=1:numRows % Ncode change to 24 for Vert
  247. locIndx=locIndx+1;
  248. rc=[n,m];
  249. p4rcTmp(locIndx)={rc};
  250. p4pIndxTmp(locIndx)=locIndx;
  251. end
  252. end
  253. p4L1{5}=p4rcTmp;
  254. p4L1{6}=p4pIndxTmp;
  255. p4L1{7}=Ag;
  256. p4L1{8}=par4scanselIntensStd;
  257. p4L1{9}=par4scanselTimesStd;
  258. p4L1{10}=par4scanTimesELr;
  259. p4L1{11}=par4scanIntensELr;
  260. p4L1{12}=par4scanCFparameters;
  261. p4L1{13}=par4scanCFdate;
  262. p4L1{14}=outC2;
  263. p4L1{15}=outCstd2;
  264. p4L1{16}=selScan;
  265. p4L1{17}=cirPixA;
  266. p4L1{18}=datestr((now),31); % TODO this seems bad
  267. p4L2=p4L1;
  268. end