comparison nn_classifier.xml @ 0:d638aa11a4f0 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tools/sklearn commit 0e582cf1f3134c777cce3aa57d71b80ed95e6ba9
author bgruening
date Fri, 16 Feb 2018 09:17:36 -0500
parents
children 478034e9826b
comparison
equal deleted inserted replaced
-1:000000000000 0:d638aa11a4f0
1 <tool id="nn_classifier" name="Nearest Neighbors Classification" version="@VERSION@">
2 <description></description>
3 <macros>
4 <import>main_macros.xml</import>
5 </macros>
6 <expand macro="python_requirements"/>
7 <expand macro="macro_stdio"/>
8 <version_command>echo "@VERSION@"</version_command>
9 <command><![CDATA[
10 python "$nnc_script" '$inputs'
11 ]]>
12 </command>
13 <configfiles>
14 <inputs name="inputs"/>
15 <configfile name="nnc_script">
16 <![CDATA[
17 import sys
18 import json
19 import numpy as np
20 import sklearn.neighbors
21 import pandas
22 import pickle
23
24 input_json_path = sys.argv[1]
25 params = json.load(open(input_json_path, "r"))
26
27
28 #if $selected_tasks.selected_task == "load":
29
30 classifier_object = pickle.load(open("$infile_model", 'r'))
31
32 data = pandas.read_csv("$selected_tasks.infile_data", sep='\t', header=0, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
33 prediction = classifier_object.predict(data)
34 prediction_df = pandas.DataFrame(prediction)
35 res = pandas.concat([data, prediction_df], axis=1)
36 res.to_csv(path_or_buf = "$outfile_predict", sep="\t", index=False)
37
38 #else:
39
40 data_train = pandas.read_csv("$selected_tasks.infile_train", sep='\t', header=0, index_col=None, parse_dates=True, encoding=None, tupleize_cols=False )
41
42 data = data_train.ix[:,0:len(data_train.columns)-1]
43 labels = np.array(data_train[data_train.columns[len(data_train.columns)-1]])
44
45 selected_algorithm = params["selected_tasks"]["selected_algorithms"]["selected_algorithm"]
46
47 if selected_algorithm == "nneighbors":
48 classifier = params["selected_tasks"]["selected_algorithms"]["sampling_methods"]["sampling_method"]
49 sys.stdout.write(classifier)
50 options = params["selected_tasks"]["selected_algorithms"]["sampling_methods"]["options"]
51 sys.stdout.write(str(options))
52 elif selected_algorithm == "ncentroid":
53 options = params["selected_tasks"]["selected_algorithms"]["options"]
54 classifier = "NearestCentroid"
55
56 my_class = getattr(sklearn.neighbors, classifier)
57 classifier_object = my_class(**options)
58 classifier_object.fit(data,labels)
59
60 pickle.dump(classifier_object,open("$outfile_fit", 'w+'))
61
62 #end if
63
64 ]]>
65 </configfile>
66 </configfiles>
67 <inputs>
68 <expand macro="train_loadConditional" model="zip"><!--Todo: add sparse to targets-->
69 <param name="selected_algorithm" type="select" label="Classifier type">
70 <option value="nneighbors">Nearest Neighbors</option>
71 <option value="ncentroid">Nearest Centroid</option>
72 </param>
73 <when value="nneighbors">
74 <conditional name="sampling_methods">
75 <param name="sampling_method" type="select" label="Neighbor selection method">
76 <option value="KNeighborsClassifier" selected="true">K-nearest neighbors</option>
77 <option value="RadiusNeighborsClassifier">Radius-based</option>
78 </param>
79 <when value="KNeighborsClassifier">
80 <expand macro="nn_advanced_options">
81 <param argument="n_neighbors" type="integer" optional="true" value="5" label="Number of neighbors" help=" "/>
82 </expand>
83 </when>
84 <when value="RadiusNeighborsClassifier">
85 <expand macro="nn_advanced_options">
86 <param argument="radius" type="float" optional="true" value="1.0" label="Radius"
87 help="Range of parameter space to use by default for :meth ''radius_neighbors'' queries."/>
88 </expand>
89 </when>
90 </conditional>
91 </when>
92 <when value="ncentroid">
93 <section name="options" title="Advanced Options" expanded="False">
94 <param argument="metric" type="text" optional="true" value="euclidean" label="Metric"
95 help="The metric to use when calculating distance between instances in a feature array."/>
96 <param argument="shrink_threshold" type="float" optional="true" value="" label="Shrink threshold"
97 help="Floating point number for shrinking centroids to remove features."/>
98 </section>
99 </when>
100 </expand>
101 </inputs>
102
103 <expand macro="output"/>
104
105 <tests>
106 <test>
107 <param name="infile_train" value="train_set.tabular" ftype="tabular"/>
108 <param name="selected_task" value="train"/>
109 <param name="selected_algorithm" value="nneighbors"/>
110 <param name="sampling_method" value="KNeighborsClassifier" />
111 <param name="algorithm" value="brute" />
112 <output name="outfile_fit" file="nn_model01.txt"/>
113 </test>
114 <test>
115 <param name="infile_train" value="train_set.tabular" ftype="tabular"/>
116 <param name="selected_task" value="train"/>
117 <param name="selected_algorithm" value=""/>
118 <param name="selected_algorithm" value="nneighbors"/>
119 <param name="sampling_method" value="RadiusNeighborsClassifier" />
120 <output name="outfile_fit" file="nn_model02.txt"/>
121 </test>
122 <test>
123 <param name="infile_train" value="train_set.tabular" ftype="tabular"/>
124 <param name="selected_task" value="train"/>
125 <param name="selected_algorithm" value="ncentroid"/>
126 <output name="outfile_fit" file="nn_model03.txt"/>
127 </test>
128 <test>
129 <param name="infile_model" value="nn_model01.txt" ftype="txt"/>
130 <param name="infile_data" value="test_set.tabular" ftype="tabular"/>
131 <param name="selected_task" value="load"/>
132 <output name="outfile_predict" file="nn_prediction_result01.tabular"/>
133 </test>
134 <test>
135 <param name="infile_model" value="nn_model02.txt" ftype="txt"/>
136 <param name="infile_data" value="test_set.tabular" ftype="tabular"/>
137 <param name="selected_task" value="load"/>
138 <output name="outfile_predict" file="nn_prediction_result02.tabular"/>
139 </test>
140 <test>
141 <param name="infile_model" value="nn_model03.txt" ftype="txt"/>
142 <param name="infile_data" value="test_set.tabular" ftype="tabular"/>
143 <param name="selected_task" value="load"/>
144 <output name="outfile_predict" file="nn_prediction_result03.tabular"/>
145 </test>
146 </tests>
147 <help><![CDATA[
148 **What it does**
149 This module implements the k-nearest neighbors classification algorithms.
150 For more information check http://scikit-learn.org/stable/modules/neighbors.html
151 ]]></help>
152 <expand macro="sklearn_citation"/>
153 </tool>