|  | @@ -39,11 +39,21 @@ else
 | 
	
		
			
				|  |  |      [Goal_path,Goal_file,ext] = fileparts(path2goal);
 | 
	
		
			
				|  |  |  end
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -str = inputdlg({'N of iterations for initial calc', 'N of iterations for full calc', ...
 | 
	
		
			
				|  |  | -    'Use pre-existing NLP_result to initiate? (y/n)'}, 'input', [1,35], {'100000', '500000', 'n'});
 | 
	
		
			
				|  |  | -N_fcallback1 = str2double(str{1}); % 100000  is a good guesstimate
 | 
	
		
			
				|  |  | -N_fcallback2 = str2double(str{2}); % 500000 is a good guesstimate
 | 
	
		
			
				|  |  | -pre_beamWeights = str{3};
 | 
	
		
			
				|  |  | +dialogue_box = 'no'
 | 
	
		
			
				|  |  | +switch dialogue_box
 | 
	
		
			
				|  |  | +    case 'yes'
 | 
	
		
			
				|  |  | +        str = inputdlg({'N of iterations for initial calc', 'N of iterations for full calc', ...
 | 
	
		
			
				|  |  | +            'Use pre-existing NLP_result to initiate? (y/n)'}, 'input', [1,35], {'100000', '500000', 'n'});
 | 
	
		
			
				|  |  | +        N_fcallback1 = str2double(str{1}); % 100000  is a good guesstimate
 | 
	
		
			
				|  |  | +        N_fcallback2 = str2double(str{2}); % 500000 is a good guesstimate
 | 
	
		
			
				|  |  | +        pre_beamWeights = str{3};
 | 
	
		
			
				|  |  | +    case 'no'
 | 
	
		
			
				|  |  | +        disp('dialogue box skipped')
 | 
	
		
			
				|  |  | +        N_fcallback1 = 100000;
 | 
	
		
			
				|  |  | +        N_fcallback2 = 500000;
 | 
	
		
			
				|  |  | +        pre_beamWeights = 'n';
 | 
	
		
			
				|  |  | +end
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  switch pre_beamWeights
 | 
	
		
			
				|  |  |      case 'y'
 | 
	
	
		
			
				|  | @@ -73,16 +83,23 @@ load(path2goal)
 | 
	
		
			
				|  |  |  %% -- OPTIMIZATION TARGETS --
 | 
	
		
			
				|  |  |  % -- make the optimization optGoal structure --
 | 
	
		
			
				|  |  |  for i_goal = 1:size(OptGoals.goals,1)
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |      if isfield(OptGoals.data{i_goal}, 'SupVox_num')
 | 
	
		
			
				|  |  |          SupVox_num = OptGoals.data{i_goal}.SupVox_num;
 | 
	
		
			
				|  |  |      else
 | 
	
		
			
				|  |  |          answer = inputdlg(['# of supervoxels for "' OptGoals.data{i_goal}.name '" with ' num2str(numel(OptGoals.data{i_goal}.ROI_idx)) ' vox: ("0" to skip)'])
 | 
	
		
			
				|  |  |          SupVox_num = str2double(answer{1})
 | 
	
		
			
				|  |  |      end
 | 
	
		
			
				|  |  | +    optGoal{i_goal} = OptGoals.data{i_goal};
 | 
	
		
			
				|  |  | +    optGoal{i_goal}.sss_scene_list = OptGoals.sss_scene_list;
 | 
	
		
			
				|  |  | +    optGoal{i_goal}.maxModulation = OptGoals.maxModulation;
 | 
	
		
			
				|  |  | +    optGoal{i_goal}.BeamSmoothMax = OptGoals.BeamSmoothMax;
 | 
	
		
			
				|  |  | +    
 | 
	
		
			
				|  |  | +    % modulation
 | 
	
		
			
				|  |  |      switch SupVox_num
 | 
	
		
			
				|  |  |          case 0
 | 
	
		
			
				|  |  |          % if not supervoxel, just select provided ROI_idx
 | 
	
		
			
				|  |  | -        optGoal{i_goal} = OptGoals.data{i_goal};
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          optGoal{i_goal}.beamlets_pruned = sparse(beamlets(optGoal{i_goal}.ROI_idx, :));
 | 
	
		
			
				|  |  |      
 | 
	
		
			
				|  |  |          otherwise
 | 
	
	
		
			
				|  | @@ -90,11 +107,12 @@ for i_goal = 1:size(OptGoals.goals,1)
 | 
	
		
			
				|  |  |          % - make supervoxel map
 | 
	
		
			
				|  |  |          mask = zeros(OptGoals.data{i_goal}.imgDim);
 | 
	
		
			
				|  |  |          mask(OptGoals.data{i_goal}.ROI_idx) = 1;
 | 
	
		
			
				|  |  | -        superMask = superpix_group(mask, SupVox_num); 
 | 
	
		
			
				|  |  | +        % group superpixels
 | 
	
		
			
				|  |  | +        superMask = superpix_group(mask, SupVox_num, 'no'); 
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          superVoxList = unique(superMask);
 | 
	
		
			
				|  |  |          superVoxList = superVoxList(superVoxList>0);
 | 
	
		
			
				|  |  |          
 | 
	
		
			
				|  |  | -        optGoal{i_goal} = OptGoals.data{i_goal};
 | 
	
		
			
				|  |  |          optGoal{i_goal}.ROI_idx_old = optGoal{i_goal}.ROI_idx; % copy old index data
 | 
	
		
			
				|  |  |          optGoal{i_goal}.ROI_idx = zeros(numel(superVoxList), 1);
 | 
	
		
			
				|  |  |          optGoal{i_goal}.opt_weight = optGoal{i_goal}.opt_weight * numel(optGoal{i_goal}.ROI_idx_old)/numel(optGoal{i_goal}.ROI_idx);
 | 
	
	
		
			
				|  | @@ -203,6 +221,7 @@ D_full = reshape(beamlets * w_fin, size(Geometry.data));
 | 
	
		
			
				|  |  |  %% save outputs
 | 
	
		
			
				|  |  |  NLP_result.dose = D_full;
 | 
	
		
			
				|  |  |  NLP_result.weights = w_fin;
 | 
	
		
			
				|  |  | +NLP_result.sss_scene_list = optGoal{1}.sss_scene_list;
 | 
	
		
			
				|  |  |  save([Pat_path, '\matlab_files\NLP_result_' Goal_file '.mat'], 'NLP_result');
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -222,6 +241,8 @@ function penalty = get_penalty(x, optGoal)
 | 
	
		
			
				|  |  |      fobj = zeros(NumScenarios,1);  
 | 
	
		
			
				|  |  |      sc_i = 1;
 | 
	
		
			
				|  |  |      
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    
 | 
	
		
			
				|  |  |      for nrs_i = 1:optGoal{1}.NbrRandScenarios 
 | 
	
		
			
				|  |  |          for sss_i = 1 :optGoal{1}.NbrSystSetUpScenarios % syst. setup scenarios = sss
 | 
	
		
			
				|  |  |              for rgs_i = 1:optGoal{1}.NbrRangeScenarios % range scenario = rs
 | 
	
	
		
			
				|  | @@ -311,12 +332,54 @@ function penalty = eval_f(x, optGoal, nrs_i, sss_i, rgs_i)
 | 
	
		
			
				|  |  |      
 | 
	
		
			
				|  |  |      %% add modulation penalty
 | 
	
		
			
				|  |  |      if true
 | 
	
		
			
				|  |  | -        max_modulation = 5;
 | 
	
		
			
				|  |  | +        if isfield(optGoal{goal_i}, 'maxModulation')
 | 
	
		
			
				|  |  | +            max_modulation = optGoal{goal_i}.maxModulation;
 | 
	
		
			
				|  |  | +        else
 | 
	
		
			
				|  |  | +            error('no Max modulation parameter enterd!')
 | 
	
		
			
				|  |  | +            max_modulation = 5;
 | 
	
		
			
				|  |  | +        end
 | 
	
		
			
				|  |  | +        
 | 
	
		
			
				|  |  |          mod_pen_weight = 1.0e10;
 | 
	
		
			
				|  |  |          % calc the penalty
 | 
	
		
			
				|  |  | -        mod_excess = max(0, x-5*mean(x));
 | 
	
		
			
				|  |  | -        mod_pen = mod_pen_weight*(sum(mod_excess) + numel(mod_excess)* any(mod_excess));
 | 
	
		
			
				|  |  | -        penalty = penalty + mod_pen;
 | 
	
		
			
				|  |  | +        mod_excess = max(0, x-max_modulation*mean(x));
 | 
	
		
			
				|  |  | +        mod_pen1 = mod_pen_weight*(sum(mod_excess) + numel(mod_excess)* any(mod_excess));
 | 
	
		
			
				|  |  | +        penalty = penalty + mod_pen1;
 | 
	
		
			
				|  |  | +    end
 | 
	
		
			
				|  |  | +    %% add penlty for single off beamlets - version 1
 | 
	
		
			
				|  |  | +    if false
 | 
	
		
			
				|  |  | +        if isfield(optGoal{goal_i}, 'BeamSmoothMax')
 | 
	
		
			
				|  |  | +            BeamSmoothMax = optGoal{goal_i}.BeamSmoothMax;
 | 
	
		
			
				|  |  | +        else
 | 
	
		
			
				|  |  | +            error('no Max beam smooth parameter enterd!')
 | 
	
		
			
				|  |  | +            BeamSmoothMax = 1e6;
 | 
	
		
			
				|  |  | +        end
 | 
	
		
			
				|  |  | +        mod_pen_weight = BeamSmoothMax; %1.0e6
 | 
	
		
			
				|  |  | +        x_down = zeros(size(x));
 | 
	
		
			
				|  |  | +        x_down(2:end) = x(1:end-1);
 | 
	
		
			
				|  |  | +        x_up = zeros(size(x));
 | 
	
		
			
				|  |  | +        x_up(1:end-1) = x(2:end);
 | 
	
		
			
				|  |  | +        mod_pen2 = mod_pen_weight*sum(((x-x_down).*(x-x_up)).^2)/(mean(x)^(2*2)*numel(x));
 | 
	
		
			
				|  |  | +        penalty = penalty + mod_pen2;
 | 
	
		
			
				|  |  | +    end
 | 
	
		
			
				|  |  | +    %% add penlty for single off beamlets - version 2
 | 
	
		
			
				|  |  | +    if true
 | 
	
		
			
				|  |  | +        if isfield(optGoal{goal_i}, 'BeamSmoothMax')
 | 
	
		
			
				|  |  | +            BeamSmoothMax = optGoal{goal_i}.BeamSmoothMax;
 | 
	
		
			
				|  |  | +        else
 | 
	
		
			
				|  |  | +            error('no Max beam smooth parameter enterd!')
 | 
	
		
			
				|  |  | +            BeamSmoothMax = 1e6;
 | 
	
		
			
				|  |  | +        end
 | 
	
		
			
				|  |  | +        mod_pen_weight = BeamSmoothMax; %1.0e6
 | 
	
		
			
				|  |  | +        x_down = zeros(size(x));
 | 
	
		
			
				|  |  | +        x_down(2:end) = x(1:end-1);
 | 
	
		
			
				|  |  | +        x_down2 = zeros(size(x));
 | 
	
		
			
				|  |  | +        x_down2(3:end) = x(1:end-2);
 | 
	
		
			
				|  |  | +        x_up = zeros(size(x));
 | 
	
		
			
				|  |  | +        x_up(1:end-1) = x(2:end);
 | 
	
		
			
				|  |  | +        x_up2 = zeros(size(x));
 | 
	
		
			
				|  |  | +        x_up2(1:end-2) = x(3:end);
 | 
	
		
			
				|  |  | +        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));
 | 
	
		
			
				|  |  | +        penalty = penalty + mod_pen2;
 | 
	
		
			
				|  |  |      end
 | 
	
		
			
				|  |  |  end
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -340,11 +403,13 @@ function optGoal = make_robust_optGoal(optGoal, RO_params, beamlets);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      
 | 
	
		
			
				|  |  |      % ----====#### CHANGE ROBUSTNESS HERE ####====----
 | 
	
		
			
				|  |  | -    
 | 
	
		
			
				|  |  | +    if isfield(optGoal{1}, 'sss_scene_list')
 | 
	
		
			
				|  |  | +        sss_scene_list = optGoal{1}.sss_scene_list;
 | 
	
		
			
				|  |  | +    else
 | 
	
		
			
				|  |  | +        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]};
 | 
	
		
			
				|  |  | +        optGoal{1}.sss_scene_list = sss_scene_list;
 | 
	
		
			
				|  |  | +    end
 | 
	
		
			
				|  |  |  %     sss_scene_list={[0,0,0]};
 | 
	
		
			
				|  |  | -    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]};
 | 
	
		
			
				|  |  | -%     sss_scene_list={[0,0,0], [-shift_mag,0,0], [shift_mag,0,0], [0,-shift_mag,0], [0,shift_mag,0],...
 | 
	
		
			
				|  |  | -%         [-shift_mag*2,0,0], [shift_mag*2,0,0], [0,-shift_mag*2,0], [0,shift_mag*2,0]};
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      % ----====#### CHANGE ROBUSTNESS HERE ####====----
 | 
	
		
			
				|  |  |  %     [targetIn, meta] = nrrdread('C:\010-work\003_localGit\WiscPlan_v2\data\archive\CDP_data\CDP5_DP_target.nrrd');
 |