summaryrefslogtreecommitdiff
path: root/fs.rb
blob: 3ac2c02af4ad6fb4e02b762f660a29a4fea250a9 (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
# fs.rb
# Feature Selection
# Author: Andreas Maunz


# Get list of feature selection algorithms
# @return [text/uri-list] URIs of feature selection algorithms
get '/fs/?' do
  list = [ url_for('/feature_selection/rfe', :full) ].join("\n") + "\n"
  case request.env['HTTP_ACCEPT']
  when /text\/html/
    content_type "text/html"
    OpenTox.text_to_html list
  else
    content_type 'text/uri-list'
    list
  end
end

# Get representation of recursive feature elimination algorithm
# @return [application/rdf+xml] OWL-DL representation of recursive feature elimination algorithm
get "/fs/rfe/?" do
  algorithm = OpenTox::Algorithm::Generic.new(url_for('/feature_selection/rfe',:full))
  algorithm.metadata = {
    DC.title => 'recursive feature elimination',
    DC.creator => "andreas@maunz.de, helma@in-silico.ch",
    DC.contributor => "vorgrimmlerdavid@gmx.de",
    BO.instanceOf => "http://opentox.org/ontology/ist-algorithms.owl#feature_selection_rfe",
    RDF.type => [OT.Algorithm,OTA.PatternMiningSupervised],
    OT.parameters => [
      { DC.description => "Dataset URI", OT.paramScope => "mandatory", DC.title => "dataset_uri" },
      { DC.description => "Prediction Feature URI", OT.paramScope => "mandatory", DC.title => "prediction_feature" },
      { DC.description => "Feature Dataset URI", OT.paramScope => "mandatory", DC.title => "feature_dataset_uri" },
      { DC.description => "Delete Instances with missing values", OT.paramScope => "optional", DC.title => "del_missing" }
  ]
  }
  case request.env['HTTP_ACCEPT']
  when /text\/html/
    content_type "text/html"
    OpenTox.text_to_html algorithm.to_yaml
  when /yaml/
    content_type "application/x-yaml"
    algorithm.to_yaml
  else
    response['Content-Type'] = 'application/rdf+xml'  
    algorithm.to_rdfxml
  end
end

# Run rfe algorithm on dataset
# @param [String] dataset_uri URI of the training dataset
# @param [String] prediction_feature URI
# @param [String] feature_dataset_uri URI
# @return [text/uri-list] Task URI
post '/fs/rfe/?' do 

  raise OpenTox::NotFoundError.new "Please submit a dataset_uri." unless params[:dataset_uri]
  raise OpenTox::NotFoundError.new "Please submit a prediction_feature." unless params[:prediction_feature]
  raise OpenTox::NotFoundError.new "Please submit a feature_dataset_uri." unless params[:feature_dataset_uri]

  ds_csv=OpenTox::RestClientWrapper.get( params[:dataset_uri], {:accept => "text/csv"} )
  ds=Tempfile.open(['rfe_', '.csv'])
  ds.puts(ds_csv)
  ds.flush()

  prediction_feature = params[:prediction_feature].split('/').last # get col name
  
  fds_features = OpenTox::Dataset.new(params[:feature_dataset_uri]).load_features
  fds_csv=OpenTox::RestClientWrapper.get( params[:feature_dataset_uri], {:accept => "text/csv"})
  fds=Tempfile.open(['rfe_', '.csv'])
  fds.puts(fds_csv)
  fds.flush()

  del_missing = params[:del_missing] == "true" ? true : false

  task = OpenTox::Task.create("Recursive Feature Elimination", url_for('/feature_selection',:full)) do |task|
    r_result_file = OpenTox::Algorithm::FeatureSelection.rfe( { :ds_csv_file => ds.path, :prediction_feature => prediction_feature, :fds_csv_file => fds.path, :del_missing => del_missing } )
    
    
    # # # Upload dataset
    ds = OpenTox::Dataset.find ( 
      OpenTox::RestClientWrapper.post(
        File.join(CONFIG[:services]["opentox-dataset"]), File.open(r_result_file).read, {:content_type => "text/csv"}
      )
    ) 
    ds.features.each { |id,info| # rewrite features
      fds_features.each { |fid,finfo|
        if ( (fid.split('/').last == id.split('/').last) && (finfo[DC.title] == info[DC.title]) )
          ds.features[id] = finfo
          break
        end
      }
    }
    r_result_uri = ds.save
    begin
      ds.close!; fds.close! 
      File.unlink(r_result_file)
    rescue
    end
    r_result_uri
  end
  response['Content-Type'] = 'text/uri-list'
  raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled"
  halt 202,task.uri.to_s+"\n"
end