linlsqOptSetup_server.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\AV004c\AV004c_geometry.mat';
  20. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV003f\AV003f_geometry.mat';
  21. % geometryFile = 'D:\FTP\Dose Painting\AAPM09\HN002b\HN002b_geometry.mat';
  22. geometryFile = 'D:\FTP\Dose Painting\AAPM09\AV001f\AV001f_geometry.mat';
  23. % define optimization settings
  24. optSettings = [];
  25. optSettings.optInfo.Niterations = 1000; % total number of iterations of beamlet weight optimization
  26. optSettings.optInfo.Nperbatch = 250; % iteration interval between writing planned dose and weights to file
  27. optimizing = 1;
  28. optSettings.optInfo.doseBatchBaseName = 'AV001f_PVC_Threshold_cropped_dosebatch';
  29. switch optimizing
  30. case 1
  31. optSettings.optInfo.optRun = 'yes'; % flag to run optimization in MATLAB
  32. optSettings.optInfo.saveOptInfo = 'no'; % save opt settings in optinput.txt file located in opt folder
  33. optSettings.beamletInfo.saveBeamletHeader = 'no'; % save beamlet info in beamlet_header.txt file located in beamlet folder
  34. optSettings.prescInfo.savePresc = 'no'; % save presc info in prescription.txt file located in input folder
  35. case 0
  36. optSettings.optInfo.optRun = 'no'; % flag to run optimization in MATLAB
  37. optSettings.optInfo.saveOptInfo = 'yes'; % save opt settings in optinput.txt file located in opt folder
  38. optSettings.beamletInfo.saveBeamletHeader = 'yes'; % save beamlet info in beamlet_header.txt file located in beamlet folder
  39. optSettings.prescInfo.savePresc = 'yes'; % save presc info in prescription.txt file located in input folder
  40. end
  41. % optSettings.optInfo.optRun = 'yes'; % flag to run optimization in MATLAB
  42. optSettings.optInfo.waitForResults = 'no'; % flag to load final dose distribution and DVH into workspace
  43. % optSettings.optInfo.saveOptInfo = 'no'; % save opt settings in optinput.txt file located in opt folder
  44. optSettings.beamletInfo.saveBeamlets = 'no'; % save beamlets when dose calc performed in MATLAB and not Condor
  45. % optSettings.beamletInfo.saveBeamletHeader = 'no'; % save beamlet info in beamlet_header.txt file located in beamlet folder
  46. % optSettings.beamletInfo.beamletFolder = '../plan/dave/HN002b/beamlets';
  47. % % specify location of beamlet folder relative to opt folder
  48. optSettings.beamletInfo.beamletFolder = 'AV001fbeamlets'; % specify location of beamlet folder relative to opt folder
  49. % optSettings.beamletInfo.beamletFolder = 'HN002bbeamlets'; % specify location of beamlet folder relative to opt folder
  50. optSettings.optInfo.optExeName = [optSettings.optInfo.doseBatchBaseName 'linlsqOpt.exe'];
  51. optSettings.beamletInfo.Nbeamletbatches = 177; % total number of beamlet batches
  52. optSettings.beamletInfo.Nbeamlets = 11322; % total number of non-zero weighted beamets
  53. % optSettings.prescInfo.savePresc = 'no'; % save presc info in prescription.txt file located in input folder
  54. optSettings.optInfo.optFolder = 'C:\WiscPlan\opt\linlsqOpt'; % directory where opt executable is located must be absolute path
  55. optSettings.optInfo.modFactor = 3.3; % mod factor truncating maximum beamlet weight relative to average weight
  56. % Prescription table
  57. % (code name) (Pinnacle tissue name) (alpha) (Bplus) (dplus) (Bminus) (dminus) (BVplus) (dVplus) (Vplus) (BVminus) (dVminus) (Vminus)
  58. prescTable = {
  59. 'cord' 'cord' 1 1 0 0 0 0 0 0 0 0 0
  60. 'GTV' 'GTV' 1 1000 70 1000 70 0 0 0 0 0 0
  61. % 'CTV60' 'CTV 60' 0 0 0 0 0 0 0 0 0 0 0
  62. % 'CTV54' 'CTV 54' 0 0 0 0 0 0 0 0 0 0 0
  63. % 'CTV-rt-scv-5' 'CTV rt scv 54' 0 0 0 0 0 0 0 0 0 0 0
  64. % 'CTV-lt-scv-50' 'CTV lt scv 50' 0 0 0 0 0 0 0 0 0 0 0
  65. % 'larynx' 'larynx' 0 1 0 0 0 0 0 0 0 0 0
  66. % 'opticApparatus' 'optic apparatus' 0 1 0 0 0 0 0 0 0 0 0
  67. 'lt_parotid' 'lt_parotid' 1 1 0 0 0 0 0 0 0 0 0
  68. 'normal_tissue' 'normal_tissue' 1 10 0 0 0 0 0 0 0 0 0
  69. 'oral_cavity' 'oral_cavity' 1 1 0 0 0 0 0 0 0 0 0
  70. 'rt_Parotid' 'rt_parotid' 1 1 0 0 0 0 0 0 0 0 0
  71. % 'Brainstem' 'Brainstem' 0 1 0 0 0 0 0 0 0 0 0
  72. % 'eyes' 'eyes' 0 1 0 0 0 0 0 0 0 0 0
  73. % 'couch' 'couch' 0 0 0 0 0 0 0 0 0 0 0
  74. % 'external' 'external' 0 0 0 0 0 0 0 0 0 0 0
  75. % 'ext+1cm' 'ext + 1 cm' 0 0 0 0 0 0 0 0 0 0 0
  76. % 'ring' 'ring' 0 1 0 0 0 0 0 0 0 0 0
  77. % 'PTV70' 'PTV 70' 0 200 70 200 70 0 0 0 0 0 0
  78. % 'PTV60' 'PTV 60' 0 0 0 0 0 0 0 0 0 0 0
  79. % 'PTV54' 'PTV 54' 0 10 0 0 0 0 0 0 0 0 0
  80. % 'PTV50' 'PTV 50' 0 10 0 0 0 0 0 0 0 0 0
  81. % 'LResidParotid' 'L resid parotid' 0 1 0 0 0 0 0 0 0 0 0
  82. % 'PTV60min70' 'PTV 60 - PTV 70' 0 10 60 200 60 0 0 0 0 0 0
  83. };
  84. %%%%% End of user supplied inputs %%%%%
  85. %%
  86. % % calc total number of initial non-zero weighted beamlets from dose calc if
  87. % % saving new beamlet header file
  88. if strcmp(optSettings.beamletInfo.saveBeamletHeader,'yes')
  89. optSettings.beamletInfo.Nbeamlets = beamletNum([optSettings.optInfo.optFolder '\' optSettings.beamletInfo.beamletFolder],optSettings.beamletInfo.Nbeamletbatches);
  90. end
  91. load(geometryFile); % load geometry file
  92. % match isocenter of coordinate system used to calculate photon beamlets or
  93. % proton spots, defined by the patient geometry
  94. x_iso = Geometry.start(1) + (Geometry.voxel_size(1).*double(size(Geometry.data,1)))./2;
  95. y_iso = Geometry.start(2) + (Geometry.voxel_size(2).*double(size(Geometry.data,2)))./2;
  96. z_iso = Geometry.start(3);
  97. iso = [x_iso y_iso z_iso];
  98. % size of CT grid
  99. [M,N,Q] = size(Geometry.data);
  100. % Create the prescription structure
  101. presc = []; % erase the old prescription structure, if it exists
  102. presc.siz = [M N Q]; % dimensions of the prescription and tissue masks
  103. ROIFlags = zeros(1,size(prescTable,1)); % flags to ensure all ROIs are found
  104. % assign the parameters to the prescription structure
  105. for j=1:size(prescTable,1)
  106. % pull out the prescription indices for each tissue
  107. tissName = prescTable{j,2}; % current tissue name
  108. % find the current tissue in the Geometry.ROIS cells
  109. for k=1:length(Geometry.ROIS)
  110. ind = 0;
  111. if strcmp(tissName,Geometry.ROIS{k}.name)
  112. ind = Geometry.ROIS{k}.ind; % extract ROI indices
  113. break;
  114. end
  115. end
  116. if ind == 0
  117. error([tissName ' can not be found in Geometry or is empty!']);
  118. end
  119. presc.tissue(j).name = prescTable{j,1};
  120. presc.tissue(j).ind = ind;
  121. presc.tissue(j).alpha = prescTable{j,3};
  122. presc.tissue(j).betaPlus = prescTable{j,4};
  123. presc.tissue(j).dPlus = single(zeros(size(ind)) + prescTable{j,5});
  124. presc.tissue(j).betaMinus = prescTable{j,6};
  125. presc.tissue(j).dMinus = single(zeros(size(ind)) + prescTable{j,7});
  126. presc.tissue(j).betaVPlus = prescTable{j,8};
  127. presc.tissue(j).dVPlus = prescTable{j,9};
  128. presc.tissue(j).vPlus = prescTable{j,10};
  129. presc.tissue(j).betaVMinus = prescTable{j,11};
  130. presc.tissue(j).dVMinus = prescTable{j,12};
  131. presc.tissue(j).vMinus = prescTable{j,13};
  132. ROIFlags(j) = 1;
  133. end
  134. % ensure that all of the ROIS were found
  135. if sum(ROIFlags) ~= length(ROIFlags)
  136. error('Missed one or more ROIS.\n');
  137. else
  138. fprintf('All ROIs located successfully for downsampled prescription.\n');
  139. end
  140. optSettings.prescInfo.presc = presc; % set prescription
  141. % Save prescription and beamlet information, which is necessary for initial
  142. % guess calculation.
  143. linlsqOptimization(optSettings);
  144. % calculate the initial guess if flagged
  145. [w0,dose0] = calcNormalizedInitialGuess([optSettings.optInfo.optFolder '/optInput.txt']);
  146. optSettings.initialGuessInfo.w0 = w0;
  147. optSettings.initialGuessInfo.saveInitialGuess = 'yes';
  148. linlsqOptimization(optSettings);
  149. if (optSettings.optInfo.optRun == 'no')
  150. fprintf('Optimization setup complete.\n');
  151. else
  152. [weights, dose] = linlsqOptimization(optSettings);
  153. fprintf('Optimization run complete.\n');
  154. if (optSettings.optInfo.waitForResults == 'yes')
  155. [optSettings,optResults] = loadOptResults([optSettings.optInfo.optFolder '/optInput.txt'],'last');
  156. frpintf('Final dose distribution and DVHs loaded.\n');
  157. end
  158. end