-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregion_deform_cifti.m
69 lines (61 loc) · 2.33 KB
/
region_deform_cifti.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
function mycifti=region_deform_cifti(R,cifti,varargin)
% function new_cifti=region_deform_cifti(R,cifti)
% Deforms data stored in a cifti structure (returned by cifti_read)
% into the original group space
% Comments:
% This function relies on cifti-matlab
% https://github.com/Washington-University/cifti-matlab
% Extension: should make this work with surface regions as well
% INPUT:
% R: Regions
% cfiti: Cifti file information (returned by cift-read)
% OPTIONAL:
% vol: Volume file information
% OUPUT:
% new_cifti: deformed cifti
% 2023 joern.diedrichsen@googlemail.com
vol =[];
vararginoptions(varargin,{'vol'});
if ~iscell(R)
R={R};
end
num_regions = length(R);
if length(cifti.diminfo{1}.models) ~=num_regions
error('number of regions must match number of brainmodels in cifti file');
end
mycifti=cifti;
mycifti.metadata = cifti_metadata_set(mycifti.metadata, 'Provenance', 'region_deform_cifti');
if isempty(vol)
spm_vol(R{1}.file);
end
mycifti.cdata = [];
% make the brainmodel axis
mycifti.diminfo{1}.vol.dims = vol.dim; % Image dimension
mycifti.diminfo{1}.vol.sform = vol.mat; % Image affine
% Now loop over regions and make them into brain models
c_old = 1; % Count old
c_new = 1; % New count
for r=1:length(R)
if size(R{r}.data,1) ~= cifti.diminfo{1}.models{r}.count
error('Number voxels in brainmodel does not fit region');
end
bm.start=c_new;
bm.count = size(R{r}.original,1);
bm.struct = cifti.diminfo{1}.models{r}.struct; % Unfortunately, we can't set the structure name to anything arbitary - need to
bm.type = cifti.diminfo{1}.models{r}.type;
% Map data into new space
if bm.type=='vox'
[i,j,k] = spmj_affine_transform(R{r}.original(:,1),R{r}.original(:,2),R{r}.original(:,3),inv(vol.mat));
bm.voxlist = int16([i j k]');
data = nan(bm.count,size(cifti.cdata,2));
idx = R{r}.map>0;
data(idx,:) = cifti.cdata(R{r}.map(idx),:); % Get the orginal data
else
error('Surface ROI not implemented yet');
end
c_new = c_new + bm.count;
c_old = c_old + cifti.diminfo{1}.models{r}.count;
mycifti.cdata = [mycifti.cdata;data];
mycifti.diminfo{1}.models{r}=bm;
end
mycifti.diminfo{1}.length=size(mycifti.cdata,1);