123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- function [weights, dose] = RDX_linlsqOptimization(optSettings)
- optSettings = parseInput(optSettings);
- origDir = pwd;
- warning off;
- cd(optSettings.optInfo.optFolder);
- savePrescription(optSettings);
- saveBeamlets(optSettings);
- saveInitialGuess(optSettings);
- saveOptInfo(optSettings);
- [weights, dose] = runOptimizer(optSettings);
- cd(origDir);
- function optSettings = parseInput(optSettings)
- optSettingsDefault.prescInfo.savePresc = 'no';
- optSettingsDefault.prescInfo.presc = [];
- optSettingsDefault.prescInfo.prescFileName = 'prescription.txt';
- optSettingsDefault.beamletInfo.saveBeamlets = 'no';
- optSettingsDefault.beamletInfo.saveBeamletHeader = 'no';
- optSettingsDefault.beamletInfo.beamletFolder = 'beamlets';
- optSettingsDefault.beamletInfo.beamlets = [];
- optSettingsDefault.beamletInfo.beamletBatchBaseName = 'beamletbatch';
- optSettingsDefault.beamletInfo.beamletBatchExtension = 'bin';
- optSettingsDefault.beamletInfo.beamletHeaderFileName = 'beamlet_header.txt';
- optSettingsDefault.beamletInfo.Nbeamlets = 1;
- optSettingsDefault.beamletInfo.Nbeamletbatches = 1;
- optSettingsDefault.beamletInfo.beamletDim = [1 1 1];
- optSettingsDefault.optInfo.saveOptInfo = 'no';
- optSettingsDefault.optInfo.optRun = 'no';
- optSettingsDefault.optInfo.waitForResults = 'no';
- optSettingsDefault.optInfo.optFolder = ...
- 'F:\Treatment_Planning\optimizer';
- optSettingsDefault.optInfo.inputFolder = 'input';
- optSettingsDefault.optInfo.outputFolder = 'output';
- optSettingsDefault.optInfo.inputFile = 'optInput.txt';
- optSettingsDefault.optInfo.Niterations = 100;
- optSettingsDefault.optInfo.Nperbatch = 50;
- optSettingsDefault.optInfo.doseBatchBaseName = 'dosebatch';
- optSettingsDefault.optInfo.doseBatchExtension = 'img';
- optSettingsDefault.optInfo.weightBatchBaseName = 'weightbatch';
- optSettingsDefault.optInfo.weightBatchExtension = 'img';
- optSettingsDefault.optInfo.optExeName = 'linlsqOptimizer.exe';
- optSettingsDefault.optInfo.objFuncFileName = 'objFunc.bin';
- optSettingsDefault.optInfo.modFactor = 1e10;
- optSettingsDefault.initialGuessInfo.saveInitialGuess = 'no';
- optSettingsDefault.initialGuessInfo.w0 = [];
- optSettingsDefault.initialGuessInfo.initBeamWeightFile = 'init_beam_weight_file.img';
- optSettingsDefault.initialGuessInfo.calcInitialGuessFlag = 1;
- nodeVec = [1 1];
- while length(nodeVec) > 1
-
-
- fldAccStr = 'optSettingsDefault';
- for k=2:length(nodeVec)
- eval(['fieldNames = fieldnames(' fldAccStr ');']);
- fldAccStr = [fldAccStr '.' fieldNames{nodeVec(k)}];
- end
-
-
- eval(['numFlag = isnumeric(' fldAccStr ');']);
- eval(['charFlag = ischar(' fldAccStr ');']);
-
- if numFlag == 1 | charFlag == 1
-
-
-
-
- fldAccStrInp = 'optSettings';
- fldAccStrDummy = 'optSettingsDefault';
- for k=2:length(nodeVec)
- eval(['fieldNamesDummy = fieldnames(' fldAccStrDummy ');']);
- eval(['fieldFlag = isfield(' fldAccStrInp ',''' fieldNamesDummy{nodeVec(k)} ''');']);
- if fieldFlag == 1
- fldAccStrInp = [fldAccStrInp '.' fieldNamesDummy{nodeVec(k)}];
- fldAccStrDummy = [fldAccStrDummy '.' fieldNamesDummy{nodeVec(k)}];
- else
- [tok,rem] = strtok(fldAccStr,'.');
- fldAccStrInp = ['optSettings' rem];
- eval([fldAccStrInp ' = ' fldAccStr ';']);
- break;
- end
- end
-
-
- while length(nodeVec) > 1
- fldAccStr = 'optSettingsDefault';
- for k=2:length(nodeVec)
- eval(['fieldNames = fieldnames(' fldAccStr ');']);
- fldAccStr = [fldAccStr '.' fieldNames{nodeVec(k)}];
- end
-
- if nodeVec(end) + 1 <= length(fieldNames)
- nodeVec(end) = nodeVec(end) + 1;
- break;
- else
- nodeVec(:,end) = [];
- end
- end
- else
- nodeVec = [nodeVec 1];
- end
- end
- if ~isempty(optSettings.beamletInfo.beamlets)
-
- [Xcount,Ycount,Zcount] = size(full(optSettings.beamletInfo.beamlets{1}.Dij{1}));
- optSettings.beamletInfo.beamletDim = [Xcount Ycount Zcount];
- optSettings.beamletInfo.Nbeamletbatches = numel(optSettings.beamletInfo.beamlets);
- optSettings.beamletInfo.Nbeamlets = 0;
- for k=1:optSettings.beamletInfo.Nbeamletbatches
- optSettings.beamletInfo.Nbeamlets = optSettings.beamletInfo.Nbeamlets ...
- + numel(optSettings.beamletInfo.beamlets{k}.Dij);
- end
- else
-
-
- if isfield(optSettings.prescInfo.presc,'siz')
- optSettings.beamletInfo.beamletDim = optSettings.prescInfo.presc.siz;
- end
- end
- function savePrescription(optSettings)
- infValue = 10e10;
- if strcmp(lower(optSettings.prescInfo.savePresc),'no')
- return;
- end
- success = mkdir(optSettings.optInfo.inputFolder);
- success = rmdir(optSettings.optInfo.outputFolder,'s');
- success = mkdir(optSettings.optInfo.outputFolder);
- prescTissueFields = {'name' 'alpha' 'betaVPlus' 'dVPlus' 'vPlus' 'betaVMinus' ...
- 'dVMinus' 'vMinus' 'betaPlus' 'betaMinus' 'ind' 'dPlus' 'dMinus'};
-
- if isempty(optSettings.prescInfo.presc)
- error('optSettings.prescInfo.presc not defined -- define it or set optSettings.prescInfo.savePresc to ''no''');
- end
-
- if ~isfield(optSettings.prescInfo.presc,'siz')
- error('Missing ''siz'' field from optSettings.prescInfo.presc structure');
- end
- if numel(optSettings.prescInfo.presc.siz) ~= 3
- error('The ''siz'' field from optSettings.prescInfo.presc structure must be a 3-element vector');
- end
- if ~isfield(optSettings.prescInfo.presc,'tissue')
- error('Missing ''tissue'' field from prescInfo.presc structure');
- end
- for k=1:numel(prescTissueFields)
- if ~isfield(optSettings.prescInfo.presc.tissue,prescTissueFields{k})
- error(['Missing ''' prescTissueFields{k} ''' field from optSettings.prescInfo.presc.tissue structure']);
- end
- end
- fid = fopen([optSettings.optInfo.inputFolder filesep optSettings.prescInfo.prescFileName],'w');
- if fid == -1
- error('Failed to open prescription file.');
- end
- fprintf(fid,'Ntissue\n');
- fprintf(fid,[num2str(numel(optSettings.prescInfo.presc.tissue)) '\n\n']);
- for k=1:numel(optSettings.prescInfo.presc.tissue)
-
- fprintf(fid,'tissueNum\n');
- fprintf(fid,[num2str(k-1) '\n']);
- fprintf(fid,'name\n');
- fprintf(fid,[optSettings.prescInfo.presc.tissue(k).name '\n']);
-
-
-
-
- fprintf(fid,'alpha\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).alpha)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).alpha) '\n']);
- end
- fprintf(fid,'betaVPlus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).betaVPlus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).betaVPlus) '\n']);
- end
-
- fprintf(fid,'dVPlus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).dVPlus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).dVPlus) '\n']);
- end
-
- fprintf(fid,'vPlus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).vPlus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).vPlus) '\n']);
- end
-
- fprintf(fid,'betaVMinus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).betaVMinus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).betaVMinus) '\n']);
- end
-
- fprintf(fid,'dVMinus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).dVMinus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).dVMinus) '\n']);
- end
-
- fprintf(fid,'vMinus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).vMinus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).vMinus) '\n']);
- end
-
-
- fprintf(fid,'betaPlus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).betaPlus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).betaPlus) '\n']);
- end
-
-
- dose_presc_filename = [regexprep(optSettings.prescInfo.presc.tissue(k).name,{' ','\','/'},'') 'DosePlus.bin'];
- fprintf(fid,'dosePlusFilename\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.optInfo.inputFolder, dose_presc_filename));
-
-
- fid2 = fopen([optSettings.optInfo.inputFolder filesep dose_presc_filename],'wb');
- fwrite(fid2,optSettings.prescInfo.presc.siz,'int');
- fwrite(fid2,length(optSettings.prescInfo.presc.tissue(k).ind),'int');
- fwrite(fid2,optSettings.prescInfo.presc.tissue(k).ind-1,'int');
- fwrite(fid2,optSettings.prescInfo.presc.tissue(k).dPlus,'single');
- fclose(fid2);
-
- fprintf(fid,'betaMinus\n');
- if isinf(optSettings.prescInfo.presc.tissue(k).betaMinus)
- fprintf(fid,[num2str(infValue) '\n']);
- else
- fprintf(fid,[num2str(optSettings.prescInfo.presc.tissue(k).betaMinus) '\n']);
- end
-
-
- dose_presc_filename = [regexprep(optSettings.prescInfo.presc.tissue(k).name,{' ','\','/'},'') 'DoseMinus.bin'];
- fprintf(fid,'doseMinusFilename\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.optInfo.inputFolder, dose_presc_filename));
-
-
- fid2 = fopen([optSettings.optInfo.inputFolder filesep dose_presc_filename],'wb');
- fwrite(fid2,optSettings.prescInfo.presc.siz,'int');
- fwrite(fid2,length(optSettings.prescInfo.presc.tissue(k).ind),'int');
- fwrite(fid2,optSettings.prescInfo.presc.tissue(k).ind-1,'int');
- fwrite(fid2,optSettings.prescInfo.presc.tissue(k).dMinus,'single');
- fclose(fid2);
-
- fprintf(fid,'\n');
- end
- fclose(fid);
- function saveBeamlets(optSettings)
- if strcmp(lower(optSettings.beamletInfo.saveBeamletHeader),'no') ...
- && strcmp(lower(optSettings.beamletInfo.saveBeamlets),'no')
- return;
- end
- if strcmp(lower(optSettings.beamletInfo.saveBeamlets),'yes')
-
- success = rmdir([optSettings.optInfo.optFolder '/' optSettings.beamletInfo.beamletFolder],'s');
- mkdir([optSettings.optInfo.optFolder '/' optSettings.beamletInfo.beamletFolder]);
- end
- mkdir(optSettings.beamletInfo.beamletFolder);
- fid = fopen([optSettings.beamletInfo.beamletFolder '/' optSettings.beamletInfo.beamletHeaderFileName],'w');
- if fid == -1
- error('Failed to open beamlet header file.');
- end
- fprintf(fid,'beamletDim\n');
- fprintf(fid,'%g %g %g\n',optSettings.beamletInfo.beamletDim);
- fprintf(fid,'Nbeamlets\n');
- fprintf(fid,'%g\n',optSettings.beamletInfo.Nbeamlets);
- fprintf(fid,'Nbeamletbatches\n');
- fprintf(fid,'%g\n',optSettings.beamletInfo.Nbeamletbatches);
- fprintf(fid,'beamletFolder\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.beamletInfo.beamletFolder));
- fprintf(fid,'beamletBatchBaseName\n');
- fprintf(fid,[optSettings.beamletInfo.beamletBatchBaseName '\n']);
- fprintf(fid,'beamletBatchExtension\n');
- fprintf(fid,[optSettings.beamletInfo.beamletBatchExtension '\n']);
- fclose(fid);
- if strcmp(lower(optSettings.beamletInfo.saveBeamlets),'no')
- return;
- end
- if isempty(optSettings.beamletInfo.beamlets)
- error('No beamlets to save -- supply beamlets or set saveBeamlets to ''no''');
- end
- beamletNum = 0;
- for k=1:optSettings.beamletInfo.Nbeamletbatches
- beamletFileName = [optSettings.beamletInfo.beamletFolder '/' ...
- optSettings.beamletInfo.beamletBatchBaseName num2str(k-1) '.' ...
- optSettings.beamletInfo.beamletBatchExtension];
- fid = fopen(beamletFileName,'wb');
- if fid == -1
- error(['Failed to open ' beamletFileName]);
- end
- Nbib = numel(optSettings.beamletInfo.beamlets{k}.Dij);
- fwrite(fid,Nbib,'int');
- for m=1:Nbib
-
- [Nx,Ny,Nz] = size(full(optSettings.beamletInfo.beamlets{k}.Dij{m}));
- if Nx - optSettings.beamletInfo.beamletDim(1) ~= 0 ...
- || Ny - optSettings.beamletInfo.beamletDim(2) ~= 0 ...
- || Nz - optSettings.beamletInfo.beamletDim(3) ~= 0
- error(['Dimensions of optSettings.beamletInfo.beamlets{' ...
- num2str(k) '}.Dij{' num2str(m) '} are not (' ...
- num2str(optSettings.beamletInfo.beamletDim(1)) ',' ...
- num2str(optSettings.beamletInfo.beamletDim(2)) ',' ...
- num2str(optSettings.beamletInfo.beamletDim(3)) ')']);
- end
-
- fwrite(fid,beamletNum,'int');
- fwrite(fid,optSettings.beamletInfo.beamletDim,'int');
- ind = find(optSettings.beamletInfo.beamlets{k}.Dij{m});
- ind = ind - 1;
- fwrite(fid,numel(ind),'int');
- fwrite(fid,ind,'int');
- fwrite(fid,nonzeros(optSettings.beamletInfo.beamlets{k}.Dij{m}),'float');
- beamletNum = beamletNum + 1;
- end
- fclose(fid);
- end
- function saveInitialGuess(optSettings)
- if strcmp(lower(optSettings.initialGuessInfo.saveInitialGuess),'no')
- return;
- end
- success = mkdir(optSettings.optInfo.inputFolder);
- success = mkdir(optSettings.optInfo.outputFolder);
- initBeamWeightFile = [optSettings.optInfo.optFolder filesep optSettings.optInfo.inputFolder ...
- filesep optSettings.initialGuessInfo.initBeamWeightFile];
- fid = fopen(initBeamWeightFile,'wb');
- if fid == -1
- error(['Failed to open initial beam weight file: ' initBeamWeightFile]);
- end
- fwrite(fid,optSettings.initialGuessInfo.w0,'float');
- fclose(fid);
- function saveOptInfo(optSettings)
- if strcmp(lower(optSettings.optInfo.saveOptInfo),'no')
- return;
- end
- success = mkdir(optSettings.optInfo.inputFolder);
- success = mkdir(optSettings.optInfo.outputFolder);
- optInfoFileName = optSettings.optInfo.inputFile;
- fid = fopen(optInfoFileName,'w');
- if fid == -1
- error(['Failed to open optimization input file: ' optInfoFileName]);
- end
- fprintf(fid,'Niterations\n');
- fprintf(fid,'%d\n',optSettings.optInfo.Niterations);
- fprintf(fid,'Nperbatch\n');
- fprintf(fid,'%d\n',optSettings.optInfo.Nperbatch);
- fprintf(fid,'prescFile\n');
- fprintf(fid,'%s\n',fullfile(optSettings.optInfo.optFolder, optSettings.optInfo.inputFolder, optSettings.prescInfo.prescFileName));
- fprintf(fid,'initBeamWeightFile\n');
- if optSettings.initialGuessInfo.calcInitialGuessFlag == 0
-
- fprintf(fid,'%s\n',[optSettings.optInfo.optFolder '/' optSettings.initialGuessInfo.initBeamWeightFile]);
- else
- fprintf(fid,'%s\n','optimizerCalculatesInitialGuess');
- end
- fprintf(fid,'beamletHeaderFileName\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.beamletInfo.beamletFolder, optSettings.beamletInfo.beamletHeaderFileName));
- fprintf(fid,'doseBatchBaseName\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.optInfo.outputFolder, optSettings.optInfo.doseBatchBaseName));
- fprintf(fid,'doseBatchExtension\n');
- fprintf(fid,[optSettings.optInfo.doseBatchExtension '\n']);
- fprintf(fid,'weightBatchBaseName\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.optInfo.outputFolder, optSettings.optInfo.weightBatchBaseName));
- fprintf(fid,'weightBatchExtension\n');
- fprintf(fid,[optSettings.optInfo.weightBatchExtension '\n']);
- fprintf(fid,'objFuncFileName\n');
- fprintf(fid,'%s\n', fullfile(optSettings.optInfo.optFolder, optSettings.optInfo.outputFolder, optSettings.optInfo.objFuncFileName));
- fprintf(fid,'modFactor\n');
- fprintf(fid,[num2str(optSettings.optInfo.modFactor) '\n']);
- fprintf(fid,'calcInitialGuessFlag\n');
- fprintf(fid,[num2str(optSettings.initialGuessInfo.calcInitialGuessFlag) '\n']);
- fclose(fid);
- function [weights, dose] = runOptimizer(optSettings)
- dose = [];
- weights = [];
- if strcmp(lower(optSettings.optInfo.optRun),'no')
- return;
- end
- currDir = pwd;
- exeDebugFile = [optSettings.optInfo.optFolder '\Debug\' optSettings.optInfo.optExeName];
- exeFile = [optSettings.optInfo.optFolder filesep optSettings.optInfo.optExeName];
- success = copyfile(exeDebugFile,exeFile);
- if success == 0
- error(['Error copying ' exeDebugFile ' to ' exeFile]);
- end
- % do the optimization
- delete('optDone.txt');
- fprintf('Running optimizer...\n');
- eval(['! ' optSettings.optInfo.optExeName ' ' optSettings.optInfo.inputFile ' &']);
- if strcmp(lower(optSettings.optInfo.waitForResults),'no')
- return;
- end
- while ~exist('optDone.txt','file')
- pause(1);
- end
- fid = fopen([optSettings.optInfo.outputFolder '/' ...
- optSettings.optInfo.weightBatchBaseName num2str(optSettings.optInfo.Niterations) '.' ...
- optSettings.optInfo.weightBatchExtension],'rb');
- if fid == -1
- error('Unable to open optimized weight results.');
- end
- weights = fread(fid,'float');
- fclose(fid);
- fid = fopen([optSettings.optInfo.outputFolder '/' ...
- optSettings.optInfo.doseBatchBaseName num2str(optSettings.optInfo.Niterations) '.' ...
- optSettings.optInfo.doseBatchExtension],'rb');
- if fid == -1
- error('Unable to open optimized dose results.');
- end
- dose = fread(fid,'float');
- try
- dose = reshape(dose,optSettings.beamletInfo.beamletDim);
- catch
- error(['Unable to reshape optimized dose distribution. Ensure ' ...
- 'that optSettings.beamletInfo.beamletDim field matches dose grid dimensions']);
- end
|