Advertisement
Guest User

batch_fitting_single[CUSTOM-TIMER]

a guest
Sep 5th, 2018
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.34 KB | None | 0 0
  1. function batch_fitting_single(roifile, protocol, model, outputfile)
  2.  
  3. %
  4. % function batch_fitting_single(roifile, protocol, model, outputfile)
  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. % author: Gary Hui Zhang (gary.zhang@ucl.ac.uk)
  20. %
  21.  
  22.  
  23. % first check if there is a file there to resume
  24. if exist(outputfile, 'file')
  25.     load(outputfile);
  26.     if exist('current_index', 'var')
  27.         % previously run and need to be restarted
  28.         current_index = current_index + 1;
  29.         fprintf('Resume an interrupted run from voxel %i\n', current_index);
  30.     else
  31.         % completed
  32.         fprintf('An output file of the same name detected.\n');
  33.         fprintf('Choose a different output file name.\n');
  34.         return;
  35.     end
  36. else
  37.     % if this is the first run
  38.     current_index = 1;
  39. end
  40.  
  41. % load the roi file
  42. load(roifile);
  43. numOfVoxels = size(roi,1);
  44.  
  45. % set up the fitting parameter variables if it is the first run
  46. if current_index == 1
  47.     gsps = zeros(numOfVoxels, model.numParams);
  48.     mlps = zeros(numOfVoxels, model.numParams);
  49.     fobj_gs = zeros(numOfVoxels, 1);
  50.     fobj_ml = zeros(numOfVoxels, 1);
  51.     error_code = zeros(numOfVoxels, 1);
  52.     if model.noOfStages == 3
  53.         mcmcps = zeros(numOfVoxels, model.MCMC.samples, model.numParams + 1);
  54.     end
  55. end
  56.  
  57. tic
  58.  
  59. fprintf('%i of voxels to fit. Approx Time Left is accurate over multiple instances. \n', numOfVoxels-current_index+1);
  60. numOf_Total_Voxels_To_Render=uint32(numOfVoxels-current_index+1);
  61. startTime=clock;
  62. loop_iterations=0;
  63.  
  64. % start the parallel fitting
  65. for i=current_index:numOfVoxels
  66.     loop_iterations=loop_iterations+1;
  67.     if mod(i,5)==0
  68.         fprintf('Fitting voxel %i of %i\n', i, numOf_Total_Voxels_To_Render);
  69.     end
  70.    
  71.     % get the MR signals for the voxel i
  72.     voxel = roi(i,:)';
  73.    
  74.     % fit the voxel
  75.     if model.noOfStages == 2
  76.         [gsps(i,:), fobj_gs(i), mlps(i,:), fobj_ml(i), error_code(i)] = ThreeStageFittingVoxel(voxel, protocol, model);
  77.     else
  78.         [gsps(i,:), fobj_gs(i), mlps(i,:), fobj_ml(i), error_code(i), mcmcps(i,:,:)] = ThreeStageFittingVoxel(voxel, protocol, model);
  79.     end
  80.    
  81.     % save the temporary results
  82.     if mod(i, 100)==0
  83.         current_index = i;
  84.         if model.noOfStages == 2
  85.             save(outputfile, 'current_index', 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'error_code');
  86.         else
  87.             save(outputfile, 'current_index', 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'mcmcps', 'error_code');
  88.         end
  89.         timeElapsed=etime(clock,startTime);
  90.         fprintf('Percent Done:%i%%\t Time Elapsed: %.3fh\t Approx Time Left: %i.3h\t Saved.\n', i/numOf_Total_Voxels_To_Render*100, (timeElapsed/60)/60, (((timeElapsed*(numOf_Total_Voxels_To_Render-i)/loop_iterations)-timeElapsed)/60)/60);
  91.        
  92.     end
  93.    
  94. end
  95.  
  96. toc
  97.  
  98. % save the fitted parameters
  99. if model.noOfStages == 2
  100.     save(outputfile, 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'error_code');
  101. else
  102.     save(outputfile, 'model', 'gsps', 'fobj_gs', 'mlps', 'fobj_ml', 'mcmcps', 'error_code');
  103. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement