Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profiling Unit Tests #575

Merged
merged 34 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
fa4e28e
removes nested for loops; reduces n to 500
Feb 17, 2022
a4eb09f
linting fixes
Feb 17, 2022
dc47b6a
reorganize imports
Feb 17, 2022
8e4682f
removes unused imports
Feb 23, 2022
2cb7f40
runs tree tests serially
Feb 23, 2022
0cccf37
fixes typo
Feb 23, 2022
684e1c7
linting fix
Feb 23, 2022
0b951fd
reorganize imports
Feb 23, 2022
d1068c0
run test serially
Feb 23, 2022
33c580f
adds tags for cate_api tests
Feb 23, 2022
caba5ce
sets up new test marks in azure pipelin
Feb 23, 2022
a588897
fixes typo in pipeline yml
Feb 23, 2022
0da6860
debug pipeline
Feb 23, 2022
fd5b3e2
reorganize imports
Feb 24, 2022
ae7086b
adds back dependency
Feb 24, 2022
57afa35
remove unused imports
Feb 24, 2022
64c0c7f
reduce size of matrix by half
Feb 24, 2022
3d7340d
reverts n
Feb 24, 2022
194743e
moves test_statsmodels to serial testing
Feb 24, 2022
ac28cf0
combines other tests to single job
Feb 25, 2022
2ac1aa6
removes nested loops from test_random_state
Feb 25, 2022
78c0391
fixes indenting
Feb 25, 2022
b6b6f49
tries running causal tests in parallel
Feb 25, 2022
b36a2cb
remove unnecessary deps
Feb 25, 2022
1ea012b
linting fix
Feb 25, 2022
7664f75
reverts to run causal tests serially
Feb 25, 2022
b94f454
correctly tags stats models
Feb 25, 2022
e943f99
re-enables pickling test
Mar 8, 2022
fea98c5
uncomments options
Mar 8, 2022
691ea6e
reorganizes unit test workflow
Mar 8, 2022
01c8001
Consolidate test marks
kbattocchi Mar 30, 2022
6ad3315
Update numpy test dependency
kbattocchi Apr 1, 2022
564af9e
Revert numpy version
kbattocchi Apr 4, 2022
96d7292
Remove workaround for downlevel shap
kbattocchi Apr 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ jobs:
# Work around https://github.com/pypa/pip/issues/9542
- script: 'pip install -U numpy~=1.21.0'
displayName: 'Upgrade numpy'

- script: 'pip install pytest pytest-runner jupyter jupyter-client nbconvert nbformat seaborn xgboost tqdm && python setup.py pytest'
- script: 'pip install pytest pytest-runner jupyter jupyter-client nbconvert nbformat seaborn xgboost tqdm && pip list && python setup.py pytest'
displayName: 'Unit tests'
env:
PYTEST_ADDOPTS: '-m "notebook"'
Expand All @@ -126,12 +126,6 @@ jobs:
# Work around https://github.com/pypa/pip/issues/9542
- script: 'pip install -U numpy~=1.21.0'
displayName: 'Upgrade numpy'

# shap 0.39 and sklearn 1.0 interact badly in these notebooks
# shap 0.40 has a bug in waterfall (https://github.com/slundberg/shap/issues/2283) that breaks our main tests
# but fixes the interaction here...
- script: 'pip install -U shap~=0.40.0'
displayName: 'Upgrade shap'

- script: 'pip install pytest pytest-runner jupyter jupyter-client nbconvert nbformat seaborn xgboost tqdm && python setup.py pytest'
displayName: 'Unit tests'
Expand Down Expand Up @@ -207,7 +201,7 @@ jobs:
- script: 'pip install pytest pytest-runner && python setup.py pytest'
displayName: 'Unit tests'
env:
PYTEST_ADDOPTS: '-m "not (notebook or automl or dml or causal)" -n 2'
PYTEST_ADDOPTS: '-m "not (notebook or automl or dml or serial or cate_api)" -n 2'
COVERAGE_PROCESS_START: 'setup.cfg'
- task: PublishTestResults@2
displayName: 'Publish Test Results **/test-results.xml'
Expand Down Expand Up @@ -253,15 +247,44 @@ jobs:
parameters:
package: '-e .[tf,plt]'
job:
job: Tests_causal
job: Tests_serial
dependsOn: 'EvalChanges'
condition: eq(dependencies.EvalChanges.outputs['output.testCode'], 'True')
displayName: 'Run tests (Causal)'
displayName: 'Run tests (Serial)'
steps:
- script: 'pip install pytest pytest-runner && python setup.py pytest'
displayName: 'Unit tests'
env:
PYTEST_ADDOPTS: '-m "causal" -n 1'
PYTEST_ADDOPTS: '-m "serial" -n 1'
COVERAGE_PROCESS_START: 'setup.cfg'
- task: PublishTestResults@2
displayName: 'Publish Test Results **/test-results.xml'
inputs:
testResultsFiles: '**/test-results.xml'
testRunTitle: 'Python $(python.version), image $(imageName)'
condition: succeededOrFailed()

