summaryrefslogtreecommitdiff
path: root/report/xml_report_util.rb
blob: fd1a1793e4eef5e9f9c2c6775b0edb491ed1ad8b (plain)
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

# = Reports::XMLReportUtil
#
# Utilities for XMLReport
#
module Reports
  class XMLReportUtil
    include REXML
    
    # creates a confusion matrix as array (to be used as input for Reports::XMLReport::add_table)
    # input is confusion matrix as returned by Lib::Predictions.confusion_matrix
    #
    # call-seq:
    #   create_confusion_matrix( confusion_matrix ) => array
    #
    def self.create_confusion_matrix( confusion_matrix )
      
      raise "confusion matrix is null" unless confusion_matrix
      num_classes = Math.sqrt(confusion_matrix.size)
      class_values = []
      confusion_matrix.each{ |key_map,value| class_values.push(key_map[:confusion_matrix_actual]) if class_values.index(key_map[:confusion_matrix_actual])==nil }
      raise "confusion matrix invalid "+confusion_matrix.inspect unless num_classes.to_i == num_classes and class_values.size == num_classes 
  
      sum_predicted = {}
      sum_actual = {}
      class_values.each do |class_value|
        sum_pred = 0
        sum_act = 0
        confusion_matrix.each do |key_map,value|
          sum_pred += value if key_map[:confusion_matrix_predicted]==class_value
          sum_act += value if key_map[:confusion_matrix_actual]==class_value
        end
        sum_predicted[class_value] = sum_pred
        sum_actual[class_value] = sum_act
      end
      
      confusion = []
      confusion.push( [ " ", " ", "actual" ] + [" "] * num_classes )
      confusion.push( [ " ", " " ] + class_values +  [ "total"])
      
      class_values.each do |predicted|
        row =  [ (confusion.size==2 ? "predicted" : " "), predicted ]
        class_values.each do |actual|
          row.push( confusion_matrix[{:confusion_matrix_actual => actual, :confusion_matrix_predicted => predicted}].to_nice_s )   
        end
        row.push( sum_predicted[predicted].to_nice_s )
        confusion.push( row )  
      end
      last_row = [ " ", "total" ] 
      class_values.each do |actual|
          last_row.push( sum_actual[actual].to_nice_s )  
      end
      last_row.push(" ")
      confusion.push( last_row )
      
      return confusion
    end
    
    def self.text_element(name, text)
      node = Element.new(name)
      node.text = text
      return node
    end
    
    def self.url_element(url, description=url )
      ulink = Element.new("ulink")
      ulink.add_attributes({"url" => url})
      ulink.text = description
      return ulink
    end
    
    def self.attribute_element(name, attributes)
      node = Element.new(name)
      node.add_attributes(attributes)
      return node
    end  
    
  end
end