summaryrefslogtreecommitdiff
path: root/example.rb
blob: 1e27077f6eec5e6cc47bea939f44fbb43fe7b4f7 (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
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

class Example
  
  @@file=File.new("data/hamster_carcinogenicity.owl","r")
  @@model=File.join @@config[:services]["opentox-model"],"1"
  @@feature="http://www.epa.gov/NCCT/dsstox/CentralFieldDef.html#ActivityOutcome_CPDBAS_Hamster"
  @@alg = File.join @@config[:services]["opentox-algorithm"],"lazar"
  @@alg_params = "feature_generation_uri="+File.join(@@config[:services]["opentox-algorithm"],"fminer")
  @@data=File.join @@config[:services]["opentox-dataset"],"1"
  @@train_data=File.join @@config[:services]["opentox-dataset"],"2"
  @@test_data=File.join @@config[:services]["opentox-dataset"],"3"
  
  @@summary=""
  
  def self.transform_example
  
    file = File.new("EXAMPLES", "r")
    res = ""
    while (line = file.gets) 
      res += line
    end
    file.close
    
    sub = { "validation_service" => @@config[:services]["opentox-validation"].chomp("/"), 
            "validation_id" => "1",
            "model_uri" => @@model,
            "dataset_uri" => @@data,
            "training_dataset_uri" => @@train_data,
            "test_dataset_uri" => @@test_data,
            "prediction_feature" => @@feature,
            "algorithm_uri" => @@alg,
            "algorithm_params" => @@alg_params,
            "crossvalidation_id" => "1",
            "validation_report_id" => "1",
            "crossvalidation_report_id" => "1",
            }
    
    sub.each do |k,v|
      res.gsub!(/<#{k}>/,v)
    end
    res
  end
  
  def self.delete_all(uri_list_service)
    uri_list = OpenTox::RestClientWrapper.get(uri_list_service)
    uri_list.split("\n").each do |uri|
      OpenTox::RestClientWrapper.delete(uri)
    end
  end
  
  def self.log(log_string)
    LOGGER.debug log_string
    @@summary += log_string+"\n"
  end
  
  def self.prepare_example_resources
    
    @@summary = ""
    delete_all(@@config[:services]["opentox-dataset"])
    
    data = File.read(@@file.path)
    data_uri = OpenTox::RestClientWrapper.post @@config[:services]["opentox-dataset"], data, :content_type => "application/rdf+xml"
    log "uploaded dataset "+data_uri
    raise "failed to prepare demo" unless data_uri==@@data
    
    Lib::Validation.auto_migrate!
    delete_all(@@config[:services]["opentox-model"])
    vali_uri = OpenTox::RestClientWrapper.post File.join(@@config[:services]["opentox-validation"],'/training_test_split'), { :dataset_uri => data_uri,
                                                         :algorithm_uri => @@alg,
                                                         :prediction_feature => @@feature,
                                                         :algorithm_params => @@alg_params }
    log "created validation via training test split "+vali_uri
    raise "failed to prepare demo" unless vali_uri==File.join(@@config[:services]["opentox-validation"],'/1')
    
    Lib::Crossvalidation.auto_migrate!
    cv_uri = OpenTox::RestClientWrapper.post File.join(@@config[:services]["opentox-validation"],'/crossvalidation'), { :dataset_uri => data_uri,
                                                         :algorithm_uri => @@alg,
                                                         :prediction_feature => @@feature,
                                                         :algorithm_params => @@alg_params,
                                                         :num_folds => 5, :stratified => false }
    log "created crossvalidation "+cv_uri
    raise "failed to prepare demo" unless cv_uri==File.join(@@config[:services]["opentox-validation"],'/crossvalidation/1')
    
    delete_all(File.join(@@config[:services]["opentox-validation"],"/report/validation"))
    val_report_uri = OpenTox::RestClientWrapper.post File.join(@@config[:services]["opentox-validation"],'/report/validation'), { :validation_uris => vali_uri }
    log "created validation report: "+val_report_uri
    raise "failed to prepare demo" unless val_report_uri==File.join(@@config[:services]["opentox-validation"],'/report/validation/1')
    
    delete_all(File.join(@@config[:services]["opentox-validation"],"/report/crossvalidation"))
    cv_report_uri = OpenTox::RestClientWrapper.post File.join(@@config[:services]["opentox-validation"],'/report/crossvalidation'), { :validation_uris => cv_uri }
    log "created crossvalidation report: "+cv_report_uri
    raise "failed to prepare demo" unless cv_report_uri==File.join(@@config[:services]["opentox-validation"],'/report/crossvalidation/1')
    log "done"

    @@summary
  end
  
  
  def self.test_examples
    lines = transform_example.split("\n")
    curl_call = false
    curl_calls = []
    
    lines.each do |line|
      if line =~ /^\s*>>>\s*.*/
        line.gsub!(/^\s*>>>\s*/,"")
        if line =~ /.*\s*\\s*$/
          curl_call = true
          line.gsub!(/\s*\\s*$/," ")
        else
          curl_call = false
        end
        curl_calls.push( line )
      elsif curl_call
        if line =~ /.*\s*\\s*$/
          curl_call = true
          line.gsub!(/\s*\\s*$/," ")
        else
          curl_call = false
        end
        curl_calls[-1] = curl_calls[-1]+line
      end
    end
    
    @@summary = ""
    num = 0
    suc = 0
    curl_calls.each do |cmd|
      num += 1
      log "testing: "+cmd
      result = ""
      IO.popen(cmd.to_s+" 2> /dev/null") do |f| 
        while line = f.gets
          result += line
        end
      end
      result.gsub!(/\n/, " \\n ")
      if ($?==0)
        log "ok ( " +result.to_s[0,50]+" )"
        suc += 1
      else
        log "failed ( " +result.to_s[0,50]+" )"
      end
    end
    log num.to_s+"/"+num.to_s+" curls succeeded"
    @@summary  
  end
  
end