annotate fitted_model_eval.py @ 2:e36ab18cbaca draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 208a8d348e7c7a182cfbe1b6f17868146428a7e2"
author bgruening
date Tue, 13 Apr 2021 22:26:07 +0000
parents 132805688fa3
children 24c1cc2dd4a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
1 import argparse
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
2 import json
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
3 import warnings
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
4
2
e36ab18cbaca "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 208a8d348e7c7a182cfbe1b6f17868146428a7e2"
bgruening
parents: 1
diff changeset
5 import pandas as pd
e36ab18cbaca "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 208a8d348e7c7a182cfbe1b6f17868146428a7e2"
bgruening
parents: 1
diff changeset
6 from galaxy_ml.utils import get_scoring, load_model, read_columns
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
7 from scipy.io import mmread
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
8 from sklearn.metrics.scorer import _check_multimetric_scoring
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
9 from sklearn.model_selection._validation import _score
2
e36ab18cbaca "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 208a8d348e7c7a182cfbe1b6f17868146428a7e2"
bgruening
parents: 1
diff changeset
10 from sklearn.pipeline import Pipeline
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
11
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
12
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
13 def _get_X_y(params, infile1, infile2):
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
14 """read from inputs and output X and y
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
15
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
16 Parameters
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
17 ----------
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
18 params : dict
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
19 Tool inputs parameter
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
20 infile1 : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
21 File path to dataset containing features
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
22 infile2 : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
23 File path to dataset containing target values
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
24
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
25 """
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
26 # store read dataframe object
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
27 loaded_df = {}
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
28
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
29 input_type = params["input_options"]["selected_input"]
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
30 # tabular input
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
31 if input_type == "tabular":
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
32 header = "infer" if params["input_options"]["header1"] else None
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
33 column_option = params["input_options"]["column_selector_options_1"]["selected_column_selector_option"]
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
34 if column_option in [
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
35 "by_index_number",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
36 "all_but_by_index_number",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
37 "by_header_name",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
38 "all_but_by_header_name",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
39 ]:
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
40 c = params["input_options"]["column_selector_options_1"]["col1"]
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
41 else:
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
42 c = None
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
43
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
44 df_key = infile1 + repr(header)
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
45 df = pd.read_csv(infile1, sep="\t", header=header, parse_dates=True)
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
46 loaded_df[df_key] = df
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
47
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
48 X = read_columns(df, c=c, c_option=column_option).astype(float)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
49 # sparse input
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
50 elif input_type == "sparse":
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
51 X = mmread(open(infile1, "r"))
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
52
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
53 # Get target y
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
54 header = "infer" if params["input_options"]["header2"] else None
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
55 column_option = params["input_options"]["column_selector_options_2"]["selected_column_selector_option2"]
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
56 if column_option in [
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
57 "by_index_number",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
58 "all_but_by_index_number",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
59 "by_header_name",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
60 "all_but_by_header_name",
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
61 ]:
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
62 c = params["input_options"]["column_selector_options_2"]["col2"]
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
63 else:
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
64 c = None
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
65
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
66 df_key = infile2 + repr(header)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
67 if df_key in loaded_df:
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
68 infile2 = loaded_df[df_key]
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
69 else:
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
70 infile2 = pd.read_csv(infile2, sep="\t", header=header, parse_dates=True)
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
71 loaded_df[df_key] = infile2
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
72
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
73 y = read_columns(infile2, c=c, c_option=column_option, sep="\t", header=header, parse_dates=True)
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
74 if len(y.shape) == 2 and y.shape[1] == 1:
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
75 y = y.ravel()
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
76
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
77 return X, y
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
78
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
79
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
80 def main(
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
81 inputs,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
82 infile_estimator,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
83 outfile_eval,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
84 infile_weights=None,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
85 infile1=None,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
86 infile2=None,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
87 ):
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
88 """
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
89 Parameter
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
90 ---------
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
91 inputs : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
92 File path to galaxy tool parameter
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
93
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
94 infile_estimator : strgit
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
95 File path to trained estimator input
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
96
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
97 outfile_eval : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
98 File path to save the evalulation results, tabular
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
99
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
100 infile_weights : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
101 File path to weights input
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
102
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
103 infile1 : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
104 File path to dataset containing features
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
105
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
106 infile2 : str
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
107 File path to dataset containing target values
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
108 """
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
109 warnings.filterwarnings("ignore")
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
110
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
111 with open(inputs, "r") as param_handler:
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
112 params = json.load(param_handler)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
113
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
114 X_test, y_test = _get_X_y(params, infile1, infile2)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
115
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
116 # load model
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
117 with open(infile_estimator, "rb") as est_handler:
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
118 estimator = load_model(est_handler)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
119
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
120 main_est = estimator
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
121 if isinstance(estimator, Pipeline):
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
122 main_est = estimator.steps[-1][-1]
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
123 if hasattr(main_est, "config") and hasattr(main_est, "load_weights"):
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
124 if not infile_weights or infile_weights == "None":
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
125 raise ValueError(
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
126 "The selected model skeleton asks for weights, " "but no dataset for weights was provided!"
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
127 )
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
128 main_est.load_weights(infile_weights)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
129
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
130 # handle scorer, convert to scorer dict
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
131 # Check if scoring is specified
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
132 scoring = params["scoring"]
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
133 if scoring is not None:
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
134 # get_scoring() expects secondary_scoring to be a comma separated string (not a list)
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
135 # Check if secondary_scoring is specified
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
136 secondary_scoring = scoring.get("secondary_scoring", None)
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
137 if secondary_scoring is not None:
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
138 # If secondary_scoring is specified, convert the list into comman separated string
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
139 scoring["secondary_scoring"] = ",".join(scoring["secondary_scoring"])
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
140
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
141 scorer = get_scoring(scoring)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
142 scorer, _ = _check_multimetric_scoring(estimator, scoring=scorer)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
143
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
144 if hasattr(estimator, "evaluate"):
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
145 scores = estimator.evaluate(X_test, y_test=y_test, scorer=scorer, is_multimetric=True)
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
146 else:
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
147 scores = _score(estimator, X_test, y_test, scorer, is_multimetric=True)
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
148
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
149 # handle output
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
150 for name, score in scores.items():
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
151 scores[name] = [score]
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
152 df = pd.DataFrame(scores)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
153 df = df[sorted(df.columns)]
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
154 df.to_csv(path_or_buf=outfile_eval, sep="\t", header=True, index=False)
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
155
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
156
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
157 if __name__ == "__main__":
0
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
158 aparser = argparse.ArgumentParser()
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
159 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
160 aparser.add_argument("-e", "--infile_estimator", dest="infile_estimator")
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
161 aparser.add_argument("-w", "--infile_weights", dest="infile_weights")
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
162 aparser.add_argument("-X", "--infile1", dest="infile1")
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
163 aparser.add_argument("-y", "--infile2", dest="infile2")
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
164 aparser.add_argument("-O", "--outfile_eval", dest="outfile_eval")
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
165 args = aparser.parse_args()
2d7016b3ae92 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2afb24f3c81d625312186750a714d702363012b5"
bgruening
parents:
diff changeset
166
1
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
167 main(
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
168 args.inputs,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
169 args.infile_estimator,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
170 args.outfile_eval,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
171 infile_weights=args.infile_weights,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
172 infile1=args.infile1,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
173 infile2=args.infile2,
132805688fa3 "planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit e2a5eade6d0e5ddf3a47630381a0ad90d80e8a04"
bgruening
parents: 0
diff changeset
174 )