NLP_beamlet_optimizer.m 13 KB

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