Home > vbmeg > functions > tool_box > dmri_processor > functions > util > tck_file_processor > dmri_fiber_tck_file_for_display_worker_start.m

dmri_fiber_tck_file_for_display_worker_start

PURPOSE ^

Process tck files.

SYNOPSIS ^

function dmri_fiber_tck_file_for_display_worker_start(parm_mat_file, worker_number)

DESCRIPTION ^

 Process tck files.

 [Usage]
    dmri_fiber_tck_file_for_display_worker_start(parm_mat_file, worker_number);

 [Input]
    parm_mat_file : tck file processing parameter.
          (variables in the file)
                    work_dir : directory of processing files.
                  fa_gz_file : FA nifti-gz file(.nii.gz) Fractional Anisotropy image.
            fa_brain_gz_file : Freesurfer nifti-gz file(.nii.gz) Cortex image.
              trans_info_dir : transform information directory.
    worker_number : This is a number given by manager program

 [Output]
    none

 [Note]
    Many workers(MATLAB) concurrently launched to process tck files.
    So, synchronous processing is required.
    To realize it, directory is used as a exclusion access control.

    1.Fiber tracking program creates directory.
         work_dir/finished/parcelA-B.tck
    2.Worker program try to delete the directory.
      if success, it means that the worker got a authority to 
      process the tck file.

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function dmri_fiber_tck_file_for_display_worker_start(parm_mat_file, worker_number)
0002 % Process tck files.
0003 %
0004 % [Usage]
0005 %    dmri_fiber_tck_file_for_display_worker_start(parm_mat_file, worker_number);
0006 %
0007 % [Input]
0008 %    parm_mat_file : tck file processing parameter.
0009 %          (variables in the file)
0010 %                    work_dir : directory of processing files.
0011 %                  fa_gz_file : FA nifti-gz file(.nii.gz) Fractional Anisotropy image.
0012 %            fa_brain_gz_file : Freesurfer nifti-gz file(.nii.gz) Cortex image.
0013 %              trans_info_dir : transform information directory.
0014 %    worker_number : This is a number given by manager program
0015 %
0016 % [Output]
0017 %    none
0018 %
0019 % [Note]
0020 %    Many workers(MATLAB) concurrently launched to process tck files.
0021 %    So, synchronous processing is required.
0022 %    To realize it, directory is used as a exclusion access control.
0023 %
0024 %    1.Fiber tracking program creates directory.
0025 %         work_dir/finished/parcelA-B.tck
0026 %    2.Worker program try to delete the directory.
0027 %      if success, it means that the worker got a authority to
0028 %      process the tck file.
0029 %
0030 % Copyright (C) 2011, ATR All Rights Reserved.
0031 % License : New BSD License(see VBMEG_LICENSE.txt)
0032 
0033 % Worker create worker directory to notify a manager to ready to process.
0034 % Note: The directory will be deleted by manager.
0035 
0036 load(parm_mat_file);
0037 worker_dir = fullfile(work_dir, ['worker', num2str(worker_number)]);
0038 mkdir(worker_dir);
0039 log_file = fullfile(worker_dir, 'log.txt');
0040 diary_flush(log_file);
0041 
0042 try
0043 
0044 % track file
0045 finished_dir   = fullfile(work_dir, filesep, 'finished');
0046 processed_dir  = fullfile(work_dir, filesep, 'processed');
0047 warp_file = fullfile(trans_info_dir, 'fa2freesurfer_warp.nii.gz');
0048 
0049 % start processing
0050 while(exist(worker_dir, 'dir') == 7)
0051 
0052     d = dir(finished_dir);
0053 
0054     for k=1:length(d)
0055         name = d(k).name;
0056         if strcmp(name, '.') || strcmp(name, '..')
0057             continue;
0058         end
0059         % To obtain an authority to process tck file, try to make a directory.
0060         % if the return value is success, the worker got an authority.
0061         [ret, warn] = mkdir(fullfile(processed_dir, filesep, name));
0062         % (mkdir always retuns 1.... second ret value check)
0063 
0064         % Get process authority
0065         if isempty(warn)
0066             % Success
0067             log_write(sprintf('%s.tck processing start\n', name));
0068             % Process tck file.
0069             base_name = name;
0070             tck_filename  = fullfile(work_dir, [base_name, '.tck']);
0071 
0072             % FA coord file
0073             coord_file    = fullfile(worker_dir, ['coord_', base_name, '.txt']);
0074             % freesurfer coordinate file
0075             fs_coord_file = fullfile(worker_dir, ['fs_coord_', base_name, '.txt']);
0076 
0077             if exist(tck_filename, 'file') ~= 2
0078                 error(sprintf('%s not found.\n', tck_filename));
0079             end
0080             % processing
0081             vtrack  = [];
0082             vtracks = [];
0083             try
0084             vtrack = read_mrtrix_tracks(tck_filename);
0085             catch
0086             end
0087             if ~isempty(vtrack) && ~isempty(vtrack.data)
0088                 ave_data = calc_mean_streamlines({vtrack.data});
0089                 dlmwrite(coord_file, ave_data, 'delimiter', ' ', 'precision', 8);
0090 
0091                 % Non-linear registration of stream lines
0092                 cmd = ['img2imgcoord -mm', ...
0093                        ' -src ', fa_gz_file, ...
0094                        ' -dest ', fs_brain_gz_file, ...
0095                        ' -warp ', warp_file, ...
0096                        ' ', coord_file, ...
0097                        '| sed ''$d'' > ' fs_coord_file];
0098                 dmri_system(cmd);
0099 
0100                 % Read coordinate values in .txt file
0101                 vtracks = dlmread(fs_coord_file, '', 1, 0);
0102             end
0103 
0104             % Write mat file
0105             fs_matfile = fullfile(work_dir, ['fs_', base_name, '.mat']);
0106             save(fs_matfile, 'vtracks');
0107 
0108             log_write(sprintf('%s.tck processing end\n', name));
0109 
0110             % delete processed name from tck list
0111             ret = rmdir(fullfile(finished_dir, filesep, name));
0112             diary_flush(log_file);
0113         else
0114             % Failure
0115             %log_write(sprintf('Failed to get a process authority %s.tck\n', name));
0116         end
0117         % Terminate
0118         if exist(worker_dir, 'dir') ~= 7, break; end
0119         pause(0.01);
0120     end
0121     pause(1);
0122 end
0123 catch
0124     errinfo = lasterror;
0125     log_write(errinfo.message);
0126     diary_flush;
0127     rethrow(errinfo);
0128 end
0129 diary_flush;
0130 exit;
0131 
0132 function log_write(message)
0133    fprintf('%s:%s', datestr(now, 31), message);
0134    
0135 function diary_flush(diary_file)
0136     diary off;
0137     if exist('diary_file', 'var') && ~isempty(diary_file)
0138         diary(diary_file);
0139     end
0140

Generated on Mon 22-May-2023 06:53:56 by m2html © 2005