-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from IMMM-SFA/feature/remove-epiweeks-dependency
remove epiweeks dependency in an attempt to fix #71
- Loading branch information
Showing
7 changed files
with
62 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from datetime import datetime | ||
import unittest | ||
|
||
from mosartwmpy.utilities.epiweek import get_epiweek_from_datetime | ||
|
||
|
||
class EpiweekTest(unittest.TestCase): | ||
|
||
def test_epiweek(self): | ||
self.assertEqual(get_epiweek_from_datetime(datetime(2017, 1, 1)), 1) | ||
self.assertEqual(get_epiweek_from_datetime(datetime(2015, 1, 1)), 53) | ||
self.assertEqual(get_epiweek_from_datetime(datetime(2016, 1, 1)), 52) | ||
self.assertEqual(get_epiweek_from_datetime(datetime(2017, 3, 15)), 11) | ||
self.assertEqual(get_epiweek_from_datetime(datetime(2017, 3, 31)), 13) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import date, datetime | ||
|
||
|
||
def get_epiweek_from_datetime(dt: datetime) -> int: | ||
"""Calculate the epiweek number from a datetime | ||
Args: | ||
dt (datetime): the datetime object to calculate the epiweek for | ||
Returns: | ||
int: the epiweek number | ||
""" | ||
year = dt.year | ||
dt_ordinal = dt.toordinal() | ||
year_start_ordinal = _get_year_start_ordinal(year) | ||
week = (dt_ordinal - year_start_ordinal) // 7 | ||
if week < 0: | ||
year = year - 1 | ||
year_start_ordinal = _get_year_start_ordinal(year) | ||
week = (dt_ordinal - year_start_ordinal) // 7 | ||
elif week >= 52: | ||
year_start_ordinal = _get_year_start_ordinal(year + 1) | ||
if date_ordinal > year_start_ordinal: | ||
year = year + 1 | ||
week = 0 | ||
week = week + 1 | ||
return week | ||
|
||
|
||
def _get_year_start_ordinal(y): | ||
year_start = date(y, 1, 1) | ||
year_start_weekday = year_start.weekday() | ||
year_start_ordinal = year_start.toordinal() - year_start_weekday - 1 | ||
if year_start_weekday > 2: | ||
return year_start_ordinal + 7 | ||
return year_start_ordinal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters