NLP_optimizer_v3.m 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 = 'yes'
  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 = 1e6; % 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. optGoal{i_goal}.optFuncNum = OptGoals.optFuncNum;
  83. % modulation
  84. switch SupVox_num
  85. case 0
  86. % if not supervoxel, just select provided ROI_idx
  87. optGoal{i_goal}.beamlets_pruned = sparse(beamlets(optGoal{i_goal}.ROI_idx, :));
  88. otherwise
  89. % -- if supervoxel, merge given columns
  90. % - make supervoxel map
  91. mask = zeros(OptGoals.data{i_goal}.imgDim);
  92. mask(OptGoals.data{i_goal}.ROI_idx) = 1;
  93. % group superpixels
  94. superMask = superpix_group(mask, SupVox_num, 'no');
  95. superVoxList = unique(superMask);
  96. superVoxList = superVoxList(superVoxList>0);
  97. optGoal{i_goal}.ROI_idx_old = optGoal{i_goal}.ROI_idx; % copy old index data
  98. optGoal{i_goal}.ROI_idx = zeros(numel(superVoxList), 1);
  99. optGoal{i_goal}.opt_weight = optGoal{i_goal}.opt_weight * numel(optGoal{i_goal}.ROI_idx_old)/numel(optGoal{i_goal}.ROI_idx);
  100. if isfield(OptGoals.data{i_goal}, 'wgt_map')
  101. tabula_wgtmap = zeros(size(superMask));
  102. tabula_wgtmap(OptGoals.data{i_goal}.ROI_idx) = OptGoals.data{i_goal}.wgt_map;
  103. end
  104. h_w1 = waitbar(0, 'merging superboxels');
  105. for i_supVox = 1:numel(superVoxList)
  106. waitbar(i_supVox/numel(superVoxList), h_w1)
  107. supVox_idx = superVoxList(i_supVox);
  108. idxList = find(superMask == supVox_idx);
  109. optGoal{i_goal}.beamlets_pruned(i_supVox,:) = sparse(mean(beamlets(idxList, :),1));
  110. if isfield(OptGoals.data{i_goal}, 'wgt_map')
  111. optGoal{i_goal}.vox_wgt(i_supVox) = sum(tabula_wgtmap(idxList));
  112. end
  113. % -- make new indeces
  114. optGoal{i_goal}.ROI_idx(i_supVox) = idxList(1);
  115. end
  116. close(h_w1)
  117. end
  118. end
  119. % -- make them robust --
  120. RO_params=0;
  121. optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
  122. % save([Goal_path, Goal_file '_robust.mat'], 'optGoal')
  123. % -- get beamlet indeces --
  124. load([Pat_path, '\all_beams.mat'])
  125. Nbeamlets = all_beams{1}.Mxp; % number of beamlets in a beam - usually 64 or 32
  126. weightTable = zeros(100,Nbeamlets);
  127. for ind_bmlt = 1:numel(all_beams)
  128. bLet_idx.y(ind_bmlt) = floor(all_beams{1, ind_bmlt}.num/Nbeamlets)+1;
  129. bLet_idx.x(ind_bmlt) = rem(all_beams{1, ind_bmlt}.num, Nbeamlets)+1;
  130. weightTable(bLet_idx.y(ind_bmlt),bLet_idx.x(ind_bmlt)) = 1;
  131. end
  132. bLet_idx.idx = find(weightTable>0);
  133. bLet_idx.Nbeamlets = Nbeamlets;
  134. disp('.')
  135. % -- CALLBACK OPTIMIZATION FUNCTION --
  136. fun1 = @(x) get_penalty(x, optGoal_beam, bLet_idx);
  137. fun2 = @(x) get_penalty(x, optGoal, bLet_idx);
  138. % -- OPTIMIZATION PARAMETERS --
  139. % define optimization parameters
  140. A = [];
  141. b = [];
  142. Aeq = [];
  143. beq = [];
  144. lb = zeros(1, numBeamlet);
  145. % lb_beam = zeros(1, numBeam);
  146. ub = [];
  147. nonlcon = [];
  148. % define opt limits, and make it fmincon progress
  149. options = optimoptions('fmincon');
  150. options.MaxFunctionEvaluations = N_fcallback1;
  151. options.Display = 'iter';
  152. options.PlotFcn = 'optimplotfval';
  153. % options.UseParallel = true;
  154. options.UseParallel = false;
  155. % options.OptimalityTolerance = 1e-9;
  156. %% -- INITIALIZE BEAMLET WEIGHTS --
  157. switch pre_beamWeights
  158. case 'y'
  159. % should have been assigned previously.
  160. disp('Provided beamlet weights used for initial comparison')
  161. case 'n'
  162. % if initial beamlet weights are not provided, get quick estimate
  163. % fprintf('\n running initial optimizer:')
  164. % initialize beamlet weights, OR
  165. w0 = ones(numBeamlet,1);
  166. % w0 = mean(optGoal{1}.D_final(optGoal{1}.ROI_idx) ./ (optGoal{1}.beamlets_pruned*w0+0.1)) * w0; % old
  167. w0 = mean(optGoal{1}.D_final(:)) ./ mean(optGoal{1}.beamlets_pruned*w0+0.05) * w0;
  168. w_beamlets = double(w0);
  169. % -- GET BEAM WEIGHTS --
  170. % tic
  171. % w_beam = fmincon(fun1,w0_beams,A,b,Aeq,beq,lb,ub,nonlcon,options);
  172. % fprintf(' done!:')
  173. % t=toc;
  174. % disp(['Optimization time for beams = ',num2str(t)]);
  175. %
  176. % w_beamlets = ones(numBeamlet,1);
  177. % numBeam=numel(unique(beam_i_list));
  178. % for beam_i = 1:numBeam % assign weights to beamlets
  179. % % beamlets from same beam get same initial weights
  180. % w_beamlets(beam_i_list == beam_i) = w_beam(beam_i);
  181. % end
  182. end
  183. %% FULL OPTIMIZATION
  184. % -- GET FULL BEAMLET WEIGHTS --
  185. options.MaxFunctionEvaluations = N_fcallback2;
  186. tic
  187. fprintf('\n running full optimizer:')
  188. w_fin = fmincon(fun2,w_beamlets,A,b,Aeq,beq,lb,ub,nonlcon,options);
  189. fprintf(' done!:')
  190. t=toc;
  191. disp(['Optimization time for beamlets = ',num2str(t)]);
  192. %% evaluate the results
  193. D_full = reshape(beamlets * w_fin, size(Geometry.data));
  194. %% save outputs
  195. NLP_result.dose = D_full;
  196. NLP_result.weights = w_fin;
  197. NLP_result.sss_scene_list = optGoal{1}.sss_scene_list;
  198. NLP_result.maxModulation = OptGoals.maxModulation;
  199. NLP_result.BeamSmoothMax = OptGoals.BeamSmoothMax;
  200. save([Pat_path, '\matlab_files\NLP_result_' Goal_file '.mat'], 'NLP_result');
  201. plot_DVH(Geometry, D_full)
  202. colorwash(Geometry.data, D_full, [500, 1500], [0, 90]);
  203. end
  204. %% support functions
  205. % ---- PENALTY FUNCTION ----
  206. function penalty = get_penalty(x, optGoal, bLet_idx)
  207. % this function gets called by the optimizer. It checks the penalty for
  208. % all the robust implementation and returns the worst result.
  209. NumScenarios = optGoal{1}.NbrRandScenarios * optGoal{1}.NbrSystSetUpScenarios * optGoal{1}.NbrRangeScenarios;
  210. fobj = zeros(NumScenarios,numel(optGoal)+2);
  211. sc_i = 1;
  212. for nrs_i = 1:optGoal{1}.NbrRandScenarios
  213. for sss_i = 1 :optGoal{1}.NbrSystSetUpScenarios % syst. setup scenarios = sss
  214. for rgs_i = 1:optGoal{1}.NbrRangeScenarios % range scenario = rs
  215. % fobj(sc_i)=eval_f(x, optGoal, nrs_i, sss_i, rgs_i, bLet_idx);
  216. fobj(sc_i,:) = eval_f(x, optGoal, nrs_i, sss_i, rgs_i, bLet_idx);
  217. sc_i = sc_i + 1;
  218. end
  219. end
  220. end
  221. switch optGoal{1}.optFuncNum
  222. case 1 % "RO max"
  223. penalty=max(sum(fobj,2));
  224. case 2 % "RO mean"
  225. penalty=mean(sum(fobj,2));
  226. case 3 % "RO objective-based"
  227. penalty=sum(max(fobj,[],1));
  228. case 4 % "Classical"
  229. penalty=sum(fobj(1,:));
  230. end
  231. % take the worst case penalty of evaluated scenarios
  232. % penalty=max(fobj);
  233. % take the median worst case (stochastic robust)
  234. % penalty=median(fobj);
  235. end
  236. % ------ supp: penalty for single scenario ------
  237. function penArray = eval_f(x, optGoal, nrs_i, sss_i, rgs_i, bLet_idx)
  238. % penalty = 0;
  239. penArray = zeros(1,numel(optGoal)+2); % all the optGoals, plus modulation plus smoothing
  240. % for each condition
  241. for goal_i = 1:numel(optGoal)
  242. switch optGoal{goal_i}.function
  243. % min, max, min_sq, max_sq, LeastSquare, min_perc_Volume, max_perc_Volume
  244. case 'min'
  245. % penalize if achieved dose is lower than target dose
  246. d_penalty = 1.0e0 * sum(max(0, ...
  247. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target) -...
  248. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)));
  249. case 'max'
  250. % penalize if achieved dose is higher than target dose
  251. d_penalty = 1.0e0 * sum(max(0, ...
  252. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  253. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target)));
  254. case 'min_sq'
  255. % penalize if achieved dose is lower than target dose
  256. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  257. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  258. d_penalty = 1.0e0 * sum(temp1.*temp1);
  259. case 'max_sq'
  260. % penalize if achieved dose is higher than target dose
  261. temp1=max(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  262. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  263. d_penalty = 1.0e0 * sum(temp1.*temp1);
  264. case 'min_step'
  265. % penalize if achieved dose is lower than target dose
  266. temp1=-min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  267. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  268. d_penalty = 1.0e1 * (sum(temp1.*temp1) + 1.0e3* sum(temp1>0)/numel(temp1));
  269. case 'max_step'
  270. % penalize if achieved dose is higher than target dose
  271. temp1=max(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  272. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  273. d_penalty = 1.0e1 * (sum(temp1.*temp1) + 1.0e3* sum(temp1>0)/numel(temp1));
  274. case 'LeastSquare'
  275. % penalize with sum of squares any deviation from target
  276. % dose
  277. temp1 = (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x) - ...
  278. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target;
  279. d_penalty = 1.0e0* sum(temp1.^2);
  280. case 'min_perc_Volume'
  281. % penalize by amount of volume under threshold
  282. perc_vox = numel(find((optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target) -...
  283. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x) > 0)) ...
  284. / numel(optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target);
  285. d_penalty = 3.0e4 * min(perc_vox-0.05, 0)
  286. case 'max_perc_Volume'
  287. % penalize by amount of volume under threshold
  288. perc_vox = numel(find((optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target) -...
  289. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x) < 0)) ...
  290. / numel(optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target);
  291. d_penalty = 3.0e4 * min(perc_vox-0.05, 0)
  292. case 'min_sq_voxwgt'
  293. % penalize if achieved dose is lower than target dose
  294. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  295. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  296. d_penalty = 1.0e0 * sum(temp1.*temp1.* optGoal{goal_i}.vox_wgt');
  297. case 'max_sq_voxwgt'
  298. % penalize if achieved dose is lower than target dose
  299. temp1=min(0, (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned * x)-...
  300. (optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target));
  301. d_penalty = 1.0e0 * sum(temp1.*temp1.* optGoal{goal_i}.vox_wgt');
  302. end
  303. % penalty = penalty + d_penalty * optGoal{goal_i}.opt_weight;
  304. penArray(goal_i) = d_penalty * optGoal{goal_i}.opt_weight;
  305. end
  306. %% add modulation penalty
  307. if true
  308. if isfield(optGoal{goal_i}, 'maxModulation')
  309. max_modulation = optGoal{goal_i}.maxModulation;
  310. else
  311. error('no Max modulation parameter enterd!')
  312. max_modulation = 5;
  313. end
  314. mod_pen_weight = 1.0e10;
  315. % calc the penalty
  316. mod_excess = max(0, x-max_modulation*mean(x));
  317. % mod_excess = max(0, x-max_modulation*mean(x(x>1)));
  318. mod_pen1 = mod_pen_weight*(sum(mod_excess) + numel(mod_excess)* any(mod_excess));
  319. % penalty = penalty + mod_pen1;
  320. penArray(end-1) = mod_pen1;
  321. end
  322. %% add penlty for single off beamlets - version 2
  323. if false
  324. if isfield(optGoal{goal_i}, 'BeamSmoothMax')
  325. BeamSmoothMax = optGoal{goal_i}.BeamSmoothMax;
  326. else
  327. error('no Max beam smooth parameter enterd!')
  328. BeamSmoothMax = 1e6;
  329. end
  330. mod_pen_weight = BeamSmoothMax; %1.0e6
  331. x_down = zeros(size(x));
  332. x_down(2:end) = x(1:end-1);
  333. x_down2 = zeros(size(x));
  334. x_down2(3:end) = x(1:end-2);
  335. x_up = zeros(size(x));
  336. x_up(1:end-1) = x(2:end);
  337. x_up2 = zeros(size(x));
  338. x_up2(1:end-2) = x(3:end);
  339. 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));
  340. % penalty = penalty + mod_pen2;
  341. penArray(end) = mod_pen2;
  342. end
  343. %% add penlty for single off beamlets - version 3
  344. if true
  345. if isfield(optGoal{goal_i}, 'BeamSmoothMax')
  346. BeamSmoothMax = optGoal{goal_i}.BeamSmoothMax;
  347. else
  348. error('no Max beam smooth parameter enterd!')
  349. BeamSmoothMax = 1e6;
  350. end
  351. mod_pen_weight = BeamSmoothMax; %1.0e6
  352. % make 2D beamletWeight map
  353. maxY = max(bLet_idx.y);
  354. tabula = zeros(maxY, bLet_idx.Nbeamlets);
  355. tabula(bLet_idx.idx) = x;
  356. % for each index calculate laplace
  357. myWeights = 1/12*[1,3,1; 1,-12,1; 1,3,1];
  358. i=2:maxY-1;
  359. j=2:bLet_idx.Nbeamlets-1;
  360. tabula2(i,j) = ...
  361. myWeights(1)*tabula(i-1,j-1) + myWeights(4)*tabula(i-1,j) + myWeights(7)*tabula(i-1,j+1)...
  362. +myWeights(2)*tabula(i,j-1) + myWeights(8)*tabula(i,j+1) ...
  363. +myWeights(3)*tabula(i+1,j-1) + myWeights(6)*tabula(i+1,j) + myWeights(9)*tabula(i+1,j+1)...
  364. +myWeights(5)*tabula(i,j);
  365. tabula2(1,j) = ...
  366. myWeights(2)*tabula(1,j-1) + myWeights(8)*tabula(1,j+1)...
  367. +myWeights(3)*tabula(2,j-1) + myWeights(6)*tabula(2,j) + myWeights(9)*tabula(2,j+1)...
  368. +myWeights(5)*tabula(1,j);
  369. tabula2(maxY,j) =...
  370. myWeights(1)*tabula(maxY-1,j-1) + myWeights(4)*tabula(maxY-1,j) + myWeights(7)*tabula(maxY-1,j+1)...
  371. +myWeights(2)*tabula(maxY,j-1) + myWeights(8)*tabula(maxY,j+1) ...
  372. +myWeights(5)*tabula(maxY,j);
  373. % make sum of squares
  374. mod_pen2 = mod_pen_weight*sum((tabula2(:)).^2)/(mean(x.^2)*numel(x));
  375. % penalty = penalty + mod_pen2;
  376. penArray(end) = mod_pen2;
  377. end
  378. end
  379. % ---- MAKE ROI ROBUST ----
  380. function optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
  381. % take regular optimal goal and translate it into several robust cases
  382. % RO_params - should have the information below
  383. % nrs - random scenarios
  384. % sss - system setup scenarios
  385. % rgs - random range scenarios
  386. % X - X>0 moves image right
  387. % Y - Y>0 moves image down
  388. % Z - in/out.
  389. shift_X = 2; % vox of shift
  390. shift_Y = 2; % vox of shift
  391. shift_Z = 1; % vox of shift
  392. nrs_scene_list={[0,0,0]};
  393. % ----====#### CHANGE ROBUSTNESS HERE ####====----
  394. if isfield(optGoal{1}, 'sss_scene_list')
  395. sss_scene_list = optGoal{1}.sss_scene_list;
  396. else
  397. error('New OptGoals should no longer reach this code!')
  398. 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]};
  399. optGoal{1}.sss_scene_list = sss_scene_list;
  400. end
  401. % sss_scene_list={[0,0,0]};
  402. % ----====#### CHANGE ROBUSTNESS HERE ####====----
  403. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
  404. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\PD_HD_dicomPhantom\Tomo_DP_target.nrrd');
  405. % [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
  406. rgs_scene_list={[0,0,0]};
  407. for i = 1:numel(optGoal)
  408. optGoal{i}.NbrRandScenarios =numel(nrs_scene_list);
  409. optGoal{i}.NbrSystSetUpScenarios=numel(sss_scene_list);
  410. optGoal{i}.NbrRangeScenarios =numel(rgs_scene_list);
  411. end
  412. for goal_i = 1:numel(optGoal)
  413. % get target
  414. idx=optGoal{goal_i}.ROI_idx;
  415. targetImg1=zeros(optGoal{goal_i}.imgDim);
  416. targetImg1(idx)=1;
  417. % get beamlets
  418. for nrs_i = 1:optGoal{goal_i}.NbrRandScenarios % num. of random scenarios
  419. % modify target and beamlets
  420. targetImg2=targetImg1;
  421. % beamlets stay the same
  422. for sss_i = 1 :optGoal{goal_i}.NbrSystSetUpScenarios % syst. setup scenarios = sss
  423. % modify target and beamlets
  424. [targetImg3 idxValid]=get_RO_sss(targetImg2, sss_scene_list{sss_i});
  425. % beamlets stay the same
  426. for rgs_i = 1:optGoal{goal_i}.NbrRangeScenarios % range scenario = rgs
  427. % modify target and beamlets
  428. targetImg4=targetImg3;
  429. % beamlets stay the same
  430. %% make new target and beamlets
  431. ROI_idx=[];
  432. ROI_idx=find(targetImg4>0);
  433. target = optGoal{goal_i}.D_final(idxValid);
  434. beamlets_pruned = beamlets(ROI_idx, :);
  435. % save to optGoal output
  436. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.ROI_idx = ROI_idx;
  437. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.beamlets_pruned = beamlets_pruned;
  438. optGoal{goal_i}.nrs{nrs_i}.sss{sss_i}.rgs{rgs_i}.target = target;
  439. end
  440. end
  441. end
  442. end
  443. end
  444. %% ------ supp: RO case SSS ------
  445. function [targetImg3 ia]=get_RO_sss(targetImg2, sss_scene_shift);
  446. % translate the target image
  447. targetImg3 = imtranslate(targetImg2,sss_scene_shift);
  448. % now we need to figure out if any target voxels fell out during the
  449. % shift
  450. imgValid = imtranslate(targetImg3,-sss_scene_shift);
  451. imgInvalid = (targetImg2-imgValid);
  452. idx_1 = find(targetImg2);
  453. idx_2 = find(imgInvalid);
  454. [idxValid,ia] = setdiff(idx_1,idx_2);
  455. [C,ia, ib] = intersect(idx_1,idxValid);
  456. end