calcNormalizedInitialGuess.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. function [w0,dose0] = calcNormalizedInitialGuess(varargin)
  2. % [w0,dose0] = calcNormalizedInitialGuess(B,presc) calculates an initial
  3. % guess for the linear least squares optimizer based on the beamlets, B,
  4. % and the prescription structure, presc. B is a cell array of Dij cell
  5. % arrays, where each element in Dij is a Matlab sparse matrix. The
  6. % structure presc has the form:
  7. % presc.siz = [M N Q] % dimensions of prescription, beamlet must match
  8. % presc.tissue(:) % structure array of tissue prescription
  9. % .dMinus % minimum dose tissue must receive
  10. %
  11. % [w0,dose] = calcNormalizedInitialGuess(optInputFile)
  12. % Calculates the initial guess for beamlets and a prescription that are not
  13. % loaded into Matlab, but can be accessed using information in
  14. % optInputFile, which is an input file to the linear least squares
  15. % optimizer.
  16. %
  17. % The goal is for all of the initial beams to have the same weight,
  18. % designed such that the integral dose to the structures that were
  19. % prescribed non-zero dose is equal to the prescribed integral dose.
  20. %
  21. % RTF 1/19/07
  22. % Check which mode the user has requested
  23. if numel(varargin) == 2
  24. if ~iscell(varargin{1})
  25. error('First input must be a cell array of beamlets.');
  26. elseif ~isstruct(varargin{2})
  27. error('Second input must be a prescription structure.');
  28. else
  29. B = varargin{1};
  30. presc = varargin{2};
  31. end
  32. Nbeamlets = 0;
  33. dose = zeros(presc.siz);
  34. for k=1:numel(B)
  35. for m=1:numel(B{k}.Dij)
  36. if ~isempty(B{k}.Dij{m}.non_zero_indices)
  37. dose(B{k}.Dij{m}.non_zero_indices) = dose(B{k}.Dij{m}.non_zero_indices) ...
  38. + (B{k}.Dij{m}.non_zero_values);
  39. end
  40. Nbeamlets = Nbeamlets + 1;
  41. end
  42. end
  43. elseif numel(varargin) == 1
  44. if ~ischar(varargin{1});
  45. error('Input must be a character string.');
  46. else
  47. optInputFile = varargin{1};
  48. end
  49. optSettings = loadOptSettings(optInputFile);
  50. presc = optSettings.prescInfo.presc; % extract the prescription information
  51. % Extract the folder name that contains the input file
  52. inputFileNameRev = fliplr(optInputFile); % flip the input filename around
  53. % pop off the reversed file name
  54. [fileNameRev,inputFolderRev] = strtok(inputFileNameRev,{'/','\'});
  55. inputFolder = fliplr(inputFolderRev);
  56. % calculate the initial dose for all unit weights
  57. dose = zeros(presc.siz,'single');
  58. Nbeamlets = 0;
  59. for k=1:optSettings.beamletInfo.Nbeamletbatches
  60. beamletBatchFile = [inputFolder '/' optSettings.beamletInfo.beamletFolder ...
  61. '/' optSettings.beamletInfo.beamletBatchBaseName num2str(k-1) '.' ...
  62. optSettings.beamletInfo.beamletBatchExtension];
  63. beamlets = open_beamlet_batch(beamletBatchFile); % load the beamlet batch
  64. for m=1:length(beamlets)
  65. if ~isempty(beamlets{m}.non_zero_indices)
  66. dose(beamlets{m}.non_zero_indices) = dose(beamlets{m}.non_zero_indices) ...
  67. + beamlets{m}.non_zero_values;
  68. end
  69. Nbeamlets = Nbeamlets + 1;
  70. end
  71. end
  72. if Nbeamlets ~= optSettings.beamletInfo.Nbeamlets
  73. error(['Beamlets read = ' num2str(Nbeamlets) '. Beamlets expected = ' num2str(optSettings.beamletInfo.Nbeamlets) '.']);
  74. end
  75. end
  76. dIntTumor = 0; % integral dose in tumor
  77. dIntTumorPresc = 0; % prescribed integral tumor dose
  78. % calculate integral dose in the non-zero prescription regions
  79. for k=1:length(presc.tissue)
  80. if presc.tissue(k).dMinus > 0
  81. dIntTumorPresc = dIntTumorPresc + sum(presc.tissue(k).dMinus);
  82. dIntTumor = dIntTumor + sum(dose(presc.tissue(k).ind));
  83. end
  84. end
  85. if dIntTumor == 0
  86. error('Zero tumor dose -- respecify initial beamlet weights.');
  87. end
  88. w0 = ones(Nbeamlets,1); % allocate a vector for the initial beamlet weights
  89. w0 = w0*dIntTumorPresc/dIntTumor; % normalized initial guess
  90. dose0 = dose*dIntTumorPresc/dIntTumor;