-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdicomsort_alt.m
115 lines (100 loc) · 3.37 KB
/
dicomsort_alt.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function dicomsort_alt(subjectParent)
% dicomSort is a recursive dicom sorting tool.
% dicomsort(input) recursively sorts all dicom files in a study folder
% regardless of directory structure or hierarchy, but preserves original
% folders, so no PatientID is involved.
%
% Syntax:
% dicomsort_alt(subjectParent)
%
% Arguments:
% subjectParent --> Input path
% Parent folder contiaing all subject folders
% Type: char | string
%
% Author: Siddhartha Dhiman
% Email: [email protected]
% First created on 02/15/2019 using MATLAB 2018b
%
% SEE ALSO DICOMSORT DIR DICOMINFO COPYFILE MOVEFILE
studyPath = subjectParent;
%% Create Subject List
subjectDir = dir(studyPath);
% Clean up listing
rmIdx = zeros(1,length(subjectDir));
for i = 1:length(subjectDir)
if any(startsWith(subjectDir(i).name,'.'));
rmIdx(i) = 1;
else
% If nothing found, don't mark for deletion
rmIdx(i) = 0;
end
end
subjectDir(rmIdx ~= 0) = [];
%% Sort Dicoms
parfor j = 1:length(subjectDir)
%% Create Recirsive Listing
studyDirDel = dir(fullfile(studyPath,subjectDir(j).name));
studyDir = dir(fullfile(studyPath,subjectDir(j).name,'**/*'));
fprintf('Subject %s: Found %d files\n',subjectDir(j).name,length(studyDir));
% Clean-up Dicom Directory Listing
rmIdx = zeros(1,length(studyDir));
for i = 1:length(studyDir)
% Check for directories
if studyDir(i).isdir == 1
rmIdx(i) = 1;
% Check for files starting with'.'
elseif any(startsWith(studyDir(i).name,'.'));
rmIdx(i) = 1;
else
% If nothing found, don't mark for deletion
rmIdx(i) = 0;
end
end
studyDir(rmIdx ~= 0) = []; % Apply deletion filter
nFiles = length(studyDir);
% Clean-up Deletion Listing
rmIdx = zeros(1,length(studyDirDel));
for i = 1:length(studyDirDel)
% Check for files starting with'.'
if any(startsWith(studyDirDel(i).name,'.'));
rmIdx(i) = 1;
% Check for directories
elseif studyDirDel(i).isdir == 1
rmIdx(i) = 0;
else
% If nothing found, don't mark for deletion
rmIdx(i) = 1;
end
end
studyDirDel(rmIdx ~= 0) = []; % Apply deletion filter
for i = 1:nFiles
try
tmp = dicominfo(fullfile(studyDir(i).folder,studyDir(i).name));
catch
continue
end
% Replace all '.' in protocol names with '_'
tmp.ProtocolName = strrep(tmp.ProtocolName,'.','_');
% Replace spaces
tmp.ProtocolName = strrep(tmp.ProtocolName,' ', '_');
% Define copy/move folder
defineFolder = fullfile(studyPath,subjectDir(j).name,...
sprintf('%s_%d',tmp.ProtocolName,tmp.SeriesNumber));
if ~exist(defineFolder,'dir')
mkdir(defineFolder);
end
if ~contains(studyDir(i).name,'.dcm')
newName = [studyDir(i).name '.dcm'];
else
newName = studyDir(i).name;
end
movefile(tmp.Filename,fullfile(defineFolder,newName));
end
% Delete old folders from StudyDirDel
for i = 1:length(studyDirDel)
rmdir(fullfile(studyDirDel(i).folder,studyDirDel(i).name),'s');
end
end
fprintf('done\n')
end