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
|
/* minccmp.c */
/* */
/* Copyright Andrew Janke - a.janke@gmail.com */
/* Permission to use, copy, modify, and distribute this software and its */
/* documentation for any purpose and without fee is hereby granted, */
/* provided that the above copyright notice appear in all copies. The */
/* author makes no representations about the */
/* suitability of this software for any purpose. It is provided "as is" */
/* without express or implied warranty. */
/* */
/* calculates measures of similarity/difference between 2 or more volumes */
/* */
/* Measures used (sum(x) denotes the sum of x over a volume): */
/* RMSE - Root Mean Squared Error */
/* = sqrt( 1/n * sum((a-b)^2)) */
/* xcorr - Cross Correlation */
/* = sum((a*b)^2) / (sqrt(sum(a^2)) * sqrt(sum(b^2)) */
/* zscore - z-score differences */
/* = sum( |((a - mean(a)) / stdev(a)) - */
/* ((b - mean(b)) / stdev(b))| ) / nvox */
/* */
/* Tue Jun 17 11:31:10 EST 2003 - initial version inspired by voldiff and */
/* peter's compare_volumes */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <math.h>
#include <float.h>
#include <voxel_loop.h>
#include <ParseArgv.h>
#ifndef FALSE
# define FALSE 0
#endif
#ifndef TRUE
# define TRUE 1
#endif
#define SQR2(x) ((x) * (x))
typedef struct {
double nvox;
double sum; /* sum of valid voxels */
double ssum; /* squared sum of valid voxels */
double min;
double max;
double mean;
double var;
double sd;
double sum_prd0; /* sum of product of file[x] with file[0] */
double ssum_add0; /* squared sum of addition of file[x] with file[0] */
double ssum_dif0; /* squared sum of difference of file[x] with file[0] */
double ssum_prd0; /* squared sum of product of file[x] with file[0] */
double sum_zdif0; /* sum of zscore differences of file[x] and file[0] */
/* result stores */
double rmse;
double xcorr;
double zscore;
double vratio;
} Vol_Data;
typedef struct {
int n_datafiles;
int mask;
int mask_idx;
/* individual volume data */
Vol_Data *vd;
} Loop_Data;
/* Function prototypes */
void pass_0(void *caller_data, long num_voxels,
int input_num_buffers, int input_vector_length,
double *input_data[],
int output_num_buffers, int output_vector_length,
double *output_data[], Loop_Info * loop_info);
void pass_1(void *caller_data, long num_voxels,
int input_num_buffers, int input_vector_length,
double *input_data[],
int output_num_buffers, int output_vector_length,
double *output_data[], Loop_Info * loop_info);
void print_result(char *title, double result);
void dump_stats(Loop_Data * ld);
void do_int_calcs(Loop_Data * ld);
void do_final_calcs(Loop_Data * ld);
/* Argument variables and table */
static int verbose = FALSE;
static int debug = FALSE;
static int quiet = FALSE;
static int clobber = FALSE;
static int max_buffer_size_in_kb = 4 * 1024;
static int check_dim_info = TRUE;
static char *mask_fname = NULL;
static double valid_range[2] = { -DBL_MAX, DBL_MAX };
static int do_all = FALSE;
static int do_ssq = FALSE;
static int do_rmse = FALSE;
static int do_xcorr = FALSE;
static int do_zscore = FALSE;
static int do_vratio = FALSE;
ArgvInfo argTable[] = {
{"-verbose", ARGV_CONSTANT, (char *)TRUE, (char *)&verbose,
"be verbose"},
{"-debug", ARGV_CONSTANT, (char *)TRUE, (char *)&debug,
"dump all stats info"},
{"-quiet", ARGV_CONSTANT, (char *)TRUE, (char *)&quiet,
"print requested values only"},
{"-clobber", ARGV_CONSTANT, (char *)TRUE, (char *)&clobber,
"clobber existing files"},
{"-max_buffer_size_in_kb", ARGV_INT, (char *)1, (char *)&max_buffer_size_in_kb,
"maximum size of internal buffers."},
{"-check_dimensions", ARGV_CONSTANT, (char *) TRUE, (char *) &check_dim_info,
"Check that files have matching dimensions (default)."},
{"-nocheck_dimensions", ARGV_CONSTANT, (char *) FALSE, (char *) &check_dim_info,
"Do not check that files have matching dimensions."},
{NULL, ARGV_HELP, (char *)NULL, (char *)NULL,
"\nVoxel selection options (applies to first volume ONLY):"},
{"-floor", ARGV_FLOAT, (char *)1, (char *)&valid_range[0],
"Ignore voxels below this value. (incl)"},
{"-ceil", ARGV_FLOAT, (char *)1, (char *)&valid_range[1],
"Ignore voxels above this value. (incl)"},
{"-range", ARGV_FLOAT, (char *)2, (char *)&valid_range,
"Ignore voxels outside the range. (incl)"},
{"-mask", ARGV_STRING, (char *)1, (char *)&mask_fname,
"Use <mask.mnc> for calculations."},
{NULL, ARGV_HELP, (char *)NULL, (char *)NULL,
"\nImage Statistics (printed in this order)"},
{"-all", ARGV_CONSTANT, (char *)TRUE, (char *)&do_all,
"all statistics (default)."},
{"-ssq", ARGV_CONSTANT, (char *)TRUE, (char *)&do_ssq,
"sum of squared difference (2 volumes)"},
{"-rmse", ARGV_CONSTANT, (char *)TRUE, (char *)&do_rmse,
"root mean squared error (2 volumes)"},
{"-xcorr", ARGV_CONSTANT, (char *)TRUE, (char *)&do_xcorr,
"cross correlation (2 volumes)"},
{"-zscore", ARGV_CONSTANT, (char *)TRUE, (char *)&do_zscore,
"z-score (2 volumes)"},
// {"-vr", ARGV_CONSTANT, (char *)TRUE, (char *)&do_vratio,
// "variance ratio (2 volumes)"},
// {NULL, ARGV_HELP, (char *)NULL, (char *)NULL,
// "\nBinary Image Only Statistics"},
// {"-kappa", ARGV_CONSTANT, (char *)TRUE, (char *)&do_kappa,
// "all statistics (default)."},
{NULL, ARGV_HELP, NULL, NULL, ""},
{NULL, ARGV_END, NULL, NULL, NULL}
};
int main(int argc, char *argv[]){
char **infiles;
int n_infiles;
Loop_Options *loop_opt;
Loop_Data ld;
int i;
/* Get arguments */
if(ParseArgv(&argc, argv, argTable, 0) || (argc < 3)){
fprintf(stderr, "\nUsage: %s [options] <in1.mnc> <in2.mnc> [<inn.mnc>]\n", argv[0]);
fprintf(stderr, " %s -help\n\n", argv[0]);
return (EXIT_FAILURE);
}
n_infiles = argc - 1;
infiles = &argv[1];
/* check arguments */
if(!do_rmse && !do_xcorr && !do_zscore && !do_vratio){
do_all = TRUE;
}
if(do_all){
do_ssq = do_rmse = do_xcorr = do_zscore = do_vratio = TRUE;
}
/* check for infiles */
if(verbose){
fprintf(stderr, "\n+++ infiles +++\n");
}
for(i = 0; i < n_infiles; i++){
if(verbose){
fprintf(stderr, " | [%02d]: %s\n", i, infiles[i]);
}
if(access(infiles[i], F_OK) != 0){
fprintf(stderr, "%s: Couldn't find %s\n", argv[0], infiles[i]);
exit(EXIT_FAILURE);
}
}
/* set up Loop_Data struct and mask file */
ld.n_datafiles = n_infiles;
if(mask_fname != NULL){
if(verbose){
fprintf(stderr, " | mask: %s\n", mask_fname);
}
if(access(mask_fname, F_OK) != 0){
fprintf(stderr, "%s: Couldn't find mask file: %s\n", argv[0], mask_fname);
exit(EXIT_FAILURE);
}
ld.mask = TRUE;
ld.mask_idx = n_infiles;
infiles[n_infiles] = mask_fname;
n_infiles++;
}
else {
ld.mask = FALSE;
ld.mask_idx = 0;
}
/* allocate space and initialise volume stats data */
ld.vd = (Vol_Data *) malloc(sizeof(Vol_Data) * ld.n_datafiles);
for(i = 0; i < ld.n_datafiles; i++){
ld.vd[i].nvox = 0;
ld.vd[i].sum = 0;
ld.vd[i].ssum = 0;
ld.vd[i].min = DBL_MAX;
ld.vd[i].max = -DBL_MAX;
ld.vd[i].sum_prd0 = 0;
ld.vd[i].ssum_add0 = 0;
ld.vd[i].ssum_dif0 = 0;
ld.vd[i].ssum_prd0 = 0;
ld.vd[i].mean = 0;
ld.vd[i].var = 0;
ld.vd[i].sd = 0;
ld.vd[i].rmse = 0.0;
ld.vd[i].xcorr = 0.0;
ld.vd[i].zscore = 0.0;
ld.vd[i].vratio = 0.0;
}
/* set up and do voxel_loop(s) */
loop_opt = create_loop_options();
set_loop_verbose(loop_opt, verbose);
set_loop_buffer_size(loop_opt, (long)1024 * max_buffer_size_in_kb);
set_loop_check_dim_info(loop_opt, check_dim_info);
/* first pass */
voxel_loop(n_infiles, infiles, 0, NULL, NULL, loop_opt, pass_0, (void *)&ld);
/* intermediate calculations */
do_int_calcs(&ld);
/* run the second pass if we have to */
if(do_zscore){
voxel_loop(n_infiles, infiles, 0, NULL, NULL, loop_opt, pass_1, (void *)&ld);
}
/* final calculations */
do_final_calcs(&ld);
free_loop_options(loop_opt);
if(debug){
dump_stats(&ld);
}
/* calculate and print result(s) */
if(do_all && !quiet){
fprintf(stdout, "file[0]: %s\n", infiles[0]);
fprintf(stdout, "file[1]: %s\n", infiles[1]);
fprintf(stdout, "mask file: %s\n", mask_fname);
}
if(do_ssq){
print_result("ssq: ", ld.vd[1].ssum_dif0);
}
if(do_rmse){
print_result("rmse: ", ld.vd[1].rmse);
}
if(do_xcorr){
print_result("xcorr: ", ld.vd[1].xcorr);
}
if(do_zscore){
print_result("zscore: ", ld.vd[1].zscore);
}
if(!quiet){
fprintf(stdout, "\n");
}
return EXIT_SUCCESS;
}
/* voxel loop function for first pass through data */
void pass_0(void *caller_data, long num_voxels,
int input_num_buffers, int input_vector_length,
double *input_data[],
int output_num_buffers, int output_vector_length,
double *output_data[], Loop_Info * loop_info){
long ivox;
double valuei, value0;
int i;
/* get pointer to loop data */
Loop_Data *ld = (Loop_Data *)caller_data;
/* shut the compiler up - yes I _know_ I don't use these */
(void)output_num_buffers;
(void)output_vector_length;
(void)output_data;
(void)loop_info;
/* sanity check */
if((input_num_buffers < 2) || (output_num_buffers != 0)){
fprintf(stderr, "Bad arguments to pass_0\n");
exit(EXIT_FAILURE);
}
/* for each voxel */
for(ivox = num_voxels * input_vector_length; ivox--;){
/* skip voxels out of the mask region */
if(ld->mask && !(int)input_data[ld->mask_idx][ivox]){
continue;
}
value0 = input_data[0][ivox];
if(value0 >= valid_range[0] && value0 <= valid_range[1]){
/* for each volume */
for(i = 0; i < ld->n_datafiles; i++){
valuei = input_data[i][ivox];
/* various voxel sums */
ld->vd[i].nvox++;
ld->vd[i].sum += valuei;
ld->vd[i].ssum += SQR2(valuei);
if(i != 0){
ld->vd[i].sum_prd0 += valuei * value0;
ld->vd[i].ssum_add0 += SQR2(valuei + value0);
ld->vd[i].ssum_dif0 += SQR2(valuei - value0);
ld->vd[i].ssum_prd0 += SQR2(valuei * value0);
}
/* min and max */
if(valuei < ld->vd[i].min){
ld->vd[i].min = valuei;
}
else if(valuei > ld->vd[i].max){
ld->vd[i].max = valuei;
}
}
}
}
return;
}
/* intermediate calculations */
void do_int_calcs(Loop_Data * ld){
int i;
double denom;
for(i = 0; i < ld->n_datafiles; i++){
/* mean */
ld->vd[i].mean = ld->vd[i].sum / ld->vd[i].nvox;
/* variance */
ld->vd[i].var = ((ld->vd[i].nvox * ld->vd[i].ssum) - SQR2(ld->vd[i].sum)) /
(ld->vd[i].nvox * (ld->vd[i].nvox - 1));
/* sd */
ld->vd[i].sd = sqrt(ld->vd[i].var);
/* RMSE */
ld->vd[i].rmse = sqrt((1.0 / ld->vd[0].nvox) * ld->vd[i].ssum_dif0);
/* xcorr */
denom = sqrt(ld->vd[0].ssum * ld->vd[i].ssum);
ld->vd[i].xcorr = (denom == 0.0) ? 0.0 : ld->vd[i].sum_prd0 / denom;
}
}
/* voxel loop function for second pass through data */
void pass_1(void *caller_data, long num_voxels,
int input_num_buffers, int input_vector_length,
double *input_data[],
int output_num_buffers, int output_vector_length,
double *output_data[], Loop_Info * loop_info){
long ivox;
double valuei, value0;
int i;
/* get pointer to loop data */
Loop_Data *ld = (Loop_Data *)caller_data;
/* shut the compiler up - yes I _know_ I don't use these */
(void)output_num_buffers;
(void)output_vector_length;
(void)output_data;
(void)loop_info;
/* sanity check */
if((input_num_buffers < 2) || (output_num_buffers != 0)){
fprintf(stderr, "Bad arguments to pass_1\n");
exit(EXIT_FAILURE);
}
/* for each voxel */
for(ivox = num_voxels * input_vector_length; ivox--;){
/* skip voxels out of the mask region */
if(ld->mask && !(int)input_data[ld->mask_idx][ivox]){
continue;
}
value0 = input_data[0][ivox];
if(value0 >= valid_range[0] && value0 <= valid_range[1]){
/* for each volume */
for(i = 0; i < ld->n_datafiles; i++){
valuei = input_data[i][ivox];
if(i != 0){
/* zscore total */
ld->vd[i].sum_zdif0 +=
fabs(((value0 - ld->vd[0].mean) / ld->vd[0].sd) -
((valuei - ld->vd[i].mean) / ld->vd[i].sd));
}
}
}
}
return;
}
/* final calculations */
void do_final_calcs(Loop_Data * ld){
int i;
for(i = 0; i < ld->n_datafiles; i++){
/* zscore */
ld->vd[i].zscore = ld->vd[i].sum_zdif0 / ld->vd[i].nvox;
}
}
/* dirty little function to print out results */
void print_result(char *title, double result){
if(!quiet){
fprintf(stdout, "%s", title);
}
fprintf(stdout, "%.10g\n", result);
}
/* debug function to dump stats structure */
void dump_stats(Loop_Data * ld){
int i;
fprintf(stdout, " + Main Loop data structure\n");
fprintf(stdout, " | n_datafiles %d\n", ld->n_datafiles);
fprintf(stdout, " | mask %d\n", ld->mask);
fprintf(stdout, " | mask_idx %d\n", ld->mask_idx);
fprintf(stdout, " +++ volume data stats\n");
for(i = 0; i < ld->n_datafiles; i++){
fprintf(stdout, " |---------------------------------\n");
fprintf(stdout, " | [%02d] nvox %10g\n", i, ld->vd[i].nvox);
fprintf(stdout, " | [%02d] sum %.10g\n", i, ld->vd[i].sum);
fprintf(stdout, " | [%02d] ssum %.10g\n", i, ld->vd[i].ssum);
fprintf(stdout, " | [%02d] min %.10g\n", i, ld->vd[i].min);
fprintf(stdout, " | [%02d] max %.10g\n", i, ld->vd[i].max);
fprintf(stdout, " | [%02d] mean %.10g\n", i, ld->vd[i].mean);
fprintf(stdout, " | [%02d] var %.10g\n", i, ld->vd[i].var);
fprintf(stdout, " | [%02d] sd %.10g\n", i, ld->vd[i].sd);
fprintf(stdout, " | [%02d] sum_prd0 %.10g\n", i, ld->vd[i].sum_prd0);
fprintf(stdout, " | [%02d] ssum_add0 %.10g\n", i, ld->vd[i].ssum_add0);
fprintf(stdout, " | [%02d] ssum_dif0 %.10g\n", i, ld->vd[i].ssum_dif0);
fprintf(stdout, " | [%02d] ssum_prd0 %.10g\n", i, ld->vd[i].ssum_prd0);
fprintf(stdout, " | [%02d] sum_zdif0 %.10g\n", i, ld->vd[i].sum_zdif0);
fprintf(stdout, " | [%02d] rmse %.10g\n", i, ld->vd[i].rmse);
fprintf(stdout, " | [%02d] xcorr %.10g\n", i, ld->vd[i].xcorr);
fprintf(stdout, " | [%02d] zscore %.10g\n", i, ld->vd[i].zscore);
fprintf(stdout, " | [%02d] vratio %.10g\n", i, ld->vd[i].vratio);
}
}
|