NLP_optimizer_v3.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. % paths.patient_dir
  2. % paths.Goal_dir (previously called DP_dir)
  3. % paths.patient
  4. % paths.goalsName
  5. % colorwash(Geometry.data, D_full, [500, 1500], [0,70])
  6. % orthoslice(D_full, [0,70])
  7. function [D_full, w_fin, Geometry, optGoal] = NLP_optimizer_v3(varargin)
  8. % This function performs the beamlet optimization
  9. % [D_full, w_fin, Geometry, optGoal] = NLP_beamlet_optimizer;
  10. %
  11. % Inputs:
  12. % () OR
  13. % (Pat_path, path2goal) OR
  14. % (Pat_path, path2goal, beamlet_weights)
  15. % Pat_path, path2goal = strings to patient folder and optimal goals
  16. % beamlet_weights = initial beamlet weights
  17. %
  18. % Outputs:
  19. % full dose image dose: [D_full, w_fin, Geometry, optGoal]
  20. %
  21. % Made by Peter Ferjancic 1. May 2018
  22. % Last updated: 1. April 2019
  23. if nargin<2
  24. load('WiscPlan_preferences.mat')
  25. [Pat_path] = uigetdir([WiscPlan_preferences.patientDataPath ], 'Select Patient folder');
  26. [Goal_file,Goal_path,indx] = uigetfile([Pat_path '\matlab_files\*.mat'], 'Select OptGoal file');
  27. path2geometry = [Pat_path, '\matlab_files\Geometry.mat'];
  28. path2goal = [Goal_path, Goal_file];
  29. else
  30. Pat_path = varargin{1};
  31. path2geometry = [Pat_path, '\matlab_files\Geometry.mat'];
  32. path2goal = varargin{2};
  33. [Goal_path,Goal_file,ext] = fileparts(path2goal);
  34. end
  35. dialogue_box = 'no'
  36. switch dialogue_box
  37. case 'yes'
  38. str = inputdlg({'N of iterations for initial calc', 'N of iterations for full calc', ...
  39. 'Use pre-existing NLP_result to initiate? (y/n)'}, 'input', [1,35], {'100000', '500000', 'n'});
  40. N_fcallback1 = str2double(str{1}); % 100000 is a good guesstimate
  41. N_fcallback2 = str2double(str{2}); % 500000 is a good guesstimate
  42. pre_beamWeights = str{3};
  43. case 'no'
  44. disp('dialogue box skipped')
  45. N_fcallback1 = 100000;
  46. N_fcallback2 = 500000;
  47. pre_beamWeights = 'n';
  48. end
  49. switch pre_beamWeights
  50. case 'y'
  51. [NLP_file,NLP_path,indx] = uigetfile([Pat_path '\matlab_files\*.mat'], 'Select NLP_result file');
  52. load([NLP_path, NLP_file])
  53. w_beamlets = NLP_result.weights;
  54. load([Pat_path, '\all_beams.mat'])
  55. if numel(all_beams) ~= numel(w_beamlets)
  56. error('Provided weight number does not match beamlet number!')
  57. end
  58. case 'n'
  59. disp('Initial beam weights will be calculated.')
  60. end
  61. %% PROGRAM STARTS HERE
  62. % - no tocar lo que hay debajo -
  63. fprintf('starting NLP optimization process... \n')
  64. % % -- LOAD GEOMETRY, GOALS, BEAMLETS --
  65. load(path2geometry)
  66. load(path2goal)
  67. [beamlets, numBeamlet] = get_beamlets(Geometry, Pat_path, OptGoals);
  68. % [beamlets, beamlets_joined, numBeamlet, numBeam, beam_i_list] = get_beam_lets(Geometry, Pat_path);
  69. %% -- OPTIMIZATION TARGETS --
  70. % -- make the optimization optGoal structure --
  71. for i_goal = 1:size(OptGoals.goals,1)
  72. if isfield(OptGoals.data{i_goal}, 'SupVox_num')
  73. SupVox_num = OptGoals.data{i_goal}.SupVox_num;
  74. else
  75. answer = inputdlg(['# of supervoxels for "' OptGoals.data{i_goal}.name '" with ' num2str(numel(OptGoals.data{i_goal}.ROI_idx)) ' vox: ("0" to skip)'])
  76. SupVox_num = str2double(answer{1})
  77. end
  78. optGoal{i_goal} = OptGoals.data{i_goal};
  79. optGoal{i_goal}.sss_scene_list = OptGoals.sss_scene_list;
  80. optGoal{i_goal}.maxModulation = OptGoals.maxModulation;
  81. optGoal{i_goal}.BeamSmoothMax = OptGoals.BeamSmoothMax;
  82. % modulation
  83. switch SupVox_num
  84. case 0
  85. % if not supervoxel, just select provided ROI_idx
  86. optGoal{i_goal}.beamlets_pruned = sparse(beamlets(optGoal{i_goal}.ROI_idx, :));
  87. otherwise
  88. % -- if supervoxel, merge given columns
  89. % - make supervoxel map
  90. mask = zeros(OptGoals.data{i_goal}.imgDim);
  91. mask(OptGoals.data{i_goal}.ROI_idx) = 1;
  92. % group superpixels
  93. superMask = superpix_group(mask, SupVox_num, 'no');
  94. superVoxList = unique(superMask);
  95. superVoxList = superVoxList(superVoxList>0);
  96. optGoal{i_goal}.ROI_idx_old = optGoal{i_goal}.ROI_idx; % copy old index data
  97. optGoal{i_goal}.ROI_idx = zeros(numel(superVoxList), 1);
  98. optGoal{i_goal}.opt_weight = optGoal{i_goal}.opt_weight * numel(optGoal{i_goal}.ROI_idx_old)/numel(optGoal{i_goal}.ROI_idx);
  99. if isfield(OptGoals.data{i_goal}, 'wgt_map')
  100. tabula_wgtmap = zeros(size(superMask));
  101. tabula_wgtmap(OptGoals.data{i_goal}.ROI_idx) = OptGoals.data{i_goal}.wgt_map;
  102. end
  103. h_w1 = waitbar(0, 'merging superboxels');
  104. for i_supVox = 1:numel(superVoxList)
  105. waitbar(i_supVox/numel(superVoxList), h_w1)
  106. supVox_idx = superVoxList(i_supVox);
  107. idxList = find(superMask == supVox_idx);
  108. optGoal{i_goal}.beamlets_pruned(i_supVox,:) = sparse(mean(beamlets(idxList, :),1));
  109. if isfield(OptGoals.data{i_goal}, 'wgt_map')
  110. optGoal{i_goal}.vox_wgt(i_supVox) = sum(tabula_wgtmap(idxList));
  111. end
  112. % -- make new indeces
  113. optGoal{i_goal}.ROI_idx(i_supVox) = idxList(1);
  114. end
  115. close(h_w1)
  116. end
  117. end
  118. % -- make them robust --
  119. RO_params=0;
  120. optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
  121. % save([Goal_path, Goal_file '_robust.mat'], 'optGoal')
  122. % -- get beamlet indeces --
  123. load([Pat_path, '\all_beams.mat'])
  124. Nbeamlets = all_beams{1}.Mxp; % number of beamlets in a beam - usually 64 or 32
  125. weightTable = zeros(100,Nbeamlets);
  126. for ind_bmlt = 1:numel(all_beams)
  127. bLet_idx.y(ind_bmlt) = floor(all_beams{1, ind_bmlt}.num/Nbeamlets)+1;
  128. bLet_idx.x(ind_bmlt) = rem(all_beams{1, ind_bmlt}.num, Nbeamlets)+1;
  129. weightTable(bLet_idx.y(ind_bmlt),bLet_idx.x(ind_bmlt)) = 1;
  130. end
  131. bLet_idx.idx = find(weightTable>0);
  132. bLet_idx.Nbeamlets = Nbeamlets;
  133. disp('.')
  134. % -- CALLBACK OPTIMIZATION FUNCTION --
  135. fun1 = @(x) get_penalty(x, optGoal_beam, bLet_idx);
  136. fun2 = @(x) get_penalty(x, optGoal, bLet_idx);
  137. % -- OPTIMIZATION PARAMETERS --
  138. % define optimization parameters
  139. A = [];
  140. b = [];
  141. Aeq = [];
  142. beq = [];
  143. lb = zeros(1, numBeamlet);
  144. % lb_beam = zeros(1, numBeam);
  145. ub = [];
  146. nonlcon = [];
  147. % define opt limits, and make it fmincon progress
  148. options = optimoptions('fmincon');
  149. options.MaxFunctionEvaluations = N_fcallback1;
  150. options.Display = 'iter';
  151. options.PlotFcn = 'optimplotfval';
  152. % options.UseParallel = true;
  153. options.UseParallel = false;
  154. % options.OptimalityTolerance = 1e-9;
  155. %% -- INITIALIZE BEAMLET WEIGHTS --
  156. switch pre_beamWeights
  157. case 'y'
  158. % should have been assigned previously.
  159. disp('Provided beamlet weights used for initial comparison')
  160. case 'n'
  161. % if initial beamlet weights are not provided, get quick estimate
  162. % fprintf('\n running initial optimizer:')
  163. % initialize beamlet weights, OR
  164. w0 = ones(numBeamlet,1);
  165. % w0 = mean(optGoal{1}.D_final(optGoal{1}.ROI_idx) ./ (optGoal{1}.beamlets_pruned*w0+0.1)) * w0; % old
  166. w0 = mean(optGoal{1}.D_final(:)) ./ mean(optGoal{1}.beamlets_pruned*w0+0.05) * w0;
  167. w_beamlets = double(w0);
  168. % -- GET BEAM WEIGHTS --
  169. % tic
  170. % w_beam = fmincon(fun1,w0_beams,A,b,Aeq,beq,lb,ub,nonlcon,options);
  171. % fprintf(' done!:')
  172. % t=toc;
  173. % disp(['Optimization time for beams = ',num2str(t)]);
  174. %
  175. % w_beamlets = ones(numBeamlet,1);
  176. % numBeam=numel(unique(beam_i_list));
  177. % for beam_i = 1:numBeam % assign weights to beamlets
  178. % % beamlets from same beam get same initial weights
  179. % w_beamlets(beam_i_list == beam_i) = w_beam(beam_i);
  180. % end
  181. end
  182. %% FULL OPTIMIZATION
  183. % -- GET FULL BEAMLET WEIGHTS --
  184. options.MaxFunctionEvaluations = N_fcallback2;
  185. tic
  186. fprintf('\n running full optimizer:')
  187. w_fin = fmincon(fun2,w_beamlets,A,b,Aeq,beq,lb,ub,nonlcon,options);
  188. fprintf(' done!:')
  189. t=toc;
  190. disp(['Optimization time for beamlets = ',num2str(t)]);
  191. %% evaluate the results
  192. D_full = reshape(beamlets * w_fin, size(Geometry.data));
  193. %% save outputs
  194. NLP_result.dose = D_full;
  195. NLP_result.weights = w_fin;
  196. NLP_result.sss_scene_list = optGoal{1}.sss_scene_list;
  197. NLP_result.maxModulation = OptGoals.maxModulation;
  198. NLP_result.BeamSmoothMax = OptGoals.BeamSmoothMax;
  199. save([Pat_path, '\matlab_files\NLP_result_' Goal_file '.mat'], 'NLP_result');
  200. plot_DVH(Geometry, D_full)
  201. colorwash(Geometry.data, D_full, [500, 1500], [0, 90]);
  202. end
  203. %% support functions
  204. % ---- PENALTY FUNCTION ----
  205. function penalty = get_penalty(x, optGoal, bLet_idx)
  206. % this function gets called by the optimizer. It checks the penalty for
  207. % all the robust implementation and returns the worst result.
  208. NumScenarios = optGoal{1}.NbrRandScenarios * optGoal{1}.NbrSystSetUpScenarios * optGoal{1}.NbrRangeScenarios;
  209. fobj = zeros(NumScenarios,1);
  210. sc_i = 1;
  211. for nrs_i = 1:optGoal{1}.NbrRandScenarios
  212. for sss_i = 1 :optGoal{1}.NbrSystSetUpScenarios % syst. setup scenarios = sss
  213. for rgs_i = 1:optGoal{1}.NbrRangeScenarios % range scenario = rs
  214. fobj(sc_i)=eval_f(x, optGoal, nrs_i, sss_i, rgs_i, bLet_idx);
  215. sc_i = sc_i + 1;
  216. end
  217. end
  218. end
  219. % take the worst case penalty of evaluated scenarios
  220. penalty=max(fobj);
  221. % take the median worst case (stochastic robust)
  222. % penalty=median(fobj);
  223. end
  224. % ------ supp: penalty for single scenario ------
  225. function penalty = eval_f(x, optGoal, nrs_i, sss_i, rgs_i, bLet_idx)
  226. penalty = 0;
  227. % for each condition
  228. for goal_i = 1:numel(optGoal)
  229. switch optGoal{goal_i}.function
  230. % min, max, min_sq, max_sq, LeastSquare, min_perc_Volume, max_perc_Volume
  231. case 'min'
  232. % penalize if achieved dose is lower than target dose
  233. d_penalty = 1.0e0 * sum(max(0, ...
  234. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target) -...
  235. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)));
  236. case 'max'
  237. % penalize if achieved dose is higher than target dose
  238. d_penalty = 1.0e0 * sum(max(0, ...
  239. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  240. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target)));
  241. case 'min_sq'
  242. % penalize if achieved dose is lower than target dose
  243. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  244. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  245. d_penalty = 1.0e0 * sum(temp1.*temp1);
  246. case 'max_sq'
  247. % penalize if achieved dose is higher than target dose
  248. temp1=max(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  249. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  250. d_penalty = 1.0e0 * sum(temp1.*temp1);
  251. case 'min_step'
  252. % penalize if achieved dose is lower than target dose
  253. temp1=-min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  254. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  255. d_penalty = 1.0e0 * (sum(temp1) + numel(temp1)* any(temp1));
  256. case 'max_step'
  257. % penalize if achieved dose is higher than target dose
  258. temp1=max(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  259. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  260. d_penalty = 1.0e0 * (sum(temp1) + numel(temp1)* any(temp1));
  261. case 'LeastSquare'
  262. % penalize with sum of squares any deviation from target
  263. % dose
  264. temp1 = (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x) - ...
  265. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target;
  266. d_penalty = 1.0e0* sum(temp1.^2);
  267. case 'min_perc_Volume'
  268. % penalize by amount of volume under threshold
  269. perc_vox = numel(find((optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target) -...
  270. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x) > 0)) ...
  271. / numel(optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target);
  272. d_penalty = 3.0e4 * min(perc_vox-0.05, 0)
  273. case 'max_perc_Volume'
  274. % penalize by amount of volume under threshold
  275. perc_vox = numel(find((optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target) -...
  276. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x) < 0)) ...
  277. / numel(optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target);
  278. d_penalty = 3.0e4 * min(perc_vox-0.05, 0)
  279. case 'min_sq_voxwgt'
  280. % penalize if achieved dose is lower than target dose
  281. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  282. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  283. d_penalty = 1.0e0 * sum(temp1.*temp1.* optGoal{goal_i}.vox_wgt');
  284. case 'max_sq_voxwgt'
  285. % penalize if achieved dose is lower than target dose
  286. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  287. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  288. d_penalty = 1.0e0 * sum(temp1.*temp1.* optGoal{goal_i}.vox_wgt');
  289. end
  290. penalty = penalty + d_penalty * optGoal{goal_i}.opt_weight;
  291. end
  292. %% add modulation penalty
  293. if true
  294. if isfield(optGoal{goal_i}, 'maxModulation')
  295. max_modulation = optGoal{goal_i}.maxModulation;
  296. else
  297. error('no Max modulation parameter enterd!')
  298. max_modulation = 5;
  299. end
  300. mod_pen_weight = 1.0e10;
  301. % calc the penalty
  302. mod_excess = max(0, x-max_modulation*mean(x));
  303. % mod_excess = max(0, x-max_modulation*mean(x(x>1)));
  304. mod_pen1 = mod_pen_weight*(sum(mod_excess) + numel(mod_excess)* any(mod_excess));
  305. penalty = penalty + mod_pen1;
  306. end
  307. %% add penlty for single off beamlets - version 2
  308. if false
  309. if isfield(optGoal{goal_i}, 'BeamSmoothMax')
  310. BeamSmoothMax = optGoal{goal_i}.BeamSmoothMax;
  311. else
  312. error('no Max beam smooth parameter enterd!')
  313. BeamSmoothMax = 1e6;
  314. end
  315. mod_pen_weight = BeamSmoothMax; %1.0e6
  316. x_down = zeros(size(x));
  317. x_down(2:end) = x(1:end-1);
  318. x_down2 = zeros(size(x));
  319. x_down2(3:end) = x(1:end-2);
  320. x_up = zeros(size(x));
  321. x_up(1:end-1) = x(2:end);
  322. x_up2 = zeros(size(x));
  323. x_up2(1:end-2) = x(3:end);
  324. mod_pen2 = mod_pen_weight*sum((x_down+x_up-2*x).^2 + 0.5*(x_down2+x_up2-2*x).^2)/(mean(x)^(2)*numel(x));
  325. penalty = penalty + mod_pen2;
  326. end
  327. %% add penlty for single off beamlets - version 3
  328. if true
  329. if isfield(optGoal{goal_i}, 'BeamSmoothMax')
  330. BeamSmoothMax = optGoal{goal_i}.BeamSmoothMax;
  331. else
  332. error('no Max beam smooth parameter enterd!')
  333. BeamSmoothMax = 1e6;
  334. end
  335. mod_pen_weight = BeamSmoothMax; %1.0e6
  336. % make 2D beamletWeight map
  337. maxY = max(bLet_idx.y);
  338. tabula = zeros(maxY, bLet_idx.Nbeamlets);
  339. tabula(bLet_idx.idx) = x;
  340. % for each index calculate laplace
  341. myWeights = 1/12*[1,3,1; 1,-12,1; 1,3,1];
  342. i=2:maxY-1;
  343. j=2:bLet_idx.Nbeamlets-1;
  344. tabula2(i,j) = ...
  345. myWeights(1)*tabula(i-1,j-1) + myWeights(4)*tabula(i-1,j) + myWeights(7)*tabula(i-1,j+1)...
  346. +myWeights(2)*tabula(i,j-1) + myWeights(8)*tabula(i,j+1) ...
  347. +myWeights(3)*tabula(i+1,j-1) + myWeights(6)*tabula(i+1,j) + myWeights(9)*tabula(i+1,j+1)...
  348. +myWeights(5)*tabula(i,j);
  349. tabula2(1,j) = ...
  350. myWeights(2)*tabula(1,j-1) + myWeights(8)*tabula(1,j+1)...
  351. +myWeights(3)*tabula(2,j-1) + myWeights(6)*tabula(2,j) + myWeights(9)*tabula(2,j+1)...
  352. +myWeights(5)*tabula(1,j);
  353. tabula2(maxY,j) =...
  354. myWeights(1)*tabula(maxY-1,j-1) + myWeights(4)*tabula(maxY-1,j) + myWeights(7)*tabula(maxY-1,j+1)...
  355. +myWeights(2)*tabula(maxY,j-1) + myWeights(8)*tabula(maxY,j+1) ...
  356. +myWeights(5)*tabula(maxY,j);
  357. % make sum of squares
  358. mod_pen2 = mod_pen_weight*sum((tabula2(:)).^2)/(mean(x.^2)*numel(x));
  359. penalty = penalty + mod_pen2;
  360. end
  361. end
  362. % ---- MAKE ROI ROBUST ----
  363. function optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
  364. % take regular optimal goal and translate it into several robust cases
  365. % RO_params - should have the information below
  366. % nrs - random scenarios
  367. % sss - system setup scenarios
  368. % rgs - random range scenarios
  369. % X - X>0 moves image right
  370. % Y - Y>0 moves image down
  371. % Z - in/out.
  372. shift_X = 2; % vox of shift
  373. shift_Y = 2; % vox of shift
  374. shift_Z = 1; % vox of shift
  375. nrs_scene_list={[0,0,0]};
  376. % ----====#### CHANGE ROBUSTNESS HERE ####====----
  377. if isfield(optGoal{1}, 'sss_scene_list')
  378. sss_scene_list = optGoal{1}.sss_scene_list;
  379. else
  380. sss_scene_list={[0,0,0], [-shift_Y,0,0], [shift_Y,0,0], [0,-shift_X,0], [0,shift_X,0], [0,0,-shift_Z], [0,0,shift_Z]};
  381. optGoal{1}.sss_scene_list = sss_scene_list;
  382. end
  383. % sss_scene_list={[0,0,0]};
  384. % ----====#### CHANGE ROBUSTNESS HERE ####====----
  385. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
  386. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\PD_HD_dicomPhantom\Tomo_DP_target.nrrd');
  387. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
  388. rgs_scene_list={[0,0,0]};
  389. for i = 1:numel(optGoal)
  390. optGoal{i}.NbrRandScenarios =numel(nrs_scene_list);
  391. optGoal{i}.NbrSystSetUpScenarios=numel(sss_scene_list);
  392. optGoal{i}.NbrRangeScenarios =numel(rgs_scene_list);
  393. end
  394. for goal_i = 1:numel(optGoal)
  395. % get target
  396. idx=optGoal{goal_i}.ROI_idx;
  397. targetImg1=zeros(optGoal{goal_i}.imgDim);
  398. targetImg1(idx)=1;
  399. % get beamlets
  400. for nrs_i = 1:optGoal{goal_i}.NbrRandScenarios % num. of random scenarios
  401. % modify target and beamlets
  402. targetImg2=targetImg1;
  403. % beamlets stay the same
  404. for sss_i = 1 :optGoal{goal_i}.NbrSystSetUpScenarios % syst. setup scenarios = sss
  405. % modify target and beamlets
  406. [targetImg3 idxValid]=get_RO_sss(targetImg2, sss_scene_list{sss_i});
  407. % beamlets stay the same
  408. for rgs_i = 1:optGoal{goal_i}.NbrRangeScenarios % range scenario = rgs
  409. % modify target and beamlets
  410. targetImg4=targetImg3;
  411. % beamlets stay the same
  412. %% make new target and beamlets
  413. ROI_idx=[];
  414. ROI_idx=find(targetImg4>0);
  415. target = optGoal{goal_i}.D_final(idxValid);
  416. beamlets_pruned = beamlets(ROI_idx, :);
  417. % save to optGoal output
  418. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.ROI_idx = ROI_idx;
  419. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned = beamlets_pruned;
  420. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target = target;
  421. end
  422. end
  423. end
  424. end
  425. end
  426. %% ------ supp: RO case SSS ------
  427. function [targetImg3 ia]=get_RO_sss(targetImg2, sss_scene_shift);
  428. % translate the target image
  429. targetImg3 = imtranslate(targetImg2,sss_scene_shift);
  430. % now we need to figure out if any target voxels fell out during the
  431. % shift
  432. imgValid = imtranslate(targetImg3,-sss_scene_shift);
  433. imgInvalid = (targetImg2-imgValid);
  434. idx_1 = find(targetImg2);
  435. idx_2 = find(imgInvalid);
  436. [idxValid,ia] = setdiff(idx_1,idx_2);
  437. [C,ia, ib] = intersect(idx_1,idxValid);
  438. end