Class: DataModeler::Framework

Inherits:
Object
  • Object
show all
Defined in:
lib/data_modeler/framework.rb

Overview

Base class, core of the DataModeler framework.

  • Initializes the system based on the config

  • Runs over the data training and testing models

  • Results and models are saved to the file system

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Framework

Returns a new instance of Framework.

Parameters:

  • config (Hash)

    configuration hash for the whole experiment setup



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/data_modeler/framework.rb', line 14

def initialize config
  @config = config
  @inputs = config[:tset][:input_series].map! &:to_sym
  @targets =  config[:tset][:target_series].map! &:to_sym
  @train_size = config[:tset][:train_size]
  @test_size = config[:tset][:test_size]
  @nruns = config[:tset][:nruns] ||= Float::INFINITY # terminates with data
  @save_models = config[:data].delete :save_models

  @data = load_data config[:data].delete :input_file
  @out_dir = prepare_output config[:data]

  @tset_gen = DataModeler::Dataset::Generator.new data, **opts_for(:dataset_gen)
  @model = DataModeler::Model.selector **opts_for(:learner)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def data
  @data
end

#inputsObject (readonly)

Returns the value of attribute inputs.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def inputs
  @inputs
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def model
  @model
end

#nrunsObject (readonly)

Returns the value of attribute nruns.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def nruns
  @nruns
end

#out_dirObject (readonly)

Returns the value of attribute out_dir.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def out_dir
  @out_dir
end

#targetsObject (readonly)

Returns the value of attribute targets.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def targets
  @targets
end

#test_sizeObject (readonly)

Returns the value of attribute test_size.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def test_size
  @test_size
end

#train_sizeObject (readonly)

Returns the value of attribute train_size.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def train_size
  @train_size
end

#tset_genObject (readonly)

Returns the value of attribute tset_gen.



10
11
12
# File 'lib/data_modeler/framework.rb', line 10

def tset_gen
  @tset_gen
end

Instance Method Details

#run(report_interval: 1000) ⇒ void

Note:

saves model, preds and obs to the file sistem at the end of each run

This method returns an undefined value.

Main control: up to ‘nruns` (or until end of data) loop train-test-save

Parameters:

  • report_interval (Integer) (defaults to: 1000)

    interval at which to print to stdout (in number of generations) – will be passed to the ‘Model`



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/data_modeler/framework.rb', line 35

def run report_interval: 1000
  printing = report_interval && report_interval > 0
  over_nruns = nruns == Float::INFINITY ? "" : "/#{nruns}"
  puts "\nStarting @ #{Time.now}\n#{self}" if printing
  1.upto(nruns) do |nrun|
    begin
      train_set = tset_gen.train(nrun)
    rescue DataModeler::Dataset::Generator::NoDataLeft
      break # there's not enough data left for a train+test set pair
    end
    puts "\nRun #{nrun}#{over_nruns} -- starting @ #{Time.now}" if printing
    model.reset
    puts "-Training" if printing
    model.train train_set, report_interval: report_interval
    puts "-Testing" if printing
    times, test_input, observations = tset_gen.test(nrun).values
    predictions = model.test test_input
    puts "-Saving" if printing
    save_run nrun, model, [times, predictions, observations]
    puts "Run #{nrun}#{over_nruns} -- ending @ #{Time.now}" if printing
  end
  puts "\nDone! @ #{Time.now}" if printing
end

#save_models?true|false

Attribute reader for instance variable ‘@save_models`, ending in ’?‘ since it’s a boolean value.

Returns:

  • (true|false)

    value of instance variable @save_models (false if nil/uninitialized)



63
64
65
# File 'lib/data_modeler/framework.rb', line 63

def save_models?
  @save_models || false
end

#to_sString

Returns:

  • (String)


68
69
70
# File 'lib/data_modeler/framework.rb', line 68

def to_s
  config.to_s
end