- task: PublishCodeCoverageResults@1
displayName: 'Publish Code Coverage Results'
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'

- template: azure-pipelines-steps.yml
parameters:
package: '-e .[tf,plt]'
job:
job: Tests_CATE_API
dependsOn: 'EvalChanges'
condition: eq(dependencies.EvalChanges.outputs['output.testCode'], 'True')
displayName: 'Run tests (Other)'
steps:
- script: 'pip install pytest pytest-runner'
displayName: 'Install pytest'
- script: 'python setup.py pytest'
displayName: 'CATE Unit tests'
env:
PYTEST_ADDOPTS: '-m "cate_api" -n auto'
COVERAGE_PROCESS_START: 'setup.cfg'
- task: PublishTestResults@2
displayName: 'Publish Test Results **/test-results.xml'
Expand Down
37 changes: 22 additions & 15 deletions econml/tests/test_causal_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
# Licensed under the MIT License.

import unittest

from contextlib import ExitStack
import itertools
import numpy as np
from numpy.core.fromnumeric import squeeze
import pandas as pd
from contextlib import ExitStack
import pytest

from econml.solutions.causal_analysis import CausalAnalysis
from econml.solutions.causal_analysis._causal_analysis import _CausalInsightsConstants

Expand All @@ -15,7 +18,7 @@ def assert_less_close(arr1, arr2):
assert np.all(np.logical_or(arr1 <= arr2, np.isclose(arr1, arr2)))


@pytest.mark.causal
@pytest.mark.serial
class TestCausalAnalysis(unittest.TestCase):

def test_basic_array(self):
Expand Down Expand Up @@ -670,21 +673,24 @@ def test_random_state(self):
inds = [0, 1, 2, 3]
cats = [2, 3]
hinds = [0, 3]
for n_model in ['linear', 'automl']:
for h_model in ['linear', 'forest']:
for classification in [True, False]:
ca = CausalAnalysis(inds, cats, hinds, classification=classification,
nuisance_models=n_model, heterogeneity_model=h_model, random_state=123)
ca.fit(X, y)
glo = ca.global_causal_effect()

ca2 = CausalAnalysis(inds, cats, hinds, classification=classification,
nuisance_models=n_model, heterogeneity_model=h_model, random_state=123)
ca2.fit(X, y)
glo2 = ca.global_causal_effect()
for n_model, h_model, classification in\
itertools.product(['linear', 'automl'],
['linear', 'forest'],
[True, False]):
kbattocchi marked this conversation as resolved.
Show resolved Hide resolved

np.testing.assert_equal(glo.point.values, glo2.point.values)
np.testing.assert_equal(glo.stderr.values, glo2.stderr.values)
ca = CausalAnalysis(inds, cats, hinds, classification=classification,
nuisance_models=n_model, heterogeneity_model=h_model, random_state=123)
ca.fit(X, y)
glo = ca.global_causal_effect()

ca2 = CausalAnalysis(inds, cats, hinds, classification=classification,
nuisance_models=n_model, heterogeneity_model=h_model, random_state=123)
ca2.fit(X, y)
glo2 = ca.global_causal_effect()

np.testing.assert_equal(glo.point.values, glo2.point.values)
np.testing.assert_equal(glo.stderr.values, glo2.stderr.values)

def test_can_set_categories(self):
y = pd.Series(np.random.choice([0, 1], size=(500,)))
Expand Down Expand Up @@ -784,6 +790,7 @@ def test_invalid_inds(self):
# Pass an example where W is irrelevant and X is confounder
# As long as DML doesnt change the order of the inputs, then things should be good. Otherwise X would be
# zeroed out and the test will fail

def test_scaling_transforms(self):
# shouldn't matter if X is scaled much larger or much smaller than W, we should still get good estimates
n = 2000
Expand Down
13 changes: 8 additions & 5 deletions econml/tests/test_dmliv.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import unittest
import pytest
import pickle
import unittest

import numpy as np
import pytest
from scipy import special
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.preprocessing import PolynomialFeatures

from econml.iv.dml import OrthoIV, DMLIV, NonParamDMLIV
from econml.iv.dr._dr import _DummyCATE
from econml.sklearn_extensions.linear_model import StatsModelsLinearRegression
from sklearn.preprocessing import PolynomialFeatures
from econml.utilities import shape
from econml.iv.dml import OrthoIV, DMLIV, NonParamDMLIV


@pytest.mark.cate_api
class TestDMLIV(unittest.TestCase):
def test_cate_api(self):
def const_marg_eff_shape(n, d_x, d_y, binary_T):
Expand Down
Loading