summaryrefslogtreecommitdiff
path: root/lazar.rb
blob: d4cdc36e0e1a84f864136d4f8cfebc9872714b88 (plain)
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
require 'rubygems'
require 'opentox-ruby'
require 'test/unit'

class Float
  def round_to(x)
    (self * 10**x).round.to_f / 10**x
  end
end

class LazarTest < Test::Unit::TestCase

=begin
=end
  def test_create_regression_model
    model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => @@regression_training_dataset.uri, :subjectid => @@subjectid}).to_s
    lazar = OpenTox::Model::Lazar.find model_uri, @@subjectid
    assert_equal 225, lazar.features.size
    compound = OpenTox::Compound.from_smiles("c1ccccc1NN")
    #puts lazar.uri
    #puts compound.inspect
    #puts "prediction"
    #puts prediction.value(compound).inspect
    prediction_uri = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    #puts prediction_uri
    prediction = OpenTox::LazarPrediction.find(prediction_uri, @@subjectid)
    assert_equal prediction.value(compound).round_to(4), 0.1618.round_to(4)
    assert_equal prediction.confidence(compound).round_to(4), 0.6114.round_to(4)
    assert_equal prediction.neighbors(compound).size, 81
    prediction.delete(@@subjectid)
    lazar.delete(@@subjectid)
  end

  def test_default_classification_model
    # create model
    model_uri = OpenTox::Algorithm::Lazar.new.run({:dataset_uri => @@classification_training_dataset.uri, :subjectid => @@subjectid}).to_s
    lazar = OpenTox::Model::Lazar.find model_uri, @@subjectid
    assert_equal lazar.features.size, 41

    # single prediction
    compound = OpenTox::Compound.from_smiles("c1ccccc1NN")
    prediction_uri = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    #puts prediction_uri
    prediction = OpenTox::LazarPrediction.find(prediction_uri, @@subjectid)
    puts prediction.inspect
    assert_equal prediction.value(compound), false
    assert_equal prediction.confidence(compound).round_to(4), 0.3005.round_to(4)
    assert_equal prediction.neighbors(compound).size, 15
    prediction.delete(@@subjectid)
    # dataset activity
    compound = OpenTox::Compound.from_smiles("CNN")
    prediction_uri  = lazar.run(:compound_uri => compound.uri, :subjectid => @@subjectid)
    prediction = OpenTox::LazarPrediction.find prediction_uri, @@subjectid
    assert !prediction.measured_activities(compound).empty?
    #puts prediction.measured_activities(compound).first.inspect
    assert_equal prediction.measured_activities(compound).first, true
    assert prediction.value(compound).nil?
    prediction.delete(@@subjectid)
    # dataset prediction
    test_dataset = OpenTox::Dataset.create_from_csv_file("data/multicolumn.csv", @@subjectid)
    prediction = OpenTox::LazarPrediction.find lazar.run(:dataset_uri => test_dataset.uri, :subjectid => @@subjectid), @@subjectid
    assert_equal prediction.compounds.size, 4
    compound = OpenTox::Compound.new prediction.compounds.first
    #puts "compound"
    #puts compound.inspect
    #puts prediction.value(compound).inspect
    assert_equal prediction.value(compound), false
    prediction.delete(@@subjectid)
    lazar.delete(@@subjectid)
  end
=begin
=end

end