sc_stats.c 16.9 KB
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
#define MAIN
#include "sctk.h"

#define SCSTATS_VERSION "1.3"

int load_pralign_file(FILE *fp, SCORES **scor, int *nscor, int maxn);


void do_exit(char *desc, char *prog, int ret);
void proc_arcs(int argc, char **argv, char *prog, char **pralign, int *npralign, int *pipein, int *feedback, char **out_dir, int *stats, int *stats_verbose, int *stats_unified, int *stats_graphs, char **stats_out_name, char **stats_formula, char **stats_test_name, int *reports, int *linewidth);
void run_multi_sys(SCORES *scor[],int nscor,char *out_dir,int stats,int stats_verbose,int stats_unified, int stats_graphs, char *stats_out_name, char *stats_formula, char *stats_test_name, int reports, int feedback, int linewidth);
 
#define STAT_MCN	0x0001
#define STAT_MAPSSWE	0x0002
#define STAT_SIGN	0x0004
#define STAT_WILC	0x0008
#define STAT_ANOVAR	0x0010
#define STAT_STDOUT	0x0020
#define STAT_MAPSSWE_SEG	0x0040

#define GRAPH_RANGE	0x0001
#define GRAPH_GRANGE	0x0002
#define GRAPH_GRANGE2	0x0008
#define GRAPH_STDOUT	0x0004
#define GRAPH_DET	0x0010

#define REPORT_NONE	0x0000
#define REPORT_SUM	0x0001
#define REPORT_RSUM	0x0002
#define REPORT_LUR	0x0004
#define REPORT_ESUM	0x0010
#define REPORT_RESUM	0x0020
#define REPORT_PRN	0x0040

char *usage = "%s: <OPTIONS>\n" 
"sc_stats Version: " SCSTATS_VERSION ", SCTK Version: " TK_VERSION "\n"
"Input Options:\n"
"    -p                             Read from stdin, the piped output\n"
"                                   alignments from another sctk program.\n"
"Output Options:\n"
"    -O output_dir                  Writes all output files into output_dir.\n"
"                                   Defaults to the hypfile's directory\n"
"    -n name                        Writes all multiple hypothesis file\n"
"                                   reports to files beginning with 'name'.\n"
"                                   Using '-' writes to stdout. Default:\n"
"                                   'Ensemble'\n"
"    -e 'ensemble description'      Description of the ensemble of hyp files.\n"
"    -l width                       Set the report line width to 'width'\n"
"Report Generation Options:\n"
"    -r [ lur | sum | rsum | es | res | prn | npr | none ]\n"
"                                   Defines the output reports.  Default 'none'.\n"
"Statistical Test Options:\n"
"    -t [ mcn | mapsswe | sign | wilc | anovar | std4 ]\n"
"                                   Create comparison matrices for each\n"
"                                   if the specified tests.\n"
"    -v                             For each test performed on a pair of\n"
"                                   hyp files, output a detailed analysis.\n"
"    -u                             Rather than creating a comparison matrix\n"
"                                   for each test, unify statistical test\n"
"                                   results into a single comparision matrix\n"
"    -f [ E | R | W ]               Percentage formula used in the sign,\n"
"                                   wilcoxon and anovar tests. Def, 'E' \n"
"                                   for Word E.\n"
"    -g [ range | grange | grange2 | det ]\n"
"                                   Create range graphs based on the % \n"
"                                   formula defined by '-f'\n";

#define MAX_HYPS 40

int main(int argc, char **argv){
    char *prog = "sc_stats";
    char *pralign[MAX_HYPS];
    int npralign;
    int pipein;
    char *out_dir;
    int stats;
    int stats_verbose;
    int stats_unified;
    int stats_graphs;
    char *stats_formula;
    char *stats_out_name = "STATS";
    char *stats_test_name;
    int feedback;
    int reports;
    SCORES *scor[MAX_HYPS];
    int nscor=0, nh;
    int linewidth;

    db = db_level = 0;
    proc_arcs(argc, argv, prog, pralign, &npralign, &pipein,
	      &feedback,&out_dir,&stats,&stats_verbose,&stats_unified,
	      &stats_graphs,&stats_out_name,&stats_formula, &stats_test_name,
	      &reports, &linewidth);

    if (feedback > 0) printf("sc_stats: " SCSTATS_VERSION "\n");

    if (pipein){
	if (!load_SCORES_sgml(stdin,scor,&nscor,MAX_HYPS)){
	    fprintf(scfp,"Error: load_SCORES_sgml Failed\n");
	    exit(1);
	}
    }

    if (stats > 0 || stats_graphs > 0 || reports > 0)
	run_multi_sys(scor,nscor,out_dir,stats,stats_verbose,stats_unified,
		      stats_graphs,stats_out_name,stats_formula,
		      stats_test_name,reports,feedback, linewidth);

    /* clean up the score structures */
    for (nh=0; nh<nscor; nh++)
	SCORES_free(scor[nh]);

    if (feedback >= 1) printf("\nSuccessful Completion\n");
    return(0);
}

