123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- function [optSettings, optResults] = loadOptResults(varargin)
- warning off;
- Ndvhbins = 1000;
- if length(varargin) == 1
- error('Too few input arguments');
- elseif length(varargin) == 2
- optimizerFolder = varargin{1};
- optInputFile = varargin{2};
- readAllOutputFiles = 1;
- elseif length(varargin) == 3
- optimizerFolder = varargin{1};
- optInputFile = varargin{2};
- if strcmp(deblank(lower(varargin{3})),'last')
- readAllOutputFiles = 0;
- else
- readAllOutputFiles = 1;
- end
- else
- error('Too many input arguments');
- end
- inputFileNameRev = fliplr(optInputFile);
- [fileNameRev,inputFolderRev] = strtok(inputFileNameRev,{'/','\'});
- inputFolder = fliplr(inputFolderRev);
- if isempty(inputFolder) % input folder is the current folder
- inputFolder = [pwd '/'];
- end
- % get the patient header
- headerRev = fliplr(optimizerFolder);
- % pop off patient name
- [headerNameRev,optFolderRev] = strtok(headerRev,{'/','\'});
- patientHeader = fliplr(headerNameRev);
- [optSettings,missingInfo] = loadOptSettings(optimizerFolder,optInputFile); % load the optimization settings
- if numel(missingInfo)
- fprintf('Information missing from optimization files:\n');
- for k=1:numel(missingInfo)
- fprintf('%s\n',missingInfo{k});
- end
- fprintf('\n');
- end
- if isfield(optSettings,'beamletInfo') && isfield(optSettings.beamletInfo,'beamletDim')
- siz = optSettings.beamletInfo.beamletDim;
- elseif isfield(optSettings,'prescInfo') && isfield(optSettings.prescInfo,'presc') ...
- && isfield(optSettings.prescInfo.presc,'siz')
- siz = optSettings.prescInfo.presc.siz;
- else
- siz = [];
- end
- if isfield(optSettings,'optInfo') && isfield(optSettings.optInfo,'Niterations') ...
- && isfield(optSettings.optInfo,'Nperbatch') && isfield(optSettings.optInfo,'outputFolder')...
- && isfield(optSettings.optInfo,'doseBatchBaseName') && isfield(optSettings.optInfo,'doseBatchExtension')...
- && isfield(optSettings.optInfo,'weightBatchBaseName') && isfield(optSettings.optInfo,'weightBatchExtension')
-
-
-
-
-
- atmp = lsl(optSettings.optInfo.outputFolder);
- btmp = size(atmp);
- if btmp(1)>4
- Nbatches = ceil((btmp(1) - 3)/2);
- elseif btmp(1)==4
- Nbatches = 1;
- else
- error('No dose batches found');
- end
-
- doseFileNames = cell(1,Nbatches);
- doseFileExist = zeros(1,Nbatches);
- weightFileNames = cell(1,Nbatches);
- weightFileExist = zeros(1,Nbatches);
-
- k=0;
- for t=1:btmp(1)
- if ~isempty(findstr(optSettings.optInfo.doseBatchBaseName,mat2str(atmp(t,:))));
- k=k+1;
- batchname = strtrim(strrep(mat2str(atmp(t,:)),'''',''));
- str = strrep(strtok(batchname,'.'),optSettings.optInfo.doseBatchBaseName,'');
- batchNames{k} = str;
- doseFileNames{k} = [optSettings.optInfo.outputFolder '/' ...
- strtrim(strrep(mat2str(atmp(t,:)),'''','')) ];
- weightFileNames{k} = strrep(doseFileNames{k},optSettings.optInfo.doseBatchBaseName,optSettings.optInfo.weightBatchBaseName);
- end
- end
- for k=1:Nbatches
-
-
-
-
-
-
-
-
-
- fid = fopen(doseFileNames{k},'rb');
- if fid == -1
- doseFileExist(k) = 0;
- else
- fclose(fid);
- doseFileExist(k) = 1;
- end
- fid = fopen(weightFileNames{k},'rb');
- if fid == -1
- weightFileExist(k) = 0;
- else
- fclose(fid);
- weightFileExist(k) = 1;
- end
- end
- optResults = [];
-
- optResults.batchNames = batchNames;
- if readAllOutputFiles == 1
- optResults.dose = {};
- optResults.weights = {};
-
- for k=1:length(doseFileNames)
- if doseFileExist(k) == 1
- fid = fopen(doseFileNames{k},'rb');
- dose = fread(fid,'float=>float');
- fclose(fid);
- if numel(siz) == 3
- dose = reshape(dose,siz);
- end
- optResults.dose{k} = dose;
- end
- end
-
- for k=1:length(weightFileNames)
- if weightFileExist(k) == 1
- fid = fopen(weightFileNames{k},'rb');
- weights = fread(fid,'float=>float');
- fclose(fid);
- optResults.weights{k} = weights;
- end
- end
- else
- for k=length(doseFileNames):-1:0
- if doseFileExist(k) == 1
- break;
- end
- end
- fid = fopen(doseFileNames{k},'rb');
- dose = fread(fid,'float=>float');
- fclose(fid);
- if numel(siz) == 3
- dose = reshape(dose,siz);
- end
- optResults.dose = dose;
- fid = fopen(weightFileNames{k},'rb');
- weights = fread(fid,'float=>float');
- fclose(fid);
- optResults.weights = weights;
- end
-
- if isfield(optSettings,'prescInfo') & isfield(optSettings.prescInfo,'presc') ...
- & isfield(optSettings.prescInfo.presc,'tissue') ...
- & isfield(optSettings.prescInfo.presc.tissue,'ind')
- optResults.presc = optSettings.prescInfo.presc;
- if readAllOutputFiles == 1
-
- for k=1:length(optResults.dose)
- dmax(k) = 1.1*max(optResults.dose{k}(:));
- end
-
- for m=1:length(optResults.presc.tissue)
- tissMask = zeros(optResults.presc.siz,'int8');
- tissMask(optResults.presc.tissue(m).ind) = 1;
- for k=1:length(optResults.dose)
- dvhbins = [0:Ndvhbins-1]*dmax(k)/Ndvhbins;
- optResults.presc.tissue(m).dvhbins{k} = dvhbins;
- optResults.presc.tissue(m).dvh{k} ...
- = single(dvh(optResults.dose{k},tissMask,dvhbins))';
- end
- end
- else
- dmax = 1.1*max(optResults.dose(:));
-
- for m=1:length(optResults.presc.tissue)
- tissMask = zeros(optResults.presc.siz,'int8');
- tissMask(optResults.presc.tissue(m).ind) = 1;
- dvhbins = [0:Ndvhbins-1]*dmax/Ndvhbins;
- optResults.presc.tissue(m).dvhbins = dvhbins;
- optResults.presc.tissue(m).dvh ...
- = single(dvh(optResults.dose,tissMask,dvhbins))';
-
- if sum(optResults.presc.tissue(m).dMinus) > 0
-
- doseRatio = zeros(optResults.presc.siz);
- doseRatio(optResults.presc.tissue(m).ind) = ...
- optResults.dose(optResults.presc.tissue(m).ind)./ ...
- optResults.presc.tissue(m).dMinus*100;
-
- doseRatio(isinf(doseRatio)) = 0;
- doseRatio(isnan(doseRatio)) = 0;
-
- edvhbins = [0:Ndvhbins-1]*max(doseRatio(:))/Ndvhbins;
- optResults.presc.tissue(m).edvhbins = edvhbins;
- optResults.presc.tissue(m).edvh = ...
- single(dvh(doseRatio,tissMask,edvhbins))';
- end
- end
- end
- else
- fprintf('Unable to load DVH information: not enough prescription fields available.\n\n');
- end
- else
- fprintf('Unable to load beamlet weights and dose distributions: not enough fields available.\n\n');
- end
- if isfield(optSettings,'optInfo') & isfield(optSettings.optInfo,'outputFolder') ...
- & isfield(optSettings.optInfo,'objFuncFileName')
- fid = fopen([optSettings.optInfo.outputFolder '/' optSettings.optInfo.objFuncFileName],'rb');
- if fid ~= -1
- optResults.objFunc = fread(fid,'float=>float');
- fclose(fid);
- end
- else
- fprintf('Unable to load objective function file: not enough fields available.');
- end
- batch_indices = cellfun(@str2num, optResults.batchNames);
- [delme perm_vector] = sort(batch_indices);
- optResults.batchNames = optResults.batchNames(perm_vector);
- optResults.dose = optResults.dose(perm_vector);
- optResults.weights = optResults.weights(perm_vector);
- optResults.dose = {optResults.dose{1}, optResults.dose{end}};
- savefile = fullfile(optimizerFolder, 'matlab_files', 'optResults.mat');
- save(savefile,'optSettings','optResults');
|