-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpet_Modelling.m
140 lines (115 loc) · 4.26 KB
/
pet_Modelling.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
function outDataset = pet_Modelling(pet_ds, idif_ds, mask_ds, destination, varargin)
%% Calculates Vt and intersection for pet images in pet_ds
% path and name of current script
[pathStep, procStep] = fileparts(mfilename('fullpath'));
% Optional parameters definition
args = inputParser();
args.addParameter('subjects', '.*');
args.addParameter('name', procStep);
args.addParameter('config', fullfile(pathStep, 'config', 'pet.json'));
args.addParameter('configsection', 'modelling');
args.parse(varargin{:});
% Getting json config file
if ischar(args.Results.config)
params = spm_jsonread(args.Results.config);
else
params = args.Results.config;
end
params = params.(args.Results.configsection);
% Exporting parameters as variables
procStep = args.Results.name;
subjects = args.Results.subjects;
outDataset = fullfile(destination, procStep);
% This will load bidsified dataset into BIDS structure
PET = bids.layout(pet_ds,...
'use_schema', false,...
'index_derivatives', false,...
'tolerant', true);
crc_bids_gen_dervative(PET, destination, procStep,...
params.image,...
subjects);
if strcmp(pet_ds, idif_ds)
IDIF = PET;
else
IDIF = bids.layout(idif_ds,...
'use_schema', false,...
'index_derivatives', false,...
'tolerant', true);
end
crc_bids_gen_dervative(IDIF, destination, procStep,...
params.idif,...
subjects);
if ~isempty(mask_ds)
if strcmp(pet_ds, mask_ds)
MASK = PET;
else
MASK = bids.layout(mask_ds,...
'use_schema', false,...
'index_derivatives', false,...
'tolerant', true);
end
query = params.mask.query;
if ~isempty(subjects)
query.sub = subjects;
end
crc_bids_gen_dervative(MASK, destination, procStep,...
params.mask,...
subjects);
end
outDataset = fullfile(destination, procStep);
% loading derivated datast as BIDS layout
DERIV = bids.layout(outDataset,...
'use_schema', false,...
'index_derivatives', false,...
'tolerant', true);
% getting list of subjects
subjects = bids.query(DERIV,'subjects', 'sub', subjects);
for iSub = 1:numel(subjects)
sub = subjects{iSub};
fprintf('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n');
fprintf('Processing subject %d/%d %s\n', iSub, numel(subjects), sub);
fprintf('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n');
base_dir = fullfile(outDataset, ['sub-' sub]);
Logan_dir = fullfile(base_dir, 'Logan');
try
% Retrieving image and times of frames
pet_img = crc_bids_query_data(DERIV, params.image, sub, 'images');
query = params.image.query;
query.sub = sub;
query.target = 'FrameTimesStart';
f_start = bids.query(DERIV, 'metadata', query);
query.target = 'FrameDuration';
f_duration = bids.query(DERIV, 'metadata', query);
if ~iscell(f_start)
f_start = {f_start};
f_duration = {f_duration};
end
% Retrieving IDIF function
idif_tab = crc_bids_query_data(DERIV, params.idif, sub, 'idif');
idif_tab = spm_load(idif_tab{1});
idif = [idif_tab.onset idif_tab.(params.idif.IF)];
for img = 1:size(pet_img, 1)
frames = [f_start{img} f_start{img} + f_duration{img}];
% Retrieving brain mask
if ~isempty(mask_ds)
mask_img = crc_bids_query_data(DERIV, params.mask, sub, 'brain mask');
mask_img = mask_img{1};
else
mask_img = {};
end
if ~exist(Logan_dir, 'dir')
mkdir(Logan_dir);
end
[~, basename, ~] = fileparts(pet_img{img});
fprintf('%s\n', basename);
pet_Model.logan_image(pet_img{img}, idif, frames, mask_img, ...
params.start_time, ...
params.end_time, ...
Logan_dir);
end
catch ME
warning('Subject %s failed: %s', sub, ME.getReport('extended'));
continue;
end
end
end