Skip to content

Commit 460a6d4

Browse files
authored
Merge pull request #198 from leaf-ai/fix_tests
#196 Fix tests that are supposed to be in the future
2 parents 286640d + 5338907 commit 460a6d4

File tree

1 file changed

+71
-33
lines changed

1 file changed

+71
-33
lines changed

covid_xprize/validation/tests/test_scenario_generator.py

Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -340,77 +340,115 @@ def _check_future(self, start_date_str, end_date_str, scenario_df, scenario_npis
340340

341341
def test_generate_scenario_mind_the_gap_freeze(self):
342342
# Scenario = Freeze
343-
start_date_str = "2021-01-01"
344-
end_date_str = "2021-01-31"
343+
nb_days = 31
345344
countries = ["Italy"]
345+
last_known_date = self.latest_df[self.latest_df.CountryName == countries[0]].Date.max()
346+
start_date = last_known_date + timedelta(days=7)
347+
start_date_str = start_date.strftime(DATE_FORMAT)
348+
end_date = start_date + timedelta(days=nb_days)
349+
end_date_str = end_date.strftime(DATE_FORMAT)
350+
inception_date = pd.to_datetime(INCEPTION_DATE, format=DATE_FORMAT)
351+
346352
scenario_df = generate_scenario(start_date_str, end_date_str, self.latest_df, countries, scenario="Freeze")
347353
self.assertIsNotNone(scenario_df)
348354
# Misleading name but checks the elements, regardless of order
349355
self.assertCountEqual(countries, scenario_df.CountryName.unique(), "Not the requested countries")
350-
# Inception is 2020-01-01. 366 days for 2020 + 31 for Jan 2021
351-
self.assertEqual(397, len(scenario_df), "Expected the number of days between inception and end date")
352-
# The last 31 rows must be the same
353-
self.assertEqual(0, scenario_df.tail(31)[NPI_COLUMNS].diff().sum().sum(),
354-
"Expected the last 31 rows to have the same frozen IP")
356+
# Check we get the expected number of days
357+
expected_days = (end_date - inception_date).days + 1 # +1 because inception_date and end_date are included
358+
self.assertEqual(expected_days, len(scenario_df), "Expected the number of days between inception and end date")
359+
# The last nb_days rows must be the same
360+
self.assertEqual(0, scenario_df.tail(nb_days)[NPI_COLUMNS].diff().sum().sum(),
361+
f"Expected the last {nb_days} rows to have the same frozen IP")
355362

356363
def test_generate_scenario_mind_the_gap_min(self):
357364
# Scenario = MIN
358-
start_date_str = "2021-01-01"
359-
end_date_str = "2021-01-31"
365+
nb_days = 31
360366
countries = ["Italy"]
367+
last_known_date = self.latest_df[self.latest_df.CountryName == countries[0]].Date.max()
368+
start_date = last_known_date + timedelta(days=7)
369+
start_date_str = start_date.strftime(DATE_FORMAT)
370+
end_date = start_date + timedelta(days=nb_days)
371+
end_date_str = end_date.strftime(DATE_FORMAT)
372+
inception_date = pd.to_datetime(INCEPTION_DATE, format=DATE_FORMAT)
373+
361374
scenario_df = generate_scenario(start_date_str, end_date_str, self.latest_df, countries, scenario="MIN")
362375
self.assertIsNotNone(scenario_df)
363376
# Misleading name but checks the elements, regardless of order
364377
self.assertCountEqual(countries, scenario_df.CountryName.unique(), "Not the requested countries")
365-
# Inception is 2020-01-01. 366 days for 2020 + 31 for Jan 2021
366-
self.assertEqual(397, len(scenario_df), "Expected the number of days between inception and end date")
367-
# The last 31 rows must be the same
368-
self.assertEqual(0, scenario_df.tail(31)[NPI_COLUMNS].sum().sum(),
369-
"Expected the last 31 rows to have NPIs set to 0")
378+
# Check we get the expected number of days
379+
expected_days = (end_date - inception_date).days + 1 # +1 because inception_date and end_date are included
380+
self.assertEqual(expected_days, len(scenario_df), "Expected the number of days between inception and end date")
381+
# The last nb_days rows must be the same
382+
self.assertEqual(0, scenario_df.tail(nb_days)[NPI_COLUMNS].sum().sum(),
383+
f"Expected the last {nb_days} rows to have NPIs set to 0")
370384

371385
def test_generate_scenario_mind_the_gap_max(self):
372386
# Scenario = MAX
373-
start_date_str = "2021-01-01"
374-
end_date_str = "2021-01-31"
387+
nb_days = 31
375388
countries = ["Italy"]
389+
last_known_date = self.latest_df[self.latest_df.CountryName == countries[0]].Date.max()
390+
start_date = last_known_date + timedelta(days=7)
391+
start_date_str = start_date.strftime(DATE_FORMAT)
392+
end_date = start_date + timedelta(days=nb_days)
393+
end_date_str = end_date.strftime(DATE_FORMAT)
394+
inception_date = pd.to_datetime(INCEPTION_DATE, format=DATE_FORMAT)
395+
376396
scenario_df = generate_scenario(start_date_str, end_date_str, self.latest_df, countries, scenario="MAX")
377397
self.assertIsNotNone(scenario_df)
378398
# Misleading name but checks the elements, regardless of order
379399
self.assertCountEqual(countries, scenario_df.CountryName.unique(), "Not the requested countries")
380-
# Inception is 2020-01-01. 366 days for 2020 + 31 for Jan 2021
381-
self.assertEqual(397, len(scenario_df), "Expected the number of days between inception and end date")
382-
# The last 31 rows must be the same
383-
self.assertEqual(sum(MAX_NPIS), scenario_df.tail(31)[NPI_COLUMNS].mean().sum(),
384-
"Expected the last 31 rows to have NPIs set to their max value")
400+
# Check we get the expected number of days
401+
expected_days = (end_date - inception_date).days + 1 # +1 because inception_date and end_date are included
402+
self.assertEqual(expected_days, len(scenario_df), "Expected the number of days between inception and end date")
403+
# The last nb_days rows must be the same
404+
self.assertEqual(sum(MAX_NPIS), scenario_df.tail(nb_days)[NPI_COLUMNS].mean().sum(),
405+
f"Expected the last {nb_days} rows to have NPIs set to their max value")
385406

386407
def test_generate_scenario_mind_the_gap_custom(self):
387408
# Scenario = Custom
388-
start_date_str = "2021-01-01"
389-
end_date_str = "2021-01-31"
409+
nb_days = 31
410+
start_lag = 7
390411
countries = ["Italy"]
391-
# Set all the NPIs to one for each day between start data and end date.
392-
scenario = [ONE_NPIS] * 31
412+
last_known_date = self.latest_df[self.latest_df.CountryName == countries[0]].Date.max()
413+
start_date = last_known_date + timedelta(days=start_lag)
414+
start_date_str = start_date.strftime(DATE_FORMAT)
415+
end_date = start_date + timedelta(days=nb_days)
416+
end_date_str = end_date.strftime(DATE_FORMAT)
417+
inception_date = pd.to_datetime(INCEPTION_DATE, format=DATE_FORMAT)
418+
419+
# Set all the NPIs to one for each day between start date and end date, as well as from last known date.
420+
scenario = [ONE_NPIS] * (nb_days + start_lag)
393421
scenario_df = generate_scenario(start_date_str, end_date_str, self.latest_df, countries, scenario=scenario)
394422
self.assertIsNotNone(scenario_df)
395423
# Misleading name but checks the elements, regardless of order
396424
self.assertCountEqual(countries, scenario_df.CountryName.unique(), "Not the requested countries")
397-
# Inception is 2020-01-01. 366 days for 2020 + 31 for Jan 2021
398-
self.assertEqual(397, len(scenario_df), "Expected the number of days between inception and end date")
425+
# Check we get the expected number of days
426+
expected_days = (end_date - inception_date).days + 1 # +1 because inception_date and end_date are included
427+
self.assertEqual(expected_days, len(scenario_df), "Expected the number of days between inception and end date")
399428
# The last 31 rows must be the same
400-
self.assertEqual(1, scenario_df.tail(31)[NPI_COLUMNS].mean().mean(),
401-
"Expected the last 31 rows to have all NPIs set to 1")
429+
self.assertEqual(1, scenario_df.tail(nb_days)[NPI_COLUMNS].mean().mean(),
430+
f"Expected the last {nb_days} rows to have all NPIs set to 1")
402431

403432
def test_generate_scenario_mind_the_gap_freeze_2_countries(self):
404433
# Check 2 countries
405-
start_date_str = "2021-01-01"
406-
end_date_str = "2021-01-31"
434+
nb_days = 31
435+
# We have 2 countries, and they may have different last know dates.
436+
# Set start date to 7 days from now to guarantee a gap
437+
start_date = datetime.now() + timedelta(days=7)
438+
start_date_str = start_date.strftime(DATE_FORMAT)
439+
end_date = start_date + timedelta(days=nb_days)
440+
end_date_str = end_date.strftime(DATE_FORMAT)
441+
inception_date = pd.to_datetime(INCEPTION_DATE, format=DATE_FORMAT)
442+
407443
countries = ["France", "Italy"]
408444
scenario_df = generate_scenario(start_date_str, end_date_str, self.latest_df, countries, scenario="Freeze")
409445
self.assertIsNotNone(scenario_df)
410446
# Misleading name but checks the elements, regardless of order
411447
self.assertCountEqual(countries, scenario_df.CountryName.unique(), "Not the requested countries")
412-
# Inception is 2020-01-01. 366 days for 2020 + 31 for Jan 2021
413-
self.assertEqual(397 * 2, len(scenario_df), "Not the expected number of days between inception and end date")
448+
# Check we get the expected number of days
449+
expected_days = (end_date - inception_date).days + 1 # +1 because inception_date and end_date are included
450+
self.assertEqual(expected_days * 2, len(scenario_df),
451+
"Not the expected number of days between inception and end date")
414452

415453
def test_generate_scenario_mind_the_gap_freeze_all_countries(self):
416454
# Check all countries, with frozen npis for 180 days, 1 week from today

0 commit comments

Comments
 (0)