def test_message_with_time(source, message, is_long, time, time_str): out = _message_with_time(source, message, time) if is_long: assert len(out) > 70 else: assert len(out) == 70 assert out.startswith('[' + source + '] ') out = out[len(source) + 3:] assert out.endswith(time_str) out = out[:-len(time_str)] assert out.endswith(', total=') out = out[:-len(', total=')] assert out.endswith(message) out = out[:-len(message)] assert out.endswith(' ') out = out[:-1] if is_long: assert not out else: assert list(set(out)) == ['.']
out = out[:-len(', total=')] assert out.endswith(message) out = out[:-len(message)] assert out.endswith(' ') out = out[:-1] if is_long: assert not out else: assert list(set(out)) == ['.'] @pytest.mark.parametrize( ['message', 'expected'], [ ('hello', _message_with_time('ABC', 'hello', 0.1) + '\n'), ('', _message_with_time('ABC', '', 0.1) + '\n'), (None, ''), ]) def test_print_elapsed_time(message, expected, capsys, monkeypatch): monkeypatch.setattr(timeit, 'default_timer', lambda: 0) with _print_elapsed_time('ABC', message): monkeypatch.setattr(timeit, 'default_timer', lambda: 0.1) assert capsys.readouterr().out == expected @pytest.mark.parametrize("value, result", [(float("nan"), True), (np.nan, True), (np.float("nan"), True), (np.float32("nan"), True), (np.float64("nan"), True),
out = out[:-len(", total=")] assert out.endswith(message) out = out[:-len(message)] assert out.endswith(" ") out = out[:-1] if is_long: assert not out else: assert list(set(out)) == ["."] @pytest.mark.parametrize( ["message", "expected"], [ ("hello", _message_with_time("ABC", "hello", 0.1) + "\n"), ("", _message_with_time("ABC", "", 0.1) + "\n"), (None, ""), ], ) def test_print_elapsed_time(message, expected, capsys, monkeypatch): monkeypatch.setattr(timeit, "default_timer", lambda: 0) with _print_elapsed_time("ABC", message): monkeypatch.setattr(timeit, "default_timer", lambda: 0.1) assert capsys.readouterr().out == expected @pytest.mark.parametrize( "value, result", [ (float("nan"), True),
def _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score=False, return_parameters=False, return_n_test_samples=False, return_times=True, return_estimator=False, error_score=np.nan): if verbose > 1: if parameters is None: msg = '' else: msg = '%s' % (', '.join('%s=%s' % (k, v) for k, v in parameters.items())) print("[CV] %s %s" % (msg, (64 - len(msg)) * '.')) # Adjust length of sample weights fit_params = fit_params if fit_params is not None else {} fit_params = _check_fit_params(X, fit_params, train) train_scores = {} if parameters is not None: # clone after setting parameters in case any parameters # are estimators (like pipeline steps) # because pipeline doesn't clone steps in fit cloned_parameters = {} for k, v in parameters.items(): cloned_parameters[k] = clone(v, safe=False) estimator = estimator.set_params(**cloned_parameters) start_time = time.perf_counter() X_train, y_train = _safe_split(estimator, X, y, train) X_test, y_test = _safe_split(estimator, X, y, test, train) try: if y_train is None: estimator.fit(X_train, **fit_params) else: estimator.fit(X_train, y_train, **fit_params) except Exception as e: # Note fit time as time until error fit_time = time.perf_counter() - start_time score_time = 0.0 if error_score == 'raise': raise elif isinstance(error_score, numbers.Number): if isinstance(scorer, dict): test_scores = {name: error_score for name in scorer} if return_train_score: train_scores = test_scores.copy() else: test_scores = error_score if return_train_score: train_scores = error_score warnings.warn( "Estimator fit failed. The score on this train-test" " partition for these parameters will be set to %f. " "Details: \n%s" % (error_score, format_exception_only(type(e), e)[0]), FitFailedWarning) else: raise ValueError("error_score must be the string 'raise' or a" " numeric value. (Hint: if using 'raise', please" " make sure that it has been spelled correctly.)") else: fit_time = time.perf_counter() - start_time test_scores = _score(estimator, X_test, y_test, scorer) score_time = time.perf_counter() - start_time - fit_time if return_train_score: train_scores = _score(estimator, X_train, y_train, scorer) if verbose > 2: if isinstance(test_scores, dict): for scorer_name in sorted(test_scores): msg += ", %s=" % scorer_name if return_train_score: msg += "(train=%.3f," % train_scores[scorer_name] msg += " test=%.3f)" % test_scores[scorer_name] else: msg += "%.3f" % test_scores[scorer_name] else: msg += ", score=" msg += ("%.3f" % test_scores if not return_train_score else "(train=%.3f, test=%.3f)" % (train_scores, test_scores)) total_time = score_time + fit_time if verbose > 1: print(_message_with_time('CV', msg, total_time)) ret = [train_scores, test_scores] if return_train_score else [test_scores] p = pickle.dumps(estimator) mem = sys.getsizeof(p) #in bytes #TODO переделать без костылей #for numpy if (isinstance(X_test, np.ndarray)): inf_example = X_test[0].reshape(1, -1) #for DataFrame if (isinstance(X_test, pd.DataFrame)): inf_example = X_test.iloc[0].to_numpy().reshape(1, -1) #Reshape your data either using array.reshape(-1, 1) #if your data has a single feature or array.reshape(1, -1) if it contains #a single sample. inference_time_start = time.perf_counter() s = estimator.predict(inf_example) inference_time_end = time.perf_counter() inference_time = inference_time_end - inference_time_start if return_n_test_samples: ret.append(_num_samples(X_test)) if return_times: ret.extend([fit_time, score_time, inference_time, mem, total_time]) if return_parameters: ret.append(parameters) if return_estimator: ret.append(estimator) return ret