void run_multi_sys(SCORES *scor[],int nscor,char *out_dir,int stats,int stats_verbose,int stats_unified, int stats_graphs, char *stats_out_name, char *stats_formula, char *stats_test_name, int reports, int feedback, int linewidth)
{
    RANK rank;
    int nwin=0, **winner[15];
    double **confidence[15];
    char *win_desc[15], *win_id[15], *stat_form_str=formula_str(stats_formula);
    char outroot[200], *sign_str=(char *)0, *wilc_str=(char *)0;
    char outname[200];

    if (feedback >= 1)
	printf("Beginning Multi-System comparisons and reports\n");

    if (strcmp(stats_out_name,"-") == 0)
	strcpy(outroot,stats_out_name);
    else
	sprintf(outroot,"%s%s%s",
		((out_dir != (char *)0) ? out_dir : ""),
		((out_dir != (char *)0) ? "/" : ""),
		stats_out_name);
    /* printf("Out Root = %s\n",outroot); */
    if (strcmp("-",outroot) == 0)
	strcpy(outname,outroot);
    else
	sprintf(outname,"%s.%s",outroot,"stats");


    if (stats > 0 || stats_graphs > 0)
	init_RANK_struct_from_SCORES(&rank, scor, nscor, stats_formula);

    /*    dump_full_RANK_struct(&rank, "Systems", "Speakers", "", "",stats_formula,
			  "test name", "Speaker ranks for the system",
			  "System ranks for the speaker");  */

    if ((stats & STAT_MAPSSWE) > 0){
	win_desc[nwin] = "Matched Pair Sentence Segment (Word Error)";
	win_id[nwin] = "MP";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	do_mtch_pairs(scor, nscor, "2", stats_test_name, 
		      !stats_unified, stats_verbose, &winner[nwin],
		      outname, feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_MAPSSWE_SEG) > 0){
        do_mtch_pairs_seg_analysis(scor, nscor, stats_test_name, 1, 1);
    }
    
    if ((stats & STAT_SIGN) > 0){
	win_desc[nwin] = (char *)TEXT_strdup((TEXT*)rsprintf("%s (%s)",
					     "Signed Paired Comparison",
						      stat_form_str));
	sign_str = win_desc[nwin];
	win_id[nwin] = "SI";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	perform_signtest(&rank, stats_verbose, !stats_unified, 
			 stat_form_str, *stats_formula, &winner[nwin],
			 outname, feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_WILC) > 0){
	win_desc[nwin] = (char*)TEXT_strdup((TEXT *)rsprintf("%s (%s)",
					 "Wilcoxon Signed Rank",
					 stat_form_str));
	wilc_str = win_desc[nwin];
	win_id[nwin] = "WI";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	perform_wilcoxon(&rank, stats_verbose, !stats_unified,
			 stat_form_str, *stats_formula, &winner[nwin],
			 outname, feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_MCN) > 0){
	win_desc[nwin] = "McNemar (Sentence Error)"; win_id[nwin] = "MN";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	McNemar_sent(scor, nscor, &winner[nwin], stats_test_name,
		     !stats_unified, stats_verbose,outname,
		     feedback, &confidence[nwin]);
	nwin++;
    }

    if ((stats & STAT_ANOVAR) > 0){
	win_desc[nwin] = "Analysis of Variance by Ranks";
	win_id[nwin] = "AN";
	if (feedback >= 1) 
	    printf("    Performing the %s Test\n",win_desc[nwin]);
	compute_anovar(&rank, stats_verbose, !stats_unified, 
		       &winner[nwin],outname,feedback,&confidence[nwin]);
	nwin++;
    }

    if (stats_unified){
	if (feedback >= 1) 
	    printf("    Printing Unified Statistical Test Reports\n");
#define new
#ifdef new
	print_composite_significance2(&rank, 80, nwin, winner, confidence,
				      win_desc, win_id,
				      1 /*matrix*/, 1 /*int report*/,
				      stats_test_name,outname, feedback,
				      (out_dir != (char *)0) ? out_dir : "");
#endif
#ifdef old
	print_composite_significance(&rank, 80, nwin, winner,
				      win_desc, win_id,
				      1 /*matrix*/, 1 /*int report*/,
				      stats_test_name,outname,feedback,
				      (out_dir != (char *)0) ? out_dir : "");
#endif
    }

    
    if ((stats_graphs & GRAPH_RANGE) > 0){
	if (strcmp("-",outroot) == 0)
	    strcpy(outname,outroot);
	else
	    sprintf(outname,"%s.%s",outroot,"range");
	if (feedback >= 1) printf("    Printing Range Graphs\n");
	print_rank_ranges(&rank, stat_form_str,stats_test_name, outname, feedback);
    }

    if ((stats_graphs & GRAPH_GRANGE) > 0){
	if (strcmp("-",outroot) == 0)
	    strcpy(outname,outroot);
	else
	    sprintf(outname,"%s.%s",outroot,"grange");
	if (feedback >= 1) printf("    Printing GNUPLOT Range Graphs\n");
	print_gnu_rank_ranges(&rank, stat_form_str,stats_test_name, outname, feedback);
    }
    if ((stats_graphs & GRAPH_GRANGE2) > 0){
	if (strcmp("-",outroot) == 0)
	    strcpy(outname,outroot);
	else
	    sprintf(outname,"%s.%s",outroot,"grange2");
	if (feedback >= 1) printf("    Printing GNUPLOT Range Graphs V2\n");
	print_gnu_rank_ranges2(&rank, stat_form_str,stats_test_name, outname, feedback);
    }

    if ((stats_graphs & GRAPH_DET) > 0){
      if (strcmp("-",outroot) != 0){
	if (make_SCORES_DET_curve(scor, nscor,outroot,feedback,stats_test_name)
	    != 0)
	  exit(1);
      } else {
	fprintf(scfp,"Warning: DET plot can not go to STDOUT\n");
      }
    }

    strcpy(outname,outroot);

    if ((reports & REPORT_SUM) > 0)
	print_N_system_summary(scor, nscor, outname, stats_test_name, 0, feedback);

    if ((reports & REPORT_RSUM) > 0)
	print_N_system_summary(scor, nscor, outname, stats_test_name, 1,
			       feedback);
    if ((reports & REPORT_ESUM) > 0)
	print_N_system_executive_summary(scor, nscor, outname,
					 stats_test_name, 0, feedback);
    if ((reports & REPORT_RESUM) > 0)
	print_N_system_executive_summary(scor, nscor, outname,
					 stats_test_name, 1, feedback);
    if ((reports & REPORT_PRN) > 0)
	print_N_SCORE(scor, nscor, outname, linewidth, feedback, 0);

    if ((reports & REPORT_LUR) > 0)
	print_N_lur(scor, nscor, outname, stats_test_name, feedback);

    if (stats > 0 || stats_graphs > 0){
	int i;
	for (i=0; i<nwin; i++)
	    free_2dimarr(winner[i],rank.n_trt,int);
	free_RANK(&rank);
    }
    if (sign_str != (char *)0) free(sign_str);
    if (wilc_str != (char *)0) free(wilc_str);

}

