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