Pre processing directories for batch import - cerr/CERR GitHub Wiki

Directory structure for batch import (for example, using, batchConvert.m)

All the DICOM files associated with a patient have to be in the same directory. Below is a function convert data exported from MIM to such a flat directory structure

function preProcessMIMdcmForCERRimport(dirName,delim)
% 
% function preProcessMIMdcmForCERRimport(dirName,delim)
% 
% Function to copy RTStruct and RTDose DICOM to the directory containing
% their parent CT.
%
% INPUTS:
% dirName: top-level directory containing MIM exported DICOM
% delim: delimiter for getting to the IDs
% 
% Example: 
% dirName = 'Z:\dicom files\2012-04__Studies';
% delim = '_';
% preProcessMIMdcmForCERRimport(dirName,delim)
%
% APA, 02/10/2017

% Get all files and directories within the passed dir
[filesS,dirsS] = rdir(dirName);

% Get unique patient IDs
allDirC = {dirsS.name};
patIDc = unique(strtok(allDirC,delim));

if ispc
    slashType = '\';
else
    slashType = '/';
end

for id = patIDc
    
    disp(['Processing ', id])
    
    matchC = strfind(allDirC,id);
    
    indMatchV = ~cellfun(@isempty,matchC);
    
    dirMatchS = dirsS(indMatchV);
    
    numDirs = length(dirMatchS);
    
    % continue if only a single directory per ID
    if numDirs == 1
        continue
    end
    
    % Find the dir containing most files
    sizV = zeros(1,numDirs);
    for dirNum = 1:numDirs
        thisDirS = dir(dirMatchS(dirNum).fullpath);
        sizV(dirNum) = length(thisDirS);
    end
    
    [~,parentIndex] = max(sizV);
    
    % copy rtStruct / rtDose dirs to its parent
    dirV = 1:numDirs;
    dirV(parentIndex) = [];
    
    for dirNum = dirV
        copyfile([dirMatchS(dirNum).fullpath,slashType,'*'],...
            dirMatchS(parentIndex).fullpath, 'f')
    end
    
    % remove rtStruct and rtDose directories
    for dirNum = dirV
        success = rmdir(dirMatchS(dirNum).fullpath,'s');
    end
    
end

disp('Finished!')