summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Helma <helma@in-silico.ch>2010-08-25 12:36:44 +0200
committerChristoph Helma <helma@in-silico.ch>2010-08-25 12:36:44 +0200
commit2ac041d483ce1ae79cd850d7e1938e4d516f8ff4 (patch)
treee7e0735b96a17a68529927e7577209baf580d90b
parentc82f2d63755393a37e156c5e40d8da0c08568364 (diff)
parent4aab535de7f45525c2c49350df2190511f93492c (diff)
Merge branch 'development'
Conflicts: opentox-ruby-api-wrapper.gemspec
-rw-r--r--Rakefile3
-rw-r--r--VERSION2
-rw-r--r--lib/algorithm.rb12
-rw-r--r--lib/dataset.rb13
-rw-r--r--lib/environment.rb2
-rw-r--r--lib/model.rb168
-rw-r--r--lib/owl.rb5
-rw-r--r--lib/task.rb66
-rw-r--r--lib/validation.rb11
9 files changed, 166 insertions, 116 deletions
diff --git a/Rakefile b/Rakefile
index 41832bb..3846bd1 100644
--- a/Rakefile
+++ b/Rakefile
@@ -34,7 +34,8 @@ begin
'dm-timestamps',
'dm-types',
'dm-migrations',
- "dm-mysql-adapter"
+ "dm-mysql-adapter",
+ "dm-validations",
].each {|dep| gem.add_dependency dep, ">= 1" }
gem.add_dependency "haml", ">=3"
['cucumber','jeweler'].each { |dep| gem.add_development_dependency dep }
diff --git a/VERSION b/VERSION
index 266146b..9edc58b 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.6.3
+1.6.4
diff --git a/lib/algorithm.rb b/lib/algorithm.rb
index 2fde0eb..d7b57af 100644
--- a/lib/algorithm.rb
+++ b/lib/algorithm.rb
@@ -64,6 +64,18 @@ module OpenTox
0.0
end
end
+ def self.euclidean(prop_a,prop_b)
+ common_properties = prop_a.keys & prop_b.keys
+ if common_properties.size > 1
+ dist_sum = 0
+ common_properties.each do |p|
+ dist_sum += (prop_a[p] - prop_b[p])**2
+ end
+ 1/(1+Math.sqrt(dist_sum))
+ else
+ nil
+ end
+ end
end
end
diff --git a/lib/dataset.rb b/lib/dataset.rb
index 0be804c..2eb2206 100644
--- a/lib/dataset.rb
+++ b/lib/dataset.rb
@@ -66,8 +66,8 @@ module OpenTox
raise "no new compounds selected" unless new_compounds and new_compounds.size>0
# load require features
- if ((defined? @dirty_features) && (@dirty_features - new_features).size > 0)
- (@dirty_features - new_features).each{|f| load_feature_values(f)}
+ if ((defined? @dirty_features) && (@dirty_features & new_features).size > 0)
+ (@dirty_features & new_features).each{|f| load_feature_values(f)}
end
dataset = OpenTox::Dataset.new
@@ -202,16 +202,19 @@ module OpenTox
# overwrite to yaml:
# in case dataset is loaded from owl:
- # * load all values
- # * set @owl to nil (not necessary in yaml)
+ # * load all values
def to_yaml
# loads all features
if ((defined? @dirty_features) && @dirty_features.size > 0)
load_feature_values
end
- @owl = nil
super
end
+
+ # * remove @owl from yaml, not necessary
+ def to_yaml_properties
+ super - ["@owl"]
+ end
# saves (changes) as new dataset in dataset service
# returns uri
diff --git a/lib/environment.rb b/lib/environment.rb
index 80899c5..cfc875d 100644
--- a/lib/environment.rb
+++ b/lib/environment.rb
@@ -25,7 +25,7 @@ end
# database
if @@config[:database]
- ['dm-core', 'dm-serializer', 'dm-timestamps', 'dm-types', 'dm-migrations' ].each{|lib| require lib }
+ ['dm-core', 'dm-serializer', 'dm-timestamps', 'dm-types', 'dm-migrations', 'dm-validations' ].each{|lib| require lib }
case @@config[:database][:adapter]
when /sqlite/i
db_dir = File.join(basedir, "db")
diff --git a/lib/model.rb b/lib/model.rb
index e8f6048..e36b538 100644
--- a/lib/model.rb
+++ b/lib/model.rb
@@ -1,13 +1,13 @@
module OpenTox
- module Model
+ module Model
- class Generic
+ class Generic
MODEL_ATTRIBS = [:uri, :title, :creator, :date, :format, :predictedVariables, :independentVariables, :dependentVariables, :trainingDataset, :algorithm]
MODEL_ATTRIBS.each{ |a| attr_accessor(a) }
- def self.find(uri)
- owl = OpenTox::Owl.from_uri(uri, "Model")
+ def self.find(uri)
+ owl = OpenTox::Owl.from_uri(uri, "Model")
return self.new(owl)
end
@@ -28,86 +28,116 @@ module OpenTox
if ENV['RACK_ENV'] =~ /test|debug/
begin
raise "uri invalid" unless Utils.is_uri?(@uri)
- raise "no algorithm" unless @algorithm and @algorithm.size>0
- raise "no dependent variables" unless @dependentVariables and @dependentVariables.size>0
- raise "no indenpendent variables" unless @independentVariables
raise "no predicted variables" unless @predictedVariables and @predictedVariables.size>0
rescue => ex
RestClientWrapper.raise_uri_error "invalid model: '"+ex.message+"'\n"+self.to_yaml+"\n",@uri.to_s
end
+ LOGGER.warn "model has no dependent variable" unless @dependentVariables and @dependentVariables.size>0
+ LOGGER.warn "model has no algorithm" unless @algorithm and @algorithm.size>0
+ LOGGER.warn "model has no indenpendent variables" unless @independentVariables
end
- end
- end
+ end
+ end
- class PredictionModel < Generic
+ class PredictionModel < Generic
+
+ def self.build( algorithm_uri, algorithm_params )
+
+ LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s
+ uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s
+ LOGGER.debug "Build model done: "+uri.to_s
+ RestClientWrapper.raise_uri_error("Invalid build model result: '"+uri.to_s+"'", algorithm_uri, algorithm_params ) unless Utils.model_uri?(uri)
+ return PredictionModel.find(uri)
+ end
- def self.build( algorithm_uri, algorithm_params )
-
- LOGGER.debug "Build model, algorithm_uri:"+algorithm_uri.to_s+", algorithm_parms: "+algorithm_params.inspect.to_s
- uri = OpenTox::RestClientWrapper.post(algorithm_uri,algorithm_params).to_s
- LOGGER.debug "Build model done: "+uri.to_s
- RestClientWrapper.raise_uri_error("Invalid build model result: '"+uri.to_s+"'", algorithm_uri, algorithm_params ) unless Utils.model_uri?(uri)
- return PredictionModel.find(uri)
- end
-
- def predict_dataset( dataset_uri )
-
- LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s
- uri = RestClientWrapper.post(@uri, {:accept => "text/uri-list", :dataset_uri=>dataset_uri})
- RestClientWrapper.raise_uri_error("Prediciton result no dataset uri: "+uri.to_s, @uri, {:dataset_uri=>dataset_uri} ) unless Utils.dataset_uri?(uri)
- uri
- end
-
- def classification?
- #HACK replace with request to ontology server
- if @title =~ /(?i)classification/
- return true
- elsif @title =~ /(?i)regression/
- return false
- elsif @uri =~/ntua/ and @title =~ /mlr/
- return false
- elsif @uri =~/tu-muenchen/ and @title =~ /regression|M5P|GaussP/
- return false
- elsif @uri =~/ambit2/ and @title =~ /pKa/ || @title =~ /Regression|Caco/
- return false
- elsif @uri =~/majority/
- return (@uri =~ /class/) != nil
- else
- raise "unknown model, uri:'"+@uri.to_s+"' title:'"+@title.to_s+"'"
- end
- end
- end
-
+ def predict_dataset( dataset_uri )
+
+ LOGGER.debug "Predict dataset: "+dataset_uri.to_s+" with model "+@uri.to_s
+ uri = RestClientWrapper.post(@uri, {:accept => "text/uri-list", :dataset_uri=>dataset_uri})
+ RestClientWrapper.raise_uri_error("Prediciton result no dataset uri: "+uri.to_s, @uri, {:dataset_uri=>dataset_uri} ) unless Utils.dataset_uri?(uri)
+ uri
+ end
+
+ def classification?
+ #HACK replace with request to ontology server
+ if @title =~ /(?i)classification/
+ return true
+ elsif @title =~ /(?i)regression/
+ return false
+ elsif @uri =~/ntua/ and @title =~ /mlr/
+ return false
+ elsif @uri =~/tu-muenchen/ and @title =~ /regression|M5P|GaussP/
+ return false
+ elsif @uri =~/ambit2/ and @title =~ /pKa/ || @title =~ /Regression|Caco/
+ return false
+ elsif @uri =~/majority/
+ return (@uri =~ /class/) != nil
+ else
+ raise "unknown model, uri:'"+@uri.to_s+"' title:'"+@title.to_s+"'"
+ end
+ end
+ end
- class Lazar < Generic
+ class Lazar < Generic
attr_accessor :feature_dataset_uri, :effects, :activities, :p_values, :fingerprints, :features
- def initialize
- @source = "http://github.com/helma/opentox-model"
- @algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
- #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative")
- @features = []
- @effects = {}
- @activities = {}
- @p_values = {}
- @fingerprints = {}
- end
+ def initialize
+ @source = "http://github.com/helma/opentox-model"
+ @algorithm = File.join(@@config[:services]["opentox-algorithm"],"lazar")
+ #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative")
+ @features = []
+ @effects = {}
+ @activities = {}
+ @p_values = {}
+ @fingerprints = {}
+ end
+
+ def save
+ @features.uniq!
+ resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
+ resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
+ end
+
+ def self.find_all
+ RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n")
+ end
+
+ def self.predict(compound_uri,model_uri)
+ #RestClientWrapper.post(model_uri,{:compound_uri => compound_uri, :accept => 'application/x-yaml'})
+ `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}`
+ end
+ end
+
+ class PropertyLazar < Generic
+
+ attr_accessor :feature_dataset_uri, :properties, :features, :activities#, :effects, :p_values
+
+ def initialize
+ @source = "http://github.com/helma/opentox-model"
+ @algorithm = File.join(@@config[:services]["opentox-algorithm"],"property_lazar")
+ #@independent_variables = File.join(@@config[:services]["opentox-algorithm"],"fminer#BBRC_representative")
+ @features = []
+ #@effects = {}
+ @activities = {}
+ #@p_values = {}
+ @properties = {}
+ end
- def save
- @features.uniq!
- resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
- resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
- end
+ def save
+ @features.uniq!
+ resource = RestClient::Resource.new(@@config[:services]["opentox-model"], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
+ resource.post(self.to_yaml, :content_type => "application/x-yaml").chomp.to_s
+ end
- def self.find_all
- RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n")
- end
+ def self.find_all
+ RestClientWrapper.get(@@config[:services]["opentox-model"]).chomp.split("\n")
+ end
def self.predict(compound_uri,model_uri)
#RestClientWrapper.post(model_uri,{:compound_uri => compound_uri, :accept => 'application/x-yaml'})
- `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}`
+ `curl -X POST -d 'compound_uri=#{compound_uri}' -H 'Accept:application/x-yaml' #{model_uri}`
end
- end
- end
+ end
+ end
end
diff --git a/lib/owl.rb b/lib/owl.rb
index a4c2e68..dcf26a5 100644
--- a/lib/owl.rb
+++ b/lib/owl.rb
@@ -518,6 +518,11 @@ module OpenTox
value_node = value_nodes[0]
compound_uri = get_value( @model.object(value_node, node('compound')) )
+ unless compound_uri
+ LOGGER.warn "'compound' missing for data-entry of feature "+feature_uri.to_s+
+ ", value: "+@model.object(feature_value_node,node("value")).to_s
+ next
+ end
value_node_type = @model.object(feature_value_node, RDF_TYPE)
if (value_node_type == node('FeatureValue'))
diff --git a/lib/task.rb b/lib/task.rb
index 971478f..1ab3893 100644
--- a/lib/task.rb
+++ b/lib/task.rb
@@ -2,7 +2,7 @@ $self_task=nil
module OpenTox
- class Task
+ class Task
# due_to_time is only set in local tasks
TASK_ATTRIBS = [ :uri, :date, :title, :creator, :description, :hasStatus, :percentageCompleted, :resultURI, :due_to_time ]
@@ -15,13 +15,13 @@ module OpenTox
end
# create is private now, use OpenTox::Task.as_task
- def self.create(max_duration)
- task_uri = RestClientWrapper.post(@@config[:services]["opentox-task"], {:max_duration => max_duration}, nil, false).to_s
- Task.find(task_uri.chomp)
- end
-
+ def self.create( params )
+ task_uri = RestClientWrapper.post(@@config[:services]["opentox-task"], params, nil, false).to_s
+ Task.find(task_uri.chomp)
+ end
+
public
- def self.find( uri, accept_header='application/rdf+xml' )
+ def self.find( uri, accept_header=nil )
task = Task.new(uri)
task.reload( accept_header )
return task
@@ -34,7 +34,14 @@ module OpenTox
return task
end
- def reload( accept_header='application/rdf+xml' )
+ def reload( accept_header=nil )
+ unless accept_header
+ if (@@config[:yaml_hosts].include?(URI.parse(uri).host))
+ accept_header = "application/x-yaml"
+ else
+ accept_header = 'application/rdf+xml'
+ end
+ end
result = RestClientWrapper.get(uri, {:accept => accept_header}, false)#'application/x-yaml'})
@http_code = result.code
reload_from_data(result, result.content_type, uri)
@@ -58,20 +65,20 @@ module OpenTox
raise "uri is null after loading" unless @uri and @uri.to_s.strip.size>0
end
- def cancel
+ def cancel
RestClientWrapper.put(File.join(@uri,'Cancelled'))
reload
- end
+ end
- def completed(uri)
- RestClientWrapper.put(File.join(@uri,'Completed'),{:resultURI => uri})
+ def completed(uri)
+ RestClientWrapper.put(File.join(@uri,'Completed'),{:resultURI => uri})
reload
- end
+ end
- def error(description)
+ def error(description)
RestClientWrapper.put(File.join(@uri,'Error'),{:description => description.to_s[0..2000]})
reload
- end
+ end
def pid=(pid)
RestClientWrapper.put(File.join(@uri,'pid'), {:pid => pid})
@@ -81,16 +88,16 @@ module OpenTox
@hasStatus.to_s == 'Running'
end
- def completed?
- @hasStatus.to_s == 'Completed'
- end
+ def completed?
+ @hasStatus.to_s == 'Completed'
+ end
- def error?
- @hasStatus.to_s == 'Error'
- end
+ def error?
+ @hasStatus.to_s == 'Error'
+ end
# waits for a task, unless time exceeds or state is no longer running
- def wait_for_completion(dur=0.3)
+ def wait_for_completion(dur=0.3)
if (@uri.match(@@config[:services]["opentox-task"]))
due_to_time = (@due_to_time.is_a?(Time) ? @due_to_time : Time.parse(@due_to_time))
@@ -102,17 +109,17 @@ module OpenTox
end
LOGGER.debug "start waiting for task "+@uri.to_s+" at: "+Time.new.to_s+", waiting at least until "+due_to_time.to_s
- while self.running?
- sleep dur
+ while self.running?
+ sleep dur
reload
check_state
if (Time.new > due_to_time)
raise "max wait time exceeded ("+running_time.to_s+"sec), task: '"+@uri.to_s+"'"
end
- end
+ end
LOGGER.debug "Task '"+@hasStatus+"': "+@uri.to_s+", Result: "+@resultURI.to_s
- end
+ end
def check_state
begin
@@ -133,10 +140,11 @@ module OpenTox
# returns the task uri
# catches halts and exceptions, task state is set to error then
- def self.as_task(max_duration=DEFAULT_TASK_MAX_DURATION)
+ def self.as_task( title, creator, max_duration=DEFAULT_TASK_MAX_DURATION, description=nil )
#return yield nil
- task = OpenTox::Task.create(max_duration)
+ params = {:title=>title, :creator=>creator, :max_duration=>max_duration, :description=>description }
+ task = OpenTox::Task.create(params)
task_pid = Spork.spork(:logger => LOGGER) do
LOGGER.debug "Task #{task.uri} started #{Time.now}"
$self_task = task
@@ -163,6 +171,6 @@ module OpenTox
LOGGER.debug "Started task: "+task.uri.to_s
task.uri
end
- end
+ end
end
diff --git a/lib/validation.rb b/lib/validation.rb
index bd38488..89a2a0c 100644
--- a/lib/validation.rb
+++ b/lib/validation.rb
@@ -4,16 +4,7 @@ module OpenTox
attr_accessor :uri
def initialize(params)
- #resource = RestClient::Resource.new(params[:uri], :user => @@users[:users].keys[0], :password => @@users[:users].values[0])
- #@uri = resource.post(params).body
- #LOGGER.debug "VALIDATION URI: " + @uri.to_s
- call = "curl -X POST "
- params.each do |k,v|
- call += " -d "+k.to_s+"=\""+URI.encode(v.to_s)+"\"" unless k == :uri
- end
- call += " "+params[:uri]
- LOGGER.debug call
- @uri = `#{call}`
+ @uri = OpenTox::RestClientWrapper.post(File.join(@@config[:services]["opentox-validation"],"/crossvalidation"),params,nil,false)
end
def self.crossvalidation(params)