Advertisement
Guest User

batch_fitting[CUSTOM-TIMER]

a guest
Sep 5th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.35 KB | None | 0 0
  1. function batch_fitting(roifile, protocol, model, outputfile, poolsize)
  2.  
  3. %
  4. % function batch_fitting(roifile, protocol, model, outputfile, poolsize)
  5. %
  6. % This function does batch fitting to the voxels in an entire ROI created with
  7. % CreateROI function.
  8. %
  9. % Input:
  10. %
  11. % roifile: the ROI file created with CreateROI
  12. %
  13. % protocol: the protocol object created with FSL2Protocol
  14. %
  15. % model: the model object created with MakeModel
  16. %
  17. % outputfile: the name of the mat file to store the fitted parameters
  18. %
  19. % poolsize (optional): the number of parallel processes to run
  20. %
  21. % author: Gary Hui Zhang (gary.zhang@ucl.ac.uk)
  22. %
  23.  
  24. % first check if there is a file there to resume
  25. if exist(outputfile, 'file')
  26.     load(outputfile);
  27.     if exist('split_end', 'var')
  28.         % previously run and need to be restarted
  29.         current_split_start = split_end + 1;
  30.         fprintf('Resume an interrupted run from %i\n', current_split_start);
  31.     else
  32.         % completed
  33.         fprintf('An output file of the same name detected.\n');
  34.         fprintf('Choose a different output file name.\n');
  35.         return;
  36.     end
  37. else
  38.     % if this is the first run
  39.     current_split_start = 1;
  40. end
  41.  
  42. % initiate the parallel environment if necessary
  43. pool = gcp('nocreate');
  44. if  isempty(pool)
  45.     if (nargin < 5)
  46.         pool = parpool('local');
  47.     else
  48.         pool = parpool('local', poolsize);
  49.     end
  50. end
  51.  
  52. % load the roi file
  53. load(roifile);
  54. numOfVoxels = size(roi,1);
  55.  
  56. % set up the fitting parameter variables if it is the first run
  57. if current_split_start == 1
  58.     gsps = zeros(numOfVoxels, model.numParams);
  59.     mlps = zeros(numOfVoxels, model.numParams);
  60.     fobj_gs = zeros(numOfVoxels, 1);
  61.     fobj_ml = zeros(numOfVoxels, 1);
  62.     error_code = zeros(numOfVoxels, 1);
  63.     if model.noOfStages == 3
  64.         mcmcps = zeros(numOfVoxels, model.MCMC.samples, model.numParams + 1);
  65.     end
  66. end
  67.  
  68. % set up the PARFOR Progress Monitor
  69. [mypath myname myext] = fileparts(mfilename('fullpath'));
  70. mypath = [mypath '/../ParforProgMonv2/java'];
  71. pctRunOnAll(['javaaddpath '  mypath]);
  72. progressStepSize = 100;
  73. ppm = ParforProgMon(['Fitting ' roifile, ' : '], numOfVoxels-current_split_start+1,...
  74.                     progressStepSize, 400, 80);
  75.  
  76. tic
  77.  
  78. fprintf('%i of voxels to fit. Approx Time Left is not accurate over multiple instances.\n', numOfVoxels-current_split_start+1);
  79. startTime=clock;
  80. iteration=0;
  81.  
  82. % start the parallel fitting
  83. for split_start=current_split_start:progressStepSize:numOfVoxels
  84.     % set up the split end
  85.     split_end = split_start + progressStepSize - 1;
  86.     if split_end > numOfVoxels
  87.         split_end = numOfVoxels;
  88.     end
  89.     iteration=iteration+1;
  90.    
  91.     % fit the split
  92.     parfor i=split_start:split_end
  93.        
  94.         % get the MR signals for the voxel i
  95.         voxel = roi(i,:)';
  96.        
  97.         % fit the voxel
  98.         if model.noOfStages == 2
  99.             [gsps(i,:), fobj_gs(i), mlps(i,:), fobj_ml(i), error_code(i)] = ThreeStageFittingVoxel(voxel, protocol, model);
  100.         else
  101.             [gsps(i,:), fobj_gs(i), mlps(i,:), fobj_ml(i), error_code(i), mcmcps(i,:,:)] = ThreeStageFittingVoxel(voxel, protocol, model);
  102.         end
  103.        
  104.         % report to the progress monitor
  105.         if mod(i, progressStepSize)==0
  106.             ppm.increment();
  107.         end
  108.        
  109.     end
  110.    
  111.     % save the temporary results of the split
  112.     if model.noOfStages == 2
  113.         save(outputfile, 'split_end', 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'error_code');
  114.     else
  115.         save(outputfile, 'split_end', 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'mcmcps', 'error_code');
  116.     end
  117.    
  118.     report_cycle_inc=1;
  119.     if mod(iteration, report_cycle_inc)==0
  120.         timeElapsed=etime(clock,startTime);
  121.         fprintf('\nReporting every %i parallel cycles.\nCurrent Voxel: %i of %i\t Percent Done: %.2f%%\t Time Elapsed: %.3fh\t Est time remaining: %.3fh\n\n', report_cycle_inc, split_end, numOfVoxels, split_end/numOfVoxels*100, timeElapsed/60/60, (timeElapsed*numOfVoxels/split_end-timeElapsed)/60/60);
  122.     end
  123.    
  124. end
  125.  
  126. toc
  127.  
  128. ppm.delete();
  129.  
  130. % save the fitted parameters
  131. if model.noOfStages == 2
  132.     save(outputfile, 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'error_code');
  133. else
  134.     save(outputfile, 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'mcmcps', 'error_code');
  135. end
  136.  
  137. % close the parallel pool
  138. delete pool;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement