loadOptSettings.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. function [optSettings,missingInfo] = loadOptSettings(optInputFile)
  2. % [optSettings,missingInfo] = loadOptSettings(optInputFile) loads the
  3. % linear least squares optimization settings from the optimization input
  4. % file into a Matlab structure. Beamlets are not loaded, but everything
  5. % else is. If any inconsistancies are found, such as missing fields, etc,
  6. % the inconsistancies are stored in the cell array, missingInfo. The
  7. % optInputFile must be that absolute path of the input file.
  8. %
  9. % RTF 1/19/07
  10. % initialize outputs
  11. optSettings = [];
  12. missingInfo = {};
  13. % Open the optimization input file
  14. fid = fopen(optInputFile,'r');
  15. if fid == -1
  16. error(['Unable to open ' optInputFile]);
  17. end
  18. optSettings = []; % optimization settings structure
  19. % read the file into a cell array, line-by-line
  20. fileText = {};
  21. while 1
  22. tline = fgetl(fid);
  23. if ~ischar(tline)
  24. break;
  25. end
  26. fileText{end+1} = tline;
  27. end
  28. fclose(fid);
  29. % Extract the folder name that contains the input file
  30. inputFileNameRev = fliplr(optInputFile); % flip the input filename around
  31. % pop off the reversed file name
  32. [fileNameRev,inputFolderRev] = strtok(inputFileNameRev,{'/','\'});
  33. inputFolder = fliplr(inputFolderRev);
  34. optSettings.optInfo.inputFile = fliplr(fileNameRev);
  35. % Field Name subfield conversion function
  36. fieldFunctions = {'Niterations' 'optInfo' 'str2num'
  37. 'Nperbatch' 'optInfo' 'str2num'
  38. 'prescFile' 'prescInfo' ''
  39. 'initBeamWeightFile' 'initialGuessInfo' ''
  40. 'beamletHeaderFileName' 'beamletInfo' ''
  41. 'doseBatchBaseName' 'optInfo' ''
  42. 'doseBatchExtension' 'optInfo' ''
  43. 'weightBatchBaseName' 'optInfo' ''
  44. 'weightBatchExtension' 'optInfo' ''
  45. 'objFuncFileName' 'optInfo' ''};
  46. % search for key fields
  47. for k=1:length(fileText)
  48. for m=1:size(fieldFunctions,1)
  49. if strcmp(deblank(fileText{k}),fieldFunctions{m,1})
  50. eval(['optSettings.' fieldFunctions{m,2} '.' fieldFunctions{m,1} ' = ' fieldFunctions{m,3} '(''' fileText{k+1} ''');']);
  51. end
  52. end
  53. end
  54. % search for missing fields
  55. for k=1:size(fieldFunctions,1)
  56. if ~isfield(optSettings,fieldFunctions{k,2}) | ~eval(['isfield(optSettings.' fieldFunctions{k,2} ',''' fieldFunctions{k,1} ''')'])
  57. missingInfo{end+1} = ['Missing: optSettings.' fieldFunctions{k,2} '.' fieldFunctions{k,1}];
  58. end
  59. end
  60. % find the output folder
  61. if isfield(optSettings,'optInfo') & isfield(optSettings.optInfo,'doseBatchBaseName')
  62. doseBatchBaseNameRev = fliplr(optSettings.optInfo.doseBatchBaseName);
  63. [fileNameRev,outputFolderRev] = strtok(doseBatchBaseNameRev,{'/','\'});
  64. optSettings.optInfo.outputFolder = fliplr(outputFolderRev);
  65. optSettings.optInfo.outputFolder(:,end) = []; % delete last character, which is a '/' or '\'
  66. optSettings.optInfo.doseBatchBaseName = fliplr(fileNameRev);
  67. end
  68. % get the weightBatchBaseName
  69. if isfield(optSettings,'optInfo') & isfield(optSettings.optInfo,'weightBatchBaseName')
  70. doseBatchBaseNameRev = fliplr(optSettings.optInfo.weightBatchBaseName);
  71. [fileNameRev,outputFolderRev] = strtok(doseBatchBaseNameRev,{'/','\'});
  72. optSettings.optInfo.weightBatchBaseName = fliplr(fileNameRev);
  73. end
  74. % find the input folder
  75. if isfield(optSettings,'prescInfo') & isfield(optSettings.prescInfo,'prescFile')
  76. prescFileRev = fliplr(optSettings.prescInfo.prescFile);
  77. [fileNameRev,inputFolderRev] = strtok(prescFileRev,{'/','\'});
  78. optSettings.optInfo.inputFolder = fliplr(inputFolderRev);
  79. optSettings.optInfo.inputFolder(:,end) = []; % delete last character, which is a '/' or '\'
  80. optSettings.prescInfo.prescFile = fliplr(fileNameRev);
  81. end
  82. % get the initial beam weights file name
  83. if isfield(optSettings,'initialGuessInfo') & isfield(optSettings.initialGuessInfo,'initBeamWeightFile')
  84. initBeamWeightFileRev = fliplr(optSettings.initialGuessInfo.initBeamWeightFile);
  85. [fileNameRev,inputFolderRev] = strtok(initBeamWeightFileRev,{'/','\'});
  86. optSettings.initialGuessInfo.initBeamWeightFile = fliplr(fileNameRev);
  87. end
  88. % trim off the objective function filename
  89. if isfield(optSettings,'optInfo') & isfield(optSettings.optInfo,'objFuncFileName')
  90. prescFileRev = fliplr(optSettings.optInfo.objFuncFileName);
  91. [fileNameRev,inputFolderRev] = strtok(prescFileRev,{'/','\'});
  92. optSettings.optInfo.objFuncFileName = fliplr(fileNameRev);
  93. end
  94. % get the beamletFolder and beamletHeader
  95. if isfield(optSettings,'beamletInfo') & isfield(optSettings.beamletInfo,'beamletHeaderFileName')
  96. beamletHeaderFileNameRev = fliplr(optSettings.beamletInfo.beamletHeaderFileName);
  97. [fileNameRev,beamletFolderRev] = strtok(beamletHeaderFileNameRev,{'/','\'});
  98. optSettings.beamletInfo.beamletFolder = fliplr(beamletFolderRev);
  99. optSettings.beamletInfo.beamletFolder(:,end) = []; % delete last character, which is a '/' or '\'
  100. optSettings.beamletInfo.beamletHeaderFileName = fliplr(fileNameRev);
  101. end
  102. % open the prescription file if it has been specified properly
  103. if isfield(optSettings,'optInfo') & isfield(optSettings,'prescInfo') ...
  104. & isfield(optSettings.optInfo,'inputFolder') & isfield(optSettings.prescInfo,'prescFile')
  105. prescFile = [inputFolder '/' optSettings.optInfo.inputFolder '/' optSettings.prescInfo.prescFile];
  106. fid = fopen(prescFile,'r');
  107. if fid == -1
  108. missingInfo{end+1} = ['Unable to open prescription file: ' prescFile];
  109. else
  110. % read the entire file into a cell array
  111. fileText = {};
  112. while 1
  113. tline = fgetl(fid);
  114. if ~ischar(tline)
  115. break;
  116. end
  117. fileText{end+1} = tline;
  118. end
  119. fclose(fid);
  120. if ~strcmp(deblank(fileText{1}),'Ntissue')
  121. error('First line of prescription file must be ''Ntissue''');
  122. else
  123. Ntissue = str2num(fileText{2});
  124. end
  125. k = 5; % skip up to the 5th line of the file, which is the tissue number
  126. % read in the tissues
  127. for m=1:Ntissue % read through the lines for the current tissue
  128. tissNum = str2num(fileText{k});
  129. k = k + 2;
  130. optSettings.prescInfo.presc.tissue(m).name = fileText{k};
  131. k = k + 2;
  132. optSettings.prescInfo.presc.tissue(m).alpha = str2num(fileText{k});
  133. k = k + 2;
  134. optSettings.prescInfo.presc.tissue(m).betaVPlus = str2num(fileText{k});
  135. k = k + 2;
  136. optSettings.prescInfo.presc.tissue(m).dVPlus = str2num(fileText{k});
  137. k = k + 2;
  138. optSettings.prescInfo.presc.tissue(m).vPlus = str2num(fileText{k});
  139. k = k + 2;
  140. optSettings.prescInfo.presc.tissue(m).betaVMinus = str2num(fileText{k});
  141. k = k + 2;
  142. optSettings.prescInfo.presc.tissue(m).dVMinus = str2num(fileText{k});
  143. k = k + 2;
  144. optSettings.prescInfo.presc.tissue(m).vMinus = str2num(fileText{k});
  145. k = k + 2;
  146. optSettings.prescInfo.presc.tissue(m).betaPlus = str2num(fileText{k});
  147. k = k + 2;
  148. optSettings.prescInfo.presc.tissue(m).dosePlusFileName = fileText{k};
  149. k = k + 2;
  150. optSettings.prescInfo.presc.tissue(m).betaMinus = str2num(fileText{k});
  151. k = k + 2;
  152. optSettings.prescInfo.presc.tissue(m).doseMinusFileName = fileText{k};
  153. % load-in the dPlus inforomation
  154. fid = fopen([inputFolder '/' optSettings.prescInfo.presc.tissue(m).dosePlusFileName],'rb');
  155. siz = fread(fid,3,'int'); % size of the sparse array
  156. Nind = fread(fid,1,'int');
  157. ind = fread(fid,Nind,'int=>int');
  158. dat = fread(fid,Nind,'float=>float');
  159. fclose(fid);
  160. optSettings.prescInfo.presc.tissue(m).ind = ind;
  161. optSettings.prescInfo.presc.tissue(m).dPlus = dat;
  162. % load-in the dMinus inforomation
  163. fid = fopen([inputFolder '/' optSettings.prescInfo.presc.tissue(m).doseMinusFileName],'rb');
  164. siz = fread(fid,3,'int')'; % size of the sparse array
  165. Nind = fread(fid,1,'int');
  166. ind = fread(fid,Nind,'int=>int');
  167. dat = fread(fid,Nind,'float=>float');
  168. fclose(fid);
  169. optSettings.prescInfo.presc.tissue(m).ind = ind;
  170. optSettings.prescInfo.presc.tissue(m).dMinus = dat;
  171. k = k + 3; % skip up to the next tissueNum value field
  172. end
  173. optSettings.prescInfo.presc.siz = siz; % size of all of the dose grid
  174. end
  175. end
  176. % read in the beamlet file and folder information
  177. if isfield(optSettings,'beamletInfo') & isfield(optSettings.beamletInfo,'beamletFolder') ...
  178. & isfield(optSettings.beamletInfo,'beamletFolder')
  179. beamletHeaderFile = [inputFolder '/' optSettings.beamletInfo.beamletFolder '/' optSettings.beamletInfo.beamletHeaderFileName];
  180. fid = fopen([inputFolder '/' optSettings.beamletInfo.beamletFolder '/' optSettings.beamletInfo.beamletHeaderFileName],'r');
  181. if fid == -1
  182. missingInfo{end+1} = ['Unable to open beamlet header file: ' beamletHeaderFile];
  183. else
  184. % read the file into a cell array, line-by-line
  185. fileText = {};
  186. while 1
  187. tline = fgetl(fid);
  188. if ~ischar(tline)
  189. break;
  190. end
  191. fileText{end+1} = tline;
  192. end
  193. fclose(fid);
  194. fieldFunctions = { ...
  195. 'beamletDim' 'beamletInfo' 'str2num'
  196. 'Nbeamlets' 'beamletInfo' 'str2num'
  197. 'Nbeamletbatches' 'beamletInfo' 'str2num'
  198. 'beamletFolder' 'beamletInfo' ''
  199. 'beamletBatchBaseName' 'beamletInfo' ''
  200. 'beamletBatchExtension' 'beamletInfo' '' };
  201. % search for key fields
  202. for k=1:length(fileText)
  203. for m=1:size(fieldFunctions,1)
  204. if strcmp(deblank(fileText{k}),fieldFunctions{m,1})
  205. eval(['optSettings.' fieldFunctions{m,2} '.' fieldFunctions{m,1} ' = ' fieldFunctions{m,3} '(''' fileText{k+1} ''');']);
  206. end
  207. end
  208. end
  209. % search for missing fields
  210. for k=1:size(fieldFunctions,1)
  211. if ~isfield(optSettings,fieldFunctions{k,2}) | ~eval(['isfield(optSettings.' fieldFunctions{k,2} ',''' fieldFunctions{k,1} ''')'])
  212. missingInfo{end+1} = ['Missing: optSettings.' fieldFunctions{k,2} '.' fieldFunctions{k,1}];
  213. end
  214. end
  215. end
  216. end