-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mat_file.m
121 lines (88 loc) · 3.79 KB
/
create_mat_file.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
116
117
118
119
120
121
function create_mat_file(dir, output)
%Generates .mat file for Automated CMB Detection
%Written by Yicheng Chen 2019
%Modified by Melanie Morrison 2022
%dir = path to subject subfolders, where you will also store the output of subjectlist.m
%output = output path, likely samepath as dir
clear; clc;
start_i = 1;
% --------------- load datadir.mat file ----------------
load([dir '/datadir.mat']) % run subjectlist.m to generate datadir.mat first.
mkdir(output)
% Extract required data from images and labels
% save as .mat data files. Name: {Subject_Num}_{Scan_Num}.mat
for i = start_i : start_i + length(datadir)
scans = datadir{i - start_i + 1};
for j = 1:length(scans)
try
fprintf('Subject %d, Scan %d\n', i, j);
[f1, fp_file] = system(['ls ' scans{i} '/*nonproj*false_positives*.nii']);
[f2, cmb_file] = system(['ls ' scans{j} '/*nonproj*finalcorrected*.nii']);
[f3, swi_file] = system(['ls ' scans{j} '/*scaled.nii']);
fp_file = strsplit(fp_file, '\n');
fp_file = [fp_file{1, 1} ' '];
cmb_file = strsplit(cmb_file, '\n');
cmb_file = [cmb_file{1, 1} ' '];
swi_file = strsplit(swi_file, '\n');
swi_file = [swi_file{1, 1} ' '];
fprintf('%s\n%s\n%s\n', swi_file, fp_file, cmb_file);
cmb = load_untouch_nii(cmb_file(1:end-5));
fp = load_untouch_nii(fp_file(1:end-5));
swi = load_untouch_nii(swi_file(1:end-5));
cmb_mask = cmb.img;
fp_mask = fp.img;
pixel_size = cmb.idf.pixelsize;
disp(pixel_size);
centroids_cmb = mask2centroid(cmb_mask);
centroids_fp = mask2centroid(fp_mask);
swi = swi.img;
n_cmb = size(centroids_cmb, 1);
n_fp = size(centroids_fp, 1);
scan_name = datadir{i-start_i+1}{j};
save([output_dir num2str(i-1) '_' num2str(j) '.mat'], ...
'pixel_size', 'centroids_cmb', 'centroids_fp', 'swi', 'cmb_mask', 'fp_mask', ...
'n_cmb', 'n_fp', 'scan_name', 'cmb_file', 'fp_file', 'swi_file');
catch
disp('Error!')
continue
end
% -----------------------------------------------------
% Each mat contains data of one scan:
% scan_name: name of the scan
% cmb_file: CMB label(mask) filename
% fp_file: FP label(mask) filename
% swi_file: SWI filename
% pixel_size: Pixel size of the scan
% centroids_cmb: centroids of CMBs, n_cmbx3 matrix
% centroids_fp: centroids of FPs, n_fpx3 matrix
% swi: SWI volume
% cmb_mask: CMB binary mask
% fp_mask: FP binary mask
% n_cmb: number of CMBs
% n_fp: number of FPs
end
end
%% check saved mat and get a summary (save as txt file)
total_cmb = 0;
total_fp = 0;
summary_file = fullfile(output, 'data_summary.txt');
fid = fopen(summary_file,'w');
for i = 1:length(datadir)
patient = datadir{i};
for j = 1:length(patient)
try
load([output num2str(i-1) '_' num2str(j) '.mat'])
line = sprintf('%3d-%2d: Scan name: %60s, CMBs: %4d, FPs: %4d\n', i-1, j, [dir scan_name], n_cmb, n_fp);
fprintf(fid, line);
fprintf(line);
total_cmb = total_cmb + n_cmb;
total_fp = total_fp + n_fp;
catch
continue
end
end
end
line = sprintf('Total CMB: %d\nTotal FP : %d\n', total_cmb, total_fp);
fprintf(fid, line);
fprintf(line);
fprintf('Wrote to file: %s\n', summary_file);