NLP_beamlet_optimizer.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. % colorwash(Geometry.data, D_full, [500, 1500], [0,70])
  2. % orthoslice(D_full, [0,70])
  3. function [D_full, w_fin, Geometry, optGoal] = NLP_beamlet_optimizer
  4. % This function performs the beamlet optimization
  5. % Inputs: none. Still needs to find "Geometry" and "beamlets" from Wiscplan
  6. % Outputs: full dose image dose: D_full, optimal beamlet weights: w_fin
  7. %
  8. % [D_full, w_fin, Geometry, optGoal] = NLP_beamlet_optimizer;
  9. %
  10. % Made by Peter Ferjancic 1. May 2018
  11. % Last updated: 14. August 2018
  12. % Inspired by Ana Barrigan's REGGUI optimization procedures
  13. N_fcallback1 = 5000;
  14. N_fcallback2 = 200000;
  15. patient = 'gbm_015';
  16. switch patient
  17. case 'gbm_005'
  18. patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatData_ausGli_005';
  19. DP_dir = '\\Mpufs5\data_wnx1\_Data\Glioma_aus\FET_FGL005\B1\Processed';
  20. case 'gbm_015'
  21. patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatData_ausGli_015';
  22. DP_dir = '\\Mpufs5\data_wnx1\_Data\Glioma_aus\FET_FGL015\B1\Processed';
  23. case 'gbm_022'
  24. patient_dir = 'C:\010-work\003_localGit\WiscPlan_v2\data\PatData_ausGli_022';
  25. DP_dir = '\\Mpufs5\data_wnx1\_Data\Glioma_aus\FET_FGL022\B1\Processed';
  26. otherwise
  27. error('invalid case')
  28. end
  29. % merge_beamlets(4, patient_dir);
  30. %% PROGRAM STARTS HERE
  31. % - no tocar lo que hay debajo -
  32. fprintf('starting NLP optimization process... \n')
  33. % % -- LOAD GEOMETRY AND BEAMLETS --
  34. load([patient_dir '\matlab_files\Geometry.mat'])
  35. %% -- OPTIMIZATION TARGETS --
  36. load([DP_dir '\RODP_files\ROI_goals.mat']);
  37. optGoal = ROI_goals.optGoal;
  38. optGoal_beam = ROI_goals.optGoal_beam;
  39. optGoal_idx = ROI_goals.optGoal_idx;
  40. targetMinMax_idx = ROI_goals.targetMinMax_idx;
  41. [beamlets, beamlets_joined, numBeamlet, numBeam, beam_i_list] = get_beam_lets(Geometry, patient_dir);
  42. % -- make them robust --
  43. RO_params=0;
  44. optGoal_beam = make_robust_optGoal(optGoal_beam, RO_params, beamlets_joined);
  45. optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
  46. %% -- INITIALIZE BEAMLET WEIGHTS --
  47. w0_beams = ones(numBeam,1);
  48. w0_beams = mean(optGoal_beam{1}.target ./ (optGoal_beam{1}.beamlets_pruned*w0_beams+0.1)) * w0_beams;
  49. w0_beams = w0_beams + (2*rand(size(w0_beams))-1) *0.1 .*w0_beams; % random perturbation
  50. w0_beams = double(w0_beams);
  51. % -- CALLBACK OPTIMIZATION FUNCTION --
  52. fun1 = @(x) get_penalty(x, optGoal_beam);
  53. fun2 = @(x) get_penalty(x, optGoal);
  54. % -- OPTIMIZATION PARAMETERS --
  55. % define optimization parameters
  56. A = [];
  57. b = [];
  58. Aeq = [];
  59. beq = [];
  60. lb = zeros(1, numBeamlet);
  61. lb_beam = zeros(1, numBeam);
  62. ub = [];
  63. nonlcon = [];
  64. % define opt limits, and make it fmincon progress
  65. options = optimoptions('fmincon');
  66. options.MaxFunctionEvaluations = N_fcallback1;
  67. options.Display = 'iter';
  68. options.PlotFcn = 'optimplotfval';
  69. % options.UseParallel = true;
  70. % options.OptimalityTolerance = 1e-9;
  71. fprintf('\n running initial optimizer:')
  72. %% Run the optimization
  73. % -- GET FULL BEAM WEIGHTS --
  74. tic
  75. w_beam = fmincon(fun1,w0_beams,A,b,Aeq,beq,lb_beam,ub,nonlcon,options);
  76. fprintf(' done!:')
  77. % t=toc;
  78. % disp(['Optimization time for beams = ',num2str(t)]);
  79. w_beamlets = ones(numBeamlet,1);
  80. numBeam=numel(unique(beam_i_list));
  81. for beam_i = 1:numBeam % assign weights to beamlets
  82. % beamlets from same beam get same initial weights
  83. w_beamlets(beam_i_list == beam_i) = w_beam(beam_i);
  84. end
  85. w_beamlets = w_beamlets + (2*rand(size(w_beamlets))-1) *0.1 .*w_beamlets; % small random perturbation
  86. w_beamlets = 1.1* w_beamlets; % this just kicks the beamlets up a bit to make it easier for the optimizer to start
  87. % -- GET FULL BEAMLET WEIGHTS --
  88. options.MaxFunctionEvaluations = N_fcallback2;
  89. % tic
  90. fprintf('\n running full optimizer:')
  91. w_fin = fmincon(fun2,w_beamlets,A,b,Aeq,beq,lb,ub,nonlcon,options);
  92. fprintf(' done!:')
  93. t=toc;
  94. disp(['Optimization time for beamlets = ',num2str(t)]);
  95. %% evaluate the results
  96. D_full = reshape(beamlets * w_fin, size(Geometry.data));
  97. %% save outputs
  98. NLP_result.dose = D_full;
  99. NLP_result.weights = w_fin;
  100. save([patient_dir '\matlab_files\NLP_result.mat'], 'NLP_result');
  101. plot_DVH(D_full, optGoal, optGoal_idx, targetMinMax_idx)
  102. colorwash(Geometry.data, D_full, [-500, 500], [0, 110]);
  103. % plot_DVH_robust(D_full, optGoal, optGoal_idx)
  104. end
  105. %% support functions
  106. % ---- PENALTY FUNCTION ----
  107. function penalty = get_penalty(x, optGoal)
  108. % this function gets called by the optimizer. It checks the penalty for
  109. % all the robust implementation and returns the worst result.
  110. NumScenarios = optGoal{1}.NbrRandScenarios * optGoal{1}.NbrSystSetUpScenarios * optGoal{1}.NbrRangeScenarios;
  111. fobj = zeros(NumScenarios,1);
  112. sc_i = 1;
  113. for nrs_i = 1:optGoal{1}.NbrRandScenarios
  114. for sss_i = 1 :optGoal{1}.NbrSystSetUpScenarios % syst. setup scenarios = sss
  115. for rrs_i = 1:optGoal{1}.NbrRangeScenarios % range scenario = rs
  116. fobj(sc_i)=eval_f(x, optGoal, nrs_i, sss_i, rrs_i);
  117. sc_i = sc_i + 1;
  118. end
  119. end
  120. end
  121. % take the worst case penalty of evaluated scenarios
  122. penalty=max(fobj);
  123. end
  124. % ------ supp: penalty for single scenario ------
  125. function penalty = eval_f(x, optGoal, nrs_i, sss_i, rrs_i)
  126. penalty = 0;
  127. % for each condition
  128. for goal_i = 1:numel(optGoal)
  129. switch optGoal{goal_i}.function
  130. % min, max, min_sq, max_sq, LeastSquare, min_perc_Volume, max_perc_Volume
  131. case 'min'
  132. % penalize if achieved dose is lower than target dose
  133. d_penalty = 1.0e0 * sum(max(0, ...
  134. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target) -...
  135. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x)));
  136. case 'max'
  137. % penalize if achieved dose is higher than target dose
  138. d_penalty = 1.0e0 * sum(max(0, ...
  139. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x)-...
  140. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target)));
  141. case 'min_sq'
  142. % penalize if achieved dose is higher than target dose
  143. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x)-...
  144. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target));
  145. d_penalty = 1.0e0 * sum(temp1.^2);
  146. case 'max_sq'
  147. % penalize if achieved dose is higher than target dose
  148. temp1=max(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x)-...
  149. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target));
  150. d_penalty = 1.0e0 * sum(temp1.^2);
  151. case 'LeastSquare'
  152. % penalize with sum of squares any deviation from target
  153. % dose
  154. d_penalty = 1.0e-1* sum(((...
  155. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x) - ...
  156. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target).^2);
  157. case 'min_perc_Volume'
  158. % penalize by amount of volume under threshold
  159. perc_vox = numel(find((optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target) -...
  160. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x) > 0)) ...
  161. / numel(optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target);
  162. d_penalty = 3.0e5 * min(perc_vox-0.05, 0)
  163. case 'max_perc_Volume'
  164. % penalize by amount of volume under threshold
  165. perc_vox = numel(find((optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target) -...
  166. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned * x) < 0)) ...
  167. / numel(optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target);
  168. d_penalty = 3.0e4 * min(perc_vox-0.05, 0)
  169. end
  170. penalty = penalty + d_penalty * optGoal{goal_i}.opt_weight;
  171. end
  172. end
  173. % ---- MAKE ROI ROBUST ----
  174. function optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
  175. % take regular optimal goal and translate it into several robust cases
  176. % RO_params - should have the information below
  177. % nrs - random scenarios
  178. % sss - system setup scenarios
  179. % rrs - random range scenarios
  180. % X - X>0 moves image right
  181. % Y - Y>0 moves image down
  182. % Z - in/out.
  183. shift_mag = 3; % vox of shift
  184. nrs_scene_list={[0,0,0]};
  185. % sss_scene_list={[0,0,0]};
  186. sss_scene_list={[0,0,0], [-shift_mag,0,0], [shift_mag,0,0], [0,-shift_mag,0], [0,shift_mag,0]};
  187. % sss_scene_list={[0,0,0], [-shift_mag,0,0], [shift_mag,0,0], [0,-shift_mag,0], [0,shift_mag,0],...
  188. % [-shift_mag*2,0,0], [shift_mag*2,0,0], [0,-shift_mag*2,0], [0,shift_mag*2,0]};
  189. rrs_scene_list={[0,0,0]};
  190. [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
  191. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\PD_HD_dicomPhantom\Tomo_DP_target.nrrd');
  192. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
  193. for i = 1:numel(optGoal)
  194. optGoal{i}.NbrRandScenarios =numel(nrs_scene_list);
  195. optGoal{i}.NbrSystSetUpScenarios=numel(sss_scene_list);
  196. optGoal{i}.NbrRangeScenarios =numel(rrs_scene_list);
  197. end
  198. for goal_i = 1:numel(optGoal)
  199. % get target
  200. idx=optGoal{goal_i}.ROI_idx;
  201. targetImg1=zeros(optGoal{goal_i}.imgDim);
  202. targetImg1(idx)=1;
  203. % get beamlets
  204. for nrs_i = 1:optGoal{goal_i}.NbrRandScenarios % num. of random scenarios
  205. % modify target and beamlets
  206. targetImg2=targetImg1;
  207. % beamlets stay the same
  208. for sss_i = 1 :optGoal{goal_i}.NbrSystSetUpScenarios % syst. setup scenarios = sss
  209. % modify target and beamlets
  210. targetImg3=get_RO_sss(targetImg2, sss_scene_list{sss_i});
  211. % beamlets stay the same
  212. for rrs_i = 1:optGoal{goal_i}.NbrRangeScenarios % range scenario = rrs
  213. % modify target and beamlets
  214. targetImg4=targetImg3;
  215. % beamlets stay the same
  216. %% make new target and beamlets
  217. ROI_idx=[];
  218. ROI_idx=find(targetImg4>0);
  219. if isfield(optGoal{goal_i}, 'target_alpha')
  220. target = double(optGoal{goal_i}.target_alpha * targetIn(ROI_idx));
  221. % disp('exists')
  222. else
  223. target = ones(numel(ROI_idx), 1) .* optGoal{goal_i}.D_final;
  224. % disp('not exists')
  225. end
  226. beamlets_pruned = beamlets(ROI_idx, :);
  227. % save to optGoal output
  228. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.ROI_idx = ROI_idx;
  229. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.beamlets_pruned = beamlets_pruned;
  230. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rrs{rrs_i}.target = target;
  231. end
  232. end
  233. end
  234. end
  235. end
  236. % ------ supp: RO case SSS ------
  237. function targetImg3=get_RO_sss(targetImg2, sss_scene_shift);
  238. % translate the target image
  239. targetImg3 = imtranslate(targetImg2,sss_scene_shift);
  240. end