@@ -91,35 +91,42 @@ class CoxExposuresTransformerSuite extends SharedContext {
9191 assert(result === expected)
9292 }
9393
94- " withExposureStart" should " add a column with the start of the exposure" in {
94+ " withExposureStart" should " add exposureStart column correctly if the minPurchases value " +
95+ " is passed as 2" in {
9596 val sqlCtx = sqlContext
9697 import sqlCtx .implicits ._
9798
9899 // Given
100+ import fr .polytechnique .cmap .cnam .filtering .cox .CoxConfig .CoxExposureDefinition
101+ val coxExposureDefintion = CoxExposureDefinition (
102+ minPurchases = 2 ,
103+ purchasesWindow = 6 ,
104+ startDelay = 3
105+ )
99106 val input = Seq (
100- (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 3 , 1 ), makeTS(2008 , 6 , 29 )),
101- (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 1 , 1 ), makeTS(2008 , 6 , 29 )),
102- (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 8 , 1 ), makeTS(2008 , 6 , 29 )),
103- (" Patient_A" , " molecule" , " SULFONYLUREA" , makeTS(2008 , 9 , 1 ), makeTS(2008 , 6 , 29 )),
104- (" Patient_A" , " molecule" , " SULFONYLUREA" , makeTS(2008 , 10 , 1 ), makeTS(2008 , 6 , 29 )),
105- (" Patient_B" , " molecule" , " PIOGLITAZONE" , makeTS(2009 , 1 , 1 ), makeTS(2009 , 6 , 29 )),
106- (" Patient_B" , " molecule" , " BENFLUOREX" , makeTS(2007 , 1 , 1 ), makeTS(2009 , 6 , 29 ))
107+ (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 8 , 1 ), makeTS(2008 , 6 , 29 )),
108+ (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 1 , 1 ), makeTS(2008 , 6 , 29 )),
109+ (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 9 , 1 ), makeTS(2008 , 6 , 29 )),
110+ (" Patient_A" , " molecule" , " SULFONYLUREA" , makeTS(2009 , 3 , 1 ), makeTS(2008 , 6 , 29 )),
111+ (" Patient_A" , " molecule" , " SULFONYLUREA" , makeTS(2009 , 4 , 1 ), makeTS(2008 , 6 , 29 )),
112+ (" Patient_B" , " molecule" , " PIOGLITAZONE" , makeTS(2009 , 1 , 1 ), makeTS(2009 , 6 , 29 )),
113+ (" Patient_B" , " molecule" , " BENFLUOREX" , makeTS(2007 , 1 , 1 ), makeTS(2009 , 6 , 29 ))
107114 ).toDF(" PatientID" , " category" , " eventId" , " start" , " followUpStart" )
108115
109116 val expected = Seq (
110- (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 6 , 29 ))),
111- (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 6 , 29 ))),
112- (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 6 , 29 ))),
113- (" Patient_A" , " SULFONYLUREA" , Some (makeTS(2009 , 1 , 1 ))),
114- (" Patient_A" , " SULFONYLUREA" , Some (makeTS(2009 , 1 , 1 ))),
117+ (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 12 , 1 ))),
118+ (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 12 , 1 ))),
119+ (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 12 , 1 ))),
120+ (" Patient_A" , " SULFONYLUREA" , Some (makeTS(2009 , 7 , 1 ))),
121+ (" Patient_A" , " SULFONYLUREA" , Some (makeTS(2009 , 7 , 1 ))),
115122 (" Patient_B" , " PIOGLITAZONE" , None ),
116123 (" Patient_B" , " BENFLUOREX" , None )
117124 ).toDF(" PatientID" , " eventId" , " exposureStart" )
118125
119126
120127 // When
121128 import fr .polytechnique .cmap .cnam .filtering .cox .CoxExposuresTransformer .ExposuresDataFrame
122- val result = input.withExposureStart.select(" PatientID" , " eventId" , " exposureStart" )
129+ val result = input.withExposureStart(coxExposureDefintion) .select(" PatientID" , " eventId" , " exposureStart" )
123130
124131 // Then
125132 import RichDataFrames ._
@@ -130,6 +137,52 @@ class CoxExposuresTransformerSuite extends SharedContext {
130137 assert(result === expected)
131138 }
132139
140+ it should " compute the exposureStart date correctly if the minPurchases value is passed as 1" in {
141+ val sqlCtx = sqlContext
142+ import sqlCtx .implicits ._
143+
144+ // Given
145+ import fr .polytechnique .cmap .cnam .filtering .cox .CoxConfig .CoxExposureDefinition
146+ val coxExposureDefintion = CoxExposureDefinition (
147+ minPurchases = 1 ,
148+ purchasesWindow = 6 ,
149+ startDelay = 3
150+ )
151+ val input = Seq (
152+ (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 3 , 1 ), makeTS(2008 , 6 , 29 )),
153+ (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 1 , 1 ), makeTS(2008 , 6 , 29 )),
154+ (" Patient_A" , " molecule" , " PIOGLITAZONE" , makeTS(2008 , 8 , 1 ), makeTS(2008 , 6 , 29 )),
155+ (" Patient_A" , " molecule" , " SULFONYLUREA" , makeTS(2009 , 3 , 1 ), makeTS(2008 , 6 , 29 )),
156+ (" Patient_A" , " molecule" , " SULFONYLUREA" , makeTS(2009 , 4 , 1 ), makeTS(2008 , 6 , 29 )),
157+ (" Patient_B" , " molecule" , " PIOGLITAZONE" , makeTS(2009 , 1 , 1 ), makeTS(2009 , 6 , 29 )),
158+ (" Patient_B" , " molecule" , " BENFLUOREX" , makeTS(2009 , 6 , 1 ), makeTS(2009 , 6 , 29 ))
159+ ).toDF(" PatientID" , " category" , " eventId" , " start" , " followUpStart" )
160+
161+ val expected = Seq (
162+ (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 6 , 29 ))),
163+ (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 6 , 29 ))),
164+ (" Patient_A" , " PIOGLITAZONE" , Some (makeTS(2008 , 6 , 29 ))),
165+ (" Patient_A" , " SULFONYLUREA" , Some (makeTS(2009 , 6 , 1 ))),
166+ (" Patient_A" , " SULFONYLUREA" , Some (makeTS(2009 , 6 , 1 ))),
167+ (" Patient_B" , " PIOGLITAZONE" , Some (makeTS(2009 , 6 , 29 ))),
168+ (" Patient_B" , " BENFLUOREX" , Some (makeTS(2009 , 9 , 1 )))
169+ ).toDF(" PatientID" , " eventId" , " exposureStart" )
170+
171+ // When
172+ import fr .polytechnique .cmap .cnam .filtering .cox .CoxExposuresTransformer .ExposuresDataFrame
173+ val result = input.withExposureStart(coxExposureDefintion)
174+ .select(" PatientID" , " eventId" , " exposureStart" )
175+
176+ // Then
177+ import RichDataFrames ._
178+ println(" Result:" )
179+ result.show
180+ println(" Expected:" )
181+ expected.show
182+ assert(result === expected)
183+
184+ }
185+
133186 " transform" should " return a valid Dataset for a known input" in {
134187
135188 val sqlCtx = sqlContext
@@ -247,7 +300,7 @@ class CoxExposuresTransformerSuite extends SharedContext {
247300 ).toDF
248301
249302 // When
250- val result = CoxExposuresTransformer .transform(input, false )
303+ val result = CoxExposuresTransformer .transform(input, filterDelayedPatients = false )
251304
252305 // Then
253306 result.show
0 commit comments