loadOptSettings.m 12 KB

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