rbbt-dm 1.1.47 → 1.1.52

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,57 @@
1
+ require File.join(File.expand_path(File.dirname(__FILE__)), '../../..', 'test_helper.rb')
2
+ require 'rbbt/vector/model/tensorflow'
3
+
4
+ class TestTensorflowModel < Test::Unit::TestCase
5
+
6
+ def test_keras
7
+ TmpFile.with_file() do |dir|
8
+ FileUtils.mkdir_p dir
9
+
10
+ model = TensorFlowModel.new(
11
+ dir,
12
+ optimizer:'adam',
13
+ loss: 'sparse_categorical_crossentropy',
14
+ metrics: ['accuracy']
15
+ )
16
+
17
+ model.keras_graph do
18
+ tf = tensorflow
19
+ tf.keras.models.Sequential.new([
20
+ tf.keras.layers.Flatten.new(input_shape: [28, 28]),
21
+ tf.keras.layers.Dense.new(128, activation:'relu'),
22
+ tf.keras.layers.Dropout.new(0.2),
23
+ tf.keras.layers.Dense.new(10, activation:'softmax')
24
+ ])
25
+ end
26
+
27
+ sum = predictions = nil
28
+ model.tensorflow do
29
+ tf = tensorflow
30
+ mnist_db = tf.keras.datasets.mnist
31
+
32
+ (x_train, y_train), (x_test, y_test) = mnist_db.load_data()
33
+ x_train, x_test = x_train / 255.0, x_test / 255.0
34
+
35
+ num = PyCall.len(x_train)
36
+
37
+ num.times do |i|
38
+ model.add x_train[i], y_train[i]
39
+ end
40
+
41
+ model.train
42
+
43
+ predictions = model.eval_list x_test.tolist()
44
+ sum = 0
45
+ predictions.zip(y_test.tolist()).each do |pred,label|
46
+ sum += 1 if label.to_i == pred
47
+ end
48
+
49
+ end
50
+
51
+ assert sum.to_f / predictions.length > 0.7
52
+
53
+
54
+ end
55
+ end
56
+ end
57
+
@@ -71,4 +71,358 @@ cat(label, file="#{results}");
71
71
  end
72
72
  end
73
73
 
