Blame view

tools/sctk-2.4.10/src/slatreport/slatreport.pl 12.4 KB
8dcb6dfcb   Yannick Estève   first commit
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
519
  #!/usr/bin/env perl
  #
  # $Id: slatreport.pl,v 1.13 2009/03/03 19:55:25 ajot Exp $
  
  use warnings;
  use strict;
  use Getopt::Long;
  use Data::Dumper;
  use Pod::Usage;
  
  my $GNUPLOT = `which gnuplot`;
  $GNUPLOT = "" if($GNUPLOT !~ /^\//);
  
  my $version = "$1" if('$Revision: 1.13 $' =~ /(\d+\.\d+)/);
  
  my $man = 0;
  my $help = 0;
  my $rttmfile = "";
  my $outputfile = "";
  my $typeslist = "";
  my $stypeslist = "";
  my $filelist = "";
  my $chnllist = "";
  my $namelist = "";
  my $histogrampartitions = 10;
  
  Getopt::Long::Configure("no_ignore_case");
  
  GetOptions
  (
  	'help|h'        => \$help,
  	'man|m'         => \$man,
  	'version'       => sub { my $name = $0; $name =~ s/.*\/(.+)/$1/; print "$name version $version
  "; exit(0); },
  	'i=s'           => \$rttmfile,
  	'o=s'           => \$outputfile,
  	'type|t=s'      => \$typeslist,
  	'stype|s=s'     => \$stypeslist,
  	'file|f=s'      => \$filelist,
  	'chnl|c=s'      => \$chnllist,
  	'name|n=s'      => \$namelist,
  	'Histogram|H=i' => \$histogrampartitions,
  ) or pod2usage(1);
  
  # Docs
  pod2usage(1) if $help;
  pod2usage(-exitvalue => 0, -verbose => 2) if $man;
  
  # Checking the inputs
  pod2usage("Error: RTTM file must be specified.
  ") if ($rttmfile eq "");
  pod2usage("Error: Output filename must be specified.
  ") if($outputfile eq "");
  pod2usage("Error: Histogram partitions 'H' must be bigger than 1.
  ") if($histogrampartitions < 2);
  
  my @Files = split(/,/, $filelist);
  my @Chnl = split(/,/, $chnllist);
  my @Names = split(/,/, $namelist);
  my @Types = split(/,/, $typeslist);
  my @STypes = split(/,/, $stypeslist);
  
  print "Conditions:
  ";
  
  print "  FILE: " .  ($filelist eq "" ? "ALL" : $filelist) . "
  ";
  print "  CHANNEL: " .  ($chnllist eq "" ? "ALL" : $chnllist) . "
  ";
  print "  NAME: " .  ($namelist eq "" ? "ALL" : $namelist) . "
  ";
  print "  TYPE: " .  ($typeslist eq "" ? "ALL" : $typeslist) . "
  ";
  print "  SUBTYPE: " .  ($stypeslist eq "" ? "ALL" : $stypeslist) . "
  ";
  
  print "
  ";
  
  my $data = LoadRTTM($rttmfile);
  my ($LatencyBegMean, $LatencyBegStddev, $LatencyBegDistribution, $LatencyMidMean, $LatencyMidStddev, $LatencyMidDistribution, $LatencyEndMean, $LatencyEndStddev, $LatencyEndDistribution) = Compute($data, $histogrampartitions, \@Files, \@Chnl, \@Names, \@Types, \@STypes);
  
  print "Sample Processing Latency Begin Time Based (SPLb):
  ";
  print "  Mean: " . sprintf("%.3f", $LatencyBegMean) . "
  ";
  print "  Standard Deviation: " . sprintf("%.3f", $LatencyBegStddev) . "
  " if(defined($LatencyBegStddev));
  
  print "  Distribution:
  ";
  
  if(defined($LatencyBegDistribution))
  {
  	for(sort {$a <=> $b} keys %$LatencyBegDistribution)
  	{
  		my $beginrange = sprintf("%6.3f", $LatencyBegDistribution->{$_}{RANGE_MIN});
  		my $endrange = sprintf("%6.3f", $LatencyBegDistribution->{$_}{RANGE_MAX});
  		my $count = $LatencyBegDistribution->{$_}{COUNT};
  		
  		print "    $beginrange - $endrange: $count
  ";
  	}
  	
  	BuildPNG($LatencyBegDistribution, "$outputfile.SPLbDistribution.$histogrampartitions", "Sample Processing Latency Begin Time Based Distribution - $histogrampartitions partitions") if($outputfile ne "/dev/null");
  }
  else
  {
  	print "No distribution with one data point.
  ";
  }
  
  print "
  ";
  print "Sample Processing Latency Mid-point Time Based (SPLm):
  ";
  print "  Mean: " . sprintf("%.3f", $LatencyMidMean) . "
  ";
  print "  Standard Deviation: " . sprintf("%.3f", $LatencyMidStddev) . "
  " if(defined($LatencyMidStddev));
  print "  Distribution:
  ";
  
  if(defined($LatencyMidDistribution))
  {
  	for(sort {$a <=> $b} keys %$LatencyMidDistribution)
  	{
  		my $beginrange = sprintf("%6.3f", $LatencyMidDistribution->{$_}{RANGE_MIN});
  		my $endrange = sprintf("%6.3f", $LatencyMidDistribution->{$_}{RANGE_MAX});
  		my $count = $LatencyMidDistribution->{$_}{COUNT};
  		
  		print "    $beginrange - $endrange: $count
  ";
  	}
  	
  	BuildPNG($LatencyMidDistribution, "$outputfile.SPLmDistribution.$histogrampartitions", "Sample Processing Latency Mid-point Time Based Distribution - $histogrampartitions partitions") if($outputfile ne "/dev/null");
  }
  else
  {
  	print "No distribution with one data point.
  ";
  }
  
  print "
  ";
  print "Sample Processing Latency End Time Based (SPLe):
  ";
  print "  Mean: " . sprintf("%.3f", $LatencyEndMean) . "
  ";
  print "  Standard Deviation: " . sprintf("%.3f", $LatencyEndStddev) . "
  " if(defined($LatencyEndStddev));
  print "  Distribution:
  ";
  
  if(defined($LatencyEndDistribution))
  {
  	for(sort {$a <=> $b} keys %$LatencyEndDistribution)
  	{
  		my $beginrange = sprintf("%6.3f", $LatencyEndDistribution->{$_}{RANGE_MIN});
  		my $endrange = sprintf("%6.3f", $LatencyEndDistribution->{$_}{RANGE_MAX});
  		my $count = $LatencyEndDistribution->{$_}{COUNT};
  		
  		print "    $beginrange - $endrange: $count
  ";
  	}
  	
  	BuildPNG($LatencyEndDistribution, "$outputfile.SPLeDistribution.$histogrampartitions", "Sample Processing Latency End Time Based Distribution - $histogrampartitions partitions") if($outputfile ne "/dev/null");
  }
  else
  {
  	print "No distribution with one data point.
  ";
  }
  
  ################
  ### Funtions ###
  ################
  sub BuildPNG
  {
  	my ($data, $filename, $title) = @_;
  	
  	if($GNUPLOT eq "")
  	{
  		print "PNG: 'gnuplot not found' - no PNG generated. Install 'gnuplot' to have the PNGs.
  ";
  		return;
  	}
  	
  	# Create the dat file
  	my $rectstr = "";
  	
  	my $maxx = -1;
  	my $maxy = -1;
  	
  	for(sort {$a <=> $b} keys %$data)
  	{
  		my $xbl = $data->{$_}{RANGE_MIN};
  		my $ybl = 0;
  		my $xtr = $data->{$_}{RANGE_MAX};
  		my $ytr = $data->{$_}{COUNT};
  		
  		$maxx = $data->{$_}{RANGE_MAX} if($data->{$_}{RANGE_MAX} > $maxx);
  		$maxy = $data->{$_}{COUNT} if($data->{$_}{COUNT} > $maxy);
  		
  		$rectstr .= "set object rect from $xbl,$ybl to $xtr,$ytr fc lt 1
  ";
  	}
  	
  	open(FILE, ">", "$filename.plt")
  		or die "Cannot open file '$filename.plt'";
  	
  	print FILE "set terminal png medium
  ";
  	print FILE "set nokey
  ";
  	print FILE "set title \"$title\"
  ";
  	print FILE "set xrange [0:$maxx]
  ";
  	print FILE "set yrange [0:$maxy]
  ";
  	print FILE $rectstr;	
  	print FILE "plot -1
  ";
  
  	close(FILE);
  	
  	system("cat $filename.plt | gnuplot > $filename.png");
  	
  	unlink("$filename.plt");
  	
  	print "PNG: $filename.png
  ";
  }
  
  sub Compute
  {
  	my ($data, $HistPar, $Fil, $Chn, $Nam, $Typ, $STy) = @_;
  
  	my @statLacencyBeg = ();
  	my @statLacencyMid = ();
  	my @statLacencyEnd = ();
  	
  	foreach my $t (keys %{ $data })
  	{
  		next if(! IsElement($t, $Typ));
  		
  		foreach my $f (keys %{ $data->{$t} })
  		{
  			next if(! IsElement($f, $Fil));
  			
  			foreach my $c (keys %{ $data->{$t}{$f} })
  			{
  				next if(! IsElement($c, $Chn));
  				
  				foreach my $s (keys %{ $data->{$t}{$f}{$c} })
  				{
  					next if(! IsElement($s, $STy));
  					
  					foreach my $n (keys %{ $data->{$t}{$f}{$c}{$s} })
  					{
  						next if(! IsElement($n, $Nam));
  						
  						foreach my $TBEG (keys %{ $data->{$t}{$f}{$c}{$s}{$n} })
  						{
  							my $TMID = $data->{$t}{$f}{$c}{$s}{$n}{$TBEG}{TMID};
  							my $TEND = $data->{$t}{$f}{$c}{$s}{$n}{$TBEG}{TEND};
  							my $TSLAT = $data->{$t}{$f}{$c}{$s}{$n}{$TBEG}{TSLAT};
  							
  							push(@statLacencyBeg, $TSLAT-$TBEG);
  							push(@statLacencyMid, $TSLAT-$TMID);
  							push(@statLacencyEnd, $TSLAT-$TEND);
  						}
  					}
  				}
  			}
  		}
  	}
  
  	if(scalar(@statLacencyBeg) == 0)
  	{
  		print "No data found in files regarding criteria for Latency.
  ";
  		exit;
  	}
  	
  	if(scalar(@statLacencyBeg) == 1)
  	{
  		return( mean(\@statLacencyBeg), undef, undef,
  	            mean(\@statLacencyMid), undef, undef,
  	            mean(\@statLacencyEnd), undef, undef );
  	}
  	
  	my $lbd = frequency_distribution(\@statLacencyBeg, $HistPar);
  	my $lmd = frequency_distribution(\@statLacencyMid, $HistPar);
  	my $led = frequency_distribution(\@statLacencyEnd, $HistPar);
  	
  	return( mean(\@statLacencyBeg), stddev(\@statLacencyBeg), $lbd,
  			mean(\@statLacencyMid), stddev(\@statLacencyMid), $lmd,
  			mean(\@statLacencyEnd), stddev(\@statLacencyEnd), $led );
  }
  
  sub IsElement
  {
  	my ($e, $l) = @_;
  	
  	return 1
  		if(scalar(@$l) == 0);
  	
  	for(my $i=0; $i<scalar(@$l); $i++)
  	{
  		return 1
  			if($l->[$i] eq $e);
  	}
  	
  	return 0;
  }
  
  sub LoadRTTM
  {
  	my ($file) = @_;
  	
  	open(FILE, "<", $file)
  		or die "Cannot open file '$file'";
  	
  	my %h;
  	
  	while ( <FILE> )
  	{
  		chomp;
  		next if($_ =~ /^\;;/);
  		next if($_ =~ /^\s*$/);
  		my @a = split(/\s+/, $_);
  		
  		die "This file '$file' is not properly formated: SLAT information missing"
  			if(scalar(@a) != 10);
  		
  		next if($a[9] =~ /<NA>/);
  		
  		my $tdur = 0;
  		$tdur = $a[4] if($a[4] !~ /<NA>/);
  		
  		$h{$a[0]}{$a[1]}{$a[2]}{$a[6]}{$a[7]}{$a[3]}{TMID} = $a[3]+($tdur/2);
  		$h{$a[0]}{$a[1]}{$a[2]}{$a[6]}{$a[7]}{$a[3]}{TEND} = $a[3]+$tdur;
  		$h{$a[0]}{$a[1]}{$a[2]}{$a[6]}{$a[7]}{$a[3]}{TSLAT} = $a[9];
  	}
  		
  	close(FILE);
  	
  	return \%h;
  }
  
  sub mean
  {
  	my ($list) = @_;
  	
  	my $sum = 0;
  	
  	for(my $i=0; $i<scalar(@$list); $i++)
  	{
  		$sum += $list->[$i];
  	}
  	
  	return($sum/scalar(@$list));
  }
  
  sub stddev
  {
  	my ($list) = @_;
  	
  	my $mean = mean($list);
  	my $stddev = 0;
  	
  	for(my $i=0; $i<scalar(@$list); $i++)
  	{
  		$stddev += ($list->[$i] - $mean)*($list->[$i] - $mean);
  	}
  	
  	return(sqrt($stddev/scalar(@$list)));
  }
  
  sub frequency_distribution
  {
  	my ($list, $partitions) = @_;
  	
  	my %hash;
  	
  	my @sorted_list = sort {$a <=> $b} @$list;
  	
  	my $min = $sorted_list[0];
  	my $max = $sorted_list[scalar(@sorted_list)-1];
  	
  	my $range_iter = ($max-$min)/$partitions;
  	
  	for(my $i=0; $i<$partitions; $i++)
  	{
  		my $min_range = $min+$i*$range_iter;
  		my $max_range = $min_range+$range_iter;
  		my $key_range = ($min_range+$max_range)/2;
  		
  		my $range_max_use = $max_range;
  		
  		if($i == $partitions-1)
  		{
  			#last one, make sure we have the max elements
  			$range_max_use += $range_iter
  		}
  		
  		for (my $j=0; $j<scalar(@sorted_list); $j++)
  		{
  			if( ($sorted_list[$j] >= $min_range) && ($sorted_list[$j] < $range_max_use) )
  			{
  				$hash{$i}{RANGE_MIN} = $min_range;
  				$hash{$i}{RANGE_MAX} = $max_range;
  				$hash{$i}{COUNT}++;
  			}
  		}
  	}
  	
  	return(\%hash);
  }
  
  __END__
  
  =head1 NAME
  
  slatreport.pl -- Create reports for SLAT information in RTTM files.
  
  =head1 SYNOPSIS
  
  B<slatreport.pl> B<-i> F<FILE> B<-o> F<FILE> [B<-H> F<NUMBER>] [OPTIONS]
  
  =head1 DESCRIPTION
  
  The script analyses and generates a report and bar charts based on the SLAT information from the RTTM file (RT09 specifications). It generates Sample Processing Latency based on the begin, mid-point and end time of all RTTM Objects with SLAT information.
  
  =head1 OPTIONS
  
  =over 25
  
  =item B<-i> F<FILE>
  
  RTTM input file.
  
  =item B<-o> F<FILE>
  
  Base filename for the PNG bar charts. If '/dev/null' is passed then it will not generate the charts.
  
  =item B<-H> F<NUMBER>
  
  Generates reports and bar charts with F<NUMBER> partitions (default: 10).
  
  =item B<-f> F<FILENAME>[,F<FILENAME>[,...]]
  
  Analyses the data only the F<FILENAME>s.
  
  =item B<-c> F<CHANNEL>[,F<CHANNEL>[,...]]
  
  Analyses the data only the F<CHANNEL>s.
  
  =item  B<-n> F<NAME>[,F<NAME>[,...]]
  
  Analyses the data for only the F<NAME> objects.  Default is all objects.
  
  =item B<-t> F<TYPE>[,F<TYPE>[,...]]
  
  Analyses the data for only the objects of type F<TYPE>s.  The default is all types. 
  
  =item B<-s> F<SUBTYPE>[,F<SUBTYPE>[,...]]
  
  Analyses the data for only the objects of type F<SUBTYPE>s.  The default is all subtypes. 
  
  =item B<-h>, B<--help>
  
  Print the help.
  
  =item B<-m>, B<--man>
  
  Print the manual.
  
  =item B<--version>
  
  Print the version number.
  
  =back
  
  =head1 BUGS
  
  No known bugs.
  
  =head1 NOTES
  
  A report that shows the Sample Processing Latency for LEXEMEs and lex (as the sub-type):
  
  $ slatreport.pl -i file.rttm -o slatreport -t LEXEME -s lex
  
  If only one element is in the data pool, the standard deviation and distributions are not calculated.
  
  See RTTM and SLAT specifications on the Rich Transcription 2009 Evaluation website (http://nist.gov/speech/tests/rt/2009/index.html).
  
  Sample Processing Latency begin time based (SPLb) is calculated from the begin time to the SLAT.
  
  Sample Processing Latency mid-point time based (SPLm) is calculated from the  mid-point time to the SLAT.
  
  Sample Processing Latency end time based (SPLe) is calculated from the end time to the SLAT.
  
  =head1 AUTHOR
  
  Jerome Ajot <jerome.ajot@nist.gov>
  
  =head1 VERSION
  
  slatreport.pl $Revision: 1.13 $
  
  =head1 COPYRIGHT 
  
  This software was developed at the National Institute of Standards and Technology by employees of the Federal Government in the course of their official duties.  Pursuant to Title 17 Section 105 of the United States Code this software is not subject to copyright protection within the United States and is in the public domain. It is an experimental system.  NIST assumes no responsibility whatsoever for its use by any party.
  
  THIS SOFTWARE IS PROVIDED "AS IS."  With regard to this software, NIST MAKES NO EXPRESS OR IMPLIED WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.