NLP_beamlet_optimizer.m 11 KB

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