-
Notifications
You must be signed in to change notification settings - Fork 2
Allow fit methods to accept pd.Series and pd.DataFrame (#62) #92
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
163cdd9
Allow fit methods to accept pd.Series and pd.DataFrame (#62)
okiner-3 a774b20
Add dev dependencies for CI
okiner-3 931d926
Fix type hints
okiner-3 01beb81
Add a test for utils
okiner-3 b4c441c
Fix docstrings of fit() methods
okiner-3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| import unittest | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use test_utils.py?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's fine to test _convert_to_ndarray only. Alternativel, we can add a test case for each estimator class. |
||
| import numpy as np | ||
| import pandas as pd | ||
| from unittest.mock import MagicMock | ||
| from sklearn.linear_model import LogisticRegression | ||
| from dte_adj import ( | ||
| SimpleDistributionEstimator, | ||
| AdjustedDistributionEstimator, | ||
| SimpleStratifiedDistributionEstimator, | ||
| AdjustedStratifiedDistributionEstimator, | ||
| SimpleLocalDistributionEstimator, | ||
| AdjustedLocalDistributionEstimator, | ||
| ) | ||
|
|
||
|
|
||
| class TestPandasInputSimple(unittest.TestCase): | ||
| """Test that Simple/Adjusted DistributionEstimator accept pandas inputs.""" | ||
|
|
||
| def setUp(self): | ||
| np.random.seed(42) | ||
| n = 20 | ||
| self.covariates_df = pd.DataFrame(np.zeros((n, 5)), columns=[f"x{i}" for i in range(5)]) | ||
| self.treatment_arms_series = pd.Series(np.hstack([np.zeros(10), np.ones(10)])) | ||
| self.outcomes_series = pd.Series(np.arange(n, dtype=float)) | ||
|
|
||
| def test_simple_estimator_with_dataframe_and_series(self): | ||
| estimator = SimpleDistributionEstimator() | ||
| result = estimator.fit( | ||
| self.covariates_df, self.treatment_arms_series, self.outcomes_series | ||
| ) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
|
|
||
| def test_simple_estimator_predict_after_pandas_fit(self): | ||
| estimator = SimpleDistributionEstimator() | ||
| estimator.fit(self.covariates_df, self.treatment_arms_series, self.outcomes_series) | ||
|
|
||
| output = estimator.predict(0, np.array([3, 6])) | ||
| expected = np.array([0.4, 0.7]) | ||
| np.testing.assert_array_almost_equal(output, expected, decimal=2) | ||
|
|
||
| def test_adjusted_estimator_with_dataframe_and_series(self): | ||
| base_model = MagicMock() | ||
| base_model.predict_proba.side_effect = lambda x, y: x | ||
| estimator = AdjustedDistributionEstimator(base_model, folds=2) | ||
| result = estimator.fit( | ||
| self.covariates_df, self.treatment_arms_series, self.outcomes_series | ||
| ) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
|
|
||
|
|
||
| class TestPandasInputStratified(unittest.TestCase): | ||
| """Test that Stratified estimators accept pandas inputs.""" | ||
|
|
||
| def setUp(self): | ||
| np.random.seed(42) | ||
| n = 100 | ||
| self.covariates_df = pd.DataFrame( | ||
| np.random.randn(n, 5), columns=[f"x{i}" for i in range(5)] | ||
| ) | ||
| self.treatment_arms_series = pd.Series(np.random.choice([0, 1], size=n)) | ||
| self.outcomes_series = pd.Series(np.random.randn(n)) | ||
| self.strata_series = pd.Series(np.random.choice([0, 1, 2], size=n)) | ||
|
|
||
| def test_simple_stratified_with_pandas(self): | ||
| estimator = SimpleStratifiedDistributionEstimator() | ||
| result = estimator.fit( | ||
| self.covariates_df, | ||
| self.treatment_arms_series, | ||
| self.outcomes_series, | ||
| self.strata_series, | ||
| ) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
| self.assertIsInstance(result.strata, np.ndarray) | ||
|
|
||
| def test_adjusted_stratified_with_pandas(self): | ||
| base_model = LogisticRegression(random_state=42) | ||
| estimator = AdjustedStratifiedDistributionEstimator(base_model, folds=2) | ||
| result = estimator.fit( | ||
| self.covariates_df, | ||
| self.treatment_arms_series, | ||
| self.outcomes_series, | ||
| self.strata_series, | ||
| ) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
| self.assertIsInstance(result.strata, np.ndarray) | ||
|
|
||
|
|
||
| class TestPandasInputLocal(unittest.TestCase): | ||
| """Test that Local estimators accept pandas inputs.""" | ||
|
|
||
| def setUp(self): | ||
| np.random.seed(42) | ||
| n = 100 | ||
| self.covariates_df = pd.DataFrame( | ||
| np.random.randn(n, 3), columns=[f"x{i}" for i in range(3)] | ||
| ) | ||
| self.treatment_arms_series = pd.Series(np.random.choice([0, 1], size=n)) | ||
| self.treatment_indicator_series = pd.Series(np.random.choice([0, 1], size=n)) | ||
| self.outcomes_series = pd.Series(np.random.randn(n)) | ||
| self.strata_series = pd.Series(np.random.choice([0, 1], size=n)) | ||
|
|
||
| def test_simple_local_with_pandas(self): | ||
| estimator = SimpleLocalDistributionEstimator() | ||
| result = estimator.fit( | ||
| self.covariates_df, | ||
| self.treatment_arms_series, | ||
| self.treatment_indicator_series, | ||
| self.outcomes_series, | ||
| self.strata_series, | ||
| ) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.treatment_indicator, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
| self.assertIsInstance(result.strata, np.ndarray) | ||
|
|
||
| def test_adjusted_local_with_pandas(self): | ||
| base_model = LogisticRegression(random_state=42) | ||
| estimator = AdjustedLocalDistributionEstimator(base_model=base_model) | ||
| result = estimator.fit( | ||
| self.covariates_df, | ||
| self.treatment_arms_series, | ||
| self.treatment_indicator_series, | ||
| self.outcomes_series, | ||
| self.strata_series, | ||
| ) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.treatment_indicator, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
| self.assertIsInstance(result.strata, np.ndarray) | ||
|
|
||
|
|
||
| class TestNumpyInputStillWorks(unittest.TestCase): | ||
| """Verify that np.ndarray inputs continue to work as before.""" | ||
|
|
||
| def test_simple_estimator_with_numpy(self): | ||
| estimator = SimpleDistributionEstimator() | ||
| covariates = np.zeros((20, 5)) | ||
| treatment_arms = np.hstack([np.zeros(10), np.ones(10)]) | ||
| outcomes = np.arange(20, dtype=float) | ||
|
|
||
| result = estimator.fit(covariates, treatment_arms, outcomes) | ||
|
|
||
| self.assertIsInstance(result.covariates, np.ndarray) | ||
| self.assertIsInstance(result.treatment_arms, np.ndarray) | ||
| self.assertIsInstance(result.outcomes, np.ndarray) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we update the type hint to Dataframe, ndarray and Series only?