loadOptResults.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. function [optSettings, 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. %bemal
  5. % [S,R] = 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. % [S,R] = 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. % constants
  39. Ndvhbins = 1000; % number of bins to use for the cumulative DVH calculation
  40. if length(varargin) == 1
  41. optInputFile = varargin{1};
  42. readAllOutputFiles = 1;
  43. elseif length(varargin) == 2
  44. optInputFile = varargin{1};
  45. if strcmp(deblank(lower(varargin{2})),'last')
  46. readAllOutputFiles = 0;
  47. else
  48. readAllOutputFiles = 1;
  49. end
  50. else
  51. error('Too many input arguments');
  52. end
  53. % Extract the folder name that contains the input file
  54. inputFileNameRev = fliplr(optInputFile); % flip the input filename around
  55. % pop off the reversed file name
  56. [fileNameRev,inputFolderRev] = strtok(inputFileNameRev,{'/','\'});
  57. inputFolder = fliplr(inputFolderRev);
  58. [optSettings,missingInfo] = loadOptSettings(optInputFile); % load the optimization settings
  59. if numel(missingInfo)
  60. fprintf('Information missing from optimization files:\n');
  61. for k=1:numel(missingInfo)
  62. fprintf('%s\n',missingInfo{k});
  63. end
  64. fprintf('\n');
  65. end
  66. % find which dose and weights files are present
  67. Nbatches = ceil(optSettings.optInfo.Niterations/optSettings.optInfo.Nperbatch)+1; % extra '1' is for initial guess
  68. doseFileNames = cell(1,Nbatches);
  69. doseFileExist = zeros(1,Nbatches); % flags to test for existence of files
  70. weightFileNames = cell(1,Nbatches);
  71. weightFileExist = zeros(1,Nbatches);
  72. for k=1:Nbatches
  73. doseFileNames{k} = [inputFolder optSettings.optInfo.outputFolder '/' ...
  74. optSettings.optInfo.doseBatchBaseName ...
  75. num2str((k-1)*optSettings.optInfo.Nperbatch) '.' ...
  76. optSettings.optInfo.doseBatchExtension];
  77. weightFileNames{k} = [inputFolder optSettings.optInfo.outputFolder '/' ...
  78. optSettings.optInfo.weightBatchBaseName ...
  79. num2str((k-1)*optSettings.optInfo.Nperbatch) '.' ...
  80. optSettings.optInfo.weightBatchExtension];
  81. fid = fopen(doseFileNames{k},'rb');
  82. if fid == -1 % batch file doesn't exist
  83. doseFileExist{k} = 0;
  84. else
  85. fclose(fid);
  86. doseFileExist(k) = 1; % mark that file exists
  87. end
  88. fid = fopen(weightFileNames{k},'rb');
  89. if fid == -1 % batch file doesn't exist
  90. weightFileExist{k} = 0;
  91. else
  92. fclose(fid);
  93. weightFileExist(k) = 1; % mark that file exists
  94. end
  95. end
  96. optResults = [];
  97. if readAllOutputFiles == 1
  98. optResults.dose = {};
  99. optResults.weights = {};
  100. % read in the appropriate dose files
  101. for k=1:length(doseFileNames)
  102. if doseFileExist(k) == 1
  103. fid = fopen(doseFileNames{k},'rb');
  104. dose = fread(fid,'float');
  105. fclose(fid);
  106. dose = reshape(dose,optSettings.prescInfo.presc.siz);
  107. optResults.dose{k} = dose;
  108. end
  109. end
  110. % read in the appropriate weights files
  111. for k=1:length(weightFileNames)
  112. if weightFileExist(k) == 1
  113. fid = fopen(weightFileNames{k},'rb');
  114. weights = fread(fid,'float');
  115. fclose(fid);
  116. optResults.weights{k} = weights;
  117. end
  118. end
  119. else % read in only the last dose and weights files
  120. for k=length(doseFileNames):-1:0
  121. if doseFileExist(k) == 1 % found last dose file
  122. break;
  123. end
  124. end
  125. fid = fopen(doseFileNames{k},'rb');
  126. dose = fread(fid,'float');
  127. fclose(fid);
  128. dose = reshape(dose,optSettings.prescInfo.presc.siz);
  129. optResults.dose = dose;
  130. fid = fopen(weightFileNames{k},'rb');
  131. weights = fread(fid,'float');
  132. fclose(fid);
  133. optResults.weights = weights;
  134. end
  135. optResults.presc = optSettings.prescInfo.presc;
  136. % calculate a max dose vector
  137. if readAllOutputFiles == 1
  138. for k=1:length(optResults.dose)
  139. dmax(k) = 1.1*max(optResults.dose{k}(:));
  140. end
  141. % calculate DVHs for each tissue, for each batch number
  142. for m=1:length(optResults.presc.tissue)
  143. tissMask = zeros(optResults.presc.siz,'int8');
  144. tissMask(optResults.presc.tissue(m).ind) = 1;
  145. for k=1:length(optResults.dose)
  146. dvhbins = [0:Ndvhbins-1]*dmax(k)/Ndvhbins;
  147. optResults.presc.tissue(m).dvhbins{k} = dvhbins;
  148. optResults.presc.tissue(m).dvh{k} ...
  149. = dvh(optResults.dose{k},tissMask,dvhbins)';
  150. end
  151. end
  152. else
  153. dmax = 1.1*max(optResults.dose(:));
  154. % calculate DVHs for each tissue
  155. for m=1:length(optResults.presc.tissue)
  156. tissMask = zeros(optResults.presc.siz,'int8');
  157. tissMask(optResults.presc.tissue(m).ind) = 1;
  158. dvhbins = [0:Ndvhbins-1]*dmax/Ndvhbins;
  159. optResults.presc.tissue(m).dvhbins = dvhbins;
  160. optResults.presc.tissue(m).dvh ...
  161. = dvh(optResults.dose,tissMask,dvhbins)';
  162. end
  163. end
  164. % load the objective function
  165. fid = fopen([inputFolder '/' optSettings.optInfo.outputFolder '/' optSettings.optInfo.objFuncFileName],'rb');
  166. optResults.objFunc = fread(fid,'float');
  167. fclose(fid);