summaryrefslogtreecommitdiff
path: root/report/report_content.rb
blob: 1345e6f3e75e141c92e4d9ed0163665d44b4c426 (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
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

# = Reports::ReportContent
#
# wraps an xml-report, adds functionality for adding sections, adds a hash for tmp files
#
class Reports::ReportContent
  
  attr_accessor :xml_report, :tmp_files
  
  def initialize(title)
    @xml_report = Reports::XMLReport.new(title, Time.now.strftime("Created at %m.%d.%Y - %H:%M"))
    @tmp_file_count = 0
    @current_section = @xml_report.get_root_element
  end
  
  def add_section( section_title, section_text=nil )
    @current_section = @xml_report.add_section(@xml_report.get_root_element, section_title)
    @xml_report.add_paragraph(@current_section, section_text) if section_text
  end
  
  def end_section()
    @current_section = @xml_report.get_root_element
  end
  
  def add_paired_ttest_table( validation_set,
                       group_attribute, 
                       test_attribute,
                       section_title = "Paired t-test",
                       section_text = nil)
                       
    level = 0.90                       
    test_matrix = Reports::ReportStatisticalTest.test_matrix( validation_set.validations, 
      group_attribute, test_attribute, "paired_ttest", level )
    puts test_matrix.inspect
    titles = test_matrix[:titles]
    matrix = test_matrix[:matrix]
    table = []
    puts titles.inspect
    table << [""] + titles
    titles.size.times do |i|
      table << [titles[i]] + matrix[i].collect{|v| (v==nil || v==0) ? "" : (v<0 ? "-" : "+") }
    end
    
    section_test = @xml_report.add_section(@current_section, section_title)
    @xml_report.add_paragraph(section_test, section_text) if section_text
    @xml_report.add_table(section_test, test_attribute.to_s+", significance-level: "+level.to_s, table, true, true)  
  end
  
  def add_predictions( validation_set, 
                              validation_attributes=[],
                              section_title="Predictions",
                              section_text=nil,
                              table_title="Predictions")

    #PENING
    raise "validation attributes not implemented in get prediction array" if  validation_attributes.size>0
    
    section_table = @xml_report.add_section(@current_section, section_title)
    if validation_set.validations[0].get_predictions
      @xml_report.add_paragraph(section_table, section_text) if section_text
      @xml_report.add_table(section_table, table_title, Lib::OTPredictions.to_array(validation_set.validations.collect{|v| v.get_predictions}, 
        true, true))
    else
      @xml_report.add_paragraph(section_table, "No prediction info available.")
    end
  end


  def add_result_overview( validation_set,
                        attribute_col,
                        attribute_row, 
                        attribute_values,
                        table_titles=nil,
                        section_title="Result overview",
                        section_text=nil )
    
    
    section_table = @xml_report.add_section(@current_section, section_title)
    @xml_report.add_paragraph(section_table, section_text) if section_text
    
    attribute_values.size.times do |i|
      attribute_val = attribute_values[i]
      table_title = table_titles ? table_titles[i] : "Result overview for "+attribute_val.to_s
      vals = validation_set.to_table( attribute_col, attribute_row, attribute_val)
      @xml_report.add_table(section_table, table_title, vals, true, true)  
    end
  end

  # result (could be transposed)
  #
  #  attr1      | attr2     | attr3
  #  ===========|===========|===========
  #  val1-attr1 |val1-attr2 |val1-attr3 
  #  val2-attr1 |val2-attr2 |val2-attr3
  #  val3-attr1 |val3-attr2 |val3-attr3
  #
  def add_result( validation_set, 
                        validation_attributes,
                        table_title,
                        section_title="Results",
                        section_text=nil,
                        #rem_equal_vals_attr=[],
                        search_for_existing_report_type=nil)

    section_table = @xml_report.add_section(@current_section, section_title)
    @xml_report.add_paragraph(section_table, section_text) if section_text
    vals = validation_set.to_array(validation_attributes, true)
    vals = vals.collect{|a| a.collect{|v| v.to_s }}
    
    if (search_for_existing_report_type)
      vals.size.times do |i|
        puts i
        if (i==0)
          vals[i] = [ "Reports" ] + vals[i]
          puts vals[i].inspect
        else
          if search_for_existing_report_type=="validation"
            vals[i] = [ validation_set.validations[i-1].validation_report_uri() ] + vals[i]
          elsif search_for_existing_report_type=="crossvalidation"
            vals[i] = [ validation_set.validations[i-1].cv_report_uri() ] + vals[i]
          else
            raise "illegal report type: "+search_for_existing_report_type.to_s
          end
        end
      end
    end
    #PENDING transpose values if there more than 4 columns, and there are more than columns than rows
    transpose = vals[0].size>4 && vals[0].size>vals.size
    @xml_report.add_table(section_table, table_title, vals, !transpose, transpose, transpose)
  end
  
  def add_confusion_matrix(  validation, 
                                section_title="Confusion Matrix",
                                section_text=nil,
                                table_title="Confusion Matrix")
    section_confusion = @xml_report.add_section(@current_section, section_title)
    @xml_report.add_paragraph(section_confusion, section_text) if section_text
    @xml_report.add_table(section_confusion, table_title, 
      Reports::XMLReportUtil::create_confusion_matrix( validation.confusion_matrix ), true, true)
  end
  
  def add_regression_plot( validation_set,
                            name_attribute,
                            section_title="Regression Plot",
                            section_text=nil,
                            image_title=nil,
                            image_caption=nil)
                            
    image_title = "Regression plot" unless image_title
    
    section_regr = @xml_report.add_section(@current_section, section_title)
    prediction_set = validation_set.collect{ |v| v.get_predictions }
        
    if prediction_set.size>0
      
      section_text += "\nWARNING: regression plot information not available for all validation results" if prediction_set.size!=validation_set.size
      @xml_report.add_paragraph(section_regr, section_text) if section_text
      plot_file_name = "regr_plot"+@tmp_file_count.to_s+".svg"
      @tmp_file_count += 1
      begin
        plot_file_path = add_tmp_file(plot_file_name)
        Reports::PlotFactory.create_regression_plot( plot_file_path, prediction_set, name_attribute )
        @xml_report.add_imagefigure(section_regr, image_title, plot_file_name, "SVG", 120, image_caption)
      rescue RuntimeError => ex
        LOGGER.error("Could not create regression plot: "+ex.message)
        rm_tmp_file(plot_file_name)
        @xml_report.add_paragraph(section_regr, "could not create regression plot: "+ex.message)
      end  
    else
      @xml_report.add_paragraph(section_regr, "No prediction info for regression available.")
    end
  end

  def add_roc_plot( validation_set,
                            split_set_attribute = nil,
                            section_title="ROC Plots",
                            section_text=nil,
                            image_titles=nil,
                            image_captions=nil)
                            
    section_roc = @xml_report.add_section(@current_section, section_title)
    prediction_set = validation_set.collect{ |v| v.get_predictions && v.get_predictions.confidence_values_available? }
        
    if prediction_set.size>0
      if prediction_set.size!=validation_set.size
        section_text += "\nWARNING: roc plot information not available for all validation results"
        LOGGER.error "WARNING: roc plot information not available for all validation results:\n"+
          "validation set size: "+validation_set.size.to_s+", prediction set size: "+prediction_set.size.to_s
      end
      @xml_report.add_paragraph(section_roc, section_text) if section_text

      class_domain = validation_set.get_class_domain
      class_domain.size.times do |i|
        class_value = class_domain[i]
        image_title = image_titles ? image_titles[i] : "ROC Plot for class-value '"+class_value.to_s+"'"
        image_caption = image_captions ? image_captions[i] : nil
        plot_file_name = "roc_plot"+@tmp_file_count.to_s+".svg"
        @tmp_file_count += 1
        begin
          plot_file_path = add_tmp_file(plot_file_name)
          Reports::PlotFactory.create_roc_plot( plot_file_path, prediction_set, class_value, split_set_attribute, false )#prediction_set.size>1 )
          @xml_report.add_imagefigure(section_roc, image_title, plot_file_name, "SVG", 120, image_caption)
        rescue RuntimeError => ex
          msg = "WARNING could not create roc plot for class value '"+class_value.to_s+"': "+ex.message
          LOGGER.error(msg)
          rm_tmp_file(plot_file_name)
          @xml_report.add_paragraph(section_roc, msg)
        end  
      end
    else
      @xml_report.add_paragraph(section_roc, "No prediction-confidence info for roc plot available.")
    end
    
  end
  
  def add_ranking_plots( validation_set,
                            compare_attribute,
                            equal_attribute,
                            rank_attributes,
                            section_title="Ranking Plots",
                            section_text="This section contains the ranking plots.")
    
    section_rank = @xml_report.add_section(@current_section, section_title)
    @xml_report.add_paragraph(section_rank, section_text) if section_text
    
    rank_attributes.each do |a|
      add_ranking_plot(section_rank, validation_set, compare_attribute, equal_attribute, a)
    end
  end
  
  def add_ranking_plot( report_section, 
                        validation_set,
                        compare_attribute,
                        equal_attribute,
                        rank_attribute,
                        image_titles=nil,
                        image_captions=nil)

    class_domain = validation_set.get_domain_for_attr(rank_attribute)
    puts "ranking plot for "+rank_attribute.to_s+", class values: "+class_domain.to_s
    
    class_domain.size.times do |i|  
      class_value = class_domain[i]
      if image_titles
        image_title = image_titles[i]
      else
        if class_value!=nil
          image_title = rank_attribute.to_s+" Ranking Plot for class-value '"+class_value.to_s+"'"
        else 
          image_title = rank_attribute.to_s+" Ranking Plot"
        end
      end
      image_caption = image_captions ? image_captions[i] : nil
      plot_file_name = "ranking_plot"+@tmp_file_count.to_s+".svg"
      @tmp_file_count += 1
      plot_file_path = add_tmp_file(plot_file_name)
      Reports::PlotFactory::create_ranking_plot(plot_file_path, validation_set, compare_attribute, equal_attribute, rank_attribute, class_value)
      @xml_report.add_imagefigure(report_section, image_title, plot_file_name, "SVG", 75, image_caption)
    end
  end
  
  def add_bar_plot(validation_set,
                            title_attribute,
                            value_attributes,
                            section_title="Bar Plot",
                            section_text=nil,
                            image_title="Bar Plot",
                            image_caption=nil)
    
    section_bar = @xml_report.add_section(@current_section, section_title)
    @xml_report.add_paragraph(section_bar, section_text) if section_text
    
    plot_file_name = "bar_plot"+@tmp_file_count.to_s+".svg"
    @tmp_file_count += 1
    plot_file_path = add_tmp_file(plot_file_name)
    Reports::PlotFactory.create_bar_plot(plot_file_path, validation_set, title_attribute, value_attributes )
    @xml_report.add_imagefigure(section_bar, image_title, plot_file_name, "SVG", 120, image_caption)
  end  
  
  private
  def add_tmp_file(tmp_file_name)
    
    @tmp_files = {} unless @tmp_files
    raise "file name already exits" if @tmp_files[tmp_file_name] || (@text_files && @text_files[tmp_file_name])  
    tmp_file_path = Reports::Util.create_tmp_file(tmp_file_name)
    @tmp_files[tmp_file_name] = tmp_file_path
    return tmp_file_path
  end
  
  def rm_tmp_file(tmp_file_name)
    @tmp_files.delete(tmp_file_name) if @tmp_files.has_key?(tmp_file_name)
  end
  
end