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 "Non-linear least-squares fitting of b-value curves" |
28 |
|
|
static const char *_tend_bfitInfoL = |
29 |
|
|
(INFO |
30 |
|
|
". Axis 0 is replaced by three values: amp, dec, err, based on a " |
31 |
|
|
"non-linear least-squares fit of amp*exp(-b*dec) to the range of DWI " |
32 |
|
|
"values along input axis 0, as a function of changing b values. "); |
33 |
|
|
|
34 |
|
|
int |
35 |
|
|
tend_bfitMain(int argc, const char **argv, const char *me, |
36 |
|
|
hestParm *hparm) { |
37 |
|
|
int pret; |
38 |
|
2 |
hestOpt *hopt = NULL; |
39 |
|
1 |
char *perr, *err; |
40 |
|
|
airArray *mop; |
41 |
|
|
|
42 |
|
1 |
Nrrd *nin, *nout; |
43 |
|
1 |
double *bb, *ww, *_ww, eps; |
44 |
|
1 |
unsigned int ii, bbLen, _wwLen; |
45 |
|
1 |
int iterMax; |
46 |
|
1 |
char *outS; |
47 |
|
|
|
48 |
|
1 |
hparm->respFileEnable = AIR_TRUE; |
49 |
|
|
|
50 |
|
1 |
hestOptAdd(&hopt, "i", "nin", airTypeOther, 1, 1, &nin, "-", |
51 |
|
|
"Input nrrd. List of DWIs from different b-values must " |
52 |
|
1 |
"be along axis 0", NULL, NULL, nrrdHestNrrd); |
53 |
|
1 |
hestOptAdd(&hopt, "b", "b1 b2", airTypeDouble, 2, -1, &bb, NULL, |
54 |
|
|
"b values across axis 0 of input nrrd", &bbLen); |
55 |
|
1 |
hestOptAdd(&hopt, "w", "w1 w2", airTypeDouble, 2, -1, &_ww, "nan nan", |
56 |
|
|
"weights for samples in non-linear fitting", &_wwLen); |
57 |
|
1 |
hestOptAdd(&hopt, "imax", "# iter", airTypeInt, 1, 1, &iterMax, "10", |
58 |
|
|
"max number of iterations to use in non-linear fitting, or, " |
59 |
|
|
"use 0 to do only initial linear fit"); |
60 |
|
1 |
hestOptAdd(&hopt, "eps", "epsilon", airTypeDouble, 1, 1, &eps, "1", |
61 |
|
|
"epsilon convergence threshold for non-linear fitting"); |
62 |
|
1 |
hestOptAdd(&hopt, "o", "nout", airTypeString, 1, 1, &outS, "-", |
63 |
|
|
"output tensor volume"); |
64 |
|
|
|
65 |
|
1 |
mop = airMopNew(); |
66 |
|
1 |
airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways); |
67 |
✓✗ |
2 |
USAGE(_tend_bfitInfoL); |
68 |
|
|
PARSE(); |
69 |
|
|
airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways); |
70 |
|
|
|
71 |
|
|
if (!( bbLen == nin->axis[0].size )) { |
72 |
|
|
char stmp[AIR_STRLEN_SMALL]; |
73 |
|
|
fprintf(stderr, "%s: got %d b-values but axis 0 size is %s\n", me, |
74 |
|
|
bbLen, airSprintSize_t(stmp, nin->axis[0].size)); |
75 |
|
|
airMopError(mop); return 1; |
76 |
|
|
} |
77 |
|
|
if (AIR_EXISTS(_ww[0])) { |
78 |
|
|
if (!( _wwLen == nin->axis[0].size )) { |
79 |
|
|
char stmp[AIR_STRLEN_SMALL]; |
80 |
|
|
fprintf(stderr, "%s: got %d weights but axis 0 size is %s\n", me, |
81 |
|
|
_wwLen, airSprintSize_t(stmp, nin->axis[0].size)); |
82 |
|
|
airMopError(mop); return 1; |
83 |
|
|
} |
84 |
|
|
ww = _ww; |
85 |
|
|
} else { |
86 |
|
|
/* no explicit weights specified */ |
87 |
|
|
ww = (double*)calloc(nin->axis[0].size, sizeof(double)); |
88 |
|
|
airMopAdd(mop, ww, airFree, airMopAlways); |
89 |
|
|
for (ii=0; ii<nin->axis[0].size; ii++) { |
90 |
|
|
ww[ii] = 1.0; |
91 |
|
|
} |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
nout = nrrdNew(); |
95 |
|
|
airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); |
96 |
|
|
|
97 |
|
|
if (tenBVecNonLinearFit(nout, nin, bb, ww, iterMax, eps)) { |
98 |
|
|
airMopAdd(mop, err=biffGetDone(TEN), airFree, airMopAlways); |
99 |
|
|
fprintf(stderr, "%s: trouble:\n%s\n", me, err); |
100 |
|
|
airMopError(mop); return 1; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
if (nrrdSave(outS, nout, NULL)) { |
104 |
|
|
airMopAdd(mop, err=biffGetDone(NRRD), airFree, airMopAlways); |
105 |
|
|
fprintf(stderr, "%s: trouble writing:\n%s\n", me, err); |
106 |
|
|
airMopError(mop); return 1; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
airMopOkay(mop); |
110 |
|
|
return 0; |
111 |
|
1 |
} |
112 |
|
|
TEND_CMD(bfit, INFO); |