74
+ def test_model_list
75
+ text =<<-EOF
76
+ 1 0;1;1
77
+ 1 1;0;1
78
+ 1 1;1;1
79
+ 1 0;1;1
80
+ 1 1;1;1
81
+ 0 0;1;0
82
+ 0 1;0;0
83
+ 0 0;1;0
84
+ 0 1;0;0
85
+ EOF
86
+
87
+ TmpFile.with_file() do |dir|
88
+ FileUtils.mkdir_p dir
89
+ model = VectorModel.new(dir)
90
+
91
+ model.extract_features = Proc.new{|element,list|
92
+ if element
93
+ element.split(";")
94
+ elsif list
95
+ list.collect{|e| e.split(";") }
96
+ end
97
+ }
98
+
99
+ model.train_model = Proc.new{|model_file,features,labels|
100
+ TmpFile.with_file do |feature_file|
101
+ Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
102
+ Open.write(feature_file + '.class', labels * "\n")
103
+ R.run <<-EOF
104
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
105
+ labels = scan("#{ feature_file }.class", what=numeric());
106
+ features = cbind(features, class = labels);
107
+ rbbt.require('e1071')
108
+ model = svm(class ~ ., data = features)
109
+ save(model, file="#{ model_file }");
110
+ EOF
111
+ end
112
+ }
113
+
114
+ model.eval_model = Proc.new{|model_file,features|
115
+ TmpFile.with_file do |feature_file|
116
+ TmpFile.with_file do |results|
117
+ Open.write(feature_file, features * "\t")
118
+ puts R.run(<<-EOF
119
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
120
+ library(e1071)
121
+ load(file="#{ model_file }")
122
+ label = predict(model, features);
123
+ cat(label, file="#{results}");
124
+ EOF
125
+ ).read
126
+ Open.read(results)
127
+ end
128
+ end
129
+
130
+ }
131
+
132
+ pairs = text.split(/\n/).collect do |line|
133
+ label, features = line.split(" ")
134
+ [features, label]
135
+ end
136
+
137
+ model.add_list(*Misc.zip_fields(pairs))
138
+
139
+ model.train
140
+
141
+ assert model.eval("1;1;1").to_f > 0.5
142
+ assert model.eval("0;0;0").to_f < 0.5
143
+ end
144
+ end
145
+
146
+ def test_model_list2
147
+ text =<<-EOF
148
+ 1 0;1;1
149
+ 1 1;0;1
150
+ 1 1;1;1
151
+ 1 0;1;1
152
+ 1 1;1;1
153
+ 0 0;1;0
154
+ 0 1;0;0
155
+ 0 0;1;0
156
+ 0 1;0;0
157
+ EOF
158
+
159
+ TmpFile.with_file() do |dir|
160
+ FileUtils.mkdir_p dir
161
+ model = VectorModel.new(dir)
162
+
163
+ model.extract_features = Proc.new{|element|
164
+ element.split(";")
165
+ }
166
+
167
+ model.train_model = Proc.new{|model_file,features,labels|
168
+ TmpFile.with_file do |feature_file|
169
+ Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
170
+ Open.write(feature_file + '.class', labels * "\n")
171
+ R.run <<-EOF
172
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
173
+ labels = scan("#{ feature_file }.class", what=numeric());
174
+ features = cbind(features, class = labels);
175
+ rbbt.require('e1071')
176
+ model = svm(class ~ ., data = features)
177
+ save(model, file="#{ model_file }");
178
+ EOF
179
+ end
180
+ }
181
+
182
+ model.eval_model = Proc.new{|model_file,features|
183
+ TmpFile.with_file do |feature_file|
184
+ TmpFile.with_file do |results|
185
+ Open.write(feature_file, features * "\t")
186
+ puts R.run(<<-EOF
187
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
188
+ library(e1071)
189
+ load(file="#{ model_file }")
190
+ label = predict(model, features);
191
+ cat(label, file="#{results}");
192
+ EOF
193
+ ).read
194
+ Open.read(results)
195
+ end
196
+ end
197
+
198
+ }
199
+
200
+ pairs = text.split(/\n/).collect do |line|
201
+ label, features = line.split(" ")
202
+ [features, label]
203
+ end
204
+
205
+ model.add_list(*Misc.zip_fields(pairs))
206
+
207
+ model.train
208
+
209
+ assert model.eval("1;1;1").to_f > 0.5
210
+ assert model.eval("0;0;0").to_f < 0.5
211
+ end
212
+ end
213
+
214
+ def test_model_list
215
+ text =<<-EOF
216
+ 1 0;1;1
217
+ 1 1;0;1
218
+ 1 1;1;1
219
+ 1 0;1;1
220
+ 1 1;1;1
221
+ 0 0;1;0
222
+ 0 1;0;0
223
+ 0 0;1;0
224
+ 0 1;0;0
225
+ EOF
226
+
227
+ TmpFile.with_file() do |dir|
228
+ FileUtils.mkdir_p dir
229
+ model = VectorModel.new(dir)
230
+
231
+ model.extract_features = Proc.new{|element,list|
232
+ if element
233
+ element.split(";")
234
+ elsif list
235
+ list.collect{|e| e.split(";") }
236
+ end
237
+ }
238
+
239
+ model.train_model = Proc.new{|model_file,features,labels|
240
+ TmpFile.with_file do |feature_file|
241
+ Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
242
+ Open.write(feature_file + '.class', labels * "\n")
243
+ R.run <<-EOF
244
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
245
+ labels = scan("#{ feature_file }.class", what=numeric());
246
+ features = cbind(features, class = labels);
247
+ rbbt.require('e1071')
248
+ model = svm(class ~ ., data = features)
249
+ save(model, file="#{ model_file }");
250
+ EOF
251
+ end
252
+ }
253
+
254
+ model.eval_model = Proc.new{|model_file,features|
255
+ TmpFile.with_file do |feature_file|
256
+ TmpFile.with_file do |results|
257
+ Open.write(feature_file, features * "\t")
258
+ puts R.run(<<-EOF
259
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
260
+ library(e1071)
261
+ load(file="#{ model_file }")
262
+ label = predict(model, features);
263
+ cat(label, file="#{results}");
264
+ EOF
265
+ ).read
266
+ Open.read(results)
267
+ end
268
+ end
269
+
270
+ }
271
+
272
+ pairs = text.split(/\n/).collect do |line|
273
+ label, features = line.split(" ")
274
+ model.add features, label
275
+ end
276
+
277
+ model.train
278
+
279
+ assert model.eval("1;1;1").to_f > 0.5
280
+ assert model.eval("0;0;0").to_f < 0.5
281
+ end
282
+ end
283
+
284
+ def test_model_save
285
+ text =<<-EOF
286
+ 1 0;1;1
287
+ 1 1;0;1
288
+ 1 1;1;1
289
+ 1 0;1;1
290
+ 1 1;1;1
291
+ 0 0;1;0
292
+ 0 1;0;0
293
+ 0 0;1;0
294
+ 0 1;0;0
295
+ EOF
296
+
297
+ TmpFile.with_file() do |dir|
298
+ FileUtils.mkdir_p dir
299
+ model = VectorModel.new(dir)
300
+
301
+ model.extract_features = Proc.new{|element|
302
+ element.split(";")
303
+ }
304
+
305
+ model.train_model = Proc.new{|model_file,features,labels|
306
+ TmpFile.with_file do |feature_file|
307
+ Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
308
+ Open.write(feature_file + '.class', labels * "\n")
309
+ R.run <<-EOF
310
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
311
+ labels = scan("#{ feature_file }.class", what=numeric());
312
+ features = cbind(features, class = labels);
313
+ rbbt.require('e1071')
314
+ model = svm(class ~ ., data = features)
315
+ save(model, file="#{ model_file }");
316
+ EOF
317
+ end
318
+ }
319
+
320
+ model.eval_model = Proc.new{|model_file,features|
321
+ TmpFile.with_file do |feature_file|
322
+ TmpFile.with_file do |results|
323
+ Open.write(feature_file, features * "\t")
324
+ puts R.run(<<-EOF
325
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
326
+ library(e1071)
327
+ load(file="#{ model_file }")
328
+ label = predict(model, features);
329
+ cat(label, file="#{results}");
330
+ EOF
331
+ ).read
332
+ Open.read(results)
333
+ end
334
+ end
335
+
336
+ }
337
+
338
+ pairs = text.split(/\n/).collect do |line|
339
+ label, features = line.split(" ")
340
+ [features, label]
341
+ end
342
+
343
+ model.add_list(*Misc.zip_fields(pairs))
344
+
345
+ model.train
346
+
347
+ assert model.eval("1;1;1").to_f > 0.5
348
+ assert model.eval("0;0;0").to_f < 0.5
349
+ end
350
+ end
351
+
352
+ def test_model_save
353
+ text =<<-EOF
354
+ 1 0;1;1
355
+ 1 1;0;1
356
+ 1 1;1;1
357
+ 1 0;1;1
358
+ 1 1;1;1
359
+ 0 0;1;0
360
+ 0 1;0;0
361
+ 0 0;1;0
362
+ 0 1;0;0
363
+ EOF
364
+
365
+ TmpFile.with_file() do |dir|
366
+ FileUtils.mkdir_p dir
367
+ model = VectorModel.new(dir)
368
+
369
+ model.extract_features = Proc.new{|element,list|
370
+ if element
371
+ element.split(";")
372
+ elsif list
373
+ list.collect{|e| e.split(";") }
374
+ end
375
+ }
376
+
377
+ model.train_model = Proc.new{|model_file,features,labels|
378
+ TmpFile.with_file do |feature_file|
379
+ Open.write(feature_file, features.collect{|feats| feats * "\t"} * "\n")
380
+ Open.write(feature_file + '.class', labels * "\n")
381
+ R.run <<-EOF
382
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
383
+ labels = scan("#{ feature_file }.class", what=numeric());
384
+ features = cbind(features, class = labels);
385
+ rbbt.require('e1071')
386
+ model = svm(class ~ ., data = features)
387
+ save(model, file="#{ model_file }");
388
+ EOF
389
+ end
390
+ }
391
+
392
+ model.eval_model = Proc.new{|model_file,features|
393
+ TmpFile.with_file do |feature_file|
394
+ TmpFile.with_file do |results|
395
+ Open.write(feature_file, features * "\t")
396
+ puts R.run(<<-EOF
397
+ features = read.table("#{ feature_file }", sep ="\\t", stringsAsFactors=FALSE);
398
+ library(e1071)
399
+ load(file="#{ model_file }")
400
+ label = predict(model, features);
401
+ cat(label, file="#{results}");
402
+ EOF
403
+ ).read
404
+ Open.read(results)
405
+ end
406
+ end
407
+
408
+ }
409
+
410
+ pairs = text.split(/\n/).collect do |line|
411
+ label, features = line.split(" ")
412
+ model.add features, label
413
+ end
414
+
415
+ model.train
416
+
417
+ model = VectorModel.new(dir)
418
+ pairs = text.split(/\n/).collect do |line|
419
+ label, features = line.split(" ")
420
+ model.add features, label
421
+ end
422
+
423
+ assert model.eval("1;1;1").to_f > 0.5
424
+ assert model.eval("0;0;0").to_f < 0.5
425
+ end
426
+ end
427
+
74
428
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbbt-dm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.47
4
+ version: 1.1.52
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miguel Vazquez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-12 00:00:00.000000000 Z
11
+ date: 2021-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rbbt-util
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: priority_queue_cxx
42
+ name: priority_queue_cxx17
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -105,11 +105,18 @@ files:
105
105
  - lib/rbbt/statistics/hypergeometric.rb
