loadOptResultsOrig.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. function optResults = loadOptResults(varargin)
  2. % Loads the weights and dose distribution corresponding to a given input
  3. % filename. The input filename has the following format:
  4. %
  5. % optResults = loadOptResults(inputFileName) loads all of the files from
  6. % the linlsq optimization associated with inputFileName. DVHs are
  7. % calculated for all of the associated dose files.
  8. % optResults = loadOptResults(inputFileName,'last') loads only the last
  9. % dose and beamlet weights files, and calculates the DVHs for each
  10. % tissue type only for those files.
  11. %
  12. % inputFileName has the following format:
  13. % Niterations
  14. % 2000
  15. % Nperbatch
  16. % 50
  17. % prescription_filename
  18. % input0/prescription.txt
  19. % initial_beam_weights_filename
  20. % input0/init_beam_weights.img
  21. % beamlet_header_file
  22. % beamletbatches0/beamlet_header.txt
  23. % dose_batch_base_name
  24. % output0/dosebatch
  25. % dose_batch_extension
  26. % img
  27. % weight_batch_base_name
  28. % output0/weightbatch
  29. % weight_batch_extension
  30. % img
  31. % obj_func_name
  32. % output0/objFunc.img
  33. %
  34. % Where all paths are relative to the directory containing the
  35. % inputFileName argument.
  36. %
  37. % RTF 1/6/07
  38. if length(varargin) == 1
  39. inputFileName = varargin{1};
  40. readAllOutputFiles = 1;
  41. elseif length(varargin) == 2
  42. inputFileName = varargin{1};
  43. if strcmp(deblank(lower(varargin{2})),'last')
  44. readAllOutputFiles = 0;
  45. else
  46. readAllOutputFiles = 1;
  47. end
  48. else
  49. error('Too many input arguments');
  50. end
  51. Ndvhbins = 1000; % number of bins to use for the cumulative DVH calculation
  52. % Open the optimization input file
  53. fid = fopen(inputFileName,'r');
  54. % read the file into a cell array, line-by-line
  55. fileText = {};
  56. while 1
  57. tline = fgetl(fid);
  58. if ~ischar(tline)
  59. break;
  60. end
  61. fileText{end+1} = tline;
  62. end
  63. fclose(fid);
  64. % Extract the folder name that contains the input file
  65. inputFileNameRev = fliplr(inputFileName); % flip the input filename around
  66. % pop off the reversed file name
  67. [fileNameRev,inputFolderRev] = strtok(inputFileNameRev,{'/','\'});
  68. inputFolder = fliplr(inputFolderRev);
  69. optResults = []; % optimization results structure
  70. % search for key fields
  71. for k=1:length(fileText)
  72. if strcmp(deblank(fileText{k}),'Niterations')
  73. optResults.optParam.Niterations = str2num(fileText{k+1});
  74. end
  75. if strcmp(deblank(fileText{k}),'Nperbatch')
  76. optResults.optParam.Nperbatch = str2num(fileText{k+1});
  77. end
  78. if strcmp(deblank(fileText{k}),'prescription_filename')
  79. optResults.optParam.prescFileName = fileText{k+1};
  80. end
  81. if strcmp(deblank(fileText{k}),'initial_beam_weights_filename')
  82. optResults.optParam.initialBeamWeightsFileName = fileText{k+1};
  83. end
  84. if strcmp(deblank(fileText{k}),'beamlet_header_file')
  85. optResults.optParam.beamletHeaderFileName = fileText{k+1};
  86. end
  87. if strcmp(deblank(fileText{k}),'dose_batch_base_name')
  88. optResults.optParam.doseBatchBaseName = fileText{k+1};
  89. end
  90. if strcmp(deblank(fileText{k}),'dose_batch_extension')
  91. optResults.optParam.doseBatchExt = fileText{k+1};
  92. end
  93. if strcmp(deblank(fileText{k}),'weight_batch_base_name')
  94. optResults.optParam.weightBatchBaseName = fileText{k+1};
  95. end
  96. if strcmp(deblank(fileText{k}),'weight_batch_extension')
  97. optResults.optParam.weightBatchExt = fileText{k+1};
  98. end
  99. if strcmp(deblank(fileText{k}),'obj_func_name')
  100. optResults.optParam.objFuncName = fileText{k+1};
  101. end
  102. end
  103. % open the prescription file
  104. fid = fopen([inputFolder '/' optResults.optParam.prescFileName],'r');
  105. % read the entire file into a cell array
  106. fileText = {};
  107. while 1
  108. tline = fgetl(fid);
  109. if ~ischar(tline)
  110. break;
  111. end
  112. fileText{end+1} = tline;
  113. end
  114. fclose(fid);
  115. if ~strcmp(deblank(fileText{1}),'Ntissue')
  116. error('First line of prescription file must be ''Ntissue''');
  117. else
  118. Ntissue = str2num(fileText{2});
  119. end
  120. k = 5; % skip up to the 5th line of the file, which is the tissue number
  121. % read in the tissues
  122. for m=1:Ntissue % read through the lines for the current tissue
  123. tissNum = str2num(fileText{k});
  124. k = k + 2;
  125. optResults.presc.tissue(m).name = fileText{k};
  126. k = k + 2;
  127. optResults.presc.tissue(m).alpha = str2num(fileText{k});
  128. k = k + 2;
  129. optResults.presc.tissue(m).betaVPlus = str2num(fileText{k});
  130. k = k + 2;
  131. optResults.presc.tissue(m).dVPlus = str2num(fileText{k});
  132. k = k + 2;
  133. optResults.presc.tissue(m).vPlus = str2num(fileText{k});
  134. k = k + 2;
  135. optResults.presc.tissue(m).betaVMinus = str2num(fileText{k});
  136. k = k + 2;
  137. optResults.presc.tissue(m).dVMinus = str2num(fileText{k});
  138. k = k + 2;
  139. optResults.presc.tissue(m).vMinus = str2num(fileText{k});
  140. k = k + 2;
  141. optResults.presc.tissue(m).betaPlus = str2num(fileText{k});
  142. k = k + 2;
  143. optResults.presc.tissue(m).dosePlusFileName = fileText{k};
  144. k = k + 2;
  145. optResults.presc.tissue(m).betaMinus = str2num(fileText{k});
  146. k = k + 2;
  147. optResults.presc.tissue(m).doseMinusFileName = fileText{k};
  148. % load-in the dPlus inforomation
  149. fid = fopen([inputFolder '/' optResults.presc.tissue(m).dosePlusFileName],'rb');
  150. siz = fread(fid,3,'int'); % size of the sparse array
  151. Nind = fread(fid,1,'int');
  152. ind = fread(fid,Nind,'int=>int');
  153. dat = fread(fid,Nind,'float=>float');
  154. fclose(fid);
  155. optResults.presc.tissue(m).ind = ind;
  156. optResults.presc.tissue(m).dPlus = dat;
  157. % load-in the dMinus inforomation
  158. fid = fopen([inputFolder '/' optResults.presc.tissue(m).doseMinusFileName],'rb');
  159. siz = fread(fid,3,'int')'; % size of the sparse array
  160. Nind = fread(fid,1,'int');
  161. ind = fread(fid,Nind,'int=>int');
  162. dat = fread(fid,Nind,'float=>float');
  163. fclose(fid);
  164. optResults.presc.tissue(m).ind = ind;
  165. optResults.presc.tissue(m).dMinus = dat;
  166. k = k + 3; % skip up to the next tissueNum value field
  167. end
  168. optResults.presc.siz = siz; % size of all of the dose grids
  169. % find which dose and weights files are present
  170. Nbatches = ceil(optResults.optParam.Niterations/optResults.optParam.Nperbatch)+1; % extra '1' is for initial guess
  171. doseFileNames = cell(1,Nbatches);
  172. doseFileExist = zeros(1,Nbatches); % flags to test for existence of files
  173. weightFileNames = cell(1,Nbatches);
  174. weightFileExist = zeros(1,Nbatches);
  175. for k=1:Nbatches
  176. doseFileNames{k} = [inputFolder optResults.optParam.doseBatchBaseName ...
  177. num2str((k-1)*optResults.optParam.Nperbatch) '.' ...
  178. optResults.optParam.doseBatchExt];
  179. weightFileNames{k} = [inputFolder optResults.optParam.weightBatchBaseName ...
  180. num2str((k-1)*optResults.optParam.Nperbatch) '.' ...
  181. optResults.optParam.weightBatchExt];
  182. fid = fopen(doseFileNames{k},'rb');
  183. if fid == -1 % batch file doesn't exist
  184. doseFileExist{k} = 0;
  185. else
  186. fclose(fid);
  187. doseFileExist(k) = 1; % mark that file exists
  188. end
  189. fid = fopen(weightFileNames{k},'rb');
  190. if fid == -1 % batch file doesn't exist
  191. weightFileExist{k} = 0;
  192. else
  193. fclose(fid);
  194. weightFileExist(k) = 1; % mark that file exists
  195. end
  196. end
  197. if readAllOutputFiles == 1
  198. optResults.dose = {};
  199. optResults.weights = {};
  200. % read in the appropriate dose files
  201. for k=1:length(doseFileNames)
  202. if doseFileExist(k) == 1
  203. fid = fopen(doseFileNames{k},'rb');
  204. dose = fread(fid,'float');
  205. fclose(fid);
  206. dose = reshape(dose,siz);
  207. optResults.dose{k} = dose;
  208. end
  209. end
  210. % read in the appropriate weights files
  211. for k=1:length(weightFileNames)
  212. if weightFileExist(k) == 1
  213. fid = fopen(weightFileNames{k},'rb');
  214. weights = fread(fid,'float');
  215. fclose(fid);
  216. optResults.weights{k} = weights;
  217. end
  218. end
  219. else % read in only the last dose and weights files
  220. for k=length(doseFileNames):-1:0
  221. if doseFileExist(k) == 1 % found last dose file
  222. break;
  223. end
  224. end
  225. fid = fopen(doseFileNames{k},'rb');
  226. dose = fread(fid,'float');
  227. fclose(fid);
  228. dose = reshape(dose,siz);
  229. optResults.dose = dose;
  230. fid = fopen(weightFileNames{k},'rb');
  231. weights = fread(fid,'float');
  232. fclose(fid);
  233. optResults.weights = weights;
  234. end
  235. % calculate a max dose vector
  236. if readAllOutputFiles == 1
  237. for k=1:length(optResults.dose)
  238. dmax(k) = 1.1*max(optResults.dose{k}(:));
  239. end
  240. % calculate DVHs for each tissue, for each batch number
  241. for m=1:length(optResults.presc.tissue)
  242. tissMask = zeros(optResults.presc.siz,'int8');
  243. tissMask(optResults.presc.tissue(m).ind) = 1;
  244. for k=1:length(optResults.dose)
  245. dvhbins = [0:Ndvhbins-1]*dmax(k)/Ndvhbins;
  246. optResults.presc.tissue(m).dvhbins{k} = dvhbins;
  247. optResults.presc.tissue(m).dvh{k} ...
  248. = dvh(optResults.dose{k},tissMask,dvhbins)';
  249. end
  250. end
  251. fprintf('Finished calculating DVHs for %s\n',optResults.presc.tissue(m).name);
  252. else
  253. dmax = 1.1*max(optResults.dose(:));
  254. % calculate DVHs for each tissue
  255. for m=1:length(optResults.presc.tissue)
  256. tissMask = zeros(optResults.presc.siz,'int8');
  257. tissMask(optResults.presc.tissue(m).ind) = 1;
  258. dvhbins = [0:Ndvhbins-1]*dmax/Ndvhbins;
  259. optResults.presc.tissue(m).dvhbins = dvhbins;
  260. optResults.presc.tissue(m).dvh ...
  261. = dvh(optResults.dose,tissMask,dvhbins)';
  262. end
  263. end
  264. % load the objective function
  265. fid = fopen([inputFolder optResults.optParam.objFuncName],'rb');
  266. optResults.objFunc = fread(fid,'float');
  267. fclose(fid);