-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshape_train.m
124 lines (118 loc) · 4.93 KB
/
shape_train.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
function shape_train(input,opt)
% FORMAT shape_train(input,opt)
% input - Matlab structure or JSON file containing input filenames
% opt - Matlab structure or JSON file containing option values
%
% Train a shape model on some data. This is a wrapper around the
% `shape_model` function that can take JSON files as inputs and that can be
% compiled and used on command line.
%
% Both json should contain structured values (equivalent to a a maltab
% structure). Type `help shape_model` for details on these structures and
% their fields.
% ---------------------------------------------------------------------
% Read options
if nargin > 1
if ischar(opt)
try
opt = spm_jsonread(opt);
catch
error('Error reading the option JSON file')
end
elseif ~isstruct(opt)
error('Argument `opt `should be either a Matlab structure or a JSON file')
end
else
opt = struct;
end
% ---------------------------------------------------------------------
% Read input files
if nargin == 0 || (ischar(input) && ...
(strcmp(input,'--help') || ...
strcmp(input,'--h') || ...
strcmp(input,'-h')))
show_instructions;
return
elseif ischar(input)
try
input = spm_jsonread(input);
catch
error('Error reading the input JSON file')
end
elseif ~isstruct(opt)
error('Argument `input` should be either a Matlab structure or a JSON file')
end
% ---------------------------------------------------------------------
% Convert input matrices
if isfield(opt, 'z') && isfield(opt.z, 'A0')
if ischar(opt.z.A0) && exist(opt.z.A0, 'file')
opt.z.A0 = load(opt.z.A0);
elseif isnumeric(opt.z.A0) && isvector(opt.z.A0)
opt.z.A0 = diag(opt.z.A0);
end
end
if isfield(opt, 'q') && isfield(opt.q, 'A0')
if ischar(opt.q.A0) && exist(opt.q.A0, 'file')
opt.q.A0 = load(opt.q.A0);
elseif isnumeric(opt.q.A0) && isvector(opt.q.A0)
opt.q.A0 = diag(opt.q.A0);
end
end
% ---------------------------------------------------------------------
% Run the algorithm
shape_model(input,opt);
end
% Temporary help
function show_instructions
descr_str = 'Train a shape model on a set of 2D or 3D images';
usg_str = 'Usage: shape_train input.json opt.json';
cpr_str = 'Copyright (C) 2018 Wellcome Centre for Human Neuroimaging';
help_str = ['' ...
'INPUT\n' ...
'-----\n' ...
'\n' ...
'The input structure should contain the key "f" associated to a list of \n' ...
'filenames (NIfTI or Analyze format) containing the observed images.\n' ...
'They can be binary, categorical or intensity images. If the list is \n' ...
'two-dimensional, the second dimension (i.e., the most nested one)\n' ...
'should contain different classes (or modalities) of the same subject.\n' ...
'\n' ...
'Optionally, the keys "w", "mu" and "a" can be used to provide an initial\n' ...
'value for, respectively, the principal subspace, template or log-template.\n' ...
'\n' ...
'OPTIONS\n' ...
'-------\n' ...
'\n' ...
'Options take the form of a hierarchical dictionary. We provide\n' ...
'descriptions for the most useful ones using Matlab notations\n' ...
'(key1.key2 should be written {"k1":{"k2":value}} in JSON).\n' ...
'A complete list can be found in the online documentation or README file.\n' ...
'\n' ...
'model.name - Data type/model: ''categorical''/''bernoulli''/[''normal'']\n' ...
'model.nc - (categorical only) Number of classes [from input]\n' ...
'pg.K - Number of principal geodesics [32]\n' ...
'pg.prm - Parameters of the geodesic operator [0.001 0 10 0.1 0.2]\n' ...
'tpl.vs - Template lattice voxel size [auto]\n' ...
'tpl.lat - Template lattice dimensions [auto]\n' ...
'tpl.prm - Parameters of the field operator [0.001 0.1 0]\n', ...
'v.l0 - Prior expected anatomical noise precision [17]\n' ...
'f.M - Force same voxel-to-world to all images [from file]\n' ...
'lb.threshold - Convergence criterion (lower bound gain) [1e-3]\n' ...
'split.par - Parallelise processing (number of workers): 0/n/[auto]\n' ...
'ui.verbose - Talk during processing [true]\n' ...
'dir.model - Directory where to store model data [''.'']\n' ...
'dir.dat - Directory where to store data arrays [next to input]\n' ...
];
fprintf([repmat('_',1,80) '\n']);
fprintf('\n');
fprintf([' ' descr_str '\n']);
fprintf('\n');
fprintf([' ' usg_str '\n']);
fprintf('\n');
fprintf([repmat('-',1,80) '\n']);
fprintf('\n');
fprintf(help_str);
fprintf('\n');
fprintf([repmat('_',1,80) '\n']);
fprintf([' ' cpr_str '\n']);
end