Class: Spout::Commands::Outliers

Inherits:
Object
  • Object
show all
Includes:
Helpers::NumberHelper
Defined in:
lib/spout/commands/outliers.rb

Instance Method Summary collapse

Methods included from Helpers::NumberHelper

number_with_delimiter

Constructor Details

#initialize(standard_version, argv) ⇒ Outliers

Returns a new instance of Outliers.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spout/commands/outliers.rb', line 17

def initialize(standard_version, argv)
  @standard_version = standard_version
  @console = (argv.delete("--console") != nil)

  @variable_files = Dir.glob("variables/**/*.json")
  @valid_ids = []
  @number_of_rows = nil

  @config = Spout::Helpers::ConfigReader.new

  @subject_loader = Spout::Helpers::SubjectLoader.new(@variable_files, @valid_ids, @standard_version, @number_of_rows, @config.visit)
  @subject_loader.load_subjects_from_csvs!
  @subjects = @subject_loader.subjects
  run_outliers_report!
end

Instance Method Details

#run_outliers_report!Object



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
# File 'lib/spout/commands/outliers.rb', line 33

def run_outliers_report!
  puts "Generating: outliers.html\n\n"

  @outlier_results = @subject_loader.all_methods.collect do |method, csv_files|
    Spout::Models::OutlierResult.new(@subjects, method, csv_files)
  end

  @outlier_results.select!{|outlier_result| ["numeric", "integer"].include?(outlier_result.variable_type) }
  @outlier_results.sort!{|a,b| [a.weight, a.method] <=> [b.weight, b.method]}

  @overall_results = @subject_loader.csv_files.collect do |csv_file|
    major_outliers = @outlier_results.select{|outlier_result| outlier_result.csv_files.include?(csv_file) and outlier_result.weight == 0 }.count
    minor_outliers = @outlier_results.select{|outlier_result| outlier_result.csv_files.include?(csv_file) and outlier_result.weight == 1 }.count
    total_outliers = major_outliers + minor_outliers
    [ csv_file, major_outliers, minor_outliers, total_outliers ]
  end

  coverage_folder = File.join(Dir.pwd, "coverage")
  FileUtils.mkpath coverage_folder
  html_file = File.join(coverage_folder, "outliers.html")

  File.open(html_file, "w+") do |file|
    erb_location = File.join( File.dirname(__FILE__), "../views/outliers.html.erb" )
    file.puts ERB.new(File.read(erb_location)).result(binding)
  end

  unless @console
    open_command = "open"  if RUBY_PLATFORM.match(/darwin/) != nil
    open_command = "start" if RUBY_PLATFORM.match(/mingw/) != nil

    system "#{open_command} #{html_file}" if ["start", "open"].include?(open_command)
  end
  puts "#{html_file}\n\n"
  return self
end