1
1
import unittest
2
2
import pandas as pd
3
- import numpy as np
4
- import matplotlib .pyplot as plt
5
- from causal_testing .specification .variable import Input
6
- from causal_testing .utils .validation import CausalValidator
3
+ from causal_testing .specification .variable import Input , Output
7
4
8
5
from causal_testing .estimation .ipcw_estimator import IPCWEstimator
9
6
@@ -13,113 +10,85 @@ class TestIPCWEstimator(unittest.TestCase):
13
10
Test the IPCW estimator class
14
11
"""
15
12
13
+ def setUp (self ) -> None :
14
+ self .outcome = Output ("outcome" , float )
15
+ self .status_column = "ok"
16
+ self .timesteps_per_intervention = 1
17
+ self .control_strategy = [[t , "t" , 0 ] for t in range (1 , 4 , self .timesteps_per_intervention )]
18
+ self .treatment_strategy = [[t , "t" , 1 ] for t in range (1 , 4 , self .timesteps_per_intervention )]
19
+ self .fit_bl_switch_formula = "xo_t_do ~ time"
20
+ self .df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
21
+ self .df [self .status_column ] = self .df ["outcome" ] == 1
22
+
16
23
def test_estimate_hazard_ratio (self ):
17
- timesteps_per_intervention = 1
18
- control_strategy = [[t , "t" , 0 ] for t in range (1 , 4 , timesteps_per_intervention )]
19
- treatment_strategy = [[t , "t" , 1 ] for t in range (1 , 4 , timesteps_per_intervention )]
20
- outcome = "outcome"
21
- fit_bl_switch_formula = "xo_t_do ~ time"
22
- df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
23
- df ["ok" ] = df ["outcome" ] == 1
24
24
estimation_model = IPCWEstimator (
25
- df ,
26
- timesteps_per_intervention ,
27
- control_strategy ,
28
- treatment_strategy ,
29
- outcome ,
30
- "ok" ,
31
- fit_bl_switch_formula = fit_bl_switch_formula ,
32
- fit_bltd_switch_formula = fit_bl_switch_formula ,
25
+ self . df ,
26
+ self . timesteps_per_intervention ,
27
+ self . control_strategy ,
28
+ self . treatment_strategy ,
29
+ self . outcome ,
30
+ self . status_column ,
31
+ fit_bl_switch_formula = self . fit_bl_switch_formula ,
32
+ fit_bltd_switch_formula = self . fit_bl_switch_formula ,
33
33
eligibility = None ,
34
34
)
35
- estimate , intervals = estimation_model .estimate_hazard_ratio ()
35
+ estimate , _ = estimation_model .estimate_hazard_ratio ()
36
36
self .assertEqual (round (estimate ["trtrand" ], 3 ), 1.351 )
37
37
38
38
def test_invalid_treatment_strategies (self ):
39
- timesteps_per_intervention = 1
40
- control_strategy = [[t , "t" , 0 ] for t in range (1 , 4 , timesteps_per_intervention )]
41
- treatment_strategy = [[t , "t" , 1 ] for t in range (1 , 4 , timesteps_per_intervention )]
42
- outcome = "outcome"
43
- fit_bl_switch_formula = "xo_t_do ~ time"
44
- df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
45
- df ["t" ] = (["1" , "0" ] * len (df ))[: len (df )]
46
- df ["ok" ] = df ["outcome" ] == 1
47
39
with self .assertRaises (ValueError ):
48
- estimation_model = IPCWEstimator (
49
- df ,
50
- timesteps_per_intervention ,
51
- control_strategy ,
52
- treatment_strategy ,
53
- outcome ,
54
- "ok" ,
55
- fit_bl_switch_formula = fit_bl_switch_formula ,
56
- fit_bltd_switch_formula = fit_bl_switch_formula ,
40
+ IPCWEstimator (
41
+ self . df . assign ( t = ([ "1" , "0" ] * len ( self . df ))[: len ( self . df )]) ,
42
+ self . timesteps_per_intervention ,
43
+ self . control_strategy ,
44
+ self . treatment_strategy ,
45
+ self . outcome ,
46
+ self . status_column ,
47
+ fit_bl_switch_formula = self . fit_bl_switch_formula ,
48
+ fit_bltd_switch_formula = self . fit_bl_switch_formula ,
57
49
eligibility = None ,
58
50
)
59
51
60
52
def test_invalid_fault_t_do (self ):
61
- timesteps_per_intervention = 1
62
- control_strategy = [[t , "t" , 0 ] for t in range (1 , 4 , timesteps_per_intervention )]
63
- treatment_strategy = [[t , "t" , 1 ] for t in range (1 , 4 , timesteps_per_intervention )]
64
- outcome = "outcome"
65
- fit_bl_switch_formula = "xo_t_do ~ time"
66
- df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
67
- df ["ok" ] = df ["outcome" ] == 1
68
53
estimation_model = IPCWEstimator (
69
- df ,
70
- timesteps_per_intervention ,
71
- control_strategy ,
72
- treatment_strategy ,
73
- outcome ,
74
- "ok" ,
75
- fit_bl_switch_formula = fit_bl_switch_formula ,
76
- fit_bltd_switch_formula = fit_bl_switch_formula ,
54
+ self . df . assign ( outcome = 1 ) ,
55
+ self . timesteps_per_intervention ,
56
+ self . control_strategy ,
57
+ self . treatment_strategy ,
58
+ self . outcome ,
59
+ self . status_column ,
60
+ fit_bl_switch_formula = self . fit_bl_switch_formula ,
61
+ fit_bltd_switch_formula = self . fit_bl_switch_formula ,
77
62
eligibility = None ,
78
63
)
79
64
estimation_model .df ["fault_t_do" ] = 0
80
65
with self .assertRaises (ValueError ):
81
- estimate , intervals = estimation_model .estimate_hazard_ratio ()
66
+ estimation_model .estimate_hazard_ratio ()
82
67
83
68
def test_no_individual_began_control_strategy (self ):
84
- timesteps_per_intervention = 1
85
- control_strategy = [[t , "t" , 0 ] for t in range (1 , 4 , timesteps_per_intervention )]
86
- treatment_strategy = [[t , "t" , 1 ] for t in range (1 , 4 , timesteps_per_intervention )]
87
- outcome = "outcome"
88
- fit_bl_switch_formula = "xo_t_do ~ time"
89
- df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
90
- df ["t" ] = 1
91
- df ["ok" ] = df ["outcome" ] == 1
92
69
with self .assertRaises (ValueError ):
93
- estimation_model = IPCWEstimator (
94
- df ,
95
- timesteps_per_intervention ,
96
- control_strategy ,
97
- treatment_strategy ,
98
- outcome ,
99
- "ok" ,
100
- fit_bl_switch_formula = fit_bl_switch_formula ,
101
- fit_bltd_switch_formula = fit_bl_switch_formula ,
70
+ IPCWEstimator (
71
+ self . df . assign ( t = 1 ) ,
72
+ self . timesteps_per_intervention ,
73
+ self . control_strategy ,
74
+ self . treatment_strategy ,
75
+ self . outcome ,
76
+ self . status_column ,
77
+ fit_bl_switch_formula = self . fit_bl_switch_formula ,
78
+ fit_bltd_switch_formula = self . fit_bl_switch_formula ,
102
79
eligibility = None ,
103
80
)
104
81
105
82
def test_no_individual_began_treatment_strategy (self ):
106
- timesteps_per_intervention = 1
107
- control_strategy = [[t , "t" , 0 ] for t in range (1 , 4 , timesteps_per_intervention )]
108
- treatment_strategy = [[t , "t" , 1 ] for t in range (1 , 4 , timesteps_per_intervention )]
109
- outcome = "outcome"
110
- fit_bl_switch_formula = "xo_t_do ~ time"
111
- df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
112
- df ["t" ] = 0
113
- df ["ok" ] = df ["outcome" ] == 1
114
83
with self .assertRaises (ValueError ):
115
- estimation_model = IPCWEstimator (
116
- df ,
117
- timesteps_per_intervention ,
118
- control_strategy ,
119
- treatment_strategy ,
120
- outcome ,
121
- "ok" ,
122
- fit_bl_switch_formula = fit_bl_switch_formula ,
123
- fit_bltd_switch_formula = fit_bl_switch_formula ,
84
+ IPCWEstimator (
85
+ self . df . assign ( t = 0 ) ,
86
+ self . timesteps_per_intervention ,
87
+ self . control_strategy ,
88
+ self . treatment_strategy ,
89
+ self . outcome ,
90
+ self . status_column ,
91
+ fit_bl_switch_formula = self . fit_bl_switch_formula ,
92
+ fit_bltd_switch_formula = self . fit_bl_switch_formula ,
124
93
eligibility = None ,
125
94
)
0 commit comments