comparison train_test_eval.py @ 0:59e8b4328c82 draft

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