changeset 13:78c664cc1841 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 5d71c93a3dd804b1469852240a86021ab9130364
author bgruening
date Mon, 09 Jul 2018 14:31:08 -0400
parents ada7bb28fe13
children 973ffe8fe4f1
files main_macros.xml svm.xml
diffstat 2 files changed, 71 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/main_macros.xml	Sun Jul 01 03:18:53 2018 -0400
+++ b/main_macros.xml	Mon Jul 09 14:31:08 2018 -0400
@@ -64,6 +64,45 @@
   return new_selector
   </token>
 
+  <token name="@GET_X_y_FUNCTION@">
+def get_X_y(params, file1, file2):
+  input_type = params["selected_tasks"]["selected_algorithms"]["input_options"]["selected_input"]
+  if input_type=="tabular":
+    header = 'infer' if params["selected_tasks"]["selected_algorithms"]["input_options"]["header1"] else None
+    column_option = params["selected_tasks"]["selected_algorithms"]["input_options"]["column_selector_options_1"]["selected_column_selector_option"]
+    if column_option in ["by_index_number", "all_but_by_index_number", "by_header_name", "all_but_by_header_name"]:
+      c = params["selected_tasks"]["selected_algorithms"]["input_options"]["column_selector_options_1"]["col1"]
+    else:
+      c = None
+    X = read_columns(
+      file1,
+      c = c,
+      c_option = column_option,
+      sep='\t',
+      header=header,
+      parse_dates=True
+    )
+  else:
+    X = mmread(open(file1, 'r'))
+
+  header = 'infer' if params["selected_tasks"]["selected_algorithms"]["input_options"]["header2"] else None
+  column_option = params["selected_tasks"]["selected_algorithms"]["input_options"]["column_selector_options_2"]["selected_column_selector_option2"]
+  if column_option in ["by_index_number", "all_but_by_index_number", "by_header_name", "all_but_by_header_name"]:
+    c = params["selected_tasks"]["selected_algorithms"]["input_options"]["column_selector_options_2"]["col2"]
+  else:
+    c = None
+  y = read_columns(
+    file2,
+    c = c,
+    c_option = column_option,
+    sep='\t',
+    header=header,
+    parse_dates=True
+  )
+  y=y.ravel()
+  return X, y
+  </token>
+
   <xml name="python_requirements">
       <requirements>
           <requirement type="package" version="2.7">python</requirement>
@@ -81,34 +120,6 @@
 
 
   <!--Generic interface-->
-  <xml name="train_loadConditional" token_train="tabular" token_data="tabular" token_model="txt">
-    <conditional name="selected_tasks">
-        <param name="selected_task" type="select" label="Select a Classification Task">
-            <option value="train" selected="true">Train a model</option>
-            <option value="load">Load a model and predict</option>
-        </param>
-        <when value="load">
-            <param name="infile_model" type="data" format="@MODEL@" label="Models" help="Select a model file."/>
-            <param name="infile_data" type="data" format="@DATA@" label="Data (tabular)" help="Select the dataset you want to classify."/>
-            <conditional name="prediction_options">
-                <param name="prediction_option" type="select" label="Select the type of prediction">
-                    <option value="predict">Predict class labels</option>
-                    <option value="advanced">Include advanced options</option>
-                </param>
-                <when value="predict">
-                </when>
-                <when value="advanced">
-                </when>
-            </conditional>
-        </when>
-        <when value="train">
-            <param name="infile_train" type="data" format="@TRAIN@" label="Training samples (tabular)"/>
-            <conditional name="selected_algorithms">
-                <yield />
-            </conditional>
-        </when>
-    </conditional>
-  </xml>
 
   <xml name="sl_Conditional" token_train="tabular" token_data="tabular" token_model="txt">
     <conditional name="selected_tasks">
--- a/svm.xml	Sun Jul 01 03:18:53 2018 -0400
+++ b/svm.xml	Mon Jul 09 14:31:08 2018 -0400
@@ -22,6 +22,9 @@
 import pandas
 import pickle
 
+@COLUMNS_FUNCTION@
+@GET_X_y_FUNCTION@
+
 input_json_path = sys.argv[1]
 params = json.load(open(input_json_path, "r"))
 
@@ -29,7 +32,8 @@
 
 classifier_object = pickle.load(open("$infile_model", 'rb'))
 
