File: write_mrtrix.m

package info (click to toggle)
mrtrix3 3.0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,300 kB
  • sloc: cpp: 130,470; python: 9,603; sh: 597; makefile: 62; xml: 47
file content (158 lines) | stat: -rw-r--r-- 5,065 bytes parent folder | download
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
% Copyright (c) 2008-2025 the MRtrix3 contributors.
%
% This Source Code Form is subject to the terms of the Mozilla Public
% License, v. 2.0. If a copy of the MPL was not distributed with this
% file, You can obtain one at http://mozilla.org/MPL/2.0/.
%
% Covered Software is provided under this License on an "as is"
% basis, without warranty of any kind, either expressed, implied, or
% statutory, including, without limitation, warranties that the
% Covered Software is free of defects, merchantable, fit for a
% particular purpose or non-infringing.
% See the Mozilla Public License v. 2.0 for more details.
%
% For more details, see http://www.mrtrix.org/.

function write_mrtrix (image, filename)

% function: write_mrtrix (image, filename)
%
% write the data contained in the structure 'image' in the MRtrix
% format image 'filename' (i.e. files with the extension '.mif' or '.mih').
%
% 'image' is either a N-dimensional array (N <= 16), or a structure containing
% the following fields:
%    image.data:            a N-dimensional array (N <= 16)
%    image.vox:             N-vector of voxel sizes (in mm) (default: { 2 }) [optional]
%    image.comments:        a cell array of strings [optional]
%    image.datatype:        the datatype specifier (default: float32) [optional]
%    image.mrtrix_version:  a character array [optional]
%    image.transform:       a 4x4 matrix [optional]
%    image.dw_scheme:       a NDWx4 matrix of gradient directions [optional]
 
% helper function: acts as ternary conditional operator
choose = @(varargin)varargin{end-varargin{1}};


fid = fopen (filename, 'w');
assert(fid ~= -1, 'error opening %s', filename);
fprintf (fid, 'mrtrix image\ndim: ');

if isstruct(image)
  dim = size(image.data);
else
  dim = size(image);
end

while prod(size(dim)) < 3
  dim(end+1) = 1;
end

fprintf (fid, '%d', dim(1));
fprintf (fid, ',%d', dim(2:end));

fprintf (fid, '\nvox: ');
if isstruct (image) && isfield (image, 'vox')
  fprintf (fid, '%.15f', image.vox(1));
  fprintf (fid, ',%.15f', image.vox(2:end));
else
  fprintf(fid, '2');
  fprintf(fid, ',%d', 2*ones(1,size(dim,2)-1));
end

fprintf (fid, '\nlayout: +0');
fprintf (fid, ',+%d', 1:(size(dim,2)-1));

[computerType, maxSize, endian] = computer;
if isstruct (image) && isfield (image, 'datatype')
  datatype = lower(image.datatype);
  switch datatype(end-1:end)
    case 'le', precision = datatype(1:end-2); byteorder = 'l';
    case 'be', precision = datatype(1:end-2); byteorder = 'b';
    otherwise
      switch datatype
        case 'bit',                precision = 'bit1'; byteorder = 'n';
        case { 'int8', 'uint8' },  precision = datatype; byteorder = 'n';
        otherwise, 
          precision = datatype;
          datatype = [ datatype choose(endian=='L','le','be') ];
          byteorder = choose (endian=='L', 'l', 'b');
      end
  end
else
  datatype = [ 'float32' choose(endian=='L','le','be') ];
  precision = 'float32';
  byteorder = choose (endian=='L', 'l', 'b');
end
fprintf (fid, [ '\ndatatype: ' datatype ]);

fprintf (fid, '\nmrtrix_version: %s', 'matlab');

if isstruct (image) && isfield (image, 'transform')
  fprintf (fid, '\ntransform: %.15f', image.transform(1,1));
  fprintf (fid, ',%.15f', image.transform(1,2:4));
  fprintf (fid, '\ntransform: %.15f', image.transform(2,1));
  fprintf (fid, ',%.15f', image.transform(2,2:4));
  fprintf (fid, '\ntransform: %.15f', image.transform(3,1));
  fprintf (fid, ',%.15f', image.transform(3,2:4));
end

if isstruct (image) && isfield (image, 'dw_scheme')
  for i=1:size(image.dw_scheme,1)
    fprintf (fid, '\ndw_scheme: %.15f', image.dw_scheme(i,1));
    fprintf (fid, ',%.15f', image.dw_scheme(i,2:4));
   end
end

% write out any other fields:
if isstruct (image)
  f = fieldnames(image);
  % don't worry about fields that have already been dealt with:
  f(strcmp(f,'dim')) = [];
  f(strcmp(f,'vox')) = [];
  f(strcmp(f,'layout')) = [];
  f(strcmp(f,'datatype')) = [];
  f(strcmp(f,'transform')) = [];
  f(strcmp(f,'dw_scheme')) = [];
  f(strcmp(f,'data')) = [];

  % write out contents of the remainging fields:
  for n = 1:numel(f)
    val = getfield (image, f{n});
    if iscell (val)
      for i=1:numel(val)
        fprintf (fid, '\n%s: %s', f{n}, val{i});
      end
    else
      fprintf (fid, '\n%s: %s', f{n}, val);
    end
  end
end

fprintf (fid, '\nfile: ');

[filepath,basename,ext] = fileparts(filename);
if strcmp(ext, '.mif')
  dataoffset = ftell (fid) + 18;
  dataoffset = dataoffset + mod((4 - mod(dataoffset, 4)), 4);
  s = sprintf ('. %d\nEND\n              ', dataoffset);
  s = s(1:(dataoffset-ftell(fid)));
  fprintf (fid, s);
  fclose (fid);
  fid = fopen (filename, 'a+', byteorder);
elseif strcmp(ext, '.mih')
  datafile = fullfile(filepath, strcat(basename,'.dat'));
  fprintf (fid, '%s 0\nEND\n', datafile);
  fclose(fid);
  fid = fopen (datafile, 'w', byteorder);
else
  fclose(fid);
  error('unknown file suffix "%s" - aborting', ext);
end

if isstruct(image)
  fwrite (fid, image.data, precision);
else
  fwrite (fid, image, precision);
end
fclose (fid);