Tidy Culprit and WALLI


%rename folder name with patient info
maindir = 'Z:\Culprit Plaque\USA';  % contains folders of each patient, MR folder in each folder, and in each MR folder is .dcm
Destdir='E:\C-Tsinghua\Culprit';  % patient name as folder name, in each starting from 1.dcm
serial='3DI_MC';
minslicenum=70;
continuenotfoundthenskip=2; %search extra number of serial, if not found times exceed this number, then skip
sourcedat={};
subdir  = dir( maindir );
display(['Total: ',mat2str(numel(subdir([subdir(:).isdir]))-2),' cases with serial ',serial,' to download']);
for i = 16 : length( subdir )
    sourcedat={};
    if( isequal( subdir(i).name, '.' )||...
        isequal( subdir(i).name, '..')||...
        ~subdir(i).isdir)               
        continue;
    end

%%% IF SUBSUB dir exist
    subdirpath= fullfile(maindir, subdir(i).name);
    pardir=dir(subdirpath);
    for par=1:length(pardir)
        if( isequal( pardir(par).name, '.' )||...
            isequal( pardir(par).name, '..')||...
            ~pardir(par).isdir)               
            continue;
        end
        display(['Sub dir: ',mat2str(par-2)]);
        subdirpath=fullfile(maindir, subdir(i).name, pardir(par).name);
%%% IF SUBSUB dir exist
    
    notfoundlastseh=0;
    for seh=1:20
        notfoundlastseo=0;
        for seo=1:10
            
            searchformat=['\**\MR\*S',mat2str(seh),'0',mat2str(seo),'I*.dcm'];
            dat=rdir([subdirpath,searchformat]);  
            if ~isempty(dat)
                notfoundlastseo=0;
                notfoundlastseh=0;
            	firstdcm=fullfile(dat(1).name);
            	metadata=dicominfo(firstdcm);
                if strcmp(metadata.SeriesDescription,serial)&& length(dat)>=minslicenum
                    warning(['HIT: ','Serial ',mat2str(seh),'0',mat2str(seo),': ',metadata.SeriesDescription,' *',mat2str(length(dat))]);
                    sourcedat=dat;
                    break;
                else
                    display(['Ingore: ','Serial ',mat2str(seh),'0',mat2str(seo),': ',metadata.SeriesDescription,' *',mat2str(length(dat))]);    
                end
            else
                %display(['Not found: ','Serial ',mat2str(seh),'0',mat2str(seo)]);    
                notfoundlastseo=notfoundlastseo+1;
            end
            if notfoundlastseo==continuenotfoundthenskip
                if seo==continuenotfoundthenskip
                    notfoundlastseh=notfoundlastseh+1;
                end
                break;
            end
        end
        if ~isempty(sourcedat) || notfoundlastseh==continuenotfoundthenskip
            break;
        end
    end
    
       
    if isempty(sourcedat)
        display(['No such serial in case #',mat2str(i-2)]);
        continue;
    end
    
    filename = fullfile(sourcedat(1).name); % select first dcm to get metadata
    metadata=dicominfo(filename);
    
    patientname=metadata.PatientName.FamilyName;
    patientsex=metadata.PatientSex;
    if isfield(metadata, 'PatientAge')
        patientage=str2num(metadata.PatientAge(1:end-1));
    else
        patientage=str2num(metadata.ContentDate(1:4))-str2num(metadata.PatientBirthDate(1:4));
    end
    newfoldername=[mat2str(patientage), '_', patientname, '_' , patientsex];
    
    display(['Processing and downloading case #',mat2str(i-2),': ',newfoldername]);

    n=length(sourcedat);
    loc=zeros(n,2);
    for j=1:n
        Fullfilename=fullfile(sourcedat(j).name);
        metadata=dicominfo(Fullfilename);
        loc(j,:)=[j metadata.SliceLocation];
    end
    locasc=sortrows(loc,2);
    if exist(fullfile(Destdir,newfoldername),'dir')~=7
        mkdir(fullfile(Destdir,newfoldername));
    end
    for j=1:n
        movfrom=fullfile(sourcedat(locasc(j,1)).name);
        movto=fullfile(Destdir,newfoldername,[mat2str(j),'.dcm']);
        %movefile(movfrom,movto);
        copyfile(movfrom,movto);
    end
    if length(dir(subdirpath))==2
%        rmdir(subdirpath);
    end
    
%%% IF SUBSUB dir exist
        if ~isempty(sourcedat)
            break;
        end
    end
%%% IF SUBSUB dir exist
    display(['----------',mat2str(i-2),'-END----------']);
end
display(['----------FINISHED----------']);


Last Article Next article

Comment 评论



Share 分享

New Users 最新加入

  • hokurikustr

  • refrain

New comments 最新评论

test123: aasdas Details Apr 13 16:39
admin: Thanks! Details Apr 09 11:46
admin: Google map api Details Apr 09 11:46
lqj12: cooooooooool Details Apr 08 21:34
Yunhan Huang: 这个功能是如何实现的? Details Apr 08 13:23