summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormguetlein <martin.guetlein@gmail.com>2011-02-21 16:04:07 +0100
committermguetlein <martin.guetlein@gmail.com>2011-02-21 16:04:07 +0100
commita851e4794630fbae3b6b3bf228b43f550f61febb (patch)
treef79a114dce391eb97660e38c0ad8e3acaa8d80c5
parent545f4f9e511d8e39a808a1c24de37eb33d2f026e (diff)
add new dataset test
-rw-r--r--Rakefile6
-rw-r--r--dataset.rb136
2 files changed, 107 insertions, 35 deletions
diff --git a/Rakefile b/Rakefile
index d260989..5e23751 100644
--- a/Rakefile
+++ b/Rakefile
@@ -27,6 +27,12 @@ task :teardown do
OpenTox::Authorization.logout(@@subjectid)
end
+class Exception
+ def message
+ errorCause ? errorCause.to_yaml : to_s
+ end
+end
+
[:all, :feature, :dataset, :fminer, :lazar, :authorization].each do |t|
task :teardown => t
task t => :setup
diff --git a/dataset.rb b/dataset.rb
index d15baf7..db87b2b 100644
--- a/dataset.rb
+++ b/dataset.rb
@@ -7,6 +7,7 @@ class DatasetTest < Test::Unit::TestCase
def setup
@datasets = {
+ @@regression_training_dataset.uri => nil,
@@classification_training_dataset.uri => {
:nr_compounds => 85,
:nr_features => 1,
@@ -16,7 +17,7 @@ class DatasetTest < Test::Unit::TestCase
"http://apps.ideaconsult.net:8080/ambit2/dataset/2698" => {
:nr_compounds => 3,
:nr_features => 8,
- :nr_dataset_features => 37,
+ :nr_dataset_features => 36,
:nr_data_entries => 3
}
}
@@ -29,7 +30,7 @@ class DatasetTest < Test::Unit::TestCase
=begin
def test_save_external
- @dataset = OpenTox::Dataset.find "http://apps.ideaconsult.net:8080/ambit2/dataset/2698", @@subjectid
+ @dataset = OpenTox::Dataset.find "http://apps.ideaconsult.net:8080/ambit2/dataset/2698"
#File.open("test.rdf","w+"){|f| f.puts @dataset.to_rdfxml}
@dataset.uri = "http://apps.ideaconsult.net:8080/ambit2/dataset"
uri = @dataset.save(@@subjectid)
@@ -47,7 +48,7 @@ class DatasetTest < Test::Unit::TestCase
end
def test_all
- datasets = OpenTox::Dataset.all(CONFIG[:services]["opentox-dataset"], @@subjectid)
+ datasets = OpenTox::Dataset.all
assert_kind_of Array, datasets
end
@@ -59,36 +60,36 @@ class DatasetTest < Test::Unit::TestCase
end
def test_from_yaml
- @dataset = OpenTox::Dataset.new nil, @@subjectid
+ @dataset = OpenTox::Dataset.new
@dataset.load_yaml(File.open("data/hamster_carcinogenicity.yaml").read)
hamster_carc?
end
def test_rest_csv
- uri = OpenTox::RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:accept => "text/uri-list"}, {:file => File.new("data/hamster_carcinogenicity.csv"), :subjectid => @@subjectid}).to_s.chomp
- @dataset = OpenTox::Dataset.new uri, @@subjectid
- @dataset.load_all(@@subjectid)
+ uri = OpenTox::RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:file => File.new("data/hamster_carcinogenicity.csv"), :subjectid => @@subjectid}, {:accept => "text/uri-list"}).to_s.chomp
+ @dataset = OpenTox::Dataset.new uri
+ @dataset.load_all @@subjectid
hamster_carc?
end
def test_multicolumn_csv
- uri = OpenTox::RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:accept => "text/uri-list"}, {:file => File.new("data/multicolumn.csv"), :subjectid => @@subjectid}).to_s.chomp
- @dataset = OpenTox::Dataset.new uri, @@subjectid
- @dataset.load_all(@@subjectid)
+ uri = OpenTox::RestClientWrapper.post(CONFIG[:services]["opentox-dataset"],{:file => File.new("data/multicolumn.csv"), :subjectid => @@subjectid}, {:accept => "text/uri-list"}).to_s.chomp
+ @dataset = OpenTox::Dataset.new uri
+ @dataset.load_all @@subjectid
assert_equal 5, @dataset.features.size
assert_equal 4, @dataset.compounds.size
end
def test_from_csv
- @dataset = OpenTox::Dataset.new(nil, @@subjectid)
+ @dataset = OpenTox::Dataset.new
@dataset.load_csv(File.open("data/hamster_carcinogenicity.csv").read, @@subjectid)
hamster_carc?
@dataset.delete(@@subjectid)
end
def test_from_excel
- @dataset = OpenTox::Dataset.new(nil, @@subjectid)
+ @dataset = OpenTox::Dataset.new
@dataset.load_spreadsheet(Excel.new("data/hamster_carcinogenicity.xls"), @@subjectid)
hamster_carc?
@dataset.delete(@@subjectid)
@@ -103,32 +104,32 @@ class DatasetTest < Test::Unit::TestCase
def test_load_compounds
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_compounds(@@subjectid)
- assert_equal @dataset.compounds.size,data[:nr_compounds]
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_compounds @@subjectid
+ assert_equal @dataset.compounds.size,data[:nr_compounds] if data
end
end
def test_load_features
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_features(@@subjectid)
- assert_equal @dataset.features.keys.size,data[:nr_dataset_features]
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_features @@subjectid
+ assert_equal @dataset.features.keys.size,data[:nr_dataset_features] if data
end
end
def test_load_all
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_all(@@subjectid)
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
validate data
end
end
def test_yaml
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_all(@@subjectid)
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
#@dataset = YAML.load @dataset.to_yaml
validate data
end
@@ -136,18 +137,18 @@ class DatasetTest < Test::Unit::TestCase
def test_csv
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_all(@@subjectid)
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
csv = @dataset.to_csv.split("\n")
- assert_equal csv.size, data[:nr_compounds]+1
- assert_equal csv.first.split(", ").size, data[:nr_dataset_features]+1
+ assert_equal csv.size, data[:nr_compounds]+1 if data
+ assert_equal csv.first.split(", ").size, data[:nr_dataset_features]+1 if data
end
end
def test_excel
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_all(@@subjectid)
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
book = @dataset.to_spreadsheet
assert_kind_of Spreadsheet::Workbook, book
#File.open("#{@dataset.id}.xls","w+"){|f| book.write f.path}
@@ -156,26 +157,91 @@ class DatasetTest < Test::Unit::TestCase
def test_ntriples
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_all(@@subjectid)
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
assert_kind_of String, @dataset.to_ntriples
end
end
def test_owl
@datasets.each do |uri,data|
- @dataset = OpenTox::Dataset.new(uri, @@subjectid)
- @dataset.load_all(@@subjectid)
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
assert_kind_of String, @dataset.to_rdfxml
end
end
+
+#=end
+
+ def test_rdf_conversion
+ @datasets.each do |uri,data|
+ #puts "dataset "+uri.to_s
+ @dataset = OpenTox::Dataset.new(uri)
+ @dataset.load_all @@subjectid
+
+ #puts @dataset.features.keys.size.to_s+" # features"
+ #puts "download to rdf"
+ f = Tempfile.new("ot-rdf")
+ f.puts @dataset.to_rdfxml
+ f.close
+
+ #puts "upload as rdf"
+ d = OpenTox::Dataset.create(CONFIG[:services]["opentox-dataset"], @@subjectid)
+ d.load_rdfxml_file(f)
+ f.delete
+ d.save(@@subjectid)
+ assert d.uri.uri?
+
+ #puts "compare"
+ dataset_equal(@dataset,d)
+
+ #puts "download new dataset as yaml"
+ d2 = OpenTox::Dataset.new(d.uri)
+ d2.load_all @@subjectid
+
+ #puts "compare again"
+ dataset_equal(@dataset,d2)
+ end
+ end
+
=begin
=end
+
+ def dataset_equal(d,d2)
+ assert d.compounds.sort==d2.compounds.sort,
+ d.compounds.sort.to_yaml+"\n!=\n"+d2.compounds.sort.to_yaml
+ assert d.features.keys.size==d2.features.keys.size,
+ d.features.keys.to_yaml+"\n!=\n"+d2.features.keys.to_yaml
+ assert d.features.keys.sort==d2.features.keys.sort,
+ d.features.keys.sort.to_yaml+"\n!=\n"+d2.features.keys.sort.to_yaml
+ d.compounds.each do |c|
+ d.features.keys.each do |f|
+ assert_array_about_equal d.data_entries[c][f],d2.data_entries[c][f]
+ end
+ end
+ end
+
+ def assert_array_about_equal(a,a2)
+ if (a!=nil || a2!=nil)
+ raise "no arrays #{a.class} #{a2.class}" unless a.is_a?(Array) and a2.is_a?(Array)
+ assert a.size==a2.size
+ a.sort!
+ a2.sort!
+ a.size.times do |i|
+ if (a[i].is_a?(Float) and a2[i].is_a?(Float))
+ assert (a[i]-a2[i]).abs<0.0000001,"#{a[i]}(#{a[i].class}) != #{a2[i]}(#{a2[i].class})"
+ else
+ assert a[i]==a2[i],"#{a[i]}(#{a[i].class}) != #{a2[i]}(#{a2[i].class})"
+ end
+ end
+ end
+ end
+
def validate(data)
assert_kind_of OpenTox::Dataset, @dataset
- assert_equal @dataset.data_entries.size, data[:nr_data_entries]
- assert_equal @dataset.compounds.size, data[:nr_compounds]
- assert_equal @dataset.features.size, data[:nr_dataset_features]
+ assert_equal @dataset.data_entries.size, data[:nr_data_entries] if data
+ assert_equal @dataset.compounds.size, data[:nr_compounds] if data
+ assert_equal @dataset.features.size, data[:nr_dataset_features] if data
assert_equal @dataset.uri, @dataset.metadata[XSD.anyURI]
end