~ubuntu-branches/ubuntu/saucy/caret/saucy-proposed

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*****************************************************************************
   Major portions of this software are copyrighted by the Medical College
   of Wisconsin, 1994-2000, and are released under the Gnu General Public
   License, Version 2.  See the file README.Copyright for details.
******************************************************************************/

/*---------------------------------------------------------------------------*/
/*
  These routines estimate the probability density function (PDF) 
  corresponding to the distribution of gray-matter and white-matter
  voxel intensities.  The estimate is formed as the sum of three normal
  distributions, using the Simplex algorithm for non-linear optimization
  to estimate the unknown parameters.

  File:    estpdf.c
  Author:  B. Douglas Ward
  Date:    27 May 1999

  Mod:     Extensive restructuring of the code.
  Date:    28 January 2000

  Mod:     USE_QUIET (RWCox)
  Date:    16 Apr 2003
*/

#ifndef USE_QUIET  /* 16 Apr 2003 */
# define quiet 0
#endif


/*---------------------------------------------------------------------------*/
/*
  Include header files and forward declarations.
*/

#include "pdf.h"

float calc_error (float * vertex);


/*---------------------------------------------------------------------------*/
/*
  Global variables and constants.
*/

#define DIMENSION 9      /* number of parameters to be estimated */

pdf p;                   /* empirical pdf */


/*---------------------------------------------------------------------------*/
/*
  Include source code files.
*/

#include "randgen.c"
#include "pdf.c"
#include "Simplexx.c"


/*---------------------------------------------------------------------------*/
/*
  Perform initialization for estimation of PDF from short array. 
*/

void estpdf_short_initialize 
(
  int nxyz, 
  short * sfim,
  float * gpeak,             /* estimated peak of gray-matter distribution */
  float * wpeak              /* estimated peak of white-matter distribution */
)

{
  pdf ps;
  int gmax, wmax;
  //int kk;
  int ok = 1;


  /*---- Initialize pdf's -----*/
  PDF_initialize (&p);
  PDF_initialize (&ps);


  /*----- Convert short array to pdf estimate -----*/
  PDF_short_to_pdf (nxyz, sfim, &p);
  PDF_sprint ("\nOriginal PDF:", p);


  /*----- Trim extreme values from pdf estimate -----*/
  PDF_trim (0.01, 0.99, &p);
  PDF_sprint ("\nTrimmed PDF:", p);


  /*----- Smooth the pdf estimate -----*/
  PDF_copy (p, &ps);
  PDF_smooth (&ps);
  PDF_sprint ("\nSmoothed PDF:", ps);


  /*----- Try to locate bimodality of the pdf -----*/
  ok = PDF_find_bimodal (ps, &gmax, &wmax);
  if (ok)
    {
      *gpeak = PDF_ibin_to_xvalue (ps, gmax);
      *wpeak = PDF_ibin_to_xvalue (ps, wmax);
    }
  else
    {
      printf ("Unable to find bimodal distribution \n");
      *gpeak = (2.0/3.0)*p.lower_bnd + (1.0/3.0)*p.upper_bnd;
      *wpeak = (1.0/3.0)*p.lower_bnd + (2.0/3.0)*p.upper_bnd;
    }


  if( !quiet ){
   printf ("\nInitial PDF estimates: \n");
   printf ("Lower Bnd = %8.3f   Upper Bnd  = %8.3f \n", 
	   p.lower_bnd, p.upper_bnd);
   printf ("Gray Peak = %8.3f   White Peak = %8.3f \n", *gpeak, *wpeak);
  }


  PDF_destroy (&ps);

}


/*---------------------------------------------------------------------------*/
/*
  Perform initialization for estimation of PDF from float array. 
*/

void estpdf_float_initialize 
(
  int nxyz, 
  float * ffim,
  int nbin,
  float * gpeak,             /* estimated peak of gray-matter distribution */
  float * wpeak              /* estimated peak of white-matter distribution */
)

{
  pdf ps;
  int gmax, wmax;
  //int kk;
  int ok = 1;


  /*---- Initialize pdf's -----*/
  PDF_initialize (&p);
  PDF_initialize (&ps);


  /*----- Convert float array to pdf estimate -----*/
  PDF_float_to_pdf (nxyz, ffim, nbin, &p);
  PDF_sprint ("\nOriginal PDF:", p);


  /*----- Trim extreme values from pdf estimate -----*/
  PDF_trim (0.01, 0.99, &p);
  PDF_sprint ("\nTrimmed PDF:", p);


  /*----- Smooth the pdf estimate -----*/
  PDF_copy (p, &ps);
  PDF_smooth (&ps);
  PDF_sprint ("\nSmoothed PDF:", ps);


  /*----- Try to locate bimodality of the pdf -----*/
  ok = PDF_find_bimodal (ps, &gmax, &wmax);
  if (ok)
    {
      *gpeak = PDF_ibin_to_xvalue (ps, gmax);
      *wpeak = PDF_ibin_to_xvalue (ps, wmax);
    }
  else
    {
      printf ("Unable to find bimodal distribution \n");
      *gpeak = (2.0/3.0)*p.lower_bnd + (1.0/3.0)*p.upper_bnd;
      *wpeak = (1.0/3.0)*p.lower_bnd + (2.0/3.0)*p.upper_bnd;
    }


  if( !quiet ){
    printf ("\nInitial PDF estimates: \n");
    printf ("Lower Bnd = %8.3f   Upper Bnd  = %8.3f \n", 
	    p.lower_bnd, p.upper_bnd);
    printf ("Gray Peak = %8.3f   White Peak = %8.3f \n", *gpeak, *wpeak);
  }


  PDF_destroy (&ps);

}


