Mercurial > repos > bgruening > sklearn_model_validation
annotate utils.py @ 18:492d34a75de6 draft
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 49522db5f2dc8a571af49e3f38e80c22571068f4
author | bgruening |
---|---|
date | Tue, 09 Jul 2019 19:39:58 -0400 |
parents | cf9aa11b91c8 |
children |
rev | line source |
---|---|
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
1 import ast |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
2 import json |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
3 import imblearn |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
4 import numpy as np |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
5 import pandas |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
6 import pickle |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
7 import re |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
8 import scipy |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
9 import sklearn |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
10 import skrebate |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
11 import sys |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
12 import warnings |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
13 import xgboost |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
14 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
15 from collections import Counter |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
16 from asteval import Interpreter, make_symbol_table |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
17 from imblearn import under_sampling, over_sampling, combine |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
18 from imblearn.pipeline import Pipeline as imbPipeline |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
19 from mlxtend import regressor, classifier |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
20 from scipy.io import mmread |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
21 from sklearn import ( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
22 cluster, compose, decomposition, ensemble, feature_extraction, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
23 feature_selection, gaussian_process, kernel_approximation, metrics, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
24 model_selection, naive_bayes, neighbors, pipeline, preprocessing, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
25 svm, linear_model, tree, discriminant_analysis) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
26 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
27 try: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
28 import iraps_classifier |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
29 except ImportError: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
30 pass |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
31 |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
32 try: |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
33 import model_validations |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
34 except ImportError: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
35 pass |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
36 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
37 try: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
38 import feature_selectors |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
39 except ImportError: |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
40 pass |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
41 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
42 try: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
43 import preprocessors |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
44 except ImportError: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
45 pass |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
46 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
47 # handle pickle white list file |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
48 WL_FILE = __import__('os').path.join( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
49 __import__('os').path.dirname(__file__), 'pk_whitelist.json') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
50 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
51 N_JOBS = int(__import__('os').environ.get('GALAXY_SLOTS', 1)) |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
52 |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
53 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
54 class _SafePickler(pickle.Unpickler, object): |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
55 """ |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
56 Used to safely deserialize scikit-learn model objects |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
57 Usage: |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
58 eg.: _SafePickler.load(pickled_file_object) |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
59 """ |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
60 def __init__(self, file): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
61 super(_SafePickler, self).__init__(file) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
62 # load global white list |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
63 with open(WL_FILE, 'r') as f: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
64 self.pk_whitelist = json.load(f) |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
65 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
66 self.bad_names = ( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
67 'and', 'as', 'assert', 'break', 'class', 'continue', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
68 'def', 'del', 'elif', 'else', 'except', 'exec', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
69 'finally', 'for', 'from', 'global', 'if', 'import', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
70 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
71 'raise', 'return', 'try', 'system', 'while', 'with', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
72 'True', 'False', 'None', 'eval', 'execfile', '__import__', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
73 '__package__', '__subclasses__', '__bases__', '__globals__', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
74 '__code__', '__closure__', '__func__', '__self__', '__module__', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
75 '__dict__', '__class__', '__call__', '__get__', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
76 '__getattribute__', '__subclasshook__', '__new__', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
77 '__init__', 'func_globals', 'func_code', 'func_closure', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
78 'im_class', 'im_func', 'im_self', 'gi_code', 'gi_frame', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
79 '__asteval__', 'f_locals', '__mro__') |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
80 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
81 # unclassified good globals |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
82 self.good_names = [ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
83 'copy_reg._reconstructor', '__builtin__.object', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
84 '__builtin__.bytearray', 'builtins.object', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
85 'builtins.bytearray', 'keras.engine.sequential.Sequential', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
86 'keras.engine.sequential.Model'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
87 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
88 # custom module in Galaxy-ML |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
89 self.custom_modules = [ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
90 '__main__', 'keras_galaxy_models', 'feature_selectors', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
91 'preprocessors', 'iraps_classifier', 'model_validations'] |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
92 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
93 # override |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
94 def find_class(self, module, name): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
95 # balack list first |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
96 if name in self.bad_names: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
97 raise pickle.UnpicklingError("global '%s.%s' is forbidden" |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
98 % (module, name)) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
99 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
100 # custom module in Galaxy-ML |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
101 if module in self.custom_modules: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
102 cutom_module = sys.modules.get(module, None) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
103 if cutom_module: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
104 return getattr(cutom_module, name) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
105 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
106 raise pickle.UnpicklingError("Module %s' is not imported" |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
107 % module) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
108 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
109 # For objects from outside libraries, it's necessary to verify |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
110 # both module and name. Currently only a blacklist checker |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
111 # is working. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
112 # TODO: replace with a whitelist checker. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
113 good_names = self.good_names |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
114 pk_whitelist = self.pk_whitelist |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
115 if re.match(r'^[a-zA-Z_][a-zA-Z0-9_]*$', name): |
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
116 fullname = module + '.' + name |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
117 if (fullname in good_names)\ |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
118 or (module.startswith(('sklearn.', 'xgboost.', 'skrebate.', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
119 'imblearn.', 'mlxtend.', 'numpy.')) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
120 or module == 'numpy'): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
121 if fullname not in (pk_whitelist['SK_NAMES'] + |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
122 pk_whitelist['SKR_NAMES'] + |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
123 pk_whitelist['XGB_NAMES'] + |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
124 pk_whitelist['NUMPY_NAMES'] + |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
125 pk_whitelist['IMBLEARN_NAMES'] + |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
126 pk_whitelist['MLXTEND_NAMES'] + |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
127 good_names): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
128 # raise pickle.UnpicklingError |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
129 print("Warning: global %s is not in pickler whitelist " |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
130 "yet and will loss support soon. Contact tool " |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
131 "author or leave a message at github.com" % fullname) |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
132 mod = sys.modules[module] |
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
133 return getattr(mod, name) |
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
134 |
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
135 raise pickle.UnpicklingError("global '%s' is forbidden" % fullname) |
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
136 |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
137 |
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
138 def load_model(file): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
139 """Load pickled object with `_SafePicker` |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
140 """ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
141 return _SafePickler(file).load() |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
142 |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
143 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
144 def read_columns(f, c=None, c_option='by_index_number', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
145 return_df=False, **args): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
146 """Return array from a tabular dataset by various columns selection |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
147 """ |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
148 data = pandas.read_csv(f, **args) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
149 if c_option == 'by_index_number': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
150 cols = list(map(lambda x: x - 1, c)) |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
151 data = data.iloc[:, cols] |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
152 if c_option == 'all_but_by_index_number': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
153 cols = list(map(lambda x: x - 1, c)) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
154 data.drop(data.columns[cols], axis=1, inplace=True) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
155 if c_option == 'by_header_name': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
156 cols = [e.strip() for e in c.split(',')] |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
157 data = data[cols] |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
158 if c_option == 'all_but_by_header_name': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
159 cols = [e.strip() for e in c.split(',')] |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
160 data.drop(cols, axis=1, inplace=True) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
161 y = data.values |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
162 if return_df: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
163 return y, data |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
164 else: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
165 return y |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
166 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
167 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
168 def feature_selector(inputs, X=None, y=None): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
169 """generate an instance of sklearn.feature_selection classes |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
170 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
171 Parameters |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
172 ---------- |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
173 inputs : dict |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
174 From galaxy tool parameters. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
175 X : array |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
176 Containing training features. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
177 y : array or list |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
178 Target values. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
179 """ |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
180 selector = inputs['selected_algorithm'] |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
181 if selector != 'DyRFECV': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
182 selector = getattr(sklearn.feature_selection, selector) |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
183 options = inputs['options'] |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
184 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
185 if inputs['selected_algorithm'] == 'SelectFromModel': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
186 if not options['threshold'] or options['threshold'] == 'None': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
187 options['threshold'] = None |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
188 else: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
189 try: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
190 options['threshold'] = float(options['threshold']) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
191 except ValueError: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
192 pass |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
193 if inputs['model_inputter']['input_mode'] == 'prefitted': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
194 model_file = inputs['model_inputter']['fitted_estimator'] |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
195 with open(model_file, 'rb') as model_handler: |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
196 fitted_estimator = load_model(model_handler) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
197 new_selector = selector(fitted_estimator, prefit=True, **options) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
198 else: |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
199 estimator_json = inputs['model_inputter']['estimator_selector'] |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
200 estimator = get_estimator(estimator_json) |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
201 check_feature_importances = try_get_attr( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
202 'feature_selectors', 'check_feature_importances') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
203 estimator = check_feature_importances(estimator) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
204 new_selector = selector(estimator, **options) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
205 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
206 elif inputs['selected_algorithm'] == 'RFE': |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
207 step = options.get('step', None) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
208 if step and step >= 1.0: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
209 options['step'] = int(step) |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
210 estimator = get_estimator(inputs["estimator_selector"]) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
211 check_feature_importances = try_get_attr( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
212 'feature_selectors', 'check_feature_importances') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
213 estimator = check_feature_importances(estimator) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
214 new_selector = selector(estimator, **options) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
215 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
216 elif inputs['selected_algorithm'] == 'RFECV': |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
217 options['scoring'] = get_scoring(options['scoring']) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
218 options['n_jobs'] = N_JOBS |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
219 splitter, groups = get_cv(options.pop('cv_selector')) |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
220 if groups is None: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
221 options['cv'] = splitter |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
222 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
223 options['cv'] = list(splitter.split(X, y, groups=groups)) |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
224 step = options.get('step', None) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
225 if step and step >= 1.0: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
226 options['step'] = int(step) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
227 estimator = get_estimator(inputs['estimator_selector']) |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
228 check_feature_importances = try_get_attr( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
229 'feature_selectors', 'check_feature_importances') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
230 estimator = check_feature_importances(estimator) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
231 new_selector = selector(estimator, **options) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
232 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
233 elif inputs['selected_algorithm'] == 'DyRFECV': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
234 options['scoring'] = get_scoring(options['scoring']) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
235 options['n_jobs'] = N_JOBS |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
236 splitter, groups = get_cv(options.pop('cv_selector')) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
237 if groups is None: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
238 options['cv'] = splitter |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
239 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
240 options['cv'] = list(splitter.split(X, y, groups=groups)) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
241 step = options.get('step') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
242 if not step or step == 'None': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
243 step = None |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
244 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
245 step = ast.literal_eval(step) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
246 options['step'] = step |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
247 estimator = get_estimator(inputs["estimator_selector"]) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
248 check_feature_importances = try_get_attr( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
249 'feature_selectors', 'check_feature_importances') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
250 estimator = check_feature_importances(estimator) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
251 DyRFECV = try_get_attr('feature_selectors', 'DyRFECV') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
252 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
253 new_selector = DyRFECV(estimator, **options) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
254 |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
255 elif inputs['selected_algorithm'] == 'VarianceThreshold': |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
256 new_selector = selector(**options) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
257 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
258 else: |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
259 score_func = inputs['score_func'] |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
260 score_func = getattr(sklearn.feature_selection, score_func) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
261 new_selector = selector(score_func, **options) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
262 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
263 return new_selector |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
264 |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
265 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
266 def get_X_y(params, file1, file2): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
267 """Return machine learning inputs X, y from tabluar inputs |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
268 """ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
269 input_type = (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
270 ['input_options']['selected_input']) |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
271 if input_type == 'tabular': |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
272 header = 'infer' if (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
273 ['input_options']['header1']) else None |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
274 column_option = (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
275 ['input_options']['column_selector_options_1'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
276 ['selected_column_selector_option']) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
277 if column_option in ['by_index_number', 'all_but_by_index_number', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
278 'by_header_name', 'all_but_by_header_name']: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
279 c = (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
280 ['input_options']['column_selector_options_1']['col1']) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
281 else: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
282 c = None |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
283 X = read_columns( |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
284 file1, |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
285 c=c, |
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
286 c_option=column_option, |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
287 sep='\t', |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
288 header=header, |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
289 parse_dates=True).astype(float) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
290 else: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
291 X = mmread(file1) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
292 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
293 header = 'infer' if (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
294 ['input_options']['header2']) else None |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
295 column_option = (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
296 ['input_options']['column_selector_options_2'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
297 ['selected_column_selector_option2']) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
298 if column_option in ['by_index_number', 'all_but_by_index_number', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
299 'by_header_name', 'all_but_by_header_name']: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
300 c = (params['selected_tasks']['selected_algorithms'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
301 ['input_options']['column_selector_options_2']['col2']) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
302 else: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
303 c = None |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
304 y = read_columns( |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
305 file2, |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
306 c=c, |
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
307 c_option=column_option, |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
308 sep='\t', |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
309 header=header, |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
310 parse_dates=True) |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
311 y = y.ravel() |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
312 |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
313 return X, y |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
314 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
315 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
316 class SafeEval(Interpreter): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
317 """Customized symbol table for safely literal eval |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
318 """ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
319 def __init__(self, load_scipy=False, load_numpy=False, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
320 load_estimators=False): |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
321 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
322 # File opening and other unneeded functions could be dropped |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
323 unwanted = ['open', 'type', 'dir', 'id', 'str', 'repr'] |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
324 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
325 # Allowed symbol table. Add more if needed. |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
326 new_syms = { |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
327 'np_arange': getattr(np, 'arange'), |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
328 'ensemble_ExtraTreesClassifier': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
329 getattr(ensemble, 'ExtraTreesClassifier') |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
330 } |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
331 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
332 syms = make_symbol_table(use_numpy=False, **new_syms) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
333 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
334 if load_scipy: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
335 scipy_distributions = scipy.stats.distributions.__dict__ |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
336 for k, v in scipy_distributions.items(): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
337 if isinstance(v, (scipy.stats.rv_continuous, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
338 scipy.stats.rv_discrete)): |
13
badd86b9ce24
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 8cf3d813ec755166ee0bd517b4ecbbd4f84d4df1
bgruening
parents:
12
diff
changeset
|
339 syms['scipy_stats_' + k] = v |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
340 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
341 if load_numpy: |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
342 from_numpy_random = [ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
343 'beta', 'binomial', 'bytes', 'chisquare', 'choice', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
344 'dirichlet', 'division', 'exponential', 'f', 'gamma', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
345 'geometric', 'gumbel', 'hypergeometric', 'laplace', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
346 'logistic', 'lognormal', 'logseries', 'mtrand', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
347 'multinomial', 'multivariate_normal', 'negative_binomial', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
348 'noncentral_chisquare', 'noncentral_f', 'normal', 'pareto', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
349 'permutation', 'poisson', 'power', 'rand', 'randint', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
350 'randn', 'random', 'random_integers', 'random_sample', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
351 'ranf', 'rayleigh', 'sample', 'seed', 'set_state', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
352 'shuffle', 'standard_cauchy', 'standard_exponential', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
353 'standard_gamma', 'standard_normal', 'standard_t', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
354 'triangular', 'uniform', 'vonmises', 'wald', 'weibull', 'zipf'] |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
355 for f in from_numpy_random: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
356 syms['np_random_' + f] = getattr(np.random, f) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
357 |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
358 if load_estimators: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
359 estimator_table = { |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
360 'sklearn_svm': getattr(sklearn, 'svm'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
361 'sklearn_tree': getattr(sklearn, 'tree'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
362 'sklearn_ensemble': getattr(sklearn, 'ensemble'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
363 'sklearn_neighbors': getattr(sklearn, 'neighbors'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
364 'sklearn_naive_bayes': getattr(sklearn, 'naive_bayes'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
365 'sklearn_linear_model': getattr(sklearn, 'linear_model'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
366 'sklearn_cluster': getattr(sklearn, 'cluster'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
367 'sklearn_decomposition': getattr(sklearn, 'decomposition'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
368 'sklearn_preprocessing': getattr(sklearn, 'preprocessing'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
369 'sklearn_feature_selection': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
370 getattr(sklearn, 'feature_selection'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
371 'sklearn_kernel_approximation': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
372 getattr(sklearn, 'kernel_approximation'), |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
373 'skrebate_ReliefF': getattr(skrebate, 'ReliefF'), |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
374 'skrebate_SURF': getattr(skrebate, 'SURF'), |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
375 'skrebate_SURFstar': getattr(skrebate, 'SURFstar'), |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
376 'skrebate_MultiSURF': getattr(skrebate, 'MultiSURF'), |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
377 'skrebate_MultiSURFstar': getattr(skrebate, 'MultiSURFstar'), |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
378 'skrebate_TuRF': getattr(skrebate, 'TuRF'), |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
379 'xgboost_XGBClassifier': getattr(xgboost, 'XGBClassifier'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
380 'xgboost_XGBRegressor': getattr(xgboost, 'XGBRegressor'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
381 'imblearn_over_sampling': getattr(imblearn, 'over_sampling'), |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
382 'imblearn_combine': getattr(imblearn, 'combine') |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
383 } |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
384 syms.update(estimator_table) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
385 |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
386 for key in unwanted: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
387 syms.pop(key, None) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
388 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
389 super(SafeEval, self).__init__( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
390 symtable=syms, use_numpy=False, minimal=False, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
391 no_if=True, no_for=True, no_while=True, no_try=True, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
392 no_functiondef=True, no_ifexp=True, no_listcomp=False, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
393 no_augassign=False, no_assert=True, no_delete=True, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
394 no_raise=True, no_print=True) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
395 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
396 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
397 def get_estimator(estimator_json): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
398 """Return a sklearn or compatible estimator from Galaxy tool inputs |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
399 """ |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
400 estimator_module = estimator_json['selected_module'] |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
401 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
402 if estimator_module == 'custom_estimator': |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
403 c_estimator = estimator_json['c_estimator'] |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
404 with open(c_estimator, 'rb') as model_handler: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
405 new_model = load_model(model_handler) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
406 return new_model |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
407 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
408 if estimator_module == "binarize_target": |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
409 wrapped_estimator = estimator_json['wrapped_estimator'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
410 with open(wrapped_estimator, 'rb') as model_handler: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
411 wrapped_estimator = load_model(model_handler) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
412 options = {} |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
413 if estimator_json['z_score'] is not None: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
414 options['z_score'] = estimator_json['z_score'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
415 if estimator_json['value'] is not None: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
416 options['value'] = estimator_json['value'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
417 options['less_is_positive'] = estimator_json['less_is_positive'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
418 if estimator_json['clf_or_regr'] == 'BinarizeTargetClassifier': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
419 klass = try_get_attr('iraps_classifier', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
420 'BinarizeTargetClassifier') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
421 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
422 klass = try_get_attr('iraps_classifier', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
423 'BinarizeTargetRegressor') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
424 return klass(wrapped_estimator, **options) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
425 |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
426 estimator_cls = estimator_json['selected_estimator'] |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
427 |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
428 if estimator_module == 'xgboost': |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
429 klass = getattr(xgboost, estimator_cls) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
430 else: |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
431 module = getattr(sklearn, estimator_module) |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
432 klass = getattr(module, estimator_cls) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
433 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
434 estimator = klass() |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
435 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
436 estimator_params = estimator_json['text_params'].strip() |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
437 if estimator_params != '': |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
438 try: |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
439 safe_eval = SafeEval() |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
440 params = safe_eval('dict(' + estimator_params + ')') |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
441 except ValueError: |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
442 sys.exit("Unsupported parameter input: `%s`" % estimator_params) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
443 estimator.set_params(**params) |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
444 if 'n_jobs' in estimator.get_params(): |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
445 estimator.set_params(n_jobs=N_JOBS) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
446 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
447 return estimator |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
448 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
449 |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
450 def get_cv(cv_json): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
451 """ Return CV splitter from Galaxy tool inputs |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
452 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
453 Parameters |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
454 ---------- |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
455 cv_json : dict |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
456 From Galaxy tool inputs. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
457 e.g.: |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
458 { |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
459 'selected_cv': 'StratifiedKFold', |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
460 'n_splits': 3, |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
461 'shuffle': True, |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
462 'random_state': 0 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
463 } |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
464 """ |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
465 cv = cv_json.pop('selected_cv') |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
466 if cv == 'default': |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
467 return cv_json['n_splits'], None |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
468 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
469 groups = cv_json.pop('groups_selector', None) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
470 if groups is not None: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
471 infile_g = groups['infile_g'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
472 header = 'infer' if groups['header_g'] else None |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
473 column_option = (groups['column_selector_options_g'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
474 ['selected_column_selector_option_g']) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
475 if column_option in ['by_index_number', 'all_but_by_index_number', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
476 'by_header_name', 'all_but_by_header_name']: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
477 c = groups['column_selector_options_g']['col_g'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
478 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
479 c = None |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
480 groups = read_columns( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
481 infile_g, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
482 c=c, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
483 c_option=column_option, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
484 sep='\t', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
485 header=header, |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
486 parse_dates=True) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
487 groups = groups.ravel() |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
488 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
489 for k, v in cv_json.items(): |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
490 if v == '': |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
491 cv_json[k] = None |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
492 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
493 test_fold = cv_json.get('test_fold', None) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
494 if test_fold: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
495 if test_fold.startswith('__ob__'): |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
496 test_fold = test_fold[6:] |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
497 if test_fold.endswith('__cb__'): |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
498 test_fold = test_fold[:-6] |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
499 cv_json['test_fold'] = [int(x.strip()) for x in test_fold.split(',')] |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
500 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
501 test_size = cv_json.get('test_size', None) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
502 if test_size and test_size > 1.0: |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
503 cv_json['test_size'] = int(test_size) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
504 |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
505 if cv == 'OrderedKFold': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
506 cv_class = try_get_attr('model_validations', 'OrderedKFold') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
507 elif cv == 'RepeatedOrderedKFold': |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
508 cv_class = try_get_attr('model_validations', 'RepeatedOrderedKFold') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
509 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
510 cv_class = getattr(model_selection, cv) |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
511 splitter = cv_class(**cv_json) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
512 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
513 return splitter, groups |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
514 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
515 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
516 # needed when sklearn < v0.20 |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
517 def balanced_accuracy_score(y_true, y_pred): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
518 """Compute balanced accuracy score, which is now available in |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
519 scikit-learn from v0.20.0. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
520 """ |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
521 C = metrics.confusion_matrix(y_true, y_pred) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
522 with np.errstate(divide='ignore', invalid='ignore'): |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
523 per_class = np.diag(C) / C.sum(axis=1) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
524 if np.any(np.isnan(per_class)): |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
525 warnings.warn('y_pred contains classes not in y_true') |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
526 per_class = per_class[~np.isnan(per_class)] |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
527 score = np.mean(per_class) |
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
528 return score |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
529 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
530 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
531 def get_scoring(scoring_json): |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
532 """Return single sklearn scorer class |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
533 or multiple scoers in dictionary |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
534 """ |
16
86e1e2874460
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 57f4407e278a615f47a377a3328782b1d8e0b54d
bgruening
parents:
14
diff
changeset
|
535 if scoring_json['primary_scoring'] == 'default': |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
536 return None |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
537 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
538 my_scorers = metrics.SCORERS |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
539 my_scorers['binarize_auc_scorer'] =\ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
540 try_get_attr('iraps_classifier', 'binarize_auc_scorer') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
541 my_scorers['binarize_average_precision_scorer'] =\ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
542 try_get_attr('iraps_classifier', 'binarize_average_precision_scorer') |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
543 if 'balanced_accuracy' not in my_scorers: |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
544 my_scorers['balanced_accuracy'] =\ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
545 metrics.make_scorer(balanced_accuracy_score) |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
546 |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
547 if scoring_json['secondary_scoring'] != 'None'\ |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
548 and scoring_json['secondary_scoring'] !=\ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
549 scoring_json['primary_scoring']: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
550 return_scoring = {} |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
551 primary_scoring = scoring_json['primary_scoring'] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
552 return_scoring[primary_scoring] = my_scorers[primary_scoring] |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
553 for scorer in scoring_json['secondary_scoring'].split(','): |
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
554 if scorer != scoring_json['primary_scoring']: |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
555 return_scoring[scorer] = my_scorers[scorer] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
556 return return_scoring |
12
2c1851992069
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit d00173591e4a783a4c1cb2664e4bb192ab5414f7
bgruening
parents:
diff
changeset
|
557 |
14
e244d6f2df1a
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit 2a058459e6daf0486871f93845f00fdb4a4eaca1
bgruening
parents:
13
diff
changeset
|
558 return my_scorers[scoring_json['primary_scoring']] |
17
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
559 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
560 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
561 def get_search_params(estimator): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
562 """Format the output of `estimator.get_params()` |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
563 """ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
564 params = estimator.get_params() |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
565 results = [] |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
566 for k, v in params.items(): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
567 # params below won't be shown for search in the searchcv tool |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
568 keywords = ('n_jobs', 'pre_dispatch', 'memory', 'steps', |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
569 'nthread', 'verbose') |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
570 if k.endswith(keywords): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
571 results.append(['*', k, k+": "+repr(v)]) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
572 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
573 results.append(['@', k, k+": "+repr(v)]) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
574 results.append( |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
575 ["", "Note:", |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
576 "@, params eligible for search in searchcv tool."]) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
577 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
578 return results |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
579 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
580 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
581 def try_get_attr(module, name): |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
582 """try to get attribute from a custom module |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
583 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
584 Parameters |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
585 ---------- |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
586 module : str |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
587 Module name |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
588 name : str |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
589 Attribute (class/function) name. |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
590 |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
591 Returns |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
592 ------- |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
593 class or function |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
594 """ |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
595 mod = sys.modules.get(module, None) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
596 if mod: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
597 return getattr(mod, name) |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
598 else: |
cf9aa11b91c8
planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ab963ec9498bd05d2fb2f24f75adb2fccae7958c
bgruening
parents:
16
diff
changeset
|
599 raise Exception("No module named %s." % module) |