@@ -35,7 +35,7 @@ def load_nhefs_df():
35
35
"""Get the NHEFS data from chapter 12 and put into a dataframe. NHEFS = National Health and Nutrition Examination
36
36
Survey Data I Epidemiological Follow-up Study."""
37
37
38
- nhefs_df = pd .read_csv ("tests/data/nhefs.csv" )
38
+ nhefs_df = pd .read_csv ("tests/resources/ data/nhefs.csv" )
39
39
nhefs_df ["one" ] = 1
40
40
nhefs_df ["zero" ] = 0
41
41
edu_dummies = pd .get_dummies (nhefs_df .education , prefix = "edu" )
@@ -79,7 +79,7 @@ class TestLogisticRegressionEstimator(unittest.TestCase):
79
79
80
80
@classmethod
81
81
def setUpClass (cls ) -> None :
82
- cls .scarf_df = pd .read_csv ("tests/data/scarf_data.csv" )
82
+ cls .scarf_df = pd .read_csv ("tests/resources/ data/scarf_data.csv" )
83
83
84
84
# Yes, this probably shouldn't be in here, but it uses the scarf data so it makes more sense to put it
85
85
# here than duplicating the scarf data for a single test
@@ -446,7 +446,7 @@ def setUpClass(cls) -> None:
446
446
df = pd .DataFrame ({"X1" : np .random .uniform (- 1000 , 1000 , 1000 ), "X2" : np .random .uniform (- 1000 , 1000 , 1000 )})
447
447
df ["Y" ] = 2 * df ["X1" ] - 3 * df ["X2" ] + 2 * df ["X1" ] * df ["X2" ] + 10
448
448
cls .df = df
449
- cls .scarf_df = pd .read_csv ("tests/data/scarf_data.csv" )
449
+ cls .scarf_df = pd .read_csv ("tests/resources/ data/scarf_data.csv" )
450
450
451
451
def test_X1_effect (self ):
452
452
"""When we fix the value of X2 to 0, the effect of X1 on Y should become ~2 (because X2 terms are cancelled)."""
@@ -485,7 +485,7 @@ def test_estimate_hazard_ratio(self):
485
485
treatment_strategy = TreatmentSequence (timesteps_per_intervention , [("t" , 1 ), ("t" , 1 ), ("t" , 1 )])
486
486
outcome = "outcome"
487
487
fit_bl_switch_formula = "xo_t_do ~ time"
488
- df = pd .read_csv ("tests/data/temporal_data.csv" )
488
+ df = pd .read_csv ("tests/resources/ data/temporal_data.csv" )
489
489
df ["ok" ] = df ["outcome" ] == 1
490
490
estimation_model = IPCWEstimator (
491
491
df ,
@@ -500,3 +500,48 @@ def test_estimate_hazard_ratio(self):
500
500
)
501
501
estimate , intervals = estimation_model .estimate_hazard_ratio ()
502
502
self .assertEqual (estimate ["trtrand" ], 1.0 )
503
+
504
+ def test_invalid_treatment_strategies (self ):
505
+ timesteps_per_intervention = 1
506
+ control_strategy = TreatmentSequence (timesteps_per_intervention , [("t" , 0 ), ("t" , 0 ), ("t" , 0 )])
507
+ treatment_strategy = TreatmentSequence (timesteps_per_intervention , [("t" , 1 ), ("t" , 1 ), ("t" , 1 )])
508
+ outcome = "outcome"
509
+ fit_bl_switch_formula = "xo_t_do ~ time"
510
+ df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
511
+ df ["t" ] = (["1" , "0" ] * len (df ))[: len (df )]
512
+ df ["ok" ] = df ["outcome" ] == 1
513
+ with self .assertRaises (ValueError ):
514
+ estimation_model = IPCWEstimator (
515
+ df ,
516
+ timesteps_per_intervention ,
517
+ control_strategy ,
518
+ treatment_strategy ,
519
+ outcome ,
520
+ "ok" ,
521
+ fit_bl_switch_formula = fit_bl_switch_formula ,
522
+ fit_bltd_switch_formula = fit_bl_switch_formula ,
523
+ eligibility = None ,
524
+ )
525
+
526
+ def test_invalid_fault_t_do (self ):
527
+ timesteps_per_intervention = 1
528
+ control_strategy = TreatmentSequence (timesteps_per_intervention , [("t" , 0 ), ("t" , 0 ), ("t" , 0 )])
529
+ treatment_strategy = TreatmentSequence (timesteps_per_intervention , [("t" , 1 ), ("t" , 1 ), ("t" , 1 )])
530
+ outcome = "outcome"
531
+ fit_bl_switch_formula = "xo_t_do ~ time"
532
+ df = pd .read_csv ("tests/resources/data/temporal_data.csv" )
533
+ df ["ok" ] = df ["outcome" ] == 1
534
+ estimation_model = IPCWEstimator (
535
+ df ,
536
+ timesteps_per_intervention ,
537
+ control_strategy ,
538
+ treatment_strategy ,
539
+ outcome ,
540
+ "ok" ,
541
+ fit_bl_switch_formula = fit_bl_switch_formula ,
542
+ fit_bltd_switch_formula = fit_bl_switch_formula ,
543
+ eligibility = None ,
544
+ )
545
+ estimation_model .df ["fault_t_do" ] = 0
546
+ with self .assertRaises (ValueError ):
547
+ estimate , intervals = estimation_model .estimate_hazard_ratio ()
0 commit comments