/*---------------------------------------------------------------------------*/
/*
  Generate the initial guess for the parameter vector.
*/

void generate_initial_guess (float gpeak, float wpeak, float * parameters)
{
  float b;                   /* coefficient for background distribution */
  float bmean;               /* mean for background distribution */ 
  float bsigma;              /* std. dev. for background distribution */
  float g;                   /* coefficient for gray-matter distribution */
  float gmean;               /* mean for gray-matter distribution */
  float gsigma;              /* std. dev. for gray-matter distribution */
  float w;                   /* coefficient for white-matter distribution */
  float wmean;               /* mean for white-matter distribution */
  float wsigma;              /* std. dev. for white-matter distribution */


  /*----- Initialize distribution coefficients -----*/
  b = 0.75;
  g = 0.25;
  w = 0.25;


  /*----- Initialize distribution means -----*/
  bmean = p.lower_bnd;

  if ((gpeak > p.lower_bnd) && (gpeak < p.upper_bnd) && (gpeak < wpeak)) 
    gmean = gpeak;
  else
    gmean = p.lower_bnd;

  if ((wpeak > p.lower_bnd) && (wpeak < p.upper_bnd) && (wpeak > gpeak))
    wmean = wpeak;
  else
    wmean = p.upper_bnd;

  if ((gmean-bmean) < 0.25*(wmean-bmean))  gmean = bmean + 0.25*(wmean-bmean);
  if ((wmean-gmean) < 0.25*(wmean-bmean))  gmean = wmean - 0.25*(wmean-bmean);


  /*----- Initialize distribution standard deviations -----*/
  bsigma = 0.25 * (p.upper_bnd - p.lower_bnd);
  gsigma = 0.25 * (wmean - gmean);
  wsigma = 0.25 * (wmean - gmean);


  /*----- Set parameter vector -----*/
  parameters[0] = b;
  parameters[1] = bmean;
  parameters[2] = bsigma;
  parameters[3] = g;
  parameters[4] = gmean;
  parameters[5] = gsigma;
  parameters[6] = w;
  parameters[7] = wmean;
  parameters[8] = wsigma;

}


/*---------------------------------------------------------------------------*/
/*
  Write parameter vector.
*/

void write_parameter_vector (float * parameters)
{
  int i;

  printf ("Dimension = %d \n", DIMENSION);
  for (i = 0;  i < DIMENSION;  i++)
    printf ("parameter[%d] = %f \n", i, parameters[i]);
}


/*---------------------------------------------------------------------------*/
/*
  Normal probability density function.
*/

float normal (float x, float mean, float sigma)

{
  float z;

  z = (x - mean) / sigma;

  return ( (1.0/(sqrt(2.0*PI)*sigma)) * exp (-0.5 * z * z) );
}


/*---------------------------------------------------------------------------*/
/*
  Estimate the voxel intensity distribution as the sum of three normal PDF's.
*/

float estimate (float * parameters, float x)

{
  float b, bmean, bsigma, g, gmean, gsigma, w, wmean, wsigma;
  //float z, 
  float fval;


  /*----- Initialize local variables -----*/
  b      = parameters[0];
  bmean  = parameters[1];
  bsigma = parameters[2];
  g      = parameters[3];
  gmean  = parameters[4];
  gsigma = parameters[5];
  w      = parameters[6];
  wmean  = parameters[7];
  wsigma = parameters[8];


  /*----- Calculate the sum of three normal PDF's -----*/ 
  fval  = b * normal (x, bmean, bsigma);
  fval += g * normal (x, gmean, gsigma);
  fval += w * normal (x, wmean, wsigma);


  return (fval);
  
}


/*---------------------------------------------------------------------------*/
/*
  Calculate the error sum of squares for the PDF estimate.
*/

float calc_error (float * vertex)

