linlsqOptSetup.asv 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. %% linlsqOptSetup.m
  2. % Stephen R Bowen April 2009
  3. %
  4. % Runs in MATLAB or writes setup files necessary to carry out an
  5. % optimization of photon beamlet or proton spot weight distributions by
  6. % iteratitively minimizing a linear least squares objective function. The
  7. % objective function has the following components:
  8. %
  9. % -Tissue relative importance alpha/Ntissue (alpha binary)
  10. % -Overdose and underdose relative penalties in each tissue Bplus & Bminus
  11. % -Maximum and minimum doses at each voxel within a tissue dplus & dminus
  12. % -Overdose-volume and underdose-volume relative penalties BVplus & BVminus
  13. % -Maximum and minimum doses dVplus & dVminus evaluated at each relative
  14. % tissue maximum and minimum volume percentatges Vplus & Vminus
  15. %%
  16. %%%%% Start of user supplied inputs %%%%%
  17. % specify relative location of Geometry file including ROI masks
  18. % geometryFile = '../plan/HN002b/HN002b_geometry.mat';
  19. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV003f\AV003f_geometry.mat';
  20. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV004c\AV004c_geometry.mat';
  21. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV004c\AV004c_cont_geometry.mat';
  22. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV004c\AV004c_PVC_cont_geometry.mat';
  23. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV004c\AV004c_thresh_geometry.mat';
  24. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV004c\AV004c_PVC_thresh_geometry.mat';
  25. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\HN002b\HN002b_geometry.mat';
  26. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV001c\AV001c_geometry.mat';
  27. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV001f\AV001f_geometry.mat';
  28. geometryFile = 'D:\FTP\Dose Painting\AAPM09\HN003c\HN003c_geometry.mat';
  29. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV004f\AV004f_geometry.mat';
  30. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV002f\AV002f_geometry.mat';
  31. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV005c\AV005c_geometry.mat';
  32. % define optimization settings
  33. optSettings = [];
  34. optSettings.optInfo.Niterations = 1000; % total number of iterations of beamlet weight optimization
  35. optSettings.optInfo.Nperbatch = 250; % iteration interval between writing planned dose and weights to file
  36. optimizing = 1;
  37. optSettings.optInfo.doseBatchBaseName = 'HN003c_U01_5MM_';
  38. switch optimizing
  39. case 1
  40. optSettings.optInfo.optRun = 'yes'; % flag to run optimization in MATLAB
  41. optSettings.optInfo.saveOptInfo = 'no'; % save opt settings in optinput.txt file located in opt folder
  42. optSettings.beamletInfo.saveBeamletHeader = 'no'; % save beamlet info in beamlet_header.txt file located in beamlet folder
  43. optSettings.prescInfo.savePresc = 'no'; % save presc info in prescription.txt file located in input folder
  44. case 0
  45. optSettings.optInfo.optRun = 'no'; % flag to run optimization in MATLAB
  46. optSettings.optInfo.saveOptInfo = 'yes'; % save opt settings in optinput.txt file located in opt folder
  47. optSettings.beamletInfo.saveBeamletHeader = 'yes'; % save beamlet info in beamlet_header.txt file located in beamlet folder
  48. optSettings.prescInfo.savePresc = 'yes'; % save presc info in prescription.txt file located in input folder
  49. end
  50. % optSettings.optInfo.optRun = 'yes'; % flag to run optimization in MATLAB
  51. optSettings.optInfo.waitForResults = 'no'; % flag to load final dose distribution and DVH into workspace
  52. % optSettings.optInfo.saveOptInfo = 'no'; % save opt settings in optinput.txt file located in opt folder
  53. optSettings.beamletInfo.saveBeamlets = 'no'; % save beamlets when dose calc performed in MATLAB and not Condor
  54. % optSettings.beamletInfo.saveBeamletHeader = 'no'; % save beamlet info in beamlet_header.txt file located in beamlet folder
  55. % % specify location of beamlet folder relative to opt folder
  56. % optSettings.beamletInfo.beamletFolder = 'AV003fbeamlets';
  57. % optSettings.beamletInfo.beamletFolder = 'AV004cbeamlets';
  58. % optSettings.beamletInfo.beamletFolder = 'HN002bbeamlets';
  59. % optSettings.beamletInfo.beamletFolder = 'AV001cbeamlets';
  60. % optSettings.beamletInfo.beamletFolder = 'AV001fbeamlets';
  61. optSettings.beamletInfo.beamletFolder = '../../plan/HN003cbeamlets'; % specify location of beamlet folder relative to opt folder
  62. % optSettings.beamletInfo.beamletFolder = '../../plan/AV004fbeamlets'; % specify location of beamlet folder relative to opt folder
  63. % optSettings.beamletInfo.beamletFolder = '../../plan/AV002fbeamlets';
  64. % optSettings.beamletInfo.beamletFolder = '../../plan/AV005cbeamlets';
  65. optSettings.optInfo.optExeName = [optSettings.optInfo.doseBatchBaseName 'linlsqOpt.exe'];
  66. optSettings.beamletInfo.Nbeamletbatches = 197; % total number of beamlet batches
  67. optSettings.beamletInfo.Nbeamlets = 12592; % total number of non-zero weighted beamets
  68. % optSettings.prescInfo.savePresc = 'no'; % save presc info in prescription.txt file located in input folder
  69. optSettings.optInfo.optFolder = 'C:\WiscPlan\opt\linlsqOpt'; % directory where opt executable is located must be absolute path
  70. optSettings.optInfo.modFactor = 6; % mod factor truncating maximum beamlet weight relative to average weight
  71. % Prescription table
  72. % (code name) (Pinnacle tissue name) (alpha) (Bplus) (dplus) (Bminus) (dminus) (BVplus) (dVplus) (Vplus) (BVminus) (dVminus) (Vminus)
  73. prescTable = {
  74. 'cord' 'cord' 1 1 0 0 0 0 0 0 0 0 0
  75. 'GTV' 'GTV' 1 1000 70 1000 70 0 0 0 0 0 0
  76. % 'cont_base' 'cont_base' 1 1000 60 1000 60 0 0 0 0 0 0
  77. % 'cont_boost' 'cont_boost' 1 2000 80 2000 80 0 0 0 0 0 0
  78. % 'PVC_cont_base' 'PVC_cont_base' 1 1000 60 1000 60 0 0 0 0 0 0
  79. % 'PVC_cont_boost' 'PVC_cont_boost' 1 2000 80 2000 80 0 0 0 0 0 0
  80. % 'thresh_base' 'thresh_base' 1 1000 50 1000 50 0 0 0 0 0 0
  81. % 'thresh_boost' 'thresh_boost' 1 2000 70 2000 70 0 0 0 0 0 0
  82. % 'PVC_thresh_base' 'PVC_thresh_base' 1 1000 50 1000 50 0 0 0 0 0 0
  83. % 'PVC_thresh_boost' 'PVC_thresh_boost' 1 2000 70 2000 70 0 0 0 0 0 0
  84. % 'CTV60' 'CTV 60' 0 0 0 0 0 0 0 0 0 0 0
  85. % 'CTV54' 'CTV 54' 0 0 0 0 0 0 0 0 0 0 0
  86. % 'CTV-rt-scv-5' 'CTV rt scv 54' 0 0 0 0 0 0 0 0 0 0 0
  87. % 'CTV-lt-scv-50' 'CTV lt scv 50' 0 0 0 0 0 0 0 0 0 0 0
  88. % 'larynx' 'larynx' 0 1 0 0 0 0 0 0 0 0 0
  89. % 'opticApparatus' 'optic apparatus' 0 1 0 0 0 0 0 0 0 0 0
  90. 'left_parotid' 'left_parotid' 1 1 0 0 0 0 0 0 0 0 0
  91. 'normal_tissue' 'normal_tissue' 1 10 0 0 0 0 0 0 0 0 0
  92. 'oral_cavity' 'oral_cavity' 1 1 0 0 0 0 0 0 0 0 0
  93. 'right_Parotid' 'right_parotid' 1 1 0 0 0 0 0 0 0 0 0
  94. % 'Brainstem' 'Brainstem' 0 1 0 0 0 0 0 0 0 0 0
  95. % 'eyes' 'eyes' 0 1 0 0 0 0 0 0 0 0 0
  96. % 'couch' 'couch' 0 0 0 0 0 0 0 0 0 0 0
  97. % 'external' 'external' 0 0 0 0 0 0 0 0 0 0 0
  98. % 'ext+1cm' 'ext + 1 cm' 0 0 0 0 0 0 0 0 0 0 0
  99. % 'ring' 'ring' 0 1 0 0 0 0 0 0 0 0 0
  100. % 'PTV70' 'PTV 70' 0 200 70 200 70 0 0 0 0 0 0
  101. % 'PTV60' 'PTV 60' 0 0 0 0 0 0 0 0 0 0 0
  102. % 'PTV54' 'PTV 54' 0 10 0 0 0 0 0 0 0 0 0
  103. % 'PTV50' 'PTV 50' 0 10 0 0 0 0 0 0 0 0 0
  104. % 'LResidParotid' 'L resid parotid' 0 1 0 0 0 0 0 0 0 0 0
  105. % 'PTV60min70' 'PTV 60 - PTV 70' 0 10 60 200 60 0 0 0 0 0 0
  106. };
  107. %%%%% End of user supplied inputs %%%%%
  108. %%
  109. % % calc total number of initial non-zero weighted beamlets from dose calc if
  110. % % saving new beamlet header file
  111. if strcmp(optSettings.beamletInfo.saveBeamletHeader,'yes')
  112. optSettings.beamletInfo.Nbeamlets = beamletNum([optSettings.optInfo.optFolder '\' optSettings.beamletInfo.beamletFolder],optSettings.beamletInfo.Nbeamletbatches);
  113. end
  114. load(geometryFile); % load geometry file
  115. % match isocenter of coordinate system used to calculate photon beamlets or
  116. % proton spots, defined by the patient geometry
  117. x_iso = Geometry.start(1) + (Geometry.voxel_size(1).*double(size(Geometry.data,1)))./2;
  118. y_iso = Geometry.start(2) + (Geometry.voxel_size(2).*double(size(Geometry.data,2)))./2;
  119. z_iso = Geometry.start(3);
  120. iso = [x_iso y_iso z_iso];
  121. % size of CT grid
  122. [M,N,Q] = size(Geometry.data);
  123. % Create the prescription structure
  124. presc = []; % erase the old prescription structure, if it exists
  125. presc.siz = [M N Q]; % dimensions of the prescription and tissue masks
  126. ROIFlags = zeros(1,size(prescTable,1)); % flags to ensure all ROIs are found
  127. % assign the parameters to the prescription structure
  128. for j=1:size(prescTable,1)
  129. % pull out the prescription indices for each tissue
  130. tissName = prescTable{j,2}; % current tissue name
  131. % find the current tissue in the Geometry.ROIS cells
  132. for k=1:length(Geometry.ROIS)
  133. ind = 0;
  134. if strcmp(tissName,Geometry.ROIS{k}.name)
  135. ind = Geometry.ROIS{k}.ind; % extract ROI indices
  136. break;
  137. end
  138. end
  139. if ind == 0
  140. error([tissName ' can not be found in Geometry or is empty!']);
  141. end
  142. presc.tissue(j).name = prescTable{j,1};
  143. presc.tissue(j).ind = ind;
  144. presc.tissue(j).alpha = prescTable{j,3};
  145. presc.tissue(j).betaPlus = prescTable{j,4};
  146. presc.tissue(j).dPlus = single(zeros(size(ind)) + prescTable{j,5});
  147. presc.tissue(j).betaMinus = prescTable{j,6};
  148. presc.tissue(j).dMinus = single(zeros(size(ind)) + prescTable{j,7});
  149. presc.tissue(j).betaVPlus = prescTable{j,8};
  150. presc.tissue(j).dVPlus = prescTable{j,9};
  151. presc.tissue(j).vPlus = prescTable{j,10};
  152. presc.tissue(j).betaVMinus = prescTable{j,11};
  153. presc.tissue(j).dVMinus = prescTable{j,12};
  154. presc.tissue(j).vMinus = prescTable{j,13};
  155. ROIFlags(j) = 1;
  156. end
  157. % ensure that all of the ROIS were found
  158. if sum(ROIFlags) ~= length(ROIFlags)
  159. error('Missed one or more ROIS.\n');
  160. else
  161. fprintf('All ROIs located successfully for downsampled prescription.\n');
  162. end
  163. optSettings.prescInfo.presc = presc; % set prescription
  164. % Save prescription and beamlet information, which is necessary for initial
  165. % guess calculation.
  166. linlsqOptimization(optSettings);
  167. % calculate the initial guess if flagged
  168. [w0,dose0] = calcNormalizedInitialGuess([optSettings.optInfo.optFolder '/optInput.txt']);
  169. optSettings.initialGuessInfo.w0 = w0;
  170. optSettings.initialGuessInfo.saveInitialGuess = 'yes';
  171. linlsqOptimization(optSettings);
  172. if (optSettings.optInfo.optRun == 'no')
  173. fprintf('Optimization setup complete.\n');
  174. else
  175. [weights, dose] = linlsqOptimization(optSettings);
  176. fprintf('Optimization run complete.\n');
  177. if (optSettings.optInfo.waitForResults == 'yes')
  178. [optSettings,optResults] = loadOptResults([optSettings.optInfo.optFolder '/optInput.txt'],'last');
  179. frpintf('Final dose distribution and DVHs loaded.\n');
  180. end
  181. end
  182. clear all;