Class: Ocarina::ErrorStats

Inherits:
Object
  • Object
show all
Defined in:
lib/ocarina/error_stats.rb

Overview

keeps track of running errors during evaluations

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ ErrorStats

Returns a new instance of ErrorStats.



9
10
11
12
13
14
15
16
# File 'lib/ocarina/error_stats.rb', line 9

def initialize(config)
  @num_outputs = config.num_outputs

  @chars_seen  = 0
  @chars_wrong = 0
  @bits_seen   = 0
  @bits_wrong  = 0
end

Instance Method Details

#bit_accuracyObject



56
57
58
# File 'lib/ocarina/error_stats.rb', line 56

def bit_accuracy
  (@bits_seen - @bits_wrong).to_f / @bits_seen * 100
end

#character_accuracyObject



52
53
54
# File 'lib/ocarina/error_stats.rb', line 52

def character_accuracy
  (@chars_seen - @chars_wrong).to_f / @chars_seen * 100
end

#check_error(expected, actual) ⇒ Object

check and record the error from the expected and actual integers



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ocarina/error_stats.rb', line 20

def check_error(expected, actual)
  @chars_seen += 1
  @bits_seen += @num_outputs

  if expected != actual
    @chars_wrong +=1
    puts "char wrong, expected: #{expected.chr}, guessed: #{actual.chr}"

    expected_binary_string = int_to_binary_string expected
    actual_binary_string   = int_to_binary_string actual

    @bits_wrong += count_differences expected_binary_string, actual_binary_string

    #puts "expected: #{expected_binary_string}, decimal: #{expected}"
    #puts "actual  : #{actual_binary_string}, decimal: #{actual}"
  end

end

#count_differences(a, b) ⇒ Object

assumes a.size == b.size



40
41
42
# File 'lib/ocarina/error_stats.rb', line 40

def count_differences(a, b)
  a.split(//).each.with_index.inject(0) { |diffs, (char, i)| char == b[i] ? diffs : diffs + 1}
end

#reportObject



45
46
47
48
49
50
# File 'lib/ocarina/error_stats.rb', line 45

def report
  puts "total characters evaluated: #@chars_seen"
  puts "total characters wrong    : #@chars_wrong"
  puts "character accuracy        : #{'%.2f' % character_accuracy}"
  puts "bit accuracy              : #{'%.2f' % bit_accuracy}"
end