GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/ten/tendMfit.c Lines: 29 66 43.9 %
Date: 2017-05-26 Branches: 1 26 3.8 %

Line Branch Exec Source
1
/*
2
  Teem: Tools to process and visualize scientific data and images             .
3
  Copyright (C) 2013, 2012, 2011, 2010, 2009  University of Chicago
4
  Copyright (C) 2008, 2007, 2006, 2005  Gordon Kindlmann
5
  Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998  University of Utah
6
7
  This library is free software; you can redistribute it and/or
8
  modify it under the terms of the GNU Lesser General Public License
9
  (LGPL) as published by the Free Software Foundation; either
10
  version 2.1 of the License, or (at your option) any later version.
11
  The terms of redistributing and/or modifying this software also
12
  include exceptions to the LGPL that facilitate static linking.
13
14
  This library is distributed in the hope that it will be useful,
15
  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
  Lesser General Public License for more details.
18
19
  You should have received a copy of the GNU Lesser General Public License
20
  along with this library; if not, write to Free Software Foundation, Inc.,
21
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
*/
23
24
#include "ten.h"
25
#include "privateTen.h"
26
27
#define INFO "Estimate models from a set of DW images"
28
static const char *_tend_mfitInfoL =
29
  (INFO
30
   ". More docs here.");
31
32
int
33
tend_mfitMain(int argc, const char **argv, const char *me,
34
              hestParm *hparm) {
35
  int pret;
36
2
  hestOpt *hopt = NULL;
37
1
  char *perr, *err;
38
  airArray *mop;
39
40
1
  Nrrd *nin, *nout, *nterr, *nconv, *niter;
41
1
  char *outS, *terrS, *convS, *iterS, *modS;
42
1
  int knownB0, saveB0, verbose, mlfit, typeOut;
43
1
  unsigned int maxIter, minIter, starts;
44
1
  double sigma, eps;
45
1
  const tenModel *model;
46
  tenExperSpec *espec;
47
48
1
  hestOptAdd(&hopt, "v", "verbose", airTypeInt, 1, 1, &verbose, "0",
49
             "verbosity level");
50
1
  hestOptAdd(&hopt, "m", "model", airTypeString, 1, 1, &modS, NULL,
51
             "which model to fit. Use optional \"b0+\" prefix to "
52
             "indicate that the B0 image should also be saved "
53
             "(independent of whether it was known or had to be "
54
             "estimated, according to \"-knownB0\").");
55
1
  hestOptAdd(&hopt, "ns", "# starts", airTypeUInt, 1, 1, &starts, "1",
56
             "number of random starting points at which to initialize "
57
             "fitting");
58
1
  hestOptAdd(&hopt, "ml", NULL, airTypeInt, 0, 0, &mlfit, NULL,
59
             "do ML fitting, rather than least-squares, which also "
60
             "requires setting \"-sigma\"");
61
1
  hestOptAdd(&hopt, "sigma", "sigma", airTypeDouble, 1, 1, &sigma, "nan",
62
             "Gaussian/Rician noise parameter");
63
1
  hestOptAdd(&hopt, "eps", "eps", airTypeDouble, 1, 1, &eps, "0.01",
64
             "convergence epsilon");
65
1
  hestOptAdd(&hopt, "mini", "min iters", airTypeUInt, 1, 1, &minIter, "3",
66
             "minimum required # iterations for fitting.");
67
1
  hestOptAdd(&hopt, "maxi", "max iters", airTypeUInt, 1, 1, &maxIter, "100",
68
             "maximum allowable # iterations for fitting.");
69
1
  hestOptAdd(&hopt, "knownB0", "bool", airTypeBool, 1, 1, &knownB0, NULL,
70
             "Indicates if the B=0 non-diffusion-weighted reference image "
71
             "is known (\"true\") because it appears one or more times "
72
             "amongst the DWIs, or, if it has to be estimated along with "
73
             "the other model parameters (\"false\")");
74
  /* (this is now specified as part of the "-m" model description)
75
  hestOptAdd(&hopt, "saveB0", "bool", airTypeBool, 1, 1, &saveB0, NULL,
76
             "Indicates if the B=0 non-diffusion-weighted value "
77
             "should be saved in output, regardless of whether it was "
78
             "known or had to be esimated");
79
  */
80
1
  hestOptAdd(&hopt, "t", "type", airTypeEnum, 1, 1, &typeOut, "float",
81
             "output type of model parameters",
82
1
             NULL, nrrdType);
83
1
  hestOptAdd(&hopt, "i", "dwi", airTypeOther, 1, 1, &nin, "-",
84
             "all the diffusion-weighted images in one 4D nrrd",
85
1
             NULL, NULL, nrrdHestNrrd);
86
1
  hestOptAdd(&hopt, "o", "nout", airTypeString, 1, 1, &outS, "-",
87
             "output parameter vector image");
88
1
  hestOptAdd(&hopt, "eo", "filename", airTypeString, 1, 1, &terrS, "",
89
             "Giving a filename here allows you to save out the per-sample "
90
             "fitting error.  By default, no such error is saved.");
91
1
  hestOptAdd(&hopt, "co", "filename", airTypeString, 1, 1, &convS, "",
92
             "Giving a filename here allows you to save out the per-sample "
93
             "convergence fraction.  By default, no such error is saved.");
94
1
  hestOptAdd(&hopt, "io", "filename", airTypeString, 1, 1, &iterS, "",
95
             "Giving a filename here allows you to save out the per-sample "
96
             "number of iterations needed for fitting.  "
97
             "By default, no such error is saved.");
98
99
1
  mop = airMopNew();
100
1
  airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways);
