Skip to content

Commit

Permalink
refactor: TreatmentEffect builder pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Laura Boenchen authored and MarIniOnz committed Jun 19, 2024
1 parent a1376d4 commit 0b288f0
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 376 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"python.terminal.activateEnvironment": true,
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.terminal.activateEnvInCurrentTerminal": true,
"autoDocstring.docstringFormat": "sphinx",
"autoDocstring.docstringFormat": "google",
"python.analysis.typeCheckingMode": "strict",
"[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
Expand Down
3 changes: 2 additions & 1 deletion medmodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from medmodels.medrecord import MedRecord
from medmodels.treatment_effect_estimation import TreatmentEffect

__all__ = [MedRecord]
__all__ = [MedRecord, TreatmentEffect]
3 changes: 3 additions & 0 deletions medmodels/treatment_effect_estimation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from medmodels.treatment_effect_estimation.treatment_effect import TreatmentEffect

__all__ = ["TreatmentEffect"]
48 changes: 47 additions & 1 deletion medmodels/treatment_effect_estimation/analysis_modules/adjust.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,59 @@
from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional, Set, Literal, Tuple
from typing_extensions import TypeAlias

from medmodels import MedRecord
from medmodels.medrecord.types import (
NodeIndex,
)

if TYPE_CHECKING:

Check failure on line 11 in medmodels/treatment_effect_estimation/analysis_modules/adjust.py

View workflow job for this annotation

GitHub Actions / lint

Ruff (I001)

medmodels/treatment_effect_estimation/analysis_modules/adjust.py:1:1: I001 Import block is un-sorted or un-formatted
from medmodels.treatment_effect_estimation.treatment_effect import TreatmentEffect


MatchingMethod: TypeAlias = Literal["propensity", "nearest_neighbors"]


class Adjust:
_treatment_effect: TreatmentEffect

def __init__(self, treatment_effect: TreatmentEffect) -> None:
self._treatment_effect = treatment_effect

def _apply_matching(
self,
method: Optional[MatchingMethod],
medrecord: MedRecord,
treatment_all: Set[NodeIndex],
control_true: Set[NodeIndex],
control_false: Set[NodeIndex],
) -> Tuple[Set[NodeIndex], Set[NodeIndex]]:
"""
Update the treatment effect object with the matched controls.
Args:
medrecord (MedRecord): The MedRecord object containing the data.
treatment_all (Set[NodeIndex]): The set of all patients in the treatment
group.
control_true (Set[NodeIndex]): The set of patients in the control group with
the outcome of interest.
control_false (Set[NodeIndex]): The set of patients in the control group
without the outcome of interest.
control_false (Set[NodeIndex]): The set of patients in the control group
without the outcome of interest.
Returns:
Tuple[Set[NodeIndex], Set[NodeIndex]]: The updated control_true and
control_false sets after matching.
"""
if method is None:
return control_true, control_false

# If it is not None, apply the matching method
method_function = getattr(self, method)
control_true, control_false = method_function(
medrecord, treatment_all, control_true, control_false
)

return control_true, control_false
119 changes: 0 additions & 119 deletions medmodels/treatment_effect_estimation/analysis_modules/configure.py

This file was deleted.

Loading

0 comments on commit 0b288f0

Please sign in to comment.