void proc_arcs(int argc, char **argv, char *prog, char **pralign, int *npralign, int *pipein, int *feedback, char **out_dir, int *stats, int *stats_verbose, int *stats_unified, int *stats_graphs, char **stats_out_name, char **stats_formula, char **stats_test_name, int *reports, int *linewidth){
    int opt, fbset=0, statset=0;

    if (argc <= 1) 
	do_exit("Arguments reguired",prog,1);

    *feedback=1;
    *npralign = 0;
    *pralign =(char *)0;
    *out_dir = (char *)0;
    *stats = *stats_verbose = *stats_unified = *stats_graphs = 0;    
    *stats_formula = "E";
    *stats_test_name = "";
    *stats_out_name = "Ensemble";
    *reports = REPORT_NONE;
    *linewidth = 100;

    for (opt = 1; opt < argc && (*(argv[opt]) == '-'); opt++){
	/* printf("Current OP %s\n",argv[opt]); */
	if (strcmp(argv[opt],"-a") == 0){
	    if (argc < opt + 2) do_exit("Not enough alignment files",prog,1);
	    if (*(argv[opt+1]) == '-') do_exit("Req'd alignment File name",prog,1);
	    while (argc > opt+1 && (*(argv[opt+1]) != '-'))
		pralign[(*npralign)++] = argv[++opt];
	} else if (strcmp(argv[opt],"-p") == 0){
	    *pipein = 1;
	} else if (strcmp(argv[opt],"-f") == 0){
	    if (argc <= opt+1) do_exit("Not enough Feedback arguments",prog,1);
	    *feedback = atoi(argv[++opt]);
	    fbset = 1;
	} else if (strcmp(argv[opt],"-O") == 0){
	    if (argc <= opt+1)do_exit("Output directory not specified",prog,1);
	    *out_dir = argv[++opt];
	} else if (strcmp(argv[opt],"-n") == 0){
	    if (argc <= opt+1)do_exit("Test output name not specified",prog,1);
	    *stats_out_name = argv[++opt];
	} else if (strcmp(argv[opt],"-e") == 0){
	    if (argc <= opt+1)do_exit("Test ensemble desc. not specified",
				      prog,1);
	    *stats_test_name = argv[++opt];
	} else if (strcmp(argv[opt],"-l") == 0){
	    if (argc <= opt+1)
		do_exit("Not enough Line Width arguments",prog,1);
	    *linewidth = atoi(argv[++opt]);
	} else if (strcmp(argv[opt],"-v") == 0){
	    *stats_verbose = 1;
	} else if (strcmp(argv[opt],"-u") == 0){
	    *stats_unified = 1;
	} else if (strcmp(argv[opt],"-t") == 0){
	    opt ++;
	    if (argc < opt + 1) do_exit("Not enough Test arguments",prog,1);
	    while (opt < argc && *(argv[opt]) != '-'){
		if (strcmp(argv[opt],"mcn") == 0) 
		    *stats ^= STAT_MCN;
		else if (strcmp(argv[opt],"mapsswe") == 0) 
		    *stats ^= STAT_MAPSSWE;
		else if (strcmp(argv[opt],"mapsswe_seg") == 0) 
		    *stats ^= STAT_MAPSSWE_SEG;
		else if (strcmp(argv[opt],"sign") == 0) 
		    *stats ^= STAT_SIGN;
		else if (strcmp(argv[opt],"wilc") == 0) 
		    *stats ^= STAT_WILC;
		else if (strcmp(argv[opt],"anovar") == 0) 
		    *stats ^= STAT_ANOVAR;
		else if (strcmp(argv[opt],"std4") == 0) 
		    *stats = STAT_MCN + STAT_MAPSSWE + STAT_SIGN + STAT_WILC;
		else
		    fprintf(stderr,"Unknown test '%s'\n",argv[opt]);
		opt++;
	    }
	    /* backup if we've gone to far */
	    if (opt < argc && *(argv[opt]) == '-') opt--;
	    statset=1;
	} else if (strcmp(argv[opt],"-r") == 0){
	    opt ++;
	    if (argc < opt + 1) do_exit("Not enough Report arguments",prog,1);
	    while (opt < argc && *(argv[opt]) != '-'){
		if (strcmp(argv[opt],"sum") == 0) 
		    *reports ^= REPORT_SUM;
		else if (strcmp(argv[opt],"rsum") == 0) 
		    *reports ^= REPORT_RSUM;
		else if (strcmp(argv[opt],"es") == 0) 
		    *reports ^= REPORT_ESUM;
		else if (strcmp(argv[opt],"res") == 0) 
		    *reports ^= REPORT_RESUM;
		else if (strcmp(argv[opt],"prn") == 0) 
		    *reports ^= REPORT_PRN;
		else if (strcmp(argv[opt],"lur") == 0) 
		    *reports ^= REPORT_LUR;
		else if (strcmp(argv[opt],"none") == 0) 
		    *reports = 0;
		else
		    fprintf(stderr,"Unknown report '%s'\n",argv[opt]);
		opt++;
	    }
	    /* backup if we've gone to far */
	    if (opt < argc && *(argv[opt]) == '-') opt--;
	} else if (strcmp(argv[opt],"-g") == 0){
	    opt ++;
	    if (argc < opt + 1) do_exit("Not enough Graph arguments",prog,1);
	    while (opt < argc && *(argv[opt]) != '-'){
		if (strcmp(argv[opt],"range") == 0) 
		    *stats_graphs ^= GRAPH_RANGE;
		else if (strcmp(argv[opt],"grange") == 0) 
		    *stats_graphs ^= GRAPH_GRANGE;
		else if (strcmp(argv[opt],"grange2") == 0) 
		    *stats_graphs ^= GRAPH_GRANGE2;
		else if (strcmp(argv[opt],"det") == 0) 
		    *stats_graphs ^= GRAPH_DET;
		else
		    fprintf(stderr,"Unknown graph option '%s'\n",argv[opt]);
		opt++;
	    }
	    /* backup if we've gone to far */
	    if (opt < argc && *(argv[opt]) == '-') opt--;
	    statset=1;
	} else if (strcmp(argv[opt],"-f") == 0){
	    char *sf;
	    if (argc <= opt+1) do_exit("Percentage Formula Not defined",
				       prog,1);
	    sf = *stats_formula = argv[++opt];
	    if ((strcmp(sf,"E") != 0) && (strcmp(sf,"W") != 0) &&
		(strcmp(sf,"R") != 0))
		if ((strcmp(sf,"e") == 0) || (strcmp(sf,"w") == 0) ||
		    (strcmp(sf,"r") == 0))
		    *sf = toupper(*sf);
		else
		    do_exit(rsprintf("Unrecognized Statistics percent, '%s'\n",
				     sf),prog,1);
	} else if (strcmp(argv[opt],"-w") == 0){
	    /* Do the works */
	    *stats_graphs = GRAPH_RANGE;
	    *stats = STAT_MCN + STAT_MAPSSWE + STAT_SIGN + STAT_WILC;
	    *stats_unified = 1;
	} else {
            (void) fprintf(stderr,usage,prog);
            printf("Illegal argument: %s\n",argv[opt]);
            exit(1);
        }
    }
}

