1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
class Nightly
NIGHTLY_REP_DIR = File.join(FileUtils.pwd,"nightly")
NIGHTLY_REPORT_XML = "nightly_report.xml"
NIGHTLY_REPORT_HTML = "nightly_report.html"
def self.get_nightly
LOGGER.info("Accessing nightly report.")
if File.exist?(File.join(NIGHTLY_REP_DIR,NIGHTLY_REPORT_HTML))
return File.new(File.join(NIGHTLY_REP_DIR,NIGHTLY_REPORT_HTML))
else
return "Nightly report not available, try again later"
end
end
def self.build_nightly
task_uri = OpenTox::Task.as_task() do
LOGGER.info("Building nightly report")
benchmarks = [ HamsterTrainingTestBenchmark.new,
HamsterCrossvalidationBenchmark.new,
MiniRegressionBenchmark.new,
#FatheadRegressionBenchmark.new,
]
running = []
report = Reports::XMLReport.new("Nightly Validation", Time.now.strftime("Created at %m.%d.%Y - %H:%M"))
benchmarks.each do |b|
running << b.class.to_s.gsub(/Nightly::/, "")+b.object_id.to_s
Thread.new do
begin
b.build
rescue => ex
LOGGER.error "uncaught nightly build error: "+ex.message
ensure
running.delete(b.class.to_s.gsub(/Nightly::/, "")+b.object_id.to_s)
end
end
end
wait = 0
while running.size>0
LOGGER.debug "Nighlty report waiting for "+running.inspect if wait%60==0
wait += 1
sleep 1
end
LOGGER.debug "Nighlty report, all benchmarks done "+running.inspect
section_about = report.add_section(report.get_root_element, "About this report")
report.add_paragraph(section_about,
"This a opentox internal test report. Its purpose is to maintain interoperability between the OT validation web service "+
"and other OT web services. If you have any comments, remarks, or wish your service/test-case to be added, please email "+
"to guetlein@informatik.uni-freiburg.de or use the issue tracker at http://opentox.informatik.uni-freiburg.de/simple_ot_stylesheet.css")
benchmarks.each do |b|
section = report.add_section(report.get_root_element, b.title)
section_info = report.add_section(section, "Info")
b.info.each{|i| report.add_paragraph(section_info,i)}
info_table = b.info_table
report.add_table(section_info, b.info_table_title, info_table) if info_table
section_results = report.add_section(section, "Results")
report.add_table(section_results, b.result_table_title, b.result_table)
if (b.comparison_report)
report.add_table(section_results, b.comparison_report_title, [[b.comparison_report]], false)
end
section_errors = report.add_section(section, "Errors")
if b.errors and b.errors.size>0
b.errors.each do |k,v|
elem = report.add_section(section_errors,k)
report.add_paragraph(elem,v,true)
end
else
report.add_paragraph(section_errors,"no errors occured")
end
end
report.write_to(File.new(File.join(NIGHTLY_REP_DIR,NIGHTLY_REPORT_XML), "w"))
Reports::ReportFormat.format_report_to_html(NIGHTLY_REP_DIR,
NIGHTLY_REPORT_XML,
NIGHTLY_REPORT_HTML,
nil)
#"http://www.opentox.org/portal_css/Opentox%20Theme/base-cachekey7442.css")
#"http://apps.ideaconsult.net:8080/ToxPredict/style/global.css")
LOGGER.info("Nightly report completed")
return "Nightly report completed"
end
if defined?(halt)
halt 202,task_uri
else
return task_uri
end
end
class ValidationBenchmark
def info_table_title
return title
end
def info_table
return nil
end
def result_table_title
return "Validation results"
end
def result_table
raise "no comparables" unless @comparables
raise "no validations" unless @validations
raise "no reports" unless @reports
t = []
row = [comparable_nice_name, "validation", "report"]
t << row
(0..@comparables.size-1).each do |i|
row = [ @comparables[i], @validations[i], @reports[i] ]
t << row
end
t
end
def comparison_report_title
"algorithm comparison report"
end
def comparison_report
@comparison_report
end
end
class AlgorihtmValidationBenchmark < ValidationBenchmark
def comparable_nice_name
return "algorithm"
end
def info_table
t = []
t << ["param", "uri"]
params.each do |k,v|
t << [k.to_s, v.to_s]
end
count = 1
@algs.each do |alg|
t << ["algorithm_uri"+" ["+count.to_s+"]", alg]
count += 1
end
t
end
def errors
@errors
end
def params
raise "return uri-value hash"
end
def validate(index)
raise "validate, return uri"
end
def build_report(index)
raise "build report, return uri"
end
def build()
raise "no algs" unless @algs
@comparables = @algs
@validations = Array.new(@comparables.size)
@reports = Array.new(@comparables.size)
@errors = {}
to_compare = []
# LOGGER.info "train-data: "+@train_data.to_s
# LOGGER.info "test-data: "+@test_data.to_s
# LOGGER.info "test-class-data: "+@test_class_data.to_s
running = []
(0..@comparables.size-1).each do |i|
Thread.new do
running << @comparables[i]+i.to_s
begin
LOGGER.debug "Validate: "+@algs[i].to_s
@validations[i] = validate(i)
to_compare << @validations[i] if OpenTox::Utils.is_uri?(@validations[i])
begin
LOGGER.debug "Building validation-report for "+@validations[i].to_s+" ("+@algs[i].to_s+")"
@reports[i] = build_report(i)
rescue => ex
LOGGER.error "validation-report error: "+ex.message
@reports[i] = "error"
end
rescue => ex
LOGGER.error "validation error: "+ex.message
key = "Error validating "+@comparables[i].to_s
@validations[i] = key+" (see below)"
@errors[key] = ex.message
ensure
running.delete(@comparables[i]+i.to_s)
end
end
end
wait = 0
while running.size>0
LOGGER.debug self.class.to_s.gsub(/Nightly::/, "")+" waiting for "+running.inspect if wait%20==0
wait += 1
sleep 1
end
if to_compare.size>1
LOGGER.debug self.class.to_s.gsub(/Nightly::/, "")+": build algorithm comparison report"
@comparison_report = Util.create_alg_comparison_report(to_compare)
else
LOGGER.debug self.class.to_s.gsub(/Nightly::/, "")+": nothing to compare"
end
end
end
class TrainingTestValidationBenchmark < AlgorihtmValidationBenchmark
def info
[ training_test_info ]
end
def training_test_info
"This is a training test set validation. It builds a model with an algorithm and the training dataset. "+
"The model is used to predict the test dataset. Evaluation is done by comparing the model predictions "+
"to the actual test values (in the test target dataset)."
end
def info_table_title
"Validation params"
end
def params
p = { "training_dataset_uri" => @train_data, "test_dataset_uri" => @test_data,
"prediction_feature" => @pred_feature }
p["test_target_dataset_uri"] = @test_class_data if @test_class_data
return p
end
def validate(index)
Util.validate_alg(@train_data, @test_data, @test_class_data,
@algs[index], @pred_feature, @alg_params[index]).to_s
end
def build_report(index)
Util.create_report(@validations[index])
end
def build()
raise "no train data" unless @train_data
raise "no test data" unless @test_data
raise "no pred feature" unless @pred_feature
super
end
end
class CrossValidationBenchmark < AlgorihtmValidationBenchmark
def info
[ training_test_info ]
end
def training_test_info
"This is a cross-validation."
end
def info_table_title
"Cross-validation params"
end
def params
p = { "dataset_uri" => @data, "prediction_feature" => @pred_feature,
"num_folds" => @num_folds, "random_seed" => @random_seed, "stratified" => @stratified}
return p
end
def validate(index)
Util.cross_validate_alg(@data, @algs[index], @pred_feature,
@num_folds, @random_seed, @stratified, @alg_params[index]).to_s
end
def build_report(index)
Util.create_report(@validations[index], "crossvalidation")
end
def build()
raise "no data" unless @data
raise "no pred feature" unless @pred_feature
@num_folds = 10 unless @num_folds
@random_seed = 1 unless @random_seed
@stratified = false unless @stratified
super
end
end
class HamsterCrossvalidationBenchmark < CrossValidationBenchmark
@@dataset_service = @@config[:services]["opentox-dataset"]
@@file=File.new("data/hamster_carcinogenicity.yaml","r")
@@file_type="text/x-yaml"
@@lazar_server = @@config[:services]["opentox-algorithm"]
def title()
"Training test set validation, binary classification"
end
def info
res = [ "A crossvalidation using the hamster carcinogenicity dataset." ] + super
return res
end
def build()
@algs = [
File.join(@@config[:services]["opentox-majority"],["/class/algorithm"]),
File.join(@@lazar_server,"lazar"),
"http://188.40.32.88/algorithm/lazar",
]
@alg_params = [
nil,
"feature_generation_uri="+File.join(@@lazar_server,"fminer"),
"feature_generation_uri=http://188.40.32.88/algorithm/fminer",
]
@pred_feature = "http://localhost/toxmodel/feature#Hamster%20Carcinogenicity%20(DSSTOX/CPDB)"
LOGGER.debug "upload hamster datasets"
@data = Util.upload_dataset(@@dataset_service, @@file, @@file_type).chomp("\n")
super
end
end
class HamsterTrainingTestBenchmark < TrainingTestValidationBenchmark
@@dataset_service = @@config[:services]["opentox-dataset"]
@@file=File.new("data/hamster_carcinogenicity.yaml","r")
@@file_type="text/x-yaml"
@@lazar_server = @@config[:services]["opentox-algorithm"]
def title()
"Training test set validation, binary classification"
end
def info
res = [ "A simple binary classification task using the hamster carcinogenicity dataset." ] + super
return res
end
def build()
@algs = [
File.join(@@config[:services]["opentox-majority"],["/class/algorithm"]),
File.join(@@lazar_server,"lazar"),
"http://188.40.32.88/algorithm/lazar",
]
@alg_params = [
nil,
"feature_generation_uri="+File.join(@@lazar_server,"fminer"),
"feature_generation_uri=http://188.40.32.88/algorithm/fminer",
]
@pred_feature = "http://localhost/toxmodel/feature#Hamster%20Carcinogenicity%20(DSSTOX/CPDB)"
LOGGER.debug "prepare hamster datasets"
#@test_class_data = Util.upload_dataset(@@dataset_service, @@file, @@file_type).chomp("\n")
@pred_feature = "http://188.40.32.88/toxcreate/feature#Hamster%20Carcinogenicity%20(DSSTOX/CPDB)"
@test_class_data = "http://188.40.32.88/dataset/57"
split = Util.split_dataset(@test_class_data, @pred_feature, 0.9, 1)
@train_data = split[0].to_s
@test_data = split[1].to_s
raise "could not split "+@train_data.to_s+" "+@test_data.to_s unless OpenTox::Utils.is_uri?(@train_data) and OpenTox::Utils.is_uri?(@test_data)
super
end
end
class MiniRegressionBenchmark < TrainingTestValidationBenchmark
def title
"Training test set validation, small regression dataset"
end
def info
res = [ "A very small regression task, using the training dataset as test set." ] + super
return res
end
def build()
@algs = [
"http://opentox.ntua.gr:3003/algorithm/mlr",
"http://opentox.informatik.tu-muenchen.de:8080/OpenTox-dev/algorithm/kNNregression",
File.join(@@config[:services]["opentox-majority"],["/regr/algorithm"])
]
@alg_params = [ nil, "dataset_service=http://ambit.uni-plovdiv.bg:8080/ambit2/dataset", nil]
@train_data = "http://ambit.uni-plovdiv.bg:8080/ambit2/dataset/342"
@test_data = "http://ambit.uni-plovdiv.bg:8080/ambit2/dataset/342"
@pred_feature = "http://ambit.uni-plovdiv.bg:8080/ambit2/feature/103141"
super
end
end
class FatheadRegressionBenchmark < TrainingTestValidationBenchmark
def title
"Training test set validation, regression with fathead minnow dataset"
end
def info
res = [ "This is the regression use case used in D2.2. "+
"The task is to predict LC50 values of the well known Fathead Minnow Acute Toxicity dataset. "+
"JOELIB was used to compute numerical descriptors as features." ] + super
return res
end
def build()
@algs = [
"http://opentox.informatik.tu-muenchen.de:8080/OpenTox-dev/algorithm/kNNregression",
"http://opentox.informatik.tu-muenchen.de:8080/OpenTox-dev/algorithm/M5P",
"http://opentox.informatik.tu-muenchen.de:8080/OpenTox-dev/algorithm/GaussP"
]
@alg_params = [nil, nil, nil]
@train_data = "http://ambit.uni-plovdiv.bg:8080/ambit2/dataset/639"
@test_data = "http://ambit.uni-plovdiv.bg:8080/ambit2/dataset/640"
@pred_feature = "http://ambit.uni-plovdiv.bg:8080/ambit2/feature/264185"
super
end
end
class Util
@@validation_service = @@config[:services]["opentox-validation"]
def self.upload_dataset(dataset_service, file, file_type)
raise "File not found: "+file.path.to_s unless File.exist?(file.path)
data = File.read(file.path)
data_uri = OpenTox::RestClientWrapper.post dataset_service, {:content_type => file_type}, data
#data_uri = OpenTox::Task.find(data_uri).wait_for_resource.to_s if OpenTox::Utils.task_uri?(data_uri)
return data_uri
end
def self.split_dataset(data_uri, feature, split_ratio, random_seed)
res = OpenTox::RestClientWrapper.post File.join(@@validation_service,'plain_training_test_split'), { :dataset_uri => data_uri, :prediction_feature=>feature, :split_ratio=>split_ratio, :random_seed=>random_seed}
return res.split("\n")
end
def self.validate_alg(train_data, test_data, test_class_data, alg, feature, alg_params)
uri = OpenTox::RestClientWrapper.post @@validation_service, { :training_dataset_uri => train_data, :test_dataset_uri => test_data,
:test_target_dataset_uri => test_class_data,
:algorithm_uri => alg, :prediction_feature => feature, :algorithm_params => alg_params }
#LOGGER.info "waiting for validation "+uri.to_s
#uri = OpenTox::Task.find(uri).wait_for_resource.to_s if OpenTox::Utils.task_uri?(uri)
#LOGGER.info "validaiton done "+uri.to_s
return uri
end
def self.cross_validate_alg(data, alg, feature, folds, seed, stratified, alg_params)
uri = OpenTox::RestClientWrapper.post File.join(@@validation_service,"crossvalidation"), { :dataset_uri => data,
:algorithm_uri => alg, :prediction_feature => feature, :algorithm_params => alg_params, :num_folds => folds,
:random_seed => seed, :stratified => stratified }
#LOGGER.info "waiting for validation "+uri.to_s
#uri = OpenTox::Task.find(uri).wait_for_resource.to_s if OpenTox::Utils.task_uri?(uri)
#LOGGER.info "validaiton done "+uri.to_s
return uri
end
def self.create_report(validation, type="validation")
uri = OpenTox::RestClientWrapper.post File.join(@@validation_service,"report/"+type.to_s), { :validation_uris => validation }
#uri = OpenTox::Task.find(uri).wait_for_resource.to_s if OpenTox::Utils.task_uri?(uri)
return uri
end
def self.create_alg_comparison_report(validations)
uri = OpenTox::RestClientWrapper.post File.join(@@validation_service,"report/algorithm_comparison"), { :validation_uris => validations.join("\n") }
#uri = OpenTox::Task.find(uri).wait_for_resource.to_s if OpenTox::Utils.task_uri?(uri)
return uri
end
end
end
|