summaryrefslogtreecommitdiff
path: root/compare_validation_reports.rb
blob: 415adacb787930892c69584c6cd27d6ad641b00f (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
#!/usr/bin/env ruby
require 'optparse'
require 'json'


options = {}
OptionParser.new do |opts|
  opts.banner = "Usage: compare_validation_reports.rb [options]"
  
  opts.on("-d d", "--dir=dir", "Path to the validation reports dir.") do |dir|
    options[:dir] = (dir[-1,1] == "/" ? dir : dir + "/")
  end

  opts.on("-c", "--classification", "Select only classification reports from dir.") do |c|
    if options[:regression]
      puts "Don't use optional parameters -c and -r at the same time. Mixed by default."
      exit
    end
    options[:classification] = c
  end

  opts.on("-r", "--regression", "Select only regression reports from dir.") do |r|
    if options[:classification]
      puts "Don't use optional parameters -c and -r at the same time. Mixed by default."
      exit
    end
    options[:regression] = r
  end

  opts.on("-v", "--verbose", "Display verbose report. Standard for -d mode without -c or -r parameters.") do |v|
    options[:verbose] = v
  end

  opts.on("-f f", "--file=files", "Select two or more comma seperated reports.") do |files|
    list = files.split(",")
    unless list.size == 2
      puts "You have to pass at least two files as argument with full path."
      exit
    end
    options[:files] = list
  end
  
  opts.on("-h", "--help", "Displays help") do
    puts opts
    exit
  end

end.parse!

if options.empty? || (!options[:files] && !options[:dir])
  puts "Usage: compare_validation_reports.rb -h"
  exit
end

if options[:dir]
  if options[:verbose]
    if !options[:classification] && !options[:regression]
      json = Dir[options[:dir]+'*.json'].sort.map { |f| JSON.parse File.read(f) }.flatten
      puts JSON.pretty_generate json
    end
    if options[:classification]
      json = Dir[options[:dir]+'*_classification_*.json'].sort.map { |f| JSON.parse File.read(f) }.flatten
      puts JSON.pretty_generate json
    end
    if options[:regression]
      json = Dir[options[:dir]+'*_regression_*.json'].sort.map { |f| JSON.parse File.read(f) }.flatten
      puts JSON.pretty_generate json
    end
  else

    main = {}

    if !options[:classification] && !options[:regression] && !options[:verbose]
      json = Dir[options[:dir]+'*.json'].sort.map { |f| JSON.parse File.read(f) }.flatten
      puts JSON.pretty_generate json
    end
    if options[:classification]
      json = Dir[options[:dir]+'*_classification_*.json'].sort.map { |f| JSON.parse File.read(f) }.flatten
      json.each do |report|
        main[report["endpoint"]] ||= []
        main[report["endpoint"]] << [report["species"], report["created_at"], report["crossvalidations"].map{|cv| {"accuracy": cv[1]["statistics"]["accuracy"], "weighted_accuracy": cv[1]["statistics"]["weighted_accuracy"], "true_rate": cv[1]["statistics"]["true_rate"], "predictivity": cv[1]["statistics"]["predictivity"]}}.flatten]
      end
      puts JSON.pretty_generate main
    end
    if options[:regression]
      json = Dir[options[:dir]+'*_regression_*.json'].sort.map { |f| JSON.parse File.read(f) }.flatten
      json.each do |report|
        main[report["endpoint"]] ||= []
        main[report["endpoint"]] << [report["species"], report["created_at"], report["crossvalidations"].map{|cv| {"rmse": cv[1]["statistics"]["rmse"], "r_squared": cv[1]["statistics"]["r_squared"]}}.flatten]
      end
      puts JSON.pretty_generate main
    end
  end
end

if options[:files]
  json = []
  options[:files].each do |file|
    json << JSON.parse(File.read(file))
  end
  puts JSON.pretty_generate json.flatten

end