Class: DataModeler::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/data_modeler/base.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) ⇒ Base

Returns a new instance of Base.

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/base.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[:results].delete :save_models

  @data = load_data config[:data]
  @out_dir = prepare_output config[:results]

  @tset_gen = DataModeler::DatasetGen.new data, **opts_for(:datasetgen)
  @model = DataModeler::Models.selector **opts_for(:learner)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#inputsObject (readonly)

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#modelObject (readonly)

Returns the value of attribute model.



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

def model
  @model
end

#nrunsObject (readonly)

Returns the value of attribute nruns.



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

def nruns
  @nruns
end

#out_dirObject (readonly)

Returns the value of attribute out_dir.



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

def out_dir
  @out_dir
end

#targetsObject (readonly)

Returns the value of attribute targets.



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

def targets
  @targets
end

#test_sizeObject (readonly)

Returns the value of attribute test_size.



10
11
12
# File 'lib/data_modeler/base.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/base.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/base.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
# File 'lib/data_modeler/base.rb', line 35

def run report_interval: 1000
  1.upto(nruns) do |nrun; predictions, observations| # block-local variables
    begin
      train_set = tset_gen.train(nrun)
    rescue DataModeler::DatasetGen::NoDataLeft
      # will check if there's enough data for both train&test
      break
    end
    model.reset
    model.train train_set, report_interval: report_interval
    times, test_input, observations = tset_gen.test(nrun).values
    predictions = model.test test_input
    save_run nrun, model, [times, predictions, observations]
  end
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)



55
56
57
# File 'lib/data_modeler/base.rb', line 55

def save_models?
  @save_models || false
end

#to_sString

Returns:

  • (String)


60
61
62
# File 'lib/data_modeler/base.rb', line 60

def to_s
  config.to_s
end