Пример #1
0
def test_lib_logging():
    """Test logging versions"""

    import numpy as np  # noqa
    import scipy  # noqa
    import sklearn  # noqa
    import pandas  # noqa
    with tempfile.TemporaryDirectory() as tmp:
        tmpdir = Path(tmp)
        configure_logging(fname=tmpdir / 'test1.log', level='INFO')
        logger.info('first message')
        with open(tmpdir / 'test1.log') as f:
            lines = f.readlines()
            assert any('numpy' in line for line in lines)
            assert any('scipy' in line for line in lines)
            assert any('sklearn' in line for line in lines)
            assert any('pandas' in line for line in lines)
            assert any('julearn' in line for line in lines)
Пример #2
0
# License: AGPL

import math
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.datasets import load_diabetes
from sklearn.model_selection import KFold

from julearn import run_cross_validation
from julearn.utils import configure_logging
from julearn.model_selection import StratifiedGroupsKFold

###############################################################################
# Set the logging level to info to see extra information
configure_logging(level='INFO')

###############################################################################
# load the diabetes data from sklearn as a pandas dataframe
features, target = load_diabetes(return_X_y=True, as_frame=True)

###############################################################################
# Dataset contains ten variables age, sex, body mass index, average  blood
# pressure, and six blood serum measurements (s1-s6) diabetes patients and
# a quantitative measure of disease progression one year after baseline which
# is the target we are interested in predicting.

print('Features: \n', features.head())
print('Target: \n', target.describe())

###############################################################################
Пример #3
0
def test_log():
    """Simple log test"""
    configure_logging()
    logger.info('Testing')
Пример #4
0
def test_log_file():
    """Test logging to a file"""
    with tempfile.TemporaryDirectory() as tmp:
        tmpdir = Path(tmp)
        configure_logging(fname=tmpdir / 'test1.log')
        logger.debug('Debug message')
        logger.info('Info message')
        logger.warning('Warn message')
        logger.error('Error message')
        _close_handlers(logger)
        with open(tmpdir / 'test1.log') as f:
            lines = f.readlines()
            assert not any('Debug message' in line for line in lines)
            assert not any('Info message' in line for line in lines)
            assert any('Warn message' in line for line in lines)
            assert any('Error message' in line for line in lines)

        configure_logging(fname=tmpdir / 'test2.log', level='INFO')
        logger.debug('Debug message')
        logger.info('Info message')
        logger.warning('Warn message')
        logger.error('Error message')
        _close_handlers(logger)
        with open(tmpdir / 'test2.log') as f:
            lines = f.readlines()
            assert not any('Debug message' in line for line in lines)
            assert any('Info message' in line for line in lines)
            assert any('Warn message' in line for line in lines)
            assert any('Error message' in line for line in lines)

        configure_logging(fname=tmpdir / 'test3.log', level='WARNING')
        logger.debug('Debug message')
        logger.info('Info message')
        logger.warning('Warn message')
        logger.error('Error message')
        _close_handlers(logger)
        with open(tmpdir / 'test3.log') as f:
            lines = f.readlines()
            assert not any('Debug message' in line for line in lines)
            assert not any('Info message' in line for line in lines)
            assert any('Warn message' in line for line in lines)
            assert any('Error message' in line for line in lines)

        configure_logging(fname=tmpdir / 'test4.log', level='ERROR')
        logger.debug('Debug message')
        logger.info('Info message')
        logger.warning('Warn message')
        logger.error('Error message')
        with open(tmpdir / 'test4.log') as f:
            lines = f.readlines()
            assert not any('Debug message' in line for line in lines)
            assert not any('Info message' in line for line in lines)
            assert not any('Warn message' in line for line in lines)
            assert any('Error message' in line for line in lines)

        with pytest.warns(UserWarning, match='to avoid this message'):
            configure_logging(fname=tmpdir / 'test4.log', level='WARNING')
            logger.debug('Debug2 message')
            logger.info('Info2 message')
            logger.warning('Warn2 message')
            logger.error('Error2 message')
            with open(tmpdir / 'test4.log') as f:
                lines = f.readlines()
                assert not any('Debug message' in line for line in lines)
                assert not any('Info message' in line for line in lines)
                assert not any('Warn message' in line for line in lines)
                assert any('Error message' in line for line in lines)
                assert not any('Debug2 message' in line for line in lines)
                assert not any('Info2 message' in line for line in lines)
                assert any('Warn2 message' in line for line in lines)
                assert any('Error2 message' in line for line in lines)

        configure_logging(fname=tmpdir / 'test4.log',
                          level='WARNING',
                          overwrite=True)
        logger.debug('Debug3 message')
        logger.info('Info3 message')
        logger.warning('Warn3 message')
        logger.error('Error3 message')
        with open(tmpdir / 'test4.log') as f:
            lines = f.readlines()
            assert not any('Debug message' in line for line in lines)
            assert not any('Info message' in line for line in lines)
            assert not any('Warn message' in line for line in lines)
            assert not any('Error message' in line for line in lines)
            assert not any('Debug2 message' in line for line in lines)
            assert not any('Info2 message' in line for line in lines)
            assert not any('Warn2 message' in line for line in lines)
            assert not any('Error2 message' in line for line in lines)
            assert not any('Debug3 message' in line for line in lines)
            assert not any('Info3 message' in line for line in lines)
            assert any('Warn3 message' in line for line in lines)
            assert any('Error3 message' in line for line in lines)

        with pytest.warns(RuntimeWarning, match=r"Warn raised"):
            warn('Warn raised')
        with pytest.raises(ValueError, match=r"Error raised"):
            raise_error('Error raised')
        with open(tmpdir / 'test4.log') as f:
            lines = f.readlines()
            assert any('Warn raised' in line for line in lines)
            assert any('Error raised' in line for line in lines)