Class: GitFit::Elevation::Train::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/git_fit/elevation/train/runner.rb

Constant Summary collapse

CACHE_DIR =
'data/cache/dem_fit'
MODEL_FILE =
'dem_model.msgpack'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sources: nil, output: nil) ⇒ Runner

Returns a new instance of Runner.



14
15
16
17
# File 'lib/git_fit/elevation/train/runner.rb', line 14

def initialize(sources: nil, output: nil)
  @sources = sources
  @output = output || default_output_path
end

Class Method Details

.run!(sources: nil, output: nil) ⇒ Object



96
97
98
99
# File 'lib/git_fit/elevation/train/runner.rb', line 96

def self.run!(sources: nil, output: nil)
  runner = new(sources: sources, output: output)
  runner.run
end

Instance Method Details

#default_output_pathObject



92
93
94
# File 'lib/git_fit/elevation/train/runner.rb', line 92

def default_output_path
  File.join(CACHE_DIR, MODEL_FILE)
end


71
72
73
74
75
76
77
78
# File 'lib/git_fit/elevation/train/runner.rb', line 71

def print_progress(collected, cached, missed, total)
  pct = (collected.to_f / total * 100).round(1)
  bar_len = 30
  filled = (collected.to_f / total * bar_len).round
  bar = '=' * filled + '-' * (bar_len - filled)
  $stdout.write "\r  [#{bar}] #{pct}% #{collected}/#{total} (h:#{cached} m:#{missed})"
  $stdout.flush
end


80
81
82
83
84
85
86
87
88
89
90
# File 'lib/git_fit/elevation/train/runner.rb', line 80

def print_summary(model)
  meta = model[:meta]
  puts
  puts '=== Model Trained ==='
  puts "  samples:     #{meta['n_samples']}"
  puts "  sources:     #{meta['sources'].join(', ')}"
  puts "  trained_at:  #{meta['trained_at']}"
  puts "  ridge_h:     #{model[:ridge_h].map { |w| format('%.3f', w) }.join(', ')}"
  puts "  norms:       #{model[:norms].map { |n| format('%.2f', n) }.join(', ')}"
  puts "  output:      #{@output}"
end

#runObject



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
# File 'lib/git_fit/elevation/train/runner.rb', line 19

def run
  collector = Collector.new(sources: @sources)
  total = collector.count_samples
  puts "Collecting samples (#{total} total)..."

  collected = 0
  cached = 0
  missed = 0
  skipped = 0

  collector.each_sample do |result|
    if result
      collected += 1
      if result[:cache_hit]
        cached += 1
      else
        missed += 1
      end
      print_progress(collected, cached, missed, total)
    else
      skipped += 1
    end
  end

  puts
  puts "Collected: #{collected} samples (#{cached} hit / #{missed} miss / #{skipped} skipped)"

  if collected.zero?
    warn 'No valid samples collected'
    return false
  end

  puts 'Fitting model...'
  fitter = Fitter.new(collector.samples)
  model = fitter.fit

  unless model
    warn 'Fitting failed'
    return false
  end

  puts "Writing model to #{@output}..."
  unless fitter.write_msgpack(@output)
    warn 'Failed to write model'
    return false
  end

  print_summary(model)

  true
end