{
  const float BIG_NUMBER = 1.0e+10;  /* return when constraints are violated */

  float b, bmean, bsigma, g, gmean, gsigma, w, wmean, wsigma;  /* parameters */
  float deltah, deltam;          /* rough estimate of spread of distribution */

  int i;
  float t;
  float diff, sse;

  count += 1;


  /*----- Assign local variables -----*/
  b      = vertex[0];
  bmean  = vertex[1];
  bsigma = vertex[2];
  g      = vertex[3];
  gmean  = vertex[4];
  gsigma = vertex[5];
  w      = vertex[6];
  wmean  = vertex[7];
  wsigma = vertex[8];

  deltah = p.upper_bnd - p.lower_bnd;
  deltam = wmean - gmean;


  /*----- Apply constraints? -----*/
  if ((b < 0.05) || (b > 1.5))          return (BIG_NUMBER);
  if ((g < 0.05) || (g > 1.0))          return (BIG_NUMBER);
  if ((w < 0.05) || (w > 1.0))          return (BIG_NUMBER);
  if ((b+g+w < 1.0) || (b+g+w > 2.0))   return (BIG_NUMBER);

  if ((bmean < p.lower_bnd) || (bmean > p.upper_bnd))  return (BIG_NUMBER);
  if ((gmean < p.lower_bnd) || (gmean > p.upper_bnd))  return (BIG_NUMBER);
  if ((wmean < p.lower_bnd) || (wmean > p.upper_bnd))  return (BIG_NUMBER);
  if ((gmean < bmean)    || (gmean > wmean))     return (BIG_NUMBER);

  if ((gmean-bmean) < 0.10*(wmean-bmean))        return (BIG_NUMBER);
  if ((wmean-gmean) < 0.10*(wmean-bmean))        return (BIG_NUMBER);

  if ((bsigma < 0.01*deltah) || (bsigma > 0.5*deltah))  return (BIG_NUMBER);
  if ((gsigma < 0.01*deltam) || (gsigma > 0.5*deltam))  return (BIG_NUMBER);
  if ((wsigma < 0.01*deltam) || (wsigma > 0.5*deltam))  return (BIG_NUMBER);


  /*----- Not constrained, so calculate actual error sum of squares -----*/
  sse = 0.0;

  for (i = 0;  i < p.nbin;  i++)
    {
      t = PDF_ibin_to_xvalue (p, i);
      diff = p.prob[i] - estimate (vertex, t)*p.width;
      sse += diff * diff;
    }

  
  return (sse);
}


/*---------------------------------------------------------------------------*/
/*
  Write the parameter estimates.
*/

void output_pdf_results (float * vertex, float sse)
{
  float b, bmean, bsigma, g, gmean, gsigma, w, wmean, wsigma;


  /*----- Assign variables -----*/
  b      = vertex[0];
  bmean  = vertex[1];
  bsigma = vertex[2];
  g      = vertex[3];
  gmean  = vertex[4];
  gsigma = vertex[5];
  w      = vertex[6];
  wmean  = vertex[7];
  wsigma = vertex[8];

  if( !quiet ){
   printf ("\nProbability Density Function Estimates: \n");
   printf ("Background Coef      = %f \n", b);
   printf ("Background Mean      = %f \n", bmean);
   printf ("Background Std Dev   = %f \n", bsigma);
   printf ("Gray Matter Coef     = %f \n", g);
   printf ("Gray Matter Mean     = %f \n", gmean);
   printf ("Gray Matter Std Dev  = %f \n", gsigma);
   printf ("White Matter Coef    = %f \n", w);
   printf ("White Matter Mean    = %f \n", wmean);
   printf ("White Matter Std Dev = %f \n", wsigma);
   printf ("\nrmse = %f \n", sqrt (sse / p.nbin ));
  }

}


/*---------------------------------------------------------------------------*/
/*
  Estimate the PDF of the voxel intensities.
*/

void estpdf_short (int nxyz, short * sfim, float * parameters)
{
  float gpeak;               /* estimated peak of gray-matter distribution */
  float wpeak;               /* estimated peak of white-matter distribution */
  float sse;


  /*----- Progress report -----*/
  if( !quiet )
   printf ("\nEstimating PDF of voxel intensities \n");

  
  /*----- Initialization for PDF estimation -----*/
  estpdf_short_initialize (nxyz, sfim, &gpeak, &wpeak);


  generate_initial_guess (gpeak, wpeak, parameters);
 

  /*----- Get least squares estimate for PDF parameters -----*/
  simplex_optimization (parameters, &sse);


  /*----- Report PDF parameters -----*/
  output_pdf_results (parameters, sse);


  /*----- Free memory -----*/
  /*
  PDF_destroy (&p);
  */

  return;
}


/*---------------------------------------------------------------------------*/
/*
  Estimate the PDF of the voxel intensities.
*/

void estpdf_float (int nxyz, float * ffim, int nbin, float * parameters)
{
  float gpeak;               /* estimated peak of gray-matter distribution */
  float wpeak;               /* estimated peak of white-matter distribution */
  float sse;


  /*----- Progress report -----*/
  if( !quiet )
   printf ("\nEstimating PDF of voxel intensities \n");

  
  /*----- Initialization for PDF estimation -----*/
  estpdf_float_initialize (nxyz, ffim, nbin, &gpeak, &wpeak);


  /*----- Make initial estimate of the parameters from previous results -----*/
  generate_initial_guess (gpeak, wpeak, parameters);
 

  /*----- Get least squares estimate for PDF parameters -----*/
  simplex_optimization (parameters, &sse);


  /*----- Report PDF parameters -----*/
  output_pdf_results (parameters, sse);


  /*----- Free memory -----*/
  /*
  PDF_destroy (&p);
  */
 
  return ;
}


/*---------------------------------------------------------------------------*/