-
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 4 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| from typing import Tuple | ||
| from dte_adj.stratified import ( | ||
| SimpleStratifiedDistributionEstimator, | ||
| AdjustedStratifiedDistributionEstimator, | ||
| ) | ||
| from dte_adj.util import compute_ldte, compute_lpte | ||
| from dte_adj.util import ArrayLike, compute_ldte, compute_lpte, _convert_to_ndarray | ||
|
|
||
|
|
||
| class SimpleLocalDistributionEstimator(SimpleStratifiedDistributionEstimator): | ||
|
|
@@ -28,25 +30,26 @@ def __init__(self): | |
|
|
||
| def fit( | ||
| self, | ||
| covariates: np.ndarray, | ||
| treatment_arms: np.ndarray, | ||
| treatment_indicator: np.ndarray, | ||
| outcomes: np.ndarray, | ||
| strata: np.ndarray, | ||
| ) -> "SimpleLocalDistributionEstimator": | ||
| covariates: ArrayLike, | ||
| treatment_arms: ArrayLike, | ||
| treatment_indicator: ArrayLike, | ||
| outcomes: ArrayLike, | ||
| strata: ArrayLike, | ||
| ) -> SimpleLocalDistributionEstimator: | ||
| """ | ||
| Train the SimpleLocalDistributionEstimator. | ||
|
|
||
| Args: | ||
| covariates (np.ndarray): Pre-treatment covariates. | ||
| treatment_arms (np.ndarray): Treatment assignment variable (Z). | ||
| treatment_indicator (np.ndarray): Treatment indicator variable (D). | ||
| outcomes (np.ndarray): Scalar-valued observed outcome. | ||
| strata (np.ndarray): Stratum indicators. | ||
| covariates (ArrayLike): Pre-treatment covariates. | ||
|
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. ArrayLike is not a public API; so let's probably remove these types from the docstring, as it's already shown in the type hints. |
||
| treatment_arms (ArrayLike): Treatment assignment variable (Z). | ||
| treatment_indicator (ArrayLike): Treatment indicator variable (D). | ||
| outcomes (ArrayLike): Scalar-valued observed outcome. | ||
| strata (ArrayLike): Stratum indicators. | ||
|
|
||
| Returns: | ||
| SimpleLocalDistributionEstimator: The fitted estimator. | ||
| """ | ||
| treatment_indicator = _convert_to_ndarray(treatment_indicator) | ||
| super().fit(covariates, treatment_arms, outcomes, strata) | ||
| self.treatment_indicator = treatment_indicator | ||
|
|
||
|
|
@@ -196,25 +199,26 @@ class AdjustedLocalDistributionEstimator(AdjustedStratifiedDistributionEstimator | |
|
|
||
| def fit( | ||
| self, | ||
| covariates: np.ndarray, | ||
| treatment_arms: np.ndarray, | ||
| treatment_indicator: np.ndarray, | ||
| outcomes: np.ndarray, | ||
| strata: np.ndarray, | ||
| ) -> "AdjustedLocalDistributionEstimator": | ||
| covariates: ArrayLike, | ||
| treatment_arms: ArrayLike, | ||
| treatment_indicator: ArrayLike, | ||
| outcomes: ArrayLike, | ||
| strata: ArrayLike, | ||
| ) -> AdjustedLocalDistributionEstimator: | ||
| """ | ||
| Train the AdjustedLocalDistributionEstimator. | ||
|
|
||
| Args: | ||
| covariates (np.ndarray): Pre-treatment covariates. | ||
| treatment_arms (np.ndarray): Treatment assignment variable (Z). | ||
| treatment_indicator (np.ndarray): Treatment indicator variable (D). | ||
| outcomes (np.ndarray): Scalar-valued observed outcome. | ||
| strata (np.ndarray): Stratum indicators. | ||
| covariates (ArrayLike): Pre-treatment covariates. | ||
| treatment_arms (ArrayLike): Treatment assignment variable (Z). | ||
| treatment_indicator (ArrayLike): Treatment indicator variable (D). | ||
| outcomes (ArrayLike): Scalar-valued observed outcome. | ||
| strata (ArrayLike): Stratum indicators. | ||
|
|
||
| Returns: | ||
| AdjustedLocalDistributionEstimator: The fitted estimator. | ||
| """ | ||
| treatment_indicator = _convert_to_ndarray(treatment_indicator) | ||
| super().fit(covariates, treatment_arms, outcomes, strata) | ||
| self.treatment_indicator = treatment_indicator | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import numpy as np | ||
| from dte_adj.stratified import ( | ||
| SimpleStratifiedDistributionEstimator, | ||
| AdjustedStratifiedDistributionEstimator, | ||
| ) | ||
| from dte_adj.util import ArrayLike, _convert_to_ndarray | ||
|
|
||
|
|
||
| class SimpleDistributionEstimator(SimpleStratifiedDistributionEstimator): | ||
|
|
@@ -45,19 +48,23 @@ def __init__(self): | |
| super().__init__() | ||
|
|
||
| def fit( | ||
|
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. ditto |
||
| self, covariates: np.ndarray, treatment_arms: np.ndarray, outcomes: np.ndarray | ||
| ) -> "SimpleDistributionEstimator": | ||
| self, covariates: ArrayLike, treatment_arms: ArrayLike, outcomes: ArrayLike | ||
| ) -> SimpleDistributionEstimator: | ||
| """ | ||
| Set parameters. | ||
|
|
||
| Args: | ||
| covariates (np.ndarray): Pre-treatment covariates. | ||
| treatment_arms (np.ndarray): The index of the treatment arm. | ||
| outcomes (np.ndarray): Scalar-valued observed outcome. | ||
| covariates (ArrayLike): Pre-treatment covariates. | ||
| treatment_arms (ArrayLike): The index of the treatment arm. | ||
| outcomes (ArrayLike): Scalar-valued observed outcome. | ||
|
|
||
| Returns: | ||
| SimpleDistributionEstimator: The fitted estimator. | ||
| """ | ||
| covariates = _convert_to_ndarray(covariates) | ||
| treatment_arms = _convert_to_ndarray(treatment_arms) | ||
| outcomes = _convert_to_ndarray(outcomes) | ||
|
|
||
| if covariates.shape[0] != treatment_arms.shape[0]: | ||
| raise ValueError("The shape of covariates and treatment_arm should be same") | ||
|
|
||
|
|
@@ -105,19 +112,23 @@ class AdjustedDistributionEstimator(AdjustedStratifiedDistributionEstimator): | |
| """ | ||
|
|
||
| def fit( | ||
| self, covariates: np.ndarray, treatment_arms: np.ndarray, outcomes: np.ndarray | ||
| ) -> "AdjustedDistributionEstimator": | ||
| self, covariates: ArrayLike, treatment_arms: ArrayLike, outcomes: ArrayLike | ||
| ) -> AdjustedDistributionEstimator: | ||
| """ | ||
| Set parameters. | ||
|
|
||
| Args: | ||
| covariates (np.ndarray): Pre-treatment covariates. | ||
| treatment_arms (np.ndarray): The index of the treatment arm. | ||
| outcomes (np.ndarray): Scalar-valued observed outcome. | ||
| covariates (ArrayLike): Pre-treatment covariates. | ||
| treatment_arms (ArrayLike): The index of the treatment arm. | ||
| outcomes (ArrayLike): Scalar-valued observed outcome. | ||
|
|
||
| Returns: | ||
| AdjustedDistributionEstimator: The fitted estimator. | ||
| """ | ||
| covariates = _convert_to_ndarray(covariates) | ||
| treatment_arms = _convert_to_ndarray(treatment_arms) | ||
| outcomes = _convert_to_ndarray(outcomes) | ||
|
|
||
| if covariates.shape[0] != treatment_arms.shape[0]: | ||
| raise ValueError("The shape of covariates and treatment_arm should be same") | ||
|
|
||
|
|
||
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,57 @@ | ||
| import unittest | ||
| import numpy as np | ||
| import pandas as pd | ||
| import polars as pl | ||
| from dte_adj.util import _convert_to_ndarray | ||
|
|
||
|
|
||
| class TestConvertToNdarray(unittest.TestCase): | ||
| """Test that _convert_to_ndarray correctly converts various array-like inputs.""" | ||
|
|
||
| def test_ndarray(self): | ||
| data = np.array([1, 2, 3]) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, data) | ||
|
|
||
| def test_ndarray_2d(self): | ||
| data = np.array([[1, 2], [3, 4]]) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, data) | ||
|
|
||
| def test_pandas_series(self): | ||
| data = pd.Series([1, 2, 3]) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, np.array([1, 2, 3])) | ||
|
|
||
| def test_pandas_dataframe(self): | ||
| data = pd.DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, np.array([[1, 3], [2, 4]])) | ||
|
|
||
| def test_polars_series(self): | ||
| data = pl.Series([1, 2, 3]) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, np.array([1, 2, 3])) | ||
|
|
||
| def test_polars_dataframe(self): | ||
| data = pl.DataFrame({"a": [1, 2], "b": [3, 4]}) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, np.array([[1, 3], [2, 4]])) | ||
|
|
||
| def test_list(self): | ||
| data = [1, 2, 3] | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, np.array([1, 2, 3])) | ||
|
|
||
| def test_tuple(self): | ||
| data = (1, 2, 3) | ||
| result = _convert_to_ndarray(data) | ||
| self.assertIsInstance(result, np.ndarray) | ||
| np.testing.assert_array_equal(result, np.array([1, 2, 3])) |
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.
ditto