Class: ScoutModel
- Inherits:
-
Object
- Object
- ScoutModel
- Defined in:
- lib/scout/model/base.rb,
lib/scout/model/util/run.rb,
lib/scout/model/util/save.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#options ⇒ Object
Returns the value of attribute options.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #add(sample, label = nil) ⇒ Object
- #add_list(list, labels = nil) ⇒ Object
- #eval(sample = nil, &block) ⇒ Object
- #eval_list(list = nil, &block) ⇒ Object
- #execute(method, *args) ⇒ Object
- #extract_features(sample = nil, &block) ⇒ Object
- #extract_features_list(list = nil, &block) ⇒ Object
- #init(&block) ⇒ Object
-
#initialize(directory = nil, options = {}) ⇒ ScoutModel
constructor
A new instance of ScoutModel.
- #load_method(name) ⇒ Object
- #load_options ⇒ Object
- #load_ruby_code(file) ⇒ Object
- #load_state(&block) ⇒ Object
- #post_process(result = nil, &block) ⇒ Object
- #post_process_list(list = nil, &block) ⇒ Object
- #restore ⇒ Object
- #save ⇒ Object
- #save_method(name, value) ⇒ Object
- #save_options ⇒ Object
- #save_state(&block) ⇒ Object
- #state_file ⇒ Object
- #train(&block) ⇒ Object
Constructor Details
#initialize(directory = nil, options = {}) ⇒ ScoutModel
Returns a new instance of ScoutModel.
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/scout/model/base.rb', line 7 def initialize(directory = nil, ={}) @options = if directory directory = Path.setup directory.dup unless Path === directory @directory = directory restore end @features = [] @labels = [] end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
5 6 7 |
# File 'lib/scout/model/base.rb', line 5 def directory @directory end |
#options ⇒ Object
Returns the value of attribute options.
5 6 7 |
# File 'lib/scout/model/base.rb', line 5 def @options end |
#state ⇒ Object
Returns the value of attribute state.
5 6 7 |
# File 'lib/scout/model/base.rb', line 5 def state @state end |
Instance Method Details
#add(sample, label = nil) ⇒ Object
157 158 159 160 161 |
# File 'lib/scout/model/util/run.rb', line 157 def add(sample, label = nil) features = extract_features sample @features << features @labels << label end |
#add_list(list, labels = nil) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/scout/model/util/run.rb', line 163 def add_list(list, labels = nil) if Hash === list list.each do |sample,label| add sample, label end else list = extract_features_list list @features.concat list if Hash === labels list.each do |sample| @labels << labels[sample] end elsif labels @labels.concat labels end end end |
#eval(sample = nil, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/scout/model/util/run.rb', line 40 def eval(sample = nil, &block) if block_given? @eval = block else features = extract_features sample init unless @state result = if @eval.arity == 2 execute @eval, features, nil else execute @eval, features end post_process result end end |
#eval_list(list = nil, &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/scout/model/util/run.rb', line 58 def eval_list(list = nil, &block) if block_given? @eval_list = block else list = extract_features_list list init unless @state result = if @eval_list execute @eval_list, list elsif @eval if @eval.arity == 2 execute @eval, nil, list else list.collect{|features| execute @eval, features } end end post_process_list result end end |
#execute(method, *args) ⇒ Object
2 3 4 5 6 7 8 9 |
# File 'lib/scout/model/util/run.rb', line 2 def execute(method, *args) case method when Proc instance_exec *args, &method when nil args.first end end |
#extract_features(sample = nil, &block) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/scout/model/util/run.rb', line 123 def extract_features(sample = nil, &block) if block_given? @extract_features = block else return sample if @extract_features.nil? if @extract_features.arity == 2 execute @extract_features, sample, nil else execute @extract_features, sample end end end |
#extract_features_list(list = nil, &block) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/scout/model/util/run.rb', line 137 def extract_features_list(list = nil, &block) if block_given? @extract_features_list = block else return list if @extract_features.nil? if @extract_features_list execute @extract_features_list, list elsif @extract_features if @extract_features.arity == 2 execute @extract_features, nil, list else list.collect{|sample| execute @extract_features, sample } end else return list end end end |
#init(&block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# File 'lib/scout/model/util/run.rb', line 29 def init(&block) return @state if @state if block_given? @init = block else @state = execute @init load_state @state end end |
#load_method(name) ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'lib/scout/model/util/save.rb', line 28 def load_method(name) file = directory[name.to_s] if file.exists? file.read elsif file.set_extension('rb').exists? load_ruby_code file.set_extension('rb') end end |
#load_options ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/scout/model/util/save.rb', line 12 def file = directory['options.json'] if file.exists? IndiferentHash.setup(JSON.parse(file.read)).merge @options else @options end end |
#load_ruby_code(file) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/scout/model/util/save.rb', line 21 def load_ruby_code(file) Log.debug "Loading ruby file #{file}" code = Open.read(file) code.sub!(/.*(\sdo\b|{)/, 'Proc.new\1') instance_eval code, file end |
#load_state(&block) ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/scout/model/util/run.rb', line 20 def load_state(&block) if block_given? @load_state = block else return @state unless @load_state execute @load_state, state_file end end |
#post_process(result = nil, &block) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/scout/model/util/run.rb', line 80 def post_process(result = nil, &block) if block_given? @post_process = block else return result if @post_process.nil? if @post_process.arity == 2 execute @post_process, result, nil else execute @post_process, result end end end |
#post_process_list(list = nil, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/scout/model/util/run.rb', line 94 def post_process_list(list = nil, &block) if block_given? @post_process_list = block else if @post_process_list execute @post_process_list, list elsif @post_process if @post_process.arity == 2 execute @post_process, nil, list else list.collect{|result| execute @post_process, result } end else return list end end end |
#restore ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/scout/model/util/save.rb', line 68 def restore @eval = load_method :eval @eval_list = load_method :eval_list @extract_features = load_method :extract_features @extract_features_list = load_method :extract_features_list @post_process = load_method :post_process @post_process_list = load_method :post_process_list @train = load_method :train @init = load_method :init @load_state = load_method :load_state @save_state = load_method :save_state @options = end |
#save ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/scout/model/util/save.rb', line 51 def save if @options save_method(:eval, @eval) if @eval save_method(:eval_list, @eval_list) if @eval_list save_method(:extract_features, @extract_features) if @extract_features save_method(:extract_features_list, @extract_features_list) if @extract_features_list save_method(:post_process, @post_process) if @post_process save_method(:post_process_list, @post_process_list) if @post_process_list save_method(:train, @train) if @train save_method(:init, @init) if @init save_method(:load_state, @load_state) if @load_state save_method(:save_state, @save_state) if @save_state save_state if @state end |
#save_method(name, value) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/scout/model/util/save.rb', line 38 def save_method(name, value) file = directory[name.to_s] Log.debug "Saving #{file}" case when Proc === value require 'method_source' Open.write(file.set_extension('rb'), value.source) when String === train_model Open.write(file, @train_model) end end |
#save_options ⇒ Object
7 8 9 10 |
# File 'lib/scout/model/util/save.rb', line 7 def file = directory['options.json'] file.write(.to_json) end |
#save_state(&block) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/scout/model/util/run.rb', line 11 def save_state(&block) if block_given? @save_state = block else return @state unless @save_state execute @save_state, state_file, @state end end |
#state_file ⇒ Object
2 3 4 5 |
# File 'lib/scout/model/util/save.rb', line 2 def state_file return nil unless directory directory.state end |
#train(&block) ⇒ Object
113 114 115 116 117 118 119 120 121 |
# File 'lib/scout/model/util/run.rb', line 113 def train(&block) if block_given? @train = block else init unless @state execute @train, @features, @labels save_state end end |