comparison train_test_eval.py @ 0:af2624d5ab32 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
author bgruening
date Sat, 01 May 2021 01:24:32 +0000
parents
children 9349ed2749c6
comparison
equal deleted inserted replaced
-1:000000000000 0:af2624d5ab32
1 import argparse
2 import json
3 import os
4 import pickle
5 import warnings
6 from itertools import chain
7
8 import joblib
9 import numpy as np
10 import pandas as pd
11 from galaxy_ml.model_validations import train_test_split
12 from galaxy_ml.utils import (get_module, get_scoring, load_model,
13 read_columns, SafeEval, try_get_attr)
14 from scipy.io import mmread
15 from sklearn import pipeline
16 from sklearn.metrics.scorer import _check_multimetric_scoring
17 from sklearn.model_selection import _search, _validation
18 from sklearn.model_selection._validation import _score
19 from sklearn.utils import indexable, safe_indexing
20
21 _fit_and_score = try_get_attr("galaxy_ml.model_validations", "_fit_and_score")
22 setattr(_search, "_fit_and_score", _fit_and_score)
23 setattr(_validation, "_fit_and_score", _fit_and_score)
24
25 N_JOBS = int(os.environ.get("GALAXY_SLOTS", 1))
26 CACHE_DIR = os.path.join(os.getcwd(), "cached")
27 del os
28 NON_SEARCHABLE = ("n_jobs", "pre_dispatch", "memory", "_path", "nthread", "callbacks")
29 ALLOWED_CALLBACKS = (
30 "EarlyStopping",
31 "TerminateOnNaN",
32 "ReduceLROnPlateau",
33 "CSVLogger",
34 "None",
35 )
36
37
38 def _eval_swap_params(params_builder):
39 swap_params = {}
40
41 for p in params_builder["param_set"]:
42 swap_value = p["sp_value"].strip()
43 if swap_value == "":
44 continue
45
46 param_name = p["sp_name"]
47 if param_name.lower().endswith(NON_SEARCHABLE):
48 warnings.warn(
49 "Warning: `%s` is not eligible for search and was "
50 "omitted!" % param_name
51 )
52 continue
53
54 if not swap_value.startswith(":"):
55 safe_eval = SafeEval(load_scipy=True, load_numpy=True)
56 ev = safe_eval(swap_value)
57 else:
58 # Have `:` before search list, asks for estimator evaluatio
59 safe_eval_es = SafeEval(load_estimators=True)
60 swap_value = swap_value[1:].strip()
61 # TODO maybe add regular express check
62 ev = safe_eval_es(swap_value)
63
64 swap_params[param_name] = ev
65
66 return swap_params
67
68
69 def train_test_split_none(*arrays, **kwargs):
70 """extend train_test_split to take None arrays
71 and support split by group names.
72 """
73 nones = []
74 new_arrays = []
75 for idx, arr in enumerate(arrays):
76 if arr is None:
77 nones.append(idx)
78 else:
79 new_arrays.append(arr)
80
81 if kwargs["shuffle"] == "None":
82 kwargs["shuffle"] = None
83
84 group_names = kwargs.pop("group_names", None)
85
86 if group_names is not None and group_names.strip():
87 group_names = [name.strip() for name in group_names.split(",")]
88 new_arrays = indexable(*new_arrays)
89 groups = kwargs["labels"]
90 n_samples = new_arrays[0].shape[0]
91 index_arr = np.arange(n_samples)
92 test = index_arr[np.isin(groups, group_names)]
93 train = index_arr[~np.isin(groups, group_names)]
94 rval = list(
95 chain.from_iterable(
96 (safe_indexing(a, train), safe_indexing(a, test)) for a in new_arrays
97 )
98 )
99 else:
100 rval = train_test_split(*new_arrays, **kwargs)
101
102 for pos in nones:
103 rval[pos * 2: 2] = [None, None]
104
105 return rval
106
107
108 def main(
109 inputs,
110 infile_estimator,
111 infile1,
112 infile2,
113 outfile_result,
114 outfile_object=None,
115 outfile_weights=None,
116 groups=None,
117 ref_seq=None,
118 intervals=None,
119 targets=None,
120 fasta_path=None,
121 ):
122 """
123 Parameter
124 ---------
125 inputs : str
126 File path to galaxy tool parameter
127
128 infile_estimator : str
129 File path to estimator
130
131 infile1 : str
132 File path to dataset containing features
133
134 infile2 : str
135 File path to dataset containing target values
136
137 outfile_result : str
138 File path to save the results, either cv_results or test result
139
140 outfile_object : str, optional
141 File path to save searchCV object
142
143 outfile_weights : str, optional
144 File path to save deep learning model weights
145
146 groups : str
147 File path to dataset containing groups labels
148
149 ref_seq : str
150 File path to dataset containing genome sequence file
151
152 intervals : str
153 File path to dataset containing interval file
154
155 targets : str
156 File path to dataset compressed target bed file
157
158 fasta_path : str
159 File path to dataset containing fasta file
160 """
161 warnings.simplefilter("ignore")
162
163 with open(inputs, "r") as param_handler:
164 params = json.load(param_handler)
165
166 # load estimator
167 with open(infile_estimator, "rb") as estimator_handler:
168 estimator = load_model(estimator_handler)
169
170 # swap hyperparameter
171 swapping = params["experiment_schemes"]["hyperparams_swapping"]
172 swap_params = _eval_swap_params(swapping)
173 estimator.set_params(**swap_params)
174
175 estimator_params = estimator.get_params()
176
177 # store read dataframe object
178 loaded_df = {}
179
180 input_type = params["input_options"]["selected_input"]
181 # tabular input
182 if input_type == "tabular":
183 header = "infer" if params["input_options"]["header1"] else None
184 column_option = params["input_options"]["column_selector_options_1"][
185 "selected_column_selector_option"
186 ]
187 if column_option in [
188 "by_index_number",
189 "all_but_by_index_number",
190 "by_header_name",
191 "all_but_by_header_name",
192 ]:
193 c = params["input_options"]["column_selector_options_1"]["col1"]
194 else:
195 c = None
196
197 df_key = infile1 + repr(header)
198 df = pd.read_csv(infile1, sep="\t", header=header, parse_dates=True)
199 loaded_df[df_key] = df
200
201 X = read_columns(df, c=c, c_option=column_option).astype(float)
202 # sparse input
203 elif input_type == "sparse":
204 X = mmread(open(infile1, "r"))
205
206 # fasta_file input
207 elif input_type == "seq_fasta":
208 pyfaidx = get_module("pyfaidx")
209 sequences = pyfaidx.Fasta(fasta_path)
210 n_seqs = len(sequences.keys())
211 X = np.arange(n_seqs)[:, np.newaxis]
212 for param in estimator_params.keys():
213 if param.endswith("fasta_path"):
214 estimator.set_params(**{param: fasta_path})
215 break
216 else:
217 raise ValueError(
218 "The selected estimator doesn't support "
219 "fasta file input! Please consider using "
220 "KerasGBatchClassifier with "
221 "FastaDNABatchGenerator/FastaProteinBatchGenerator "
222 "or having GenomeOneHotEncoder/ProteinOneHotEncoder "
223 "in pipeline!"
224 )
225
226 elif input_type == "refseq_and_interval":
227 path_params = {
228 "data_batch_generator__ref_genome_path": ref_seq,
229 "data_batch_generator__intervals_path": intervals,
230 "data_batch_generator__target_path": targets,
231 }
232 estimator.set_params(**path_params)
233 n_intervals = sum(1 for line in open(intervals))
234 X = np.arange(n_intervals)[:, np.newaxis]
235
236 # Get target y
237 header = "infer" if params["input_options"]["header2"] else None
238 column_option = params["input_options"]["column_selector_options_2"][
239 "selected_column_selector_option2"
240 ]
241 if column_option in [
242 "by_index_number",
243 "all_but_by_index_number",
244 "by_header_name",
245 "all_but_by_header_name",
246 ]:
247 c = params["input_options"]["column_selector_options_2"]["col2"]
248 else:
249 c = None
250
251 df_key = infile2 + repr(header)
252 if df_key in loaded_df:
253 infile2 = loaded_df[df_key]
254 else:
255 infile2 = pd.read_csv(infile2, sep="\t", header=header, parse_dates=True)
256 loaded_df[df_key] = infile2
257
258 y = read_columns(
259 infile2, c=c, c_option=column_option, sep="\t", header=header, parse_dates=True
260 )
261 if len(y.shape) == 2 and y.shape[1] == 1:
262 y = y.ravel()
263 if input_type == "refseq_and_interval":
264 estimator.set_params(data_batch_generator__features=y.ravel().tolist())
265 y = None
266 # end y
267
268 # load groups
269 if groups:
270 groups_selector = (
271 params["experiment_schemes"]["test_split"]["split_algos"]
272 ).pop("groups_selector")
273
274 header = "infer" if groups_selector["header_g"] else None
275 column_option = groups_selector["column_selector_options_g"][
276 "selected_column_selector_option_g"
277 ]
278 if column_option in [
279 "by_index_number",
280 "all_but_by_index_number",
281 "by_header_name",
282 "all_but_by_header_name",
283 ]:
284 c = groups_selector["column_selector_options_g"]["col_g"]
285 else:
286 c = None
287
288 df_key = groups + repr(header)
289 if df_key in loaded_df:
290 groups = loaded_df[df_key]
291
292 groups = read_columns(
293 groups,
294 c=c,
295 c_option=column_option,
296 sep="\t",
297 header=header,
298 parse_dates=True,
299 )
300 groups = groups.ravel()
301
302 # del loaded_df
303 del loaded_df
304
305 # handle memory
306 memory = joblib.Memory(location=CACHE_DIR, verbose=0)
307 # cache iraps_core fits could increase search speed significantly
308 if estimator.__class__.__name__ == "IRAPSClassifier":
309 estimator.set_params(memory=memory)
310 else:
311 # For iraps buried in pipeline
312 new_params = {}
313 for p, v in estimator_params.items():
314 if p.endswith("memory"):
315 # for case of `__irapsclassifier__memory`
316 if len(p) > 8 and p[:-8].endswith("irapsclassifier"):
317 # cache iraps_core fits could increase search
318 # speed significantly
319 new_params[p] = memory
320 # security reason, we don't want memory being
321 # modified unexpectedly
322 elif v:
323 new_params[p] = None
324 # handle n_jobs
325 elif p.endswith("n_jobs"):
326 # For now, 1 CPU is suggested for iprasclassifier
327 if len(p) > 8 and p[:-8].endswith("irapsclassifier"):
328 new_params[p] = 1
329 else:
330 new_params[p] = N_JOBS
331 # for security reason, types of callback are limited
332 elif p.endswith("callbacks"):
333 for cb in v:
334 cb_type = cb["callback_selection"]["callback_type"]
335 if cb_type not in ALLOWED_CALLBACKS:
336 raise ValueError("Prohibited callback type: %s!" % cb_type)
337
338 estimator.set_params(**new_params)
339
340 # handle scorer, convert to scorer dict
341 # Check if scoring is specified
342 scoring = params["experiment_schemes"]["metrics"].get("scoring", None)
343 if scoring is not None:
344 # get_scoring() expects secondary_scoring to be a comma separated string (not a list)
345 # Check if secondary_scoring is specified
346 secondary_scoring = scoring.get("secondary_scoring", None)
347 if secondary_scoring is not None:
348 # If secondary_scoring is specified, convert the list into comman separated string
349 scoring["secondary_scoring"] = ",".join(scoring["secondary_scoring"])
350 scorer = get_scoring(scoring)
351 scorer, _ = _check_multimetric_scoring(estimator, scoring=scorer)
352
353 # handle test (first) split
354 test_split_options = params["experiment_schemes"]["test_split"]["split_algos"]
355
356 if test_split_options["shuffle"] == "group":
357 test_split_options["labels"] = groups
358 if test_split_options["shuffle"] == "stratified":
359 if y is not None:
360 test_split_options["labels"] = y
361 else:
362 raise ValueError(
363 "Stratified shuffle split is not " "applicable on empty target values!"
364 )
365
366 (
367 X_train,
368 X_test,
369 y_train,
370 y_test,
371 groups_train,
372 _groups_test,
373 ) = train_test_split_none(X, y, groups, **test_split_options)
374
375 exp_scheme = params["experiment_schemes"]["selected_exp_scheme"]
376
377 # handle validation (second) split
378 if exp_scheme == "train_val_test":
379 val_split_options = params["experiment_schemes"]["val_split"]["split_algos"]
380
381 if val_split_options["shuffle"] == "group":
382 val_split_options["labels"] = groups_train
383 if val_split_options["shuffle"] == "stratified":
384 if y_train is not None:
385 val_split_options["labels"] = y_train
386 else:
387 raise ValueError(
388 "Stratified shuffle split is not "
389 "applicable on empty target values!"
390 )
391
392 (
393 X_train,
394 X_val,
395 y_train,
396 y_val,
397 groups_train,
398 _groups_val,
399 ) = train_test_split_none(X_train, y_train, groups_train, **val_split_options)
400
401 # train and eval
402 if hasattr(estimator, "validation_data"):
403 if exp_scheme == "train_val_test":
404 estimator.fit(X_train, y_train, validation_data=(X_val, y_val))
405 else:
406 estimator.fit(X_train, y_train, validation_data=(X_test, y_test))
407 else:
408 estimator.fit(X_train, y_train)
409
410 if hasattr(estimator, "evaluate"):
411 scores = estimator.evaluate(
412 X_test, y_test=y_test, scorer=scorer, is_multimetric=True
413 )
414 else:
415 scores = _score(estimator, X_test, y_test, scorer, is_multimetric=True)
416 # handle output
417 for name, score in scores.items():
418 scores[name] = [score]
419 df = pd.DataFrame(scores)
420 df = df[sorted(df.columns)]
421 df.to_csv(path_or_buf=outfile_result, sep="\t", header=True, index=False)
422
423 memory.clear(warn=False)
424
425 if outfile_object:
426 main_est = estimator
427 if isinstance(estimator, pipeline.Pipeline):
428 main_est = estimator.steps[-1][-1]
429
430 if hasattr(main_est, "model_") and hasattr(main_est, "save_weights"):
431 if outfile_weights:
432 main_est.save_weights(outfile_weights)
433 if getattr(main_est, "model_", None):
434 del main_est.model_
435 if getattr(main_est, "fit_params", None):
436 del main_est.fit_params
437 if getattr(main_est, "model_class_", None):
438 del main_est.model_class_
439 if getattr(main_est, "validation_data", None):
440 del main_est.validation_data
441 if getattr(main_est, "data_generator_", None):
442 del main_est.data_generator_
443
444 with open(outfile_object, "wb") as output_handler:
445 pickle.dump(estimator, output_handler, pickle.HIGHEST_PROTOCOL)
446
447
448 if __name__ == "__main__":
449 aparser = argparse.ArgumentParser()
450 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
451 aparser.add_argument("-e", "--estimator", dest="infile_estimator")
452 aparser.add_argument("-X", "--infile1", dest="infile1")
453 aparser.add_argument("-y", "--infile2", dest="infile2")
454 aparser.add_argument("-O", "--outfile_result", dest="outfile_result")
455 aparser.add_argument("-o", "--outfile_object", dest="outfile_object")
456 aparser.add_argument("-w", "--outfile_weights", dest="outfile_weights")
457 aparser.add_argument("-g", "--groups", dest="groups")
458 aparser.add_argument("-r", "--ref_seq", dest="ref_seq")
459 aparser.add_argument("-b", "--intervals", dest="intervals")
460 aparser.add_argument("-t", "--targets", dest="targets")
461 aparser.add_argument("-f", "--fasta_path", dest="fasta_path")
462 args = aparser.parse_args()
463
464 main(
465 args.inputs,
466 args.infile_estimator,
467 args.infile1,
468 args.infile2,
469 args.outfile_result,
470 outfile_object=args.outfile_object,
471 outfile_weights=args.outfile_weights,
472 groups=args.groups,
473 ref_seq=args.ref_seq,
474 intervals=args.intervals,
475 targets=args.targets,
476 fasta_path=args.fasta_path,
477 )