def test_classifier_gridsearch(cls): pipe = DebugPipeline([("ovrc", cls(LinearSVC(random_state=0, tol=0.1)))]) Cs = [0.1, 0.5, 0.8] cv = GridSearchCV(pipe, {"ovrc__estimator__C": Cs}) cv.fit(IRIS.data, IRIS.target) best_C = cv.best_estimator_.get_params()["ovrc__estimator__C"] assert best_C in Cs
def test_time_in_logs_when_log_callback_is_default(caplog, steps): pipe = DebugPipeline(steps, log_callback='default') caplog.clear() with caplog.at_level(logging.INFO): pipe.fit(IRIS.data, IRIS.target) assert caplog.text, f'Log should be none empty: {caplog.text}' assert f'time=' in caplog.text, f'"time=" should be in: {caplog.text}' assert caplog.text.count('time') == (len(pipe.steps) - 1), \ f'"time" should be {len(pipe.steps) - 1} times in {caplog.text}'
def test_nbytes_in_logs_when_log_callback_is_custom(caplog, steps): pipe = DebugPipeline(steps, log_callback=custom_log_callback) caplog.clear() with caplog.at_level(logging.INFO): pipe.fit(IRIS.data, IRIS.target) assert caplog.text, f'Log should be none empty: {caplog.text}' assert 'nbytes=' in caplog.text, f'"nbytes=" should be in: {caplog.text}' assert caplog.text.count('nbytes=') == (len(pipe.steps) - 1), \ f'"nbytes=" should be {len(pipe.steps) - 1} times in {caplog.text}'
def test_output_shape_in_logs_when_log_callback_is_default(caplog, steps): pipe = DebugPipeline(steps, log_callback='default') caplog.clear() with caplog.at_level(logging.INFO): pipe.fit(IRIS.data, IRIS.target) assert caplog.text, f'Log should be none empty: {caplog.text}' shape_str = f'shape={IRIS.data.shape}' assert shape_str in caplog.text, f'"{shape_str}" should be in {caplog.text}' assert caplog.text.count(shape_str) == (len(pipe.steps) - 1), \ f'"{shape_str}" should be {len(pipe.steps) - 1} times in {caplog.text}'
def test_step_name_in_logs_when_log_callback_is_default(caplog, steps): pipe = DebugPipeline(steps, log_callback='default') caplog.clear() with caplog.at_level(logging.INFO): pipe.fit(IRIS.data, IRIS.target) assert caplog.text, f'Log should be none empty: {caplog.text}' for _, step in pipe.steps[:-1]: assert str(step) in caplog.text, f'{step} should be in: {caplog.text}' assert caplog.text.count(str(step)) == 1, \ f'{step} should be once in {caplog.text}'
def test_feature_union(caplog, steps): pipe_w_default_log_callback = DebugPipeline(steps, log_callback='default') pipe_w_custom_log_callback = DebugPipeline( steps, log_callback=custom_log_callback) pipe_union = FeatureUnion([ ('pipe_w_default_log_callback', pipe_w_default_log_callback), ('pipe_w_custom_log_callback', pipe_w_custom_log_callback), ]) caplog.clear() with caplog.at_level(logging.INFO): pipe_union.fit(IRIS.data, IRIS.target) assert caplog.text, f'Log should be none empty: {caplog.text}' for pipe in [pipe_w_default_log_callback, pipe_w_custom_log_callback]: for _, step in pipe.steps[:-1]: assert str(step) in caplog.text, \ f'{step} should be in: {caplog.text}' assert caplog.text.count(str(step)) == 2, \ f'{step} should be once in {caplog.text}'
def food_recognition_model(**params): pca = PCA(512, whiten=True, random_state=0) return DebugPipeline(steps=[("scale", StandardScaler()), ("pca", pca), ("print_preprocess_stats", PrintPreprocessStats(pca)), ("model", GaussianMixture(n_components=4, covariance_type='full', random_state=0, max_iter=int(1e7)))], log_callback='default').set_params(**params)
def test_no_logs_when_log_callback_is_None(caplog, steps): pipe = DebugPipeline(steps, log_callback=None) caplog.clear() with caplog.at_level(logging.INFO): pipe.fit(IRIS.data, IRIS.target) assert not caplog.text, f'Log should be empty: {caplog.text}'