-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathica_cleaning.m
89 lines (76 loc) · 3.22 KB
/
ica_cleaning.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
% Spatial smoothing the motion-corrected images with a Gaussian kernel
% with FWHM = FWHM_ica; Run ICA on smoothed images with #IC=50
% (feed 'inputs_ica.m' to the GIFT toolbox).
%%%%%%%%%%%%%% set parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data_dir='/path/to/data';
FWHM_ica=0.7; %FWHM=0.7mm
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Yikang Liu
% Last modified data: 05/24/2020
rat_list = dir(fullfile(data_dir, 'rat*'));
for i=1:length(rat_list)
cd(fullfile(data_dir, rat_list(i).name, 'rfmri_intermediate'));
scan_list = dir('*warped.nii.gz');
for j = 1:length(scan_list)
scan_name = scan_list(j).name(1:2);
% make a folder for ICA results
folder = fullfile(pwd, [scan_name, '.gift_ica']);
mkdir(folder);
delete([folder, '/*']);
% load a motion corrected image
nii = load_nii([scan_name, '_warped.nii.gz']);
img = nii.img;
% spatial smoothing
if FWHM_ica > 0
img = rsfmri_smooth(img,FWHM_ica,nii.hdr.dime.pixdim(2));
img(isnan(img)) = 0;
nii.img = img;
tmp = fullfile(pwd, [scan_name, '_despiked_registered_aligned_sm_ica.nii']);
save_nii(nii, tmp);
else
tmp = fullfile(pwd, [scan_name, '_despiked_registered_aligned_sm_ica.nii']);
save_nii(nii, tmp);
end
% write a script for GIFT ICA
script_dir = strrep(mfilename('fullpath'), mfilename, '');
fi = fopen(fullfile(script_dir, 'inputs_ica.m'),'r'); % open a script template
fileo = fullfile(script_dir, ['inputs_ica',...
rat_list(i).name,'_',scan_list(j).name(3:4),'.m']);
fo = fopen(fileo,'w'); % the file to write into
% replace strings in the template to generate script for the scan
while ~feof(fi)
l = fgetl(fi);
if strfind(l,'outputDir') == 1
l = ['outputDir = ''',...
folder, ''';'];
end
if strfind(l,'input_data_file_patterns =') == 1
l = ['input_data_file_patterns = {''',...
tmp, '''};'];
end
fprintf(fo,'%s\n',l);
end
fclose(fi);
fclose(fo);
cd(folder);
icatb_batch_file_run(fileo); % run the script with GIFT ICA
close all;
% delete spatially-smoothed image after ICA
cd(fullfile(data_dir, rat_list(i).name, 'rfmri_intermediate'));
delete(tmp);
% save .json file
fid = fopen([scan_name, '_warped.json'], 'r');
s = fread(fid);
a = jsondecode(char(s)');
a.Steps.Order = [a.Steps.Order, '; IC Noise Cleaning'];
a.Steps.ICA.software = 'GIFT ICA';
a.Steps.ICA.spatial_smoothing_fwhm = [num2str(FWHM_ica), ' mm'];
a.Steps.ICA.IC_number = 50;
s = jsonencode(a);
json = [scan_name, '_cleaned.json'];
fid = fopen(json, 'w');
fwrite(fid, s, 'char');
fclose(fid);
a = loadjson(json); savejson('', a, json); % reformat
end
end