Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 15 additions & 6 deletions augur/dates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,28 +161,37 @@ def is_date_ambiguous(date, ambiguous_by):
Note that this can support any date format, not just YYYY-MM-DD.
"""

RE_AUGUR_MISSING_DATE = re.compile(r'^XXXX-XX-XX$')
"""
Matches an Augur-style ambiguous date with all parts masked.
This only supports YYYY-MM-DD format.
"""

@cache
def get_numerical_date_from_value(value, fmt, min_max_year=None) -> Union[float, Tuple[float, float], None]:
value = str(value)

# 1. Check if value is an exact date in the specified format (fmt).
# Check if value is a missing date.
if RE_AUGUR_MISSING_DATE.match(value):
return None
Copy link
Member Author

Choose a reason for hiding this comment

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

Error from wnv check:

TypeError: unsupported operand type(s) for /: 'NoneType' and 'int'

This is coming from augur frequencies. I'll run locally to investigate.


# Check if value is an exact date in the specified format (fmt).
try:
return date_to_numeric(datetime.datetime.strptime(value, fmt))
except:
pass

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

# 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.

# Check if value is a numeric date.
if RE_NUMERIC_DATE.match(value):
return float(value)

# Check if value is an ISO 8601-like date.

if RE_YEAR_ONLY.match(value):
start, end = AmbiguousDate(f"{value}-XX-XX", fmt="%Y-%m-%d").range(min_max_year=min_max_year)
return (date_to_numeric(start), date_to_numeric(end))
Expand All @@ -200,7 +209,7 @@ def get_numerical_date_from_value(value, fmt, min_max_year=None) -> Union[float,
# closest in-bound value.
raise InvalidDate(value, str(error)) from error

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

return None

Expand Down
31 changes: 31 additions & 0 deletions tests/functional/filter/cram/filter-missing-date.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Setup

$ source "$TESTDIR"/_setup.sh

Create metadata TSV file with two date values that should be functionally equivalent.

$ cat >metadata.tsv <<~~
> strain date
> SEQ_1
> SEQ_2 XXXX-XX-XX
> ~~

BUG: SEQ_2 passes for --min-date and --max-date.

$ ${AUGUR} filter \
> --metadata metadata.tsv \
> --min-date 2025 \
> --output-strains filtered_strains.txt
2 strains were dropped during filtering
2 were dropped because they were earlier than 2025.0 or missing a date
ERROR: All samples have been dropped! Check filter rules and metadata file format.
[2]

$ ${AUGUR} filter \
> --metadata metadata.tsv \
> --max-date 2025 \
> --output-strains filtered_strains.txt
2 strains were dropped during filtering
2 were dropped because they were later than 2025.0 or missing a date
ERROR: All samples have been dropped! Check filter rules and metadata file format.
[2]
Loading