Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions augur/dates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def is_date_ambiguous(date, ambiguous_by):
Those should be further validated by date conversion functions.
"""

RE_AUGUR_UNKNOWN_DATE = re.compile(r'^XXXX-XX-XX$')
"""
Matches an Augur-style unknown date.
"""

RE_AUGUR_AMBIGUOUS_DATE = re.compile(r'.*XX.*')
"""
Matches an Augur-style ambiguous date with 'XX' used to mask unknown parts of the date.
Expand All @@ -177,13 +182,20 @@ def get_numerical_date_from_value(value, fmt, min_max_year=None) -> Union[float,
except:
pass

# 2. Check if value is an ambiguous date in the specified format (fmt).
# 2. Check if value is an unknown date.
# This is checked before ambiguous dates since it is a subset of that with
# special handling.

if RE_AUGUR_UNKNOWN_DATE.match(value):
return (float("-inf"), float("inf"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is not handled in treetime which is leading to the errors in a couple of the pathogen-repo-ci tests?


# 3. Check if value is an ambiguous date in the specified format (fmt).

if RE_AUGUR_AMBIGUOUS_DATE.match(value):
start, end = AmbiguousDate(value, fmt=fmt).range(min_max_year=min_max_year)
return (date_to_numeric(start), date_to_numeric(end))

# 3. Check formats that are always supported.
# 4. Check formats that are always supported.

if RE_NUMERIC_DATE.match(value):
return float(value)
Expand Down Expand Up @@ -216,7 +228,7 @@ def get_numerical_date_from_value(value, fmt, min_max_year=None) -> Union[float,

return (date_to_numeric(start), date_to_numeric(end))

# 4. Return none (silent error) if the date does not match any of the checked formats.
# 5. Return none (silent error) if the date does not match any of the checked formats.

return None

Expand Down
5 changes: 5 additions & 0 deletions tests/dates/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ def test_get_numerical_date_from_value_not_ambiguous(self):
== pytest.approx(2000.242, abs=1e-3)
)

def test_get_numerical_date_from_value_unknown_date(self):
assert (dates.get_numerical_date_from_value("XXXX-XX-XX", "%Y-%m-%d")
== (float("-inf"), float("inf"))
)

@pytest.mark.parametrize(
"value",
[
Expand Down