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