forked from rordenlab/spmScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnii_orderBval.m
76 lines (72 loc) · 2.12 KB
/
nii_orderBval.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
function nii_orderBval (bval)
%DKE requires DWI volumes sorted by bvalue
% bval: name of bval file to reorder (assumes file.bval, file.bvec,file.nii)
% Examples
% nii_orderBval('DWI_dir42_AP_27_EP.bval');
if ~exist('bval','var')
bval = spm_select(1,'^.*\.(bval)$','Select b-value file to re-order');
end;
if isempty(bval) || ~exist(bval,'file'), error('Unable to find bval file'); end
%load data
[hd,im, bve,bva] = loadSub(bval);
%sort data
if issorted(bva), warning('Already sorted "%s"', bval); return; end;
[sbva, idx] = sort(bva);
sbve = bve(idx, :);
dims = size(im);
im = reshape(im, prod(dims(1:3)), dims(4));
sim = im(:, idx);
sim = reshape(sim, dims);
%save data
[p,n,x] = fileparts(bval);
n = ['o', n]; %ordered
dlmwrite(fullfile(p,[ n, '.bval']), sbva','delimiter','\t');
dlmwrite(fullfile(p,[n, '.bvec']), sbve','delimiter','\t');
hd.fname = fullfile(p,[n,'.nii']);
for vol=1:dims(4)
hd.n(1)=vol;
spm_write_vol(hd,sim(:, :, :, vol));
end;
%end nii_orderBval()
function [hd,im, bve,bva] = loadSub(fnm)
im = []; bve = []; bva = [];
[p,n,x] = spm_fileparts(fnm);
if (strcmpi(x,'.bvec')) || (strcmpi(x,'.bval'))
fnm = fullfile(p,[n,'.nii']);
if ~exist(fnm,'file')
fnm = fullfile(p,[n,'.nii.gz']);
end
[p,n,x] = spm_fileparts(fnm);
end;
if (length(x)==3) && min((x=='.gz')==1)
fnm = char(gunzip(fnm));
delnam = fnm;
[p,n,x] = spm_fileparts(char(fnm));
else
delnam = '';
end
hd = spm_vol(fnm); %input header
if hd(1).dt(1) == 128
fprintf('Warning: skipping RGB image %s\n', fnm);
if ~isempty(delnam), delete(delnam); end;
return;
end
im = spm_read_vols(hd);%Input image
hd = hd(1);
if ~isempty(delnam), delete(delnam); end;
bve = loadTxt(fullfile(p,[n,'.bvec']), 3);
if isempty(bve), bve = zeros( size(im,4),3); end;
bva = loadTxt(fullfile(p,[n,'.bval']));
if isempty(bva), bva = zeros( size(im,4),1); end;
%end loadSub()
function txt = loadTxt(fnm, nRow)
txt = [];
%fprintf('%s\n', fnm);
if exist(fnm,'file') == 0, return; end;
fileID = fopen(fnm);
txt = cell2mat( textscan(fileID,'%f'));
fclose(fileID);
if exist('nRow', 'var')
txt = reshape(txt, [numel(txt)/nRow nRow]);
end
%end loadTxt()