-data = pandas.read_csv("$selected_tasks.infile_data", sep='\t', header=0, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
+header = 'infer' if params["selected_tasks"]["header"] else None
+data = pandas.read_csv("$selected_tasks.infile_data", sep='\t', header=header, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False)
 prediction = classifier_object.predict(data)
 prediction_df = pandas.DataFrame(prediction)
 res = pandas.concat([data, prediction_df], axis=1)
@@ -37,10 +41,7 @@
 
 #else:
 
-data_train = pandas.read_csv("$selected_tasks.infile_train", sep='\t', header=0, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
-
-data = data_train.ix[:,0:len(data_train.columns)-1]
-labels = np.array(data_train[data_train.columns[len(data_train.columns)-1]])
+X, y = get_X_y(params, "$selected_tasks.selected_algorithms.input_options.infile1" ,"$selected_tasks.selected_algorithms.input_options.infile2")
 
 options = params["selected_tasks"]["selected_algorithms"]["options"]
 selected_algorithm = params["selected_tasks"]["selected_algorithms"]["selected_algorithm"]
@@ -51,7 +52,7 @@
 
 my_class = getattr(sklearn.svm, selected_algorithm)
 classifier_object = my_class(**options)
-classifier_object.fit(data,labels)
+classifier_object.fit(X, y)
 
 pickle.dump(classifier_object,open("$outfile_fit", 'w+'))
 
@@ -61,23 +62,26 @@
         </configfile>
     </configfiles>
     <inputs>
-        <expand macro="train_loadConditional" model="zip">
+        <expand macro="sl_Conditional" model="zip">
             <param name="selected_algorithm" type="select" label="Classifier type">
                 <option value="SVC">C-Support Vector Classification</option>
                 <option value="NuSVC">Nu-Support Vector Classification</option>
                 <option value="LinearSVC">Linear Support Vector Classification</option>
             </param>
             <when value="SVC">
+                <expand macro="sl_mixed_input"/>
                 <expand macro="svc_advanced_options">
                     <expand macro="C"/>
                 </expand>
             </when>
             <when value="NuSVC">
+                <expand macro="sl_mixed_input"/>
                 <expand macro="svc_advanced_options">
                     <param argument="nu" type="float" optional="true" value="0.5" label="Nu control parameter" help="Controls the number of support vectors. Should be in the interval (0, 1]. "/>
                 </expand>
             </when>
             <when value="LinearSVC">
+                <expand macro="sl_mixed_input"/>
                 <section name="options" title="Advanced Options" expanded="False">
                     <expand macro="C"/>
                     <expand macro="tol" default_value="0.001" help_text="Tolerance for stopping criterion. "/>
@@ -108,21 +112,36 @@
 
     <tests>
         <test>
-            <param name="infile_train" value="train_set.tabular" ftype="tabular"/>
+            <param name="infile1" value="train_set.tabular" ftype="tabular"/>
+            <param name="infile2" value="train_set.tabular" ftype="tabular"/>
+            <param name="header1" value="True"/>
+            <param name="header2" value="True"/>
+            <param name="col1" value="1,2,3,4"/>
+            <param name="col2" value="5"/>
             <param name="selected_task" value="train"/>
             <param name="selected_algorithm" value="SVC"/>
             <param name="random_state" value="5"/>
             <output name="outfile_fit" file="svc_model01.txt"/>
         </test>
         <test>
-            <param name="infile_train" value="train_set.tabular" ftype="tabular"/>
+            <param name="infile1" value="train_set.tabular" ftype="tabular"/>
+            <param name="infile2" value="train_set.tabular" ftype="tabular"/>
+            <param name="header1" value="True"/>
+            <param name="header2" value="True"/>
+            <param name="col1" value="1,2,3,4"/>
+            <param name="col2" value="5"/>
             <param name="selected_task" value="train"/>
             <param name="selected_algorithm" value="NuSVC"/>
             <param name="random_state" value="5"/>
             <output name="outfile_fit" file="svc_model02.txt"/>
         </test>
         <test>
-            <param name="infile_train" value="train_set.tabular" ftype="tabular"/>
+            <param name="infile1" value="train_set.tabular" ftype="tabular"/>
+            <param name="infile2" value="train_set.tabular" ftype="tabular"/>
+            <param name="header1" value="True"/>
+            <param name="header2" value="True"/>
+            <param name="col1" value="1,2,3,4"/>
+            <param name="col2" value="5"/>
             <param name="selected_task" value="train"/>
             <param name="selected_algorithm" value="LinearSVC"/>
             <param name="random_state" value="5"/>
@@ -131,18 +150,21 @@
         <test>
             <param name="infile_model" value="svc_model01.txt" ftype="txt"/>
             <param name="infile_data" value="test_set.tabular" ftype="tabular"/>
+            <param name="header" value="True"/>
             <param name="selected_task" value="load"/>
             <output name="outfile_predict" file="svc_prediction_result01.tabular"/>
         </test>
         <test>
             <param name="infile_model" value="svc_model02.txt" ftype="txt"/>
             <param name="infile_data" value="test_set.tabular" ftype="tabular"/>
+            <param name="header" value="True"/>
             <param name="selected_task" value="load"/>
             <output name="outfile_predict" file="svc_prediction_result02.tabular"/>
         </test>
         <test>
             <param name="infile_model" value="svc_model03.txt" ftype="txt"/>
             <param name="infile_data" value="test_set.tabular" ftype="tabular"/>
+            <param name="header" value="True"/>
             <param name="selected_task" value="load"/>
             <output name="outfile_predict" file="svc_prediction_result03.tabular"/>
         </test>