Java Main function
[algorithm] / fs.rb
1 # fs.rb
2 # Feature Selection
3 # Author: Andreas Maunz
4
5
6 # Get list of feature selection algorithms
7 # @return [text/uri-list] URIs of feature selection algorithms
8 get '/fs/?' do
9   list = [ url_for('/feature_selection/rfe', :full) ].join("\n") + "\n"
10   case request.env['HTTP_ACCEPT']
11   when /text\/html/
12     content_type "text/html"
13     OpenTox.text_to_html list
14   else
15     content_type 'text/uri-list'
16     list
17   end
18 end
19
20 # Get representation of recursive feature elimination algorithm
21 # @return [application/rdf+xml] OWL-DL representation of recursive feature elimination algorithm
22 get "/fs/rfe/?" do
23   algorithm = OpenTox::Algorithm::Generic.new(url_for('/feature_selection/rfe',:full))
24   algorithm.metadata = {
25     DC.title => 'recursive feature elimination',
26     DC.creator => "andreas@maunz.de, helma@in-silico.ch",
27     DC.contributor => "vorgrimmlerdavid@gmx.de",
28     BO.instanceOf => "http://opentox.org/ontology/ist-algorithms.owl#feature_selection_rfe",
29     RDF.type => [OT.Algorithm,OTA.PatternMiningSupervised],
30     OT.parameters => [
31       { DC.description => "Dataset URI", OT.paramScope => "mandatory", DC.title => "dataset_uri" },
32       { DC.description => "Prediction Feature URI", OT.paramScope => "mandatory", DC.title => "prediction_feature" },
33       { DC.description => "Feature Dataset URI", OT.paramScope => "mandatory", DC.title => "feature_dataset_uri" },
34       { DC.description => "Delete Instances with missing values", OT.paramScope => "optional", DC.title => "del_missing" }
35   ]
36   }
37   case request.env['HTTP_ACCEPT']
38   when /text\/html/
39     content_type "text/html"
40     OpenTox.text_to_html algorithm.to_yaml
41   when /yaml/
42     content_type "application/x-yaml"
43     algorithm.to_yaml
44   else
45     response['Content-Type'] = 'application/rdf+xml'  
46     algorithm.to_rdfxml
47   end
48 end
49
50 # Run rfe algorithm on dataset
51 # @param [String] dataset_uri URI of the training dataset
52 # @param [String] prediction_feature URI
53 # @param [String] feature_dataset_uri URI
54 # @return [text/uri-list] Task URI
55 post '/fs/rfe/?' do 
56
57   raise OpenTox::NotFoundError.new "Please submit a dataset_uri." unless params[:dataset_uri]
58   raise OpenTox::NotFoundError.new "Please submit a prediction_feature." unless params[:prediction_feature]
59   raise OpenTox::NotFoundError.new "Please submit a feature_dataset_uri." unless params[:feature_dataset_uri]
60
61   ds_csv=OpenTox::RestClientWrapper.get( params[:dataset_uri], {:accept => "text/csv"} )
62   ds=Tempfile.open(['rfe_', '.csv'])
63   ds.puts(ds_csv)
64   ds.flush()
65
66   prediction_feature = params[:prediction_feature].split('/').last # get col name
67   
68   fds_features = OpenTox::Dataset.new(params[:feature_dataset_uri]).load_features
69   fds_csv=OpenTox::RestClientWrapper.get( params[:feature_dataset_uri], {:accept => "text/csv"})
70   fds=Tempfile.open(['rfe_', '.csv'])
71   fds.puts(fds_csv)
72   fds.flush()
73
74   del_missing = params[:del_missing] == "true" ? true : false
75
76   task = OpenTox::Task.create("Recursive Feature Elimination", url_for('/feature_selection',:full)) do |task|
77     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 } )
78     
79     
80     # # # Upload dataset
81     ds = OpenTox::Dataset.find ( 
82       OpenTox::RestClientWrapper.post(
83         File.join(CONFIG[:services]["opentox-dataset"]), File.open(r_result_file).read, {:content_type => "text/csv"}
84       )
85     ) 
86     ds.features.each { |id,info| # rewrite features
87       fds_features.each { |fid,finfo|
88         if ( (fid.split('/').last == id.split('/').last) && (finfo[DC.title] == info[DC.title]) )
89           ds.features[id] = finfo
90           break
91         end
92       }
93     }
94     r_result_uri = ds.save
95     begin
96       ds.close!; fds.close! 
97       File.unlink(r_result_file)
98     rescue
99     end
100     r_result_uri
101   end
102   response['Content-Type'] = 'text/uri-list'
103   raise OpenTox::ServiceUnavailableError.newtask.uri+"\n" if task.status == "Cancelled"
104   halt 202,task.uri.to_s+"\n"
105 end
106