scikit-learn / scikit-learn
Showing 1 of 1 files from the diff.

@@ -0,0 +1,103 @@
Loading
1 +
import tempfile
2 +
from subprocess import check_output, STDOUT, CalledProcessError
3 +
import os
4 +
import os.path as op
5 +
import sys
6 +
import textwrap
7 +
from subprocess import TimeoutExpired
8 +
9 +
10 +
TIMEOUT = 60
11 +
12 +
13 +
def _make_cwd_env():
14 +
    """Helper to prepare environment for the child processes"""
15 +
    # This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle
16 +
    cloudpickle_repo_folder = op.normpath(
17 +
        op.join(op.dirname(__file__), '..'))
18 +
    env = os.environ.copy()
19 +
    pythonpath = "{src}{sep}tests{pathsep}{src}".format(
20 +
        src=cloudpickle_repo_folder, sep=os.sep, pathsep=os.pathsep)
21 +
    env['PYTHONPATH'] = pythonpath
22 +
    return cloudpickle_repo_folder, env
23 +
24 +
25 +
def assert_run_python_script(source_code, timeout=TIMEOUT):
26 +
    """Utility to help check pickleability of objects defined in __main__
27 +
28 +
    The script provided in the source code should return 0 and not print
29 +
    anything on stderr or stdout.
30 +
31 +
    This is a port from cloudpickle https://github.com/cloudpipe/cloudpickle
32 +
    """
33 +
    fd, source_file = tempfile.mkstemp(suffix='_src_test_sklearn.py')
34 +
    os.close(fd)
35 +
    try:
36 +
        with open(source_file, 'wb') as f:
37 +
            f.write(source_code.encode('utf-8'))
38 +
        cmd = [sys.executable, source_file]
39 +
        cwd, env = _make_cwd_env()
40 +
        kwargs = {
41 +
            'cwd': cwd,
42 +
            'stderr': STDOUT,
43 +
            'env': env,
44 +
        }
45 +
        # If coverage is running, pass the config file to the subprocess
46 +
        coverage_rc = os.environ.get("COVERAGE_PROCESS_START")
47 +
        if coverage_rc:
48 +
            kwargs['env']['COVERAGE_PROCESS_START'] = coverage_rc
49 +
50 +
        kwargs['timeout'] = timeout
51 +
        try:
52 +
            try:
53 +
                out = check_output(cmd, **kwargs)
54 +
            except CalledProcessError as e:
55 +
                raise RuntimeError(u"script errored with output:\n%s"
56 +
                                   % e.output.decode('utf-8'))
57 +
            if out != b"":
58 +
                raise AssertionError(out.decode('utf-8'))
59 +
        except TimeoutExpired as e:
60 +
            raise RuntimeError(u"script timeout, output so far:\n%s"
61 +
                               % e.output.decode('utf-8'))
62 +
    finally:
63 +
        os.unlink(source_file)
64 +
65 +
66 +
def test_imports_strategies():
67 +
    # Make sure different import strategies work or fail as expected.
68 +
69 +
    # Since Python caches the imported modules, we need to run a child process
70 +
    # for every test case. Else, the tests would not be independent
71 +
    # (manually removing the imports from the cache (sys.modules) is not
72 +
    # recommended and can lead to many complications).
73 +
74 +
    good_import = """
75 +
    from sklearn.experimental import enable_hist_gradient_boosting
76 +
    from sklearn.ensemble import GradientBoostingClassifier
77 +
    from sklearn.ensemble import GradientBoostingRegressor
78 +
    """
79 +
    assert_run_python_script(textwrap.dedent(good_import))
80 +
81 +
    good_import_with_ensemble_first = """
82 +
    import sklearn.ensemble
83 +
    from sklearn.experimental import enable_hist_gradient_boosting
84 +
    from sklearn.ensemble import GradientBoostingClassifier
85 +
    from sklearn.ensemble import GradientBoostingRegressor
86 +
    """
87 +
    assert_run_python_script(textwrap.dedent(good_import_with_ensemble_first))
88 +
89 +
    bad_imports = """
90 +
    import pytest
91 +
92 +
    with pytest.raises(ImportError):
93 +
        from sklearn.ensemble import HistGradientBoostingClassifier
94 +
95 +
    with pytest.raises(ImportError):
96 +
        from sklearn.ensemble._hist_gradient_boosting import (
97 +
            HistGradientBoostingClassifier)
98 +
99 +
    import sklearn.experimental
100 +
    with pytest.raises(ImportError):
101 +
        from sklearn.ensemble import HistGradientBoostingClassifier
102 +
    """
103 +
    assert_run_python_script(textwrap.dedent(bad_imports))
Files Coverage
sklearn 96.69%
setup.py 7.02%
Project Totals (384 files) 96.61%
Untitled
Untitled
Untitled
Untitled
1
comment: false
2

3
coverage:
4
  status:
5
    project:
6
      default:
7
        # Commits pushed to master should not make the overall
8
        # project coverage decrease by more than 1%:
9
        target: auto
10
        threshold: 1%
11
    patch:
12
      default:
13
        # Be tolerant on slight code coverage diff on PRs to limit
14
        # noisy red coverage status on github PRs.
15
        # Note The coverage stats are still uploaded
16
        # to codecov so that PR reviewers can see uncovered lines
17
        # in the github diff if they install the codecov browser
18
        # extension:
19
        # https://github.com/codecov/browser-extension
20
        target: auto
21
        threshold: 1%
22

23
ignore:
24
- "sklearn/externals"
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading