Class: VectorModel

Inherits:
Object
  • Object
show all
Defined in:
lib/rbbt/vector/model.rb

Direct Known Subclasses

SVMModel

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory, extract_features = nil, train_model = nil, eval_model = nil) ⇒ VectorModel



63
64
65
66
67
68
69
70
71
72
# File 'lib/rbbt/vector/model.rb', line 63

def initialize(directory, extract_features = nil, train_model = nil, eval_model = nil)
  @directory = directory
  FileUtils.mkdir_p @directory unless File.exists? @directory
  @model_file = File.join(@directory, "model")
  extract_features = @extract_features 
  train_model = @train_model 
  eval_model = @eval_model
  @features = []
  @labels = []
end

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



4
5
6
# File 'lib/rbbt/vector/model.rb', line 4

def directory
  @directory
end

#eval_modelObject

Returns the value of attribute eval_model.



4
5
6
# File 'lib/rbbt/vector/model.rb', line 4

def eval_model
  @eval_model
end

#extract_featuresObject

Returns the value of attribute extract_features.



4
5
6
# File 'lib/rbbt/vector/model.rb', line 4

def extract_features
  @extract_features
end

#featuresObject

Returns the value of attribute features.



5
6
7
# File 'lib/rbbt/vector/model.rb', line 5

def features
  @features
end

#labelsObject

Returns the value of attribute labels.



5
6
7
# File 'lib/rbbt/vector/model.rb', line 5

def labels
  @labels
end

#model_fileObject

Returns the value of attribute model_file.



4
5
6
# File 'lib/rbbt/vector/model.rb', line 4

def model_file
  @model_file
end

#train_modelObject

Returns the value of attribute train_model.



4
5
6
# File 'lib/rbbt/vector/model.rb', line 4

def train_model
  @train_model
end

Class Method Details

.R_eval(model_file, features, list, code) ⇒ Object



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
# File 'lib/rbbt/vector/model.rb', line 36

def self.R_eval(model_file, features, list, code)
  TmpFile.with_file do |feature_file|
    TmpFile.with_file do |results|
      if list
        Open.write(feature_file, features.collect{|feat| feat * "\t"} * "\n" + "\n")
      else
        Open.write(feature_file, features * "\t" + "\n")
      end

      io = R.run <<-EOF
features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
load(file="#{model_file}");
#{code}
cat(paste(label, sep="\\n"));
      EOF

      res = io.read.sub(/WARNING: .*?\n/s,'').split(/\s+/).collect{|l| l.to_f}

      if list
        res
      else
        res.first
      end
    end
  end
end

.R_run(model_file, features, labels, code) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/rbbt/vector/model.rb', line 7

def self.R_run(model_file, features, labels, code)
  TmpFile.with_file do |feature_file|
    Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
    Open.write(feature_file + '.class', labels * "\n")

    R.run <<-EOF
features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
labels = scan("#{ feature_file }.class");
features = cbind(features, class = labels);
#{code}
    EOF
  end
end

.R_train(model_file, features, labels, code) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rbbt/vector/model.rb', line 21

def self.R_train(model_file, features, labels, code)
  TmpFile.with_file do |feature_file|
    Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
    Open.write(feature_file + '.class', labels * "\n")

    R.run <<-EOF
features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
labels = scan("#{ feature_file }.class");
features = cbind(features, class = labels);
#{code}
save(model, file='#{model_file}')
    EOF
  end
end

Instance Method Details

#add(element, label = nil) ⇒ Object



79
80
81
82
# File 'lib/rbbt/vector/model.rb', line 79

def add(element, label = nil)
  @features << extract_features.call(element)
  @labels << label unless label.nil?
end

#clearObject



74
75
76
77
# File 'lib/rbbt/vector/model.rb', line 74

def clear
  @features = []
  @labels = []
end

#cross_validation(folds = 10) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rbbt/vector/model.rb', line 115

def cross_validation(folds = 10)
  saved_features = @features
  saved_labels = @labels
  seq = (0..features.length - 1).to_a

  chunk_size = features.length / folds

  acc = []
  folds.times do
    seq = seq.shuffle
    eval_chunk = seq[0..chunk_size]
    train_chunk = seq[chunk_size.. -1]

    eval_features = @features.values_at *eval_chunk
    eval_labels = @labels.values_at *eval_chunk

    @features = @features.values_at *train_chunk
    @labels = @labels.values_at *train_chunk

    train
    predictions = eval_list eval_features, false

    acc << predictions.zip(eval_labels).collect{|pred,lab| pred - lab < 0.5 ? 1 : 0}.inject(0){|acc,e| acc +=e} / chunk_size

    @features = saved_features
    @labels = saved_labels
  end

  acc
end

#eval(element) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/rbbt/vector/model.rb', line 97

def eval(element)
  case 
  when Proc === eval_model
    eval_model.call(@model_file, extract_features.call(element), false)
  when String === eval_model
    VectorModel.R_eval(@model_file,  extract_features.call(element), false, eval_model)
  end
end

#eval_list(elements, extract = true) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/rbbt/vector/model.rb', line 106

def eval_list(elements, extract = true)
  case 
  when Proc === eval_model
    eval_model.call(@model_file, extract ? elements.collect{|element| extract_features.call(element)} : elements, true)
  when String === eval_model
    SVMModel.R_eval(@model_file, extract ? elements.collect{|element| extract_features.call(element)} : elements, true, eval_model)
  end
end

#run(code) ⇒ Object



93
94
95
# File 'lib/rbbt/vector/model.rb', line 93

def run(code)
  VectorModel.R_run(@model_file,  @features, @labels, code)
end

#trainObject



84
85
86
87
88
89
90
91
# File 'lib/rbbt/vector/model.rb', line 84

def train
  case 
  when Proc === train_model
    train_model.call(@model_file, @features, @labels)
  when String === train_model
    VectorModel.R_train(@model_file,  @features, @labels, train_model)
  end
end