2

在我当前的项目中,当我运行测试时(使用pytest),我得到了这个输出(除了其他):

ml_framework/tests/test_impute.py: 8 warnings
ml_framework/tests/test_transform_pipeline.py: 9 warnings
ml_framework/tests/test_data_transforms.py: 27 warnings
ml_framework/tests/test_jsonizer.py: 4 warnings
  /home/sondracek/anaconda3/envs/p3.8/lib/python3.8/site-packages/sklearn/utils/validation.py:70: FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error
    warnings.warn(f"Pass {args_msg} as keyword args. From version "

FutureWarning很清楚——从 1.0 版(我0.24目前正在使用)开始,一些参数将发生变化,我将无法standardize作为位置参数传递。

但问题是 -我怎样才能找到我应该在哪里将此位置参数更改为关键字?

我可以去validation.py警告输出中提到的那个,但是有一个通用_deprecate_positional_args函数没有告诉我哪个代码调用它。

在这种特殊情况下,我猜这将来自我的代码中使用的PowerTransformer导入sklearn.preprocessing,具有standardize参数并在列出的测试中进行了测试。有没有一种通用的方法可以轻松找到原因FutureWarning还是我需要检查所有代码并尝试以某种方式找到它?

4

1 回答 1

1

Pytest 让您将 aFutureWarning作为错误引发,这样它指向引发警告的代码位置,最简单的方法是通过警告标志

pytest -W error::FutureWarning test_script.py

test_script.py通过pytestmark在里面

pytestmark = pytest.mark.filterwarnings("error::FutureWarning")

您可以在此处此处阅读有关指定对警告采取的操作的更多信息。

例子

考虑以下虚拟示例test_transform.py脚本

# test_transform.py
import pytest


pytestmark = pytest.mark.filterwarnings("error::FutureWarning")


def test_transformer_boxcox():

    from sklearn.preprocessing import PowerTransformer
    
    pt = PowerTransformer('box-cox', False)
    
    assert True
    

def test_transformer_yeo_johnson():

    from sklearn.preprocessing import PowerTransformer
    
    pt = PowerTransformer('yeo-johnson', False)
    
    assert True

并在终端上运行

pytest test_transform.py > logs.log

日志向我展示了PowerTransformer引发FutureWarning.

===================================================== test session starts =====================================================
platform linux -- Python 3.7.6, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /home/jovyan/work
collected 2 items

test_transform.py FF                                                                                                    [100%]

========================================================== FAILURES ===========================================================
___________________________________________________ test_transformer_boxcox ___________________________________________________

    def test_transformer_boxcox():
    
        from sklearn.preprocessing import PowerTransformer
    
>       pt = PowerTransformer('box-cox', False)

test_transform.py:11: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

args = (<[AttributeError("'PowerTransformer' object has no attribute 'copy'") raised in repr()] PowerTransformer object at 0x7fa0bda14d50>, 'box-cox', False)
kwargs = {}, extra_args = 1, args_msg = 'standardize=False'

    @wraps(f)
    def inner_f(*args, **kwargs):
        extra_args = len(args) - len(all_args)
        if extra_args <= 0:
            return f(*args, **kwargs)
    
        # extra_args > 0
        args_msg = ['{}={}'.format(name, arg)
                    for name, arg in zip(kwonly_args[:extra_args],
                                         args[-extra_args:])]
        args_msg = ", ".join(args_msg)
        warnings.warn(f"Pass {args_msg} as keyword args. From version "
                      f"{version} passing these as positional arguments "
>                     "will result in an error", FutureWarning)
E       FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error

/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:72: FutureWarning
________________________________________________ test_transformer_yeo_johnson _________________________________________________

    def test_transformer_yeo_johnson():
    
        from sklearn.preprocessing import PowerTransformer
    
>       pt = PowerTransformer('yeo-johnson', False)

test_transform.py:20: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

args = (<[AttributeError("'PowerTransformer' object has no attribute 'copy'") raised in repr()] PowerTransformer object at 0x7fa0bd02f750>, 'yeo-johnson', False)
kwargs = {}, extra_args = 1, args_msg = 'standardize=False'

    @wraps(f)
    def inner_f(*args, **kwargs):
        extra_args = len(args) - len(all_args)
        if extra_args <= 0:
            return f(*args, **kwargs)
    
        # extra_args > 0
        args_msg = ['{}={}'.format(name, arg)
                    for name, arg in zip(kwonly_args[:extra_args],
                                         args[-extra_args:])]
        args_msg = ", ".join(args_msg)
        warnings.warn(f"Pass {args_msg} as keyword args. From version "
                      f"{version} passing these as positional arguments "
>                     "will result in an error", FutureWarning)
E       FutureWarning: Pass standardize=False as keyword args. From version 1.0 (renaming of 0.25) passing these as positional arguments will result in an error

/opt/conda/lib/python3.7/site-packages/sklearn/utils/validation.py:72: FutureWarning
=================================================== short test summary info ===================================================
FAILED test_transform.py::test_transformer_boxcox - FutureWarning: Pass standardize=False as keyword args. From version 1.0 ...
FAILED test_transform.py::test_transformer_yeo_johnson - FutureWarning: Pass standardize=False as keyword args. From version...
====================================================== 2 failed in 0.71s ======================================================
于 2021-04-01T16:47:50.173 回答