106
106
  - lib/rbbt/statistics/random_walk.rb
107
107
  - lib/rbbt/statistics/rank_product.rb
108
+ - lib/rbbt/tensorflow.rb
108
109
  - lib/rbbt/vector/model.rb
110
+ - lib/rbbt/vector/model/spaCy.rb
109
111
  - lib/rbbt/vector/model/svm.rb
112
+ - lib/rbbt/vector/model/tensorflow.rb
110
113
  - share/R/MA.R
111
114
  - share/R/barcode.R
112
115
  - share/R/heatmap.3.R
116
+ - share/spaCy/cpu/textcat_accuracy.conf
117
+ - share/spaCy/cpu/textcat_efficiency.conf
118
+ - share/spaCy/gpu/textcat_accuracy.conf
119
+ - share/spaCy/gpu/textcat_efficiency.conf
113
120
  - test/rbbt/matrix/test_barcode.rb
114
121
  - test/rbbt/network/test_paths.rb
115
122
  - test/rbbt/statistics/test_fdr.rb
@@ -118,7 +125,9 @@ files:
118
125
  - test/rbbt/statistics/test_random_walk.rb
119
126
  - test/rbbt/test_ml_task.rb
120
127
  - test/rbbt/test_stan.rb
128
+ - test/rbbt/vector/model/test_spaCy.rb
121
129
  - test/rbbt/vector/model/test_svm.rb
130
+ - test/rbbt/vector/model/test_tensorflow.rb
122
131
  - test/rbbt/vector/test_model.rb
123
132
  - test/test_helper.rb
124
133
  homepage: http://github.com/mikisvaz/rbbt-phgx
@@ -139,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
139
148
  - !ruby/object:Gem::Version
140
149
  version: '0'
141
150
  requirements: []
142
- rubygems_version: 3.0.6
151
+ rubygems_version: 3.1.4
143
152
  signing_key:
144
153
  specification_version: 4
145
154
  summary: Data-mining and statistics
@@ -152,6 +161,8 @@ test_files:
152
161
  - test/rbbt/statistics/test_hypergeometric.rb
153
162
  - test/rbbt/test_ml_task.rb
154
163
  - test/rbbt/vector/test_model.rb
164
+ - test/rbbt/vector/model/test_spaCy.rb
165
+ - test/rbbt/vector/model/test_tensorflow.rb
155
166
  - test/rbbt/vector/model/test_svm.rb
156
167
  - test/rbbt/test_stan.rb
157
168
  - test/test_helper.rb