From e8188766495356ef156c957316ab2fea368dd271 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Thu, 19 Aug 2010 11:21:39 +0200 Subject: as_task: title and creator required, fix create_new_dataset from owl dataset --- lib/dataset.rb | 4 ++-- lib/task.rb | 66 ++++++++++++++++++++++++++++++++-------------------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/lib/dataset.rb b/lib/dataset.rb index 0be804c..ef5b97f 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 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 -- cgit v1.2.3 From 8634a68f2eb77a9e73917bb2097d97eb207a544c Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Fri, 20 Aug 2010 10:14:47 +0200 Subject: Version bump to 1.6.4 --- VERSION | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VERSION b/VERSION index 266146b..9edc58b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.6.3 +1.6.4 -- cgit v1.2.3 From b06b7c7d3312e6b913ee9e6c35d0cfe93137756c Mon Sep 17 00:00:00 2001 From: mguetlein Date: Mon, 23 Aug 2010 15:49:04 +0200 Subject: owl-fix for nil-compounds --- lib/model.rb | 6 +++--- lib/owl.rb | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/model.rb b/lib/model.rb index e8f6048..fb25126 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -28,13 +28,13 @@ 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 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')) -- cgit v1.2.3 From 7da48f4e8ab43e5fbc5379eae133dd9aa929ac55 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Mon, 23 Aug 2010 16:12:57 +0200 Subject: lib/dataset.rb --- lib/dataset.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/dataset.rb b/lib/dataset.rb index ef5b97f..2eb2206 100644 --- a/lib/dataset.rb +++ b/lib/dataset.rb @@ -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 -- cgit v1.2.3 From df3ae4a9b84b12cb668e717d5f1c30891d5a2562 Mon Sep 17 00:00:00 2001 From: mguetlein Date: Tue, 24 Aug 2010 13:58:49 +0200 Subject: add dm-validations to dependencies --- Rakefile | 3 ++- lib/environment.rb | 2 +- 2 files changed, 3 insertions(+), 2 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/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") -- cgit v1.2.3 From 7cbb2402a4d01abcbfd3850650f9687b46804723 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 24 Aug 2010 17:52:33 +0200 Subject: initial lazar version for quantitative descriptors --- lib/algorithm.rb | 12 +++ lib/model.rb | 162 +++++++++++++++++++++++---------------- lib/validation.rb | 11 +-- opentox-ruby-api-wrapper.gemspec | 4 +- 4 files changed, 111 insertions(+), 78 deletions(-) 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/model.rb b/lib/model.rb index e8f6048..1cc0825 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 @@ -36,78 +36,108 @@ module OpenTox RestClientWrapper.raise_uri_error "invalid model: '"+ex.message+"'\n"+self.to_yaml+"\n",@uri.to_s end 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/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) diff --git a/opentox-ruby-api-wrapper.gemspec b/opentox-ruby-api-wrapper.gemspec index 4240e51..1a487b0 100644 --- a/opentox-ruby-api-wrapper.gemspec +++ b/opentox-ruby-api-wrapper.gemspec @@ -5,11 +5,11 @@ Gem::Specification.new do |s| s.name = %q{opentox-ruby-api-wrapper} - s.version = "1.6.3" + s.version = "1.6.4" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Christoph Helma, Martin Guetlein"] - s.date = %q{2010-08-18} + s.date = %q{2010-08-20} s.description = %q{Ruby wrapper for the OpenTox REST API (http://www.opentox.org)} s.email = %q{helma@in-silico.ch} s.executables = ["opentox-install-ubuntu.sh", "yaml2owl.rb", "opentox-install-debian.sh"] -- cgit v1.2.3