summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@ot-dev.in-silico.ch>2011-02-25 17:51:37 +0000
committerroot <root@ot-dev.in-silico.ch>2011-02-25 17:51:37 +0000
commit18801dca5ff1458384491b5bdf5c15271f0681d5 (patch)
tree111dbec907290e65fc88abb53d067e8f0f7e7372
parentaad966c8dfd7ccdcf93c5698a8155b00a91c653f (diff)
ohm/redis backend
-rw-r--r--application.rb135
-rw-r--r--helper.rb2
-rw-r--r--model.rb119
-rw-r--r--views/classification_validation.haml8
-rw-r--r--views/model.haml4
5 files changed, 159 insertions, 109 deletions
diff --git a/application.rb b/application.rb
index 88763d9..13248e6 100644
--- a/application.rb
+++ b/application.rb
@@ -19,11 +19,8 @@ set :lock, true
helpers do
def error(message)
- @model.error_messages = message
LOGGER.error message
- @model.status = "Error"
- @model.save
- #@dataset.delete
+ @model.update :status => "Error", :error_messages => message
flash[:notice] = message
redirect url_for('/create')
end
@@ -60,13 +57,9 @@ helpers do
end
before do
- #unless env['REQUEST_METHOD'] == "GET" or ( env['REQUEST_URI'] =~ /\/login$/ and env['REQUEST_METHOD'] == "POST" ) or !AA_SERVER
if !logged_in and !( env['REQUEST_URI'] =~ /\/login$/ and env['REQUEST_METHOD'] == "POST" ) #or !AA_SERVER
login("guest","guest")
- #flash[:notice] = "You have to login first to do this."
- #redirect url_for('/login')
end
- #end
end
get '/?' do
@@ -78,7 +71,7 @@ get '/login' do
end
get '/models/?' do
- @models = ToxCreateModel.all(:order => [ :created_at.desc ])
+ @models = ToxCreateModel.all#(:order => [ :created_at.desc ])
subjectstring = session[:subjectid] ? "?subjectid=#{CGI.escape(session[:subjectid])}" : ""
#@models.each { |model| model.process }
haml :models, :locals=>{:models=>@models, :subjectstring => subjectstring}
@@ -135,8 +128,8 @@ put '/model/:id/?' do
model = ToxCreateModel.get(params[:id])
begin
if params[:name] && model.name != params[:name]
- model.name = params[:name]
- model.save
+ model.update :name => params[:name]
+ #model.save
end
redirect url_for("/model/#{model.id}/name?mode=show")
rescue
@@ -166,7 +159,7 @@ get '/model/:id/:view/?' do
end
get '/predict/?' do
- @models = ToxCreateModel.all(:order => [ :created_at.desc ])
+ @models = ToxCreateModel.all#(:order => [ :created_at.desc ])
@models = @models.collect{|m| m if m.status == 'Completed'}.compact
haml :predict
end
@@ -204,9 +197,10 @@ post '/models' do # create a new model
subjectid = session[:subjectid] ? session[:subjectid] : nil
@model = ToxCreateModel.create(:name => params[:file][:filename].sub(/\..*$/,""), :subjectid => subjectid)
@model.update :web_uri => url_for("/model/#{@model.id}", :full), :warnings => ""
- @model.save
+ #@model.save
task = OpenTox::Task.create("Uploading dataset and creating lazar model",url_for("/models",:full)) do |task|
+ task.progress(5)
@model.update :status => "Uploading and saving dataset"
begin
@dataset = OpenTox::Dataset.create(nil, subjectid)
@@ -214,7 +208,7 @@ post '/models' do # create a new model
case File.extname(params[:file][:filename])
when ".csv"
csv = params[:file][:tempfile].read
- LOGGER.debug csv
+ #LOGGER.debug csv
@dataset.load_csv(csv, subjectid)
when ".xls", ".xlsx"
@dataset.load_spreadsheet(Excel.new params[:file][:tempfile].path, subjectid)
@@ -235,12 +229,14 @@ post '/models' do # create a new model
if @dataset.metadata[OT.Errors]
error "Incorrect file format. Please follow the instructions for #{link_to "Excel", "/excel_format"} or #{link_to "CSV", "/csv_format"} formats."
end
- @model.training_dataset = @dataset.uri
- @model.nr_compounds = @dataset.compounds.size
- @model.warnings = @dataset.metadata[OT.Warnings] unless @dataset.metadata[OT.Warnings].empty?
- @model.save
-
- @model.update :status => "Creating prediction model"
+ @model.update :training_dataset => @dataset.uri, :nr_compounds => @dataset.compounds.size, :status => "Creating prediction model"
+ @model.update :warnings => @dataset.metadata[OT.Warnings] unless @dataset.metadata[OT.Warnings].empty?
+ #@model.training_dataset = @dataset.uri
+ #@model.nr_compounds = @dataset.compounds.size
+ #@model.warnings = @dataset.metadata[OT.Warnings] unless @dataset.metadata[OT.Warnings].empty?
+ #@model.save
+
+ #@model.update :status => "Creating prediction model"
task.progress(15)
begin
lazar = OpenTox::Model::Lazar.create(:dataset_uri => @dataset.uri, :subjectid => subjectid)
@@ -248,17 +244,16 @@ post '/models' do # create a new model
error "Model creation failed with '#{e.message}'. Please check if the input file is in a valid #{link_to "Excel", "/excel_format"} or #{link_to "CSV", "/csv_format"} format."
end
task.progress(25)
- @model.feature_dataset = lazar.metadata[OT.featureDataset]
- @model.uri = lazar.uri
+ #@model.feature_dataset = lazar.metadata[OT.featureDataset]
+ #@model.uri = lazar.uri
+ type = "unknown"
case lazar.metadata[OT.isA]
when /Classification/
- @model.type = "classification"
+ type = "classification"
when /Regression/
- @model.type = "regression"
- else
- @model.type = "unknown"
+ type = "regression"
end
- @model.save
+ @model.update :type => type, :feature_dataset => lazar.metadata[OT.featureDataset], :uri => lazar.uri
unless url_for("",:full).match(/localhost/)
@model.update :status => "Validating model"
@@ -272,37 +267,43 @@ post '/models' do # create a new model
nil, OpenTox::SubTask.new(task,25,80))
@model.update(:validation_uri => validation.uri)
LOGGER.debug "Validation URI: #{@model.validation_uri}"
+
+ # create summary
+ validation.summary(subjectid).each do |k,v|
+ #LOGGER.debug "mr ::: k: #{k.inspect} - v: #{v.inspect}"
+ begin
+ eval "@model.update :#{k.to_s} => v" if v
+ rescue
+ eval "@model.update :#{k.to_s} => 0"
+ end
+ end
+
+ @model.update :status => "Creating validation report"
+ #begin
+ validation_report_uri = validation.find_or_create_report(subjectid, OpenTox::SubTask.new(task,80,90)) #unless @model.dirty?
+ @model.update :validation_report_uri => validation_report_uri, :status => "Creating QMRF report"
+ #rescue => e
+ #LOGGER.debug "Validation report generation failed with #{e.message}."
+ #@model.update :warnings => @model.warnings + "\nValidation report generation failed with #{e.message}."
+ #end
+ #@model.update :status => "Creating QMRF report"
+ #begin
+ #@model.update(:validation_qmrf_report_uri => validation.create_qmrf_report(subjectid)) unless @model.dirty?
+ qmrf_report = OpenTox::Crossvalidation::QMRFReport.create(@model.uri, subjectid, OpenTox::SubTask.new(task,90,99))
+ @model.update(:validation_qmrf_uri => qmrf_report.uri, :status => "Completed")
+ #rescue => e
+ #LOGGER.debug "Validation QMRF report generation failed with #{e.message}."
+ #@model.update :warnings => @model.warnings + "\nValidation QMRF report generation failed with #{e.message}."
+ #end
+ #@model.update :status => "Completed"
+
rescue => e
LOGGER.debug "Model validation failed with #{e.message}."
- @model.warnings += "Model validation failed with #{e.message}."
- end
- # create summary
- validation.summary(subjectid).each do |k,v|
- LOGGER.debug "mr ::: k: #{k.inspect} - v: #{v.inspect}"
- begin
- eval "@model.#{k.to_s} = v" if v
- rescue
- eval "@model.#{k.to_s} = 0"
- end
+ @model.save # to avoid dirty models
+ @model.update :warnings => @model.warnings + "\nModel validation failed with #{e.message}.", :status => "Error", :error_messages => e.message
end
- @model.save
+ #@model.save
- @model.update :status => "Creating validation report"
- begin
- @model.update(:validation_report_uri => validation.find_or_create_report(subjectid, OpenTox::SubTask.new(task,80,90))) unless @model.dirty?
- rescue => e
- LOGGER.debug "Validation report generation failed with #{e.message}."
- @model.warnings += "Validation report generation failed with #{e.message}."
- end
- @model.update :status => "Creating QMRF report"
- begin
- #@model.update(:validation_qmrf_report_uri => validation.create_qmrf_report(subjectid)) unless @model.dirty?
- qmrf_report = OpenTox::Crossvalidation::QMRFReport.create(@model.uri, subjectid, OpenTox::SubTask.new(task,90,99))
- @model.update(:validation_qmrf_uri => qmrf_report.uri)
- rescue => e
- LOGGER.debug "Validation QMRF report generation failed with #{e.message}."
- @model.warnings += "Validation QMRF report generation failed with #{e.message}."
- end
end
@@ -311,11 +312,10 @@ post '/models' do # create a new model
#duplicate_warnings = ''
#parser.duplicates.each {|inchi,lines| duplicate_warnings += "<p>#{lines.join('<br/>')}</p>" if lines.size > 1 }
#@model.warnings += "<p>Duplicated structures (all structures/activities used for model building, please make sure, that the results were obtained from <em>independent</em> experiments):</p>" + duplicate_warnings unless duplicate_warnings.empty?
- @model.update :status => "Completed"
lazar.uri
end
@model.update(:task_uri => task.uri)
- @model.save
+ #@model.save
flash[:notice] = "Model creation and validation started - this may last up to several hours depending on the number and size of the training compounds."
redirect url_for('/models')
@@ -356,7 +356,7 @@ post '/predict/?' do # post chemical name to model
else
predicted_feature = prediction_dataset.metadata[OT.dependentVariables]
prediction = OpenTox::Feature.find(predicted_feature)
- LOGGER.debug prediction.to_yaml
+ #LOGGER.debug prediction.to_yaml
if prediction.metadata[OT.error]
@predictions << {
:title => model.name,
@@ -403,7 +403,7 @@ post '/predict/?' do # post chemical name to model
end
=end
end
- LOGGER.debug @predictions.inspect
+ #LOGGER.debug @predictions.inspect
haml :prediction
end
@@ -491,7 +491,24 @@ delete '/model/:id/?' do
end
delete '/?' do
- DataMapper.auto_migrate!
+ #DataMapper.auto_migrate!
+ ToxCreateModel.all.each do |model|
+# begin
+# delete_model(model, @subjectid)
+ model.delete
+# unless ToxCreateModel.get(params[:id])
+# begin
+# aa = OpenTox::Authorization.delete_policies_from_uri(model.web_uri, @subjectid)
+# LOGGER.debug "Policy deleted for Dataset URI: #{uri} with result: #{aa}"
+# rescue
+# LOGGER.warn "Policy delete error for Dataset URI: #{uri}"
+# end
+# end
+# LOGGER.debug "#{model.name} model deleted."
+# rescue
+# LOGGER.error "#{model.name} model delete error."
+# end
+ end
response['Content-Type'] = 'text/plain'
"All Models deleted."
end
diff --git a/helper.rb b/helper.rb
index 3fbd06f..d2c5593 100644
--- a/helper.rb
+++ b/helper.rb
@@ -53,7 +53,7 @@ helpers do
features = {:activating => [], :deactivating => []}
descriptors.each { |d| LOGGER.debug d.inspect; features[d[OT.effect].to_sym] << {:smarts => d[OT.smarts],:p_value => d[OT.pValue]} }
- LOGGER.debug features.to_yaml
+ #LOGGER.debug features.to_yaml
features
end
diff --git a/model.rb b/model.rb
index eb459b3..7d22a4d 100644
--- a/model.rb
+++ b/model.rb
@@ -1,51 +1,84 @@
-class ToxCreateModel
-
- include DataMapper::Resource
-
- property :id, Serial
- property :name, String, :length => 255
- property :warnings, Text, :length => 2**32-1
- property :error_messages, Text, :length => 2**32-1 # :errors interferes with datamapper validation
- property :type, String
- property :status, String, :length => 255
- property :created_at, DateTime
-
- property :task_uri, String, :length => 255
- property :uri, String, :length => 255
-
- property :training_dataset, String, :length => 255
- property :feature_dataset, String, :length => 255
- #property :validation_task_uri, String, :length => 255
- property :validation_uri, String, :length => 255
-
- #property :validation_report_task_uri, String, :length => 255
- property :validation_report_uri, String, :length => 255
-
- #property :validation_qmrf_task_uri, String, :length => 255
- property :validation_qmrf_uri, String, :length => 255
-
- property :nr_compounds, Integer
- property :nr_predictions, Integer
- property :true_positives, Integer
- property :false_positives, Integer
- property :true_negatives, Integer
- property :false_negatives, Integer
- property :correct_predictions, Integer
- property :weighted_area_under_roc, Float
- property :sensitivity, Float
- property :specificity, Float
- property :r_square, Float
- property :root_mean_squared_error, Float
- property :mean_absolute_error, Float
-
- property :web_uri, String, :length => 255
+require 'ohm'
+#require 'redis/objects'
+#@@redis = Redis.new(:thread_safe=>true)#(:host => '127.0.0.1', :port => 6379)
+
+class ToxCreateModel < Ohm::Model
+
+ #include Redis::Objects
+ #include DataMapper::Resource
+ #attribute :id
+ attribute :name
+ attribute :warnings
+ attribute :error_messages
+ attribute :type
+ attribute :status
+ attribute :created_at
+
+ attribute :task_uri
+ attribute :uri
+
+ attribute :training_dataset
+ attribute :feature_dataset
+ #attributey :validation_task_uri
+ attribute :validation_uri
+
+ #attributey :validation_report_task_uri
+ attribute :validation_report_uri
+
+ #attributey :validation_qmrf_task_uri
+ attribute :validation_qmrf_uri
+
+ attribute :nr_compounds
+ attribute :nr_predictions
+ attribute :true_positives
+ attribute :false_positives
+ attribute :true_negatives
+ attribute :false_negatives
+ attribute :correct_predictions
+ attribute :weighted_area_under_roc
+ attribute :sensitivity
+ attribute :specificity
+ attribute :r_square
+ attribute :root_mean_squared_error
+ attribute :mean_absolute_error
+
+ attribute :web_uri
attr_accessor :subjectid
@subjectid = nil
- after :save, :check_policy
+ #after :save, :check_policy
=begin
+ attr_accessor :id, :name, :warnings, :error_messages, :type, :status, :created_at, :task_uri, :uri, :training_dataset, :feature_dataset, :validation_task_uri, :validation_uri, :validation_report_task_uri, :validation_report_uri, :validation_qmrf_task_uri, :validation_qmrf_uri, :nr_compounds, :nr_predictions, :true_positives, :false_positives, :true_negatives, :false_negatives, :correct_predictions, :weighted_area_under_roc, :sensitivity, :specificity, :r_square, :root_mean_squared_error, :mean_absolute_error, :web_uri
+
+ def self.all
+ end
+
+ def self.get(id)
+ end
+
+ def self.create(params)
+ @id = @@redis.incr "toxcreate"
+ params.each { |k,v| @@redis.hset "toxcreate:#{@id}", k, v }
+ self.get(@id)
+ end
+
+ def update(params)
+ end
+
+ def method_missing
+ begin
+ rescue
+ raise "Unknown method"
+ end
+ end
+
+ attr_accessor :subjectid
+ @subjectid = nil
+
+ after :save, :check_policy
+
def status
#begin
RestClient.get(File.join(@task_uri, 'hasStatus')).body
@@ -195,4 +228,4 @@ def status
end
-DataMapper.auto_upgrade!
+#DataMapper.auto_upgrade!
diff --git a/views/classification_validation.haml b/views/classification_validation.haml
index 94ba8f9..41362ba 100644
--- a/views/classification_validation.haml
+++ b/views/classification_validation.haml
@@ -1,17 +1,17 @@
%dt Correct predictions:
%dd
- = sprintf("%.2f", model.correct_predictions) if model.correct_predictions
+ = sprintf("%.2f", model.correct_predictions.to_f) if model.correct_predictions
= '%'
%dt
%a{:href => "http://en.wikipedia.org/wiki/Receiver_operating_characteristic", :target => "_blank"} Weighted area under ROC:
%dd
- = sprintf("%.3f", model.weighted_area_under_roc) if model.weighted_area_under_roc
+ = sprintf("%.3f", model.weighted_area_under_roc.to_f) if model.weighted_area_under_roc
%dt
%a{:href => "http://en.wikipedia.org/wiki/Sensitivity_and_specificity", :target => "_blank"} Specificity:
-%dd= sprintf("%.3f", model.specificity) if model.specificity
+%dd= sprintf("%.3f", model.specificity.to_f) if model.specificity
%dt
%a{:href => "http://en.wikipedia.org/wiki/Sensitivity_and_specificity", :target => "_blank"} Sensitivity:
-%dd= sprintf("%.3f", model.sensitivity) if model.sensitivity
+%dd= sprintf("%.3f", model.sensitivity.to_f) if model.sensitivity
%dt
%a{:href => "http://en.wikipedia.org/wiki/Confusion_matrix", :target => "_blank"} Confusion Matrix:
%dd
diff --git a/views/model.haml b/views/model.haml
index 8112dd6..e1bdd99 100644
--- a/views/model.haml
+++ b/views/model.haml
@@ -35,8 +35,8 @@
%span
%br
- %dt Started:
- %dd= model.created_at.strftime("%m/%d/%Y - %I:%M:%S%p")
+ -#%dt Started:
+ -#%dd= model.created_at.strftime("%m/%d/%Y - %I:%M:%S%p")
- if model.nr_compounds
%dt Training compounds:
%dd= model.nr_compounds