comparison search_model_validation.py @ 0:3b6ee54eb7e2 draft

"planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools/sklearn commit ea12f973df4b97a2691d9e4ce6bf6fae59d57717"
author bgruening
date Sat, 01 May 2021 00:57:35 +0000
parents
children 108141350edb
comparison
equal deleted inserted replaced
-1:000000000000 0:3b6ee54eb7e2
1 import argparse
2 import collections
3 import json
4 import os
5 import pickle
6 import sys
7 import warnings
8
9 import imblearn
10 import joblib
11 import numpy as np
12 import pandas as pd
13 import skrebate
14 from galaxy_ml.utils import (clean_params, get_cv,
15 get_main_estimator, get_module, get_scoring,
16 load_model, read_columns, SafeEval, try_get_attr)
17 from scipy.io import mmread
18 from sklearn import (cluster, decomposition, feature_selection,
19 kernel_approximation, model_selection, preprocessing)
20 from sklearn.exceptions import FitFailedWarning
21 from sklearn.model_selection import _search, _validation
22 from sklearn.model_selection._validation import _score, cross_validate
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 # handle disk cache
30 CACHE_DIR = os.path.join(os.getcwd(), "cached")
31 del os
32 NON_SEARCHABLE = ("n_jobs", "pre_dispatch", "memory", "_path", "nthread", "callbacks")
33
34
35 def _eval_search_params(params_builder):
36 search_params = {}
37
38 for p in params_builder["param_set"]:
39 search_list = p["sp_list"].strip()
40 if search_list == "":
41 continue
42
43 param_name = p["sp_name"]
44 if param_name.lower().endswith(NON_SEARCHABLE):
45 print(
46 "Warning: `%s` is not eligible for search and was "
47 "omitted!" % param_name
48 )
49 continue
50
51 if not search_list.startswith(":"):
52 safe_eval = SafeEval(load_scipy=True, load_numpy=True)
53 ev = safe_eval(search_list)
54 search_params[param_name] = ev
55 else:
56 # Have `:` before search list, asks for estimator evaluatio
57 safe_eval_es = SafeEval(load_estimators=True)
58 search_list = search_list[1:].strip()
59 # TODO maybe add regular express check
60 ev = safe_eval_es(search_list)
61 preprocessings = (
62 preprocessing.StandardScaler(),
63 preprocessing.Binarizer(),
64 preprocessing.MaxAbsScaler(),
65 preprocessing.Normalizer(),
66 preprocessing.MinMaxScaler(),
67 preprocessing.PolynomialFeatures(),
68 preprocessing.RobustScaler(),
69 feature_selection.SelectKBest(),
70 feature_selection.GenericUnivariateSelect(),
71 feature_selection.SelectPercentile(),
72 feature_selection.SelectFpr(),
73 feature_selection.SelectFdr(),
74 feature_selection.SelectFwe(),
75 feature_selection.VarianceThreshold(),
76 decomposition.FactorAnalysis(random_state=0),
77 decomposition.FastICA(random_state=0),
78 decomposition.IncrementalPCA(),
79 decomposition.KernelPCA(random_state=0, n_jobs=N_JOBS),
80 decomposition.LatentDirichletAllocation(random_state=0, n_jobs=N_JOBS),
81 decomposition.MiniBatchDictionaryLearning(
82 random_state=0, n_jobs=N_JOBS
83 ),
84 decomposition.MiniBatchSparsePCA(random_state=0, n_jobs=N_JOBS),
85 decomposition.NMF(random_state=0),
86 decomposition.PCA(random_state=0),
87 decomposition.SparsePCA(random_state=0, n_jobs=N_JOBS),
88 decomposition.TruncatedSVD(random_state=0),
89 kernel_approximation.Nystroem(random_state=0),
90 kernel_approximation.RBFSampler(random_state=0),
91 kernel_approximation.AdditiveChi2Sampler(),
92 kernel_approximation.SkewedChi2Sampler(random_state=0),
93 cluster.FeatureAgglomeration(),
94 skrebate.ReliefF(n_jobs=N_JOBS),
95 skrebate.SURF(n_jobs=N_JOBS),
96 skrebate.SURFstar(n_jobs=N_JOBS),
97 skrebate.MultiSURF(n_jobs=N_JOBS),
98 skrebate.MultiSURFstar(n_jobs=N_JOBS),
99 imblearn.under_sampling.ClusterCentroids(random_state=0, n_jobs=N_JOBS),
100 imblearn.under_sampling.CondensedNearestNeighbour(
101 random_state=0, n_jobs=N_JOBS
102 ),
103 imblearn.under_sampling.EditedNearestNeighbours(
104 random_state=0, n_jobs=N_JOBS
105 ),
106 imblearn.under_sampling.RepeatedEditedNearestNeighbours(
107 random_state=0, n_jobs=N_JOBS
108 ),
109 imblearn.under_sampling.AllKNN(random_state=0, n_jobs=N_JOBS),
110 imblearn.under_sampling.InstanceHardnessThreshold(
111 random_state=0, n_jobs=N_JOBS
112 ),
113 imblearn.under_sampling.NearMiss(random_state=0, n_jobs=N_JOBS),
114 imblearn.under_sampling.NeighbourhoodCleaningRule(
115 random_state=0, n_jobs=N_JOBS
116 ),
117 imblearn.under_sampling.OneSidedSelection(
118 random_state=0, n_jobs=N_JOBS
119 ),
120 imblearn.under_sampling.RandomUnderSampler(random_state=0),
121 imblearn.under_sampling.TomekLinks(random_state=0, n_jobs=N_JOBS),
122 imblearn.over_sampling.ADASYN(random_state=0, n_jobs=N_JOBS),
123 imblearn.over_sampling.RandomOverSampler(random_state=0),
124 imblearn.over_sampling.SMOTE(random_state=0, n_jobs=N_JOBS),
125 imblearn.over_sampling.SVMSMOTE(random_state=0, n_jobs=N_JOBS),
126 imblearn.over_sampling.BorderlineSMOTE(random_state=0, n_jobs=N_JOBS),
127 imblearn.over_sampling.SMOTENC(
128 categorical_features=[], random_state=0, n_jobs=N_JOBS
129 ),
130 imblearn.combine.SMOTEENN(random_state=0),
131 imblearn.combine.SMOTETomek(random_state=0),
132 )
133 newlist = []
134 for obj in ev:
135 if obj is None:
136 newlist.append(None)
137 elif obj == "all_0":
138 newlist.extend(preprocessings[0:35])
139 elif obj == "sk_prep_all": # no KernalCenter()
140 newlist.extend(preprocessings[0:7])
141 elif obj == "fs_all":
142 newlist.extend(preprocessings[7:14])
143 elif obj == "decomp_all":
144 newlist.extend(preprocessings[14:25])
145 elif obj == "k_appr_all":
146 newlist.extend(preprocessings[25:29])
147 elif obj == "reb_all":
148 newlist.extend(preprocessings[30:35])
149 elif obj == "imb_all":
150 newlist.extend(preprocessings[35:54])
151 elif type(obj) is int and -1 < obj < len(preprocessings):
152 newlist.append(preprocessings[obj])
153 elif hasattr(obj, "get_params"): # user uploaded object
154 if "n_jobs" in obj.get_params():
155 newlist.append(obj.set_params(n_jobs=N_JOBS))
156 else:
157 newlist.append(obj)
158 else:
159 sys.exit("Unsupported estimator type: %r" % (obj))
160
161 search_params[param_name] = newlist
162
163 return search_params
164
165
166 def _handle_X_y(
167 estimator,
168 params,
169 infile1,
170 infile2,
171 loaded_df={},
172 ref_seq=None,
173 intervals=None,
174 targets=None,
175 fasta_path=None,
176 ):
177 """read inputs
178
179 Params
180 -------
181 estimator : estimator object
182 params : dict
183 Galaxy tool parameter inputs
184 infile1 : str
185 File path to dataset containing features
186 infile2 : str
187 File path to dataset containing target values
188 loaded_df : dict
189 Contains loaded DataFrame objects with file path as keys
190 ref_seq : str
191 File path to dataset containing genome sequence file
192 interval : str
193 File path to dataset containing interval file
194 targets : str
195 File path to dataset compressed target bed file
196 fasta_path : str
197 File path to dataset containing fasta file
198
199
200 Returns
201 -------
202 estimator : estimator object after setting new attributes
203 X : numpy array
204 y : numpy array
205 """
206 estimator_params = estimator.get_params()
207
208 input_type = params["input_options"]["selected_input"]
209 # tabular input
210 if input_type == "tabular":
211 header = "infer" if params["input_options"]["header1"] else None
212 column_option = params["input_options"]["column_selector_options_1"][
213 "selected_column_selector_option"
214 ]
215 if column_option in [
216 "by_index_number",
217 "all_but_by_index_number",
218 "by_header_name",
219 "all_but_by_header_name",
220 ]:
221 c = params["input_options"]["column_selector_options_1"]["col1"]
222 else:
223 c = None
224
225 df_key = infile1 + repr(header)
226
227 if df_key in loaded_df:
228 infile1 = loaded_df[df_key]
229
230 df = pd.read_csv(infile1, sep="\t", header=header, parse_dates=True)
231 loaded_df[df_key] = df
232
233 X = read_columns(df, c=c, c_option=column_option).astype(float)
234 # sparse input
235 elif input_type == "sparse":
236 X = mmread(open(infile1, "r"))
237
238 # fasta_file input
239 elif input_type == "seq_fasta":
240 pyfaidx = get_module("pyfaidx")
241 sequences = pyfaidx.Fasta(fasta_path)
242 n_seqs = len(sequences.keys())
243 X = np.arange(n_seqs)[:, np.newaxis]
244 for param in estimator_params.keys():
245 if param.endswith("fasta_path"):
246 estimator.set_params(**{param: fasta_path})
247 break
248 else:
249 raise ValueError(
250 "The selected estimator doesn't support "
251 "fasta file input! Please consider using "
252 "KerasGBatchClassifier with "
253 "FastaDNABatchGenerator/FastaProteinBatchGenerator "
254 "or having GenomeOneHotEncoder/ProteinOneHotEncoder "
255 "in pipeline!"
256 )
257
258 elif input_type == "refseq_and_interval":
259 path_params = {
260 "data_batch_generator__ref_genome_path": ref_seq,
261 "data_batch_generator__intervals_path": intervals,
262 "data_batch_generator__target_path": targets,
263 }
264 estimator.set_params(**path_params)
265 n_intervals = sum(1 for line in open(intervals))
266 X = np.arange(n_intervals)[:, np.newaxis]
267
268 # Get target y
269 header = "infer" if params["input_options"]["header2"] else None
270 column_option = params["input_options"]["column_selector_options_2"][
271 "selected_column_selector_option2"
272 ]
273 if column_option in [
274 "by_index_number",
275 "all_but_by_index_number",
276 "by_header_name",
277 "all_but_by_header_name",
278 ]:
279 c = params["input_options"]["column_selector_options_2"]["col2"]
280 else:
281 c = None
282
283 df_key = infile2 + repr(header)
284 if df_key in loaded_df:
285 infile2 = loaded_df[df_key]
286 else:
287 infile2 = pd.read_csv(infile2, sep="\t", header=header, parse_dates=True)
288 loaded_df[df_key] = infile2
289
290 y = read_columns(
291 infile2, c=c, c_option=column_option, sep="\t", header=header, parse_dates=True
292 )
293 if len(y.shape) == 2 and y.shape[1] == 1:
294 y = y.ravel()
295 if input_type == "refseq_and_interval":
296 estimator.set_params(data_batch_generator__features=y.ravel().tolist())
297 y = None
298 # end y
299
300 return estimator, X, y
301
302
303 def _do_outer_cv(searcher, X, y, outer_cv, scoring, error_score="raise", outfile=None):
304 """Do outer cross-validation for nested CV
305
306 Parameters
307 ----------
308 searcher : object
309 SearchCV object
310 X : numpy array
311 Containing features
312 y : numpy array
313 Target values or labels
314 outer_cv : int or CV splitter
315 Control the cv splitting
316 scoring : object
317 Scorer
318 error_score: str, float or numpy float
319 Whether to raise fit error or return an value
320 outfile : str
321 File path to store the restuls
322 """
323 if error_score == "raise":
324 rval = cross_validate(
325 searcher,
326 X,
327 y,
328 scoring=scoring,
329 cv=outer_cv,
330 n_jobs=N_JOBS,
331 verbose=0,
332 error_score=error_score,
333 )
334 else:
335 warnings.simplefilter("always", FitFailedWarning)
336 with warnings.catch_warnings(record=True) as w:
337 try:
338 rval = cross_validate(
339 searcher,
340 X,
341 y,
342 scoring=scoring,
343 cv=outer_cv,
344 n_jobs=N_JOBS,
345 verbose=0,
346 error_score=error_score,
347 )
348 except ValueError:
349 pass
350 for warning in w:
351 print(repr(warning.message))
352
353 keys = list(rval.keys())
354 for k in keys:
355 if k.startswith("test"):
356 rval["mean_" + k] = np.mean(rval[k])
357 rval["std_" + k] = np.std(rval[k])
358 if k.endswith("time"):
359 rval.pop(k)
360 rval = pd.DataFrame(rval)
361 rval = rval[sorted(rval.columns)]
362 rval.to_csv(path_or_buf=outfile, sep="\t", header=True, index=False)
363
364
365 def _do_train_test_split_val(
366 searcher,
367 X,
368 y,
369 params,
370 error_score="raise",
371 primary_scoring=None,
372 groups=None,
373 outfile=None,
374 ):
375 """do train test split, searchCV validates on the train and then use
376 the best_estimator_ to evaluate on the test
377
378 Returns
379 --------
380 Fitted SearchCV object
381 """
382 train_test_split = try_get_attr("galaxy_ml.model_validations", "train_test_split")
383 split_options = params["outer_split"]
384
385 # splits
386 if split_options["shuffle"] == "stratified":
387 split_options["labels"] = y
388 X, X_test, y, y_test = train_test_split(X, y, **split_options)
389 elif split_options["shuffle"] == "group":
390 if groups is None:
391 raise ValueError(
392 "No group based CV option was choosen for " "group shuffle!"
393 )
394 split_options["labels"] = groups
395 if y is None:
396 X, X_test, groups, _ = train_test_split(X, groups, **split_options)
397 else:
398 X, X_test, y, y_test, groups, _ = train_test_split(
399 X, y, groups, **split_options
400 )
401 else:
402 if split_options["shuffle"] == "None":
403 split_options["shuffle"] = None
404 X, X_test, y, y_test = train_test_split(X, y, **split_options)
405
406 if error_score == "raise":
407 searcher.fit(X, y, groups=groups)
408 else:
409 warnings.simplefilter("always", FitFailedWarning)
410 with warnings.catch_warnings(record=True) as w:
411 try:
412 searcher.fit(X, y, groups=groups)
413 except ValueError:
414 pass
415 for warning in w:
416 print(repr(warning.message))
417
418 scorer_ = searcher.scorer_
419 if isinstance(scorer_, collections.Mapping):
420 is_multimetric = True
421 else:
422 is_multimetric = False
423
424 best_estimator_ = getattr(searcher, "best_estimator_")
425
426 # TODO Solve deep learning models in pipeline
427 if best_estimator_.__class__.__name__ == "KerasGBatchClassifier":
428 test_score = best_estimator_.evaluate(
429 X_test, scorer=scorer_, is_multimetric=is_multimetric
430 )
431 else:
432 test_score = _score(
433 best_estimator_, X_test, y_test, scorer_, is_multimetric=is_multimetric
434 )
435
436 if not is_multimetric:
437 test_score = {primary_scoring: test_score}
438 for key, value in test_score.items():
439 test_score[key] = [value]
440 result_df = pd.DataFrame(test_score)
441 result_df.to_csv(path_or_buf=outfile, sep="\t", header=True, index=False)
442
443 return searcher
444
445
446 def main(
447 inputs,
448 infile_estimator,
449 infile1,
450 infile2,
451 outfile_result,
452 outfile_object=None,
453 outfile_weights=None,
454 groups=None,
455 ref_seq=None,
456 intervals=None,
457 targets=None,
458 fasta_path=None,
459 ):
460 """
461 Parameter
462 ---------
463 inputs : str
464 File path to galaxy tool parameter
465
466 infile_estimator : str
467 File path to estimator
468
469 infile1 : str
470 File path to dataset containing features
471
472 infile2 : str
473 File path to dataset containing target values
474
475 outfile_result : str
476 File path to save the results, either cv_results or test result
477
478 outfile_object : str, optional
479 File path to save searchCV object
480
481 outfile_weights : str, optional
482 File path to save model weights
483
484 groups : str
485 File path to dataset containing groups labels
486
487 ref_seq : str
488 File path to dataset containing genome sequence file
489
490 intervals : str
491 File path to dataset containing interval file
492
493 targets : str
494 File path to dataset compressed target bed file
495
496 fasta_path : str
497 File path to dataset containing fasta file
498 """
499 warnings.simplefilter("ignore")
500
501 # store read dataframe object
502 loaded_df = {}
503
504 with open(inputs, "r") as param_handler:
505 params = json.load(param_handler)
506
507 # Override the refit parameter
508 params["search_schemes"]["options"]["refit"] = (
509 True if params["save"] != "nope" else False
510 )
511
512 with open(infile_estimator, "rb") as estimator_handler:
513 estimator = load_model(estimator_handler)
514
515 optimizer = params["search_schemes"]["selected_search_scheme"]
516 optimizer = getattr(model_selection, optimizer)
517
518 # handle gridsearchcv options
519 options = params["search_schemes"]["options"]
520
521 if groups:
522 header = (
523 "infer" if (options["cv_selector"]["groups_selector"]["header_g"]) else None
524 )
525 column_option = options["cv_selector"]["groups_selector"][
526 "column_selector_options_g"
527 ]["selected_column_selector_option_g"]
528 if column_option in [
529 "by_index_number",
530 "all_but_by_index_number",
531 "by_header_name",
532 "all_but_by_header_name",
533 ]:
534 c = options["cv_selector"]["groups_selector"]["column_selector_options_g"][
535 "col_g"
536 ]
537 else:
538 c = None
539
540 df_key = groups + repr(header)
541
542 groups = pd.read_csv(groups, sep="\t", header=header, parse_dates=True)
543 loaded_df[df_key] = groups
544
545 groups = read_columns(
546 groups,
547 c=c,
548 c_option=column_option,
549 sep="\t",
550 header=header,
551 parse_dates=True,
552 )
553 groups = groups.ravel()
554 options["cv_selector"]["groups_selector"] = groups
555
556 splitter, groups = get_cv(options.pop("cv_selector"))
557 options["cv"] = splitter
558 primary_scoring = options["scoring"]["primary_scoring"]
559 # get_scoring() expects secondary_scoring to be a comma separated string (not a list)
560 # Check if secondary_scoring is specified
561 secondary_scoring = options["scoring"].get("secondary_scoring", None)
562 if secondary_scoring is not None:
563 # If secondary_scoring is specified, convert the list into comman separated string
564 options["scoring"]["secondary_scoring"] = ",".join(
565 options["scoring"]["secondary_scoring"]
566 )
567 options["scoring"] = get_scoring(options["scoring"])
568 if options["error_score"]:
569 options["error_score"] = "raise"
570 else:
571 options["error_score"] = np.nan
572 if options["refit"] and isinstance(options["scoring"], dict):
573 options["refit"] = primary_scoring
574 if "pre_dispatch" in options and options["pre_dispatch"] == "":
575 options["pre_dispatch"] = None
576
577 params_builder = params["search_schemes"]["search_params_builder"]
578 param_grid = _eval_search_params(params_builder)
579
580 estimator = clean_params(estimator)
581
582 # save the SearchCV object without fit
583 if params["save"] == "save_no_fit":
584 searcher = optimizer(estimator, param_grid, **options)
585 print(searcher)
586 with open(outfile_object, "wb") as output_handler:
587 pickle.dump(searcher, output_handler, pickle.HIGHEST_PROTOCOL)
588 return 0
589
590 # read inputs and loads new attributes, like paths
591 estimator, X, y = _handle_X_y(
592 estimator,
593 params,
594 infile1,
595 infile2,
596 loaded_df=loaded_df,
597 ref_seq=ref_seq,
598 intervals=intervals,
599 targets=targets,
600 fasta_path=fasta_path,
601 )
602
603 # cache iraps_core fits could increase search speed significantly
604 memory = joblib.Memory(location=CACHE_DIR, verbose=0)
605 main_est = get_main_estimator(estimator)
606 if main_est.__class__.__name__ == "IRAPSClassifier":
607 main_est.set_params(memory=memory)
608
609 searcher = optimizer(estimator, param_grid, **options)
610
611 split_mode = params["outer_split"].pop("split_mode")
612
613 if split_mode == "nested_cv":
614 # make sure refit is choosen
615 # this could be True for sklearn models, but not the case for
616 # deep learning models
617 if not options["refit"] and not all(
618 hasattr(estimator, attr) for attr in ("config", "model_type")
619 ):
620 warnings.warn("Refit is change to `True` for nested validation!")
621 setattr(searcher, "refit", True)
622
623 outer_cv, _ = get_cv(params["outer_split"]["cv_selector"])
624 # nested CV, outer cv using cross_validate
625 if options["error_score"] == "raise":
626 rval = cross_validate(
627 searcher,
628 X,
629 y,
630 scoring=options["scoring"],
631 cv=outer_cv,
632 n_jobs=N_JOBS,
633 verbose=options["verbose"],
634 return_estimator=(params["save"] == "save_estimator"),
635 error_score=options["error_score"],
636 return_train_score=True,
637 )
638 else:
639 warnings.simplefilter("always", FitFailedWarning)
640 with warnings.catch_warnings(record=True) as w:
641 try:
642 rval = cross_validate(
643 searcher,
644 X,
645 y,
646 scoring=options["scoring"],
647 cv=outer_cv,
648 n_jobs=N_JOBS,
649 verbose=options["verbose"],
650 return_estimator=(params["save"] == "save_estimator"),
651 error_score=options["error_score"],
652 return_train_score=True,
653 )
654 except ValueError:
655 pass
656 for warning in w:
657 print(repr(warning.message))
658
659 fitted_searchers = rval.pop("estimator", [])
660 if fitted_searchers:
661 import os
662
663 pwd = os.getcwd()
664 save_dir = os.path.join(pwd, "cv_results_in_folds")
665 try:
666 os.mkdir(save_dir)
667 for idx, obj in enumerate(fitted_searchers):
668 target_name = "cv_results_" + "_" + "split%d" % idx
669 target_path = os.path.join(pwd, save_dir, target_name)
670 cv_results_ = getattr(obj, "cv_results_", None)
671 if not cv_results_:
672 print("%s is not available" % target_name)
673 continue
674 cv_results_ = pd.DataFrame(cv_results_)
675 cv_results_ = cv_results_[sorted(cv_results_.columns)]
676 cv_results_.to_csv(target_path, sep="\t", header=True, index=False)
677 except Exception as e:
678 print(e)
679 finally:
680 del os
681
682 keys = list(rval.keys())
683 for k in keys:
684 if k.startswith("test"):
685 rval["mean_" + k] = np.mean(rval[k])
686 rval["std_" + k] = np.std(rval[k])
687 if k.endswith("time"):
688 rval.pop(k)
689 rval = pd.DataFrame(rval)
690 rval = rval[sorted(rval.columns)]
691 rval.to_csv(path_or_buf=outfile_result, sep="\t", header=True, index=False)
692 # deprecate train test split mode
693 """searcher = _do_train_test_split_val(
694 searcher, X, y, params,
695 primary_scoring=primary_scoring,
696 error_score=options['error_score'],
697 groups=groups,
698 outfile=outfile_result)"""
699 return 0
700
701 # no outer split
702 else:
703 searcher.set_params(n_jobs=N_JOBS)
704 if options["error_score"] == "raise":
705 searcher.fit(X, y, groups=groups)
706 else:
707 warnings.simplefilter("always", FitFailedWarning)
708 with warnings.catch_warnings(record=True) as w:
709 try:
710 searcher.fit(X, y, groups=groups)
711 except ValueError:
712 pass
713 for warning in w:
714 print(repr(warning.message))
715
716 cv_results = pd.DataFrame(searcher.cv_results_)
717 cv_results = cv_results[sorted(cv_results.columns)]
718 cv_results.to_csv(
719 path_or_buf=outfile_result, sep="\t", header=True, index=False
720 )
721
722 memory.clear(warn=False)
723
724 # output best estimator, and weights if applicable
725 if outfile_object:
726 best_estimator_ = getattr(searcher, "best_estimator_", None)
727 if not best_estimator_:
728 warnings.warn(
729 "GridSearchCV object has no attribute "
730 "'best_estimator_', because either it's "
731 "nested gridsearch or `refit` is False!"
732 )
733 return
734
735 # clean prams
736 best_estimator_ = clean_params(best_estimator_)
737
738 main_est = get_main_estimator(best_estimator_)
739
740 if hasattr(main_est, "model_") and hasattr(main_est, "save_weights"):
741 if outfile_weights:
742 main_est.save_weights(outfile_weights)
743 del main_est.model_
744 del main_est.fit_params
745 del main_est.model_class_
746 del main_est.validation_data
747 if getattr(main_est, "data_generator_", None):
748 del main_est.data_generator_
749
750 with open(outfile_object, "wb") as output_handler:
751 print("Best estimator is saved: %s " % repr(best_estimator_))
752 pickle.dump(best_estimator_, output_handler, pickle.HIGHEST_PROTOCOL)
753
754
755 if __name__ == "__main__":
756 aparser = argparse.ArgumentParser()
757 aparser.add_argument("-i", "--inputs", dest="inputs", required=True)
758 aparser.add_argument("-e", "--estimator", dest="infile_estimator")
759 aparser.add_argument("-X", "--infile1", dest="infile1")
760 aparser.add_argument("-y", "--infile2", dest="infile2")
761 aparser.add_argument("-O", "--outfile_result", dest="outfile_result")
762 aparser.add_argument("-o", "--outfile_object", dest="outfile_object")
763 aparser.add_argument("-w", "--outfile_weights", dest="outfile_weights")
764 aparser.add_argument("-g", "--groups", dest="groups")
765 aparser.add_argument("-r", "--ref_seq", dest="ref_seq")
766 aparser.add_argument("-b", "--intervals", dest="intervals")
767 aparser.add_argument("-t", "--targets", dest="targets")
768 aparser.add_argument("-f", "--fasta_path", dest="fasta_path")
769 args = aparser.parse_args()
770
771 main(
772 args.inputs,
773 args.infile_estimator,
774 args.infile1,
775 args.infile2,
776 args.outfile_result,
777 outfile_object=args.outfile_object,
778 outfile_weights=args.outfile_weights,
779 groups=args.groups,
780 ref_seq=args.ref_seq,
781 intervals=args.intervals,
782 targets=args.targets,
783 fasta_path=args.fasta_path,
784 )