RDXTPS_optimSetup.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. function RDXTPS_optimSetup(Nbeamlets, patient_dir, Geometry)
  2. % Sets up a dose prescription and creates the optimizer input files for
  3. % proton plans.
  4. if isempty(patient_dir)
  5. patient_dir = uifile('getdir', 'Select the patient directory');
  6. end
  7. if isempty(Geometry)
  8. load(fullfile(patient_dir, 'matlab_files', 'Geometry.mat'));
  9. end
  10. %% Setup dialog box
  11. prompt = {...
  12. sprintf('Maximum number of iterations:'), ...
  13. sprintf('Save result every ? interations:'), ...
  14. sprintf('Modulation factor:\n(typically 5 for SS, 2 for DET)') ...
  15. };
  16. defAns = {'2000', '200', '5'};
  17. %% Prompt input dialog
  18. answer = inputdlg(prompt, 'Optimization parameters', 1, defAns);
  19. Niterations = str2double(answer{1});
  20. Nperbatch = str2double(answer{2});
  21. modFactor = str2double(answer{3});
  22. %%
  23. optSettings = [];
  24. % set up the optimization:
  25. % All folder specified relative to optimizer executable
  26. optSettings.optInfo.saveOptInfo = 'yes';
  27. optSettings.optInfo.Niterations = Niterations; % total number of iterations
  28. optSettings.optInfo.Nperbatch = Nperbatch; % number of iterations per batch
  29. optSettings.optInfo.optFolder = patient_dir;
  30. optSettings.optInfo.inputFile = 'optInput.txt'; % goes in the optimizer folder
  31. optSettings.optInfo.inputFolder = 'opt_input';
  32. optSettings.optInfo.outputFolder = 'opt_output';
  33. optSettings.optInfo.modFactor = modFactor;
  34. % Flag telling the optimizer whether to calculate an initial guess itself
  35. % or to read in the initial guess file
  36. optSettings.initialGuessInfo.calcInitialGuessFlag = 1;
  37. % DCW - the initial guess only works with beamlets stored in the matlab
  38. % format as is done with the old matlab dose calculator. thus for this
  39. % application, initial guess will always be set to 'no'.
  40. % initial guess calculation sometimes time consuming, turn off with flag:
  41. calculateInitialGuess = 'no';
  42. optSettings.prescInfo.savePresc = 'yes';
  43. optSettings.beamletInfo.saveBeamletHeader = 'yes';
  44. optSettings.beamletInfo.beamletFolder = 'beamlet_batch_files';
  45. optSettings.beamletInfo.Nbeamlets = Nbeamlets;
  46. optSettings.beamletInfo.Nbeamletbatches = 1;
  47. %--------------------------------------------------------------------------
  48. % DCW 10-13-07
  49. % XMO 11-06-09
  50. ROI_names = cellfun(@(c)c.name, Geometry.ROIS, 'UniformOutput', false);
  51. [target_idx okay] = listdlg('ListString', ROI_names, ...
  52. 'SelectionMode', 'single', 'Name', 'Target Selection', ...
  53. 'PromptString', 'Please select the target ROI. ');
  54. % if okay ~= 1
  55. % msgbox('Optimization setup aborted');
  56. % return;
  57. % end
  58. % parameters set to 1 if ROI is target, 0 otherwise
  59. prescTable = cell(length(Geometry.ROIS),13);
  60. for roi_ind = 1:length(Geometry.ROIS)
  61. prescTable{roi_ind,1} = Geometry.ROIS{roi_ind}.name;
  62. prescTable{roi_ind,2} = Geometry.ROIS{roi_ind}.name;
  63. % set all parameters to zero
  64. for iloop = 3:13
  65. prescTable{roi_ind,iloop} = 0;
  66. end
  67. % set default values for target
  68. if roi_ind == target_idx
  69. prescTable{roi_ind, 3} = 100; % alpha
  70. prescTable{roi_ind, 4} = 1; % max dose penalty
  71. prescTable{roi_ind, 5} = 60; % max dose
  72. prescTable{roi_ind, 6} = 1; % min dose penalty
  73. prescTable{roi_ind, 7} = 60; % min dose
  74. prescTable{roi_ind,12} = 60; % DVH under dose (prescription dose)
  75. prescTable{roi_ind,13} = 98; % DVH under dose V (prescription volume)
  76. end
  77. end
  78. %--------------------------------------------------------------------------
  79. % % load downsampled geometry file
  80. % load(geometryFile);
  81. % size of CT grid
  82. [M,N,Q] = size(Geometry.rhomw);
  83. % Create the prescription structure
  84. presc = []; % erase the old prescription structure, if it exists
  85. presc.siz = [M N Q]; % dimensions of the prescription and tissue masks
  86. ROIFlags = zeros(1,size(prescTable,1)); % flags to ensure all ROIs are found
  87. % assign the parameters to the prescription structure
  88. for j=1:size(prescTable,1)
  89. % pull out the prescription indices for each tissue
  90. tissName = prescTable{j,2}; % current tissue name
  91. % find the current tissue in the Geometry.ROIS cells
  92. for k=1:length(Geometry.ROIS)
  93. if strcmp(tissName,strrep(Geometry.ROIS{k}.name,'%',''))
  94. ind = Geometry.ROIS{k}.ind; % extract ROI indices
  95. break;
  96. end
  97. end
  98. presc.tissue(j).name = prescTable{j,1};
  99. presc.tissue(j).ind = ind;
  100. presc.tissue(j).alpha = prescTable{j,3};
  101. presc.tissue(j).betaPlus = prescTable{j,4};
  102. presc.tissue(j).dPlus = single(zeros(size(ind)) + prescTable{j,5});
  103. presc.tissue(j).betaMinus = prescTable{j,6};
  104. presc.tissue(j).dMinus = single(zeros(size(ind)) + prescTable{j,7});
  105. presc.tissue(j).betaVPlus = prescTable{j,8};
  106. presc.tissue(j).dVPlus = prescTable{j,9};
  107. presc.tissue(j).vPlus = prescTable{j,10};
  108. presc.tissue(j).betaVMinus = prescTable{j,11};
  109. presc.tissue(j).dVMinus = prescTable{j,12};
  110. presc.tissue(j).vMinus = prescTable{j,13};
  111. presc.tissue(j).dPlus = single(zeros(size(ind)) + prescTable{j,5});
  112. presc.tissue(j).dMinus = single(zeros(size(ind)) + prescTable{j,7});
  113. ROIFlags(j) = 1;
  114. end
  115. % ensure that all of the ROIS were found
  116. if sum(ROIFlags) ~= length(ROIFlags)
  117. error('Missed one or more ROIS.\n');
  118. else
  119. fprintf('All ROIs located successfully for downsampled prescription\n');
  120. end
  121. optSettings.prescInfo.presc = presc;
  122. optSettings.beamletInfo.beamletDim = [M N Q];
  123. RDX_linlsqOptimization(optSettings);