NLP_optimizer_v3.m 23 KB

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