#!/usr/bin/perl
# digits_is_recognized.pl
# -----------------------
# Sun Dec  8 17:48:36 EST 2002
# nh, oh, & as
# -----------------------
# to see if a certain digit is recognized.  we define "recognized" as
#   - having the highest activation of all nodes in for that pattern,
#   and
#   - also being above a certain threshold (defined globally).
#
#  INPUT = a file genetared by {x,}tlearn with the node activations
#          a threshhold
# OUTPUT = for each pattern set, how many digits are recognized
#
# USAGE = ./digit_is_recognized.pl <results file> <threshold>
# -----------------------

$DIGPATH = "/home/nori/cs63/9/digits";
#$THRESHOLD = .85;  # we could pound-define this, but we chose rather
                    # to input this as an argument
$TESTFILE = "$DIGPATH/digits.res.1";

open(DIGITS, $ARGV[0]) || die "can't open $ARGV[0]: $!\n";

$THRESHOLD = $ARGV[1];

$pattern_set = 0;
$pattern_num = 0;   # there are 240 in the whole data set
$nRecognized_total = 0;
$nRecognized_set[$pattern_set] = 0; # initializing
@failedItems; # initializing
@digitFailues;

for($i = 0; $i < 10; $i++)
{
    $digitFailures[$i] = 0;
}

print "The failed cases are: \n";

while (<DIGITS>){
  if (/^\s/) {
    chomp;
    @digits = split(/\s+/, $_);
    $foo = shift(@digits);    # lops off first empty element

    $highest = 0;
    $highest_index = 0;
    $i = 0;
    foreach $digit (@digits) {
      if ($digit > $highest) {
        $highest = $digit;
        $highest_index = $i;
      }
      $i++;
    }


    # if we are above our threshold *and* have recognized the correct
    # digit ...
    if ($highest > $THRESHOLD && $highest_index == ($pattern_num % 10)) {
      $nRecognized_set[$pattern_set]++;
      $nRecognized_total++;
    } else {
	print ($pattern_num % 10);
	print ": " . join(" ",@digits) . "\n";
	$digitFailures[$pattern_num % 10]++;
	push @{$failedItems[$pattern_set]}, ($pattern_num % 10);
    }

    $pattern_num++;
    if (($pattern_num % 10) == 0) {
      $pattern_set++;
      $nRecognized_set[$pattern_set] = 0;
    }
  }
}
close DIGITS;

print "\nRESULTS:\n";
print "  got $nRecognized_total digits out of $pattern_num -- that's ";
print ($nRecognized_total / $pattern_num);
print " %\n";

$best_set = 0;
$best_set_percentage = 0;

# finds highset percentage
for ($i=0; $i<$pattern_set; $i++) {
  print "   set $i: got $nRecognized_set[$i] of 10.  \tfailed on ";
  print @{$failedItems[$i]};
  print "\n";
  if (($nRecognized_set[$i] / 10) > $best_set_percentage) {
    $best_set_percentage = ($nRecognized_set[$i] / 10);
  }
}

# finds all sets sharing highest percentage
for ($i=0; $i<$pattern_set; $i++) {
  if (($nRecognized_set[$i] / 10) == $best_set_percentage) {
    push(@best_sets,$i);
  }
}

for($i = 0; $i < 10; $i++)
{
    print $i . " failed " . $digitFailures[$i] . " times\n";
}