101
2
  USAGE(_tend_mfitInfoL);
102
  JUSTPARSE();
103
  airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways);
104
105
  nterr = NULL;
106
  nconv = NULL;
107
  niter = NULL;
108
  espec = tenExperSpecNew();
109
  airMopAdd(mop, espec, (airMopper)tenExperSpecNix, airMopAlways);
110
  nout = nrrdNew();
111
  airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways);
112
  if (tenModelParse(&model, &saveB0, AIR_FALSE, modS)) {
113
    airMopAdd(mop, err=biffGetDone(TEN), airFree, airMopAlways);
114
    fprintf(stderr, "%s: trouble parsing model \"%s\":\n%s\n", me, modS, err);
115
    airMopError(mop); return 1;
116
  }
117
  if (tenExperSpecFromKeyValueSet(espec, nin)) {
118
    airMopAdd(mop, err=biffGetDone(TEN), airFree, airMopAlways);
119
    fprintf(stderr, "%s: trouble getting exper from kvp:\n%s\n", me, err);
120
    airMopError(mop); return 1;
121
  }
122
  if (tenModelSqeFit(nout,
123
                     airStrlen(terrS) ? &nterr : NULL,
124
                     airStrlen(convS) ? &nconv : NULL,
125
                     airStrlen(iterS) ? &niter : NULL,
126
                     model, espec, nin,
127
                     knownB0, saveB0, typeOut,
128
                     minIter, maxIter, starts, eps,
129
                     NULL, verbose)) {
130
    airMopAdd(mop, err=biffGetDone(TEN), airFree, airMopAlways);
131
    fprintf(stderr, "%s: trouble fitting:\n%s\n", me, err);
132
    airMopError(mop); return 1;
133
  }
134
135
  if (nrrdSave(outS, nout, NULL)
136
      || (airStrlen(terrS) && nrrdSave(terrS, nterr, NULL))
137
      || (airStrlen(convS) && nrrdSave(convS, nconv, NULL))
138
      || (airStrlen(iterS) && nrrdSave(iterS, niter, NULL))) {
139
    airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways);
140
    fprintf(stderr, "%s: trouble writing output:\n%s\n", me, err);
141
    airMopError(mop); return 1;
142
  }
143
144
  airMopOkay(mop);
145
  return 0;
146
1
}
147
TEND_CMD(mfit, INFO);