void do_exit(char *desc, char *prog, int ret){
    fprintf(stderr,usage,prog);
    fprintf(stderr,"%s: %s\n\n",prog,desc);
    exit(ret);
}

int load_pralign_file(FILE *fp, SCORES **scor, int *nscor, int maxn){
    char *proc = "load_pralign_file";
    TEXT *buf, *buf2;
    SCORES *tscor;
    char *msg;
    int buf_len=100, buf2_len=100;

    alloc_singZ(buf,buf_len,TEXT,'\0');
    alloc_singZ(buf2,buf2_len,TEXT,'\0');

    while (!feof(fp) && TEXT_ensure_fgets(&buf,&buf_len,fp) != NULL){
	TEXT_xnewline(buf);

	if (TEXT_strCcmp(buf,(TEXT *)"System name:",12) == 0){
	    printf("PARSE: System hit:  %s",buf);

	    if (maxn <= *nscor){
		msg = rsprintf("SCORE array too small, increase size\n");
		goto FAIL;
	    }
	    if (TEXT_nth_field(&buf2,&buf2_len,buf,3) == 0){
		msg = rsprintf("Unable to extract system name from '%s'\n",buf);
		goto FAIL;
	    }
	    tscor = SCORES_init((char *)buf2,10);
	    scor[(*nscor)++] = tscor;
	} else if (TEXT_strCcmp(buf,(TEXT *)"Speaker sentences",17) == 0){
	    printf("PARSE: Speaker hit: %s",buf);
	} else if (TEXT_strCcmp(buf,(TEXT *)"id: ",4) == 0){
	    printf("PARSE: utterance hit: %s",buf);
	}


    }

    free_singarr(buf,TEXT);
    free_singarr(buf2,TEXT);
    return 1;

  FAIL:
    fprintf(scfp,"Error: %s %s\n",proc,msg);
    return(0);
}