diff options
Diffstat (limited to 'lib/model.rb')
-rw-r--r-- | lib/model.rb | 59 |
1 files changed, 48 insertions, 11 deletions
diff --git a/lib/model.rb b/lib/model.rb index 6ef4af2..85be1b5 100644 --- a/lib/model.rb +++ b/lib/model.rb @@ -6,26 +6,54 @@ module OpenTox # Run a model with parameters # @param [Hash] params Parameters for OpenTox model + # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly # @return [text/uri-list] Task or resource URI - def run(params) + def run( params, waiting_task=nil ) if CONFIG[:yaml_hosts].include?(URI.parse(@uri).host) accept = 'application/x-yaml' else accept = 'application/rdf+xml' end - begin - RestClientWrapper.post(@uri,{:accept => accept},params).to_s - rescue => e - LOGGER.error "Failed to run #{@uri} with #{params.inspect} (#{e.inspect})" - raise "Failed to run #{@uri} with #{params.inspect}" - end + RestClientWrapper.post(@uri,{:accept => accept},params,waiting_task).to_s end # Generic OpenTox model class for all API compliant services class Generic include Model + + # Find Generic Opentox Model via URI, and loads metadata + # @param [String] uri Model URI + # @return [OpenTox::Model::Generic] Model instance, nil if model was not found + def self.find(uri) + model = Generic.new(uri) + model.load_metadata + if model.metadata==nil or model.metadata.size==0 + nil + else + model + end + end + + # provides feature type, possible types are "regression" or "classification" + # @return [String] feature type, "unknown" if type could not be estimated + def feature_type + # dynamically perform restcalls if necessary + load_metadata if @metadata==nil or @metadata.size==0 or (@metadata.size==1 && @metadata.values[0]==@uri) + @dependentVariable = OpenTox::Feature.find( @metadata[OT.dependentVariables] ) unless @dependentVariable + + [@dependentVariable.feature_type, @metadata[OT.isA], @metadata[DC.title], @uri].each do |type| + case type + when /(?i)classification/ + return "classification" + when /(?i)regression/ + return "regression" + end + end + raise "unknown model "+[@dependentVariable.feature_type, @metadata[OT.isA], @metadata[DC.title], @uri].inspect + end + end - + # Lazy Structure Activity Relationship class class Lazar @@ -89,8 +117,10 @@ module OpenTox # Predict a dataset # @param [String] dataset_uri Dataset URI + # @param [optional,subjectid] + # @param [optional,OpenTox::Task] waiting_task (can be a OpenTox::Subtask as well), progress is updated accordingly # @return [OpenTox::Dataset] Dataset with predictions - def predict_dataset(dataset_uri, subjectid=nil) + def predict_dataset(dataset_uri, subjectid=nil, waiting_task=nil) @prediction_dataset = Dataset.create(CONFIG[:services]["opentox-dataset"], subjectid) @prediction_dataset.add_metadata({ OT.hasSource => @uri, @@ -99,9 +129,16 @@ module OpenTox OT.parameters => [{DC.title => "dataset_uri", OT.paramValue => dataset_uri}] }) d = Dataset.new(dataset_uri,subjectid) - d.load_compounds(subjectid) + d.load_compounds + count = 0 d.compounds.each do |compound_uri| - predict(compound_uri,false,subjectid) + begin + predict(compound_uri,false,subjectid) + count += 1 + waiting_task.progress( count/d.compounds.size.to_f*100.0 ) if waiting_task + rescue => ex + LOGGER.warn "prediction for compound "+compound_uri.to_s+" failed: "+ex.message + end end @prediction_dataset.save(subjectid) @prediction_dataset |