Skip to content

Commit

Permalink
feat: support treetime ambiguous date format
Browse files Browse the repository at this point in the history
Add support for the treetime ambiguous date format `[float_min:float_max]`, e.g. `[2004.43:2005.58]`
This is useful for when ambiguity is not limited
to month/year boundaries.
Resolves #1304

(cherry picked from commit df94593)
  • Loading branch information
corneliusroemer committed Sep 4, 2023
1 parent c814508 commit 854dc7d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
6 changes: 6 additions & 0 deletions augur/dates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ def get_numerical_date_from_value(value, fmt=None, min_max_year=None):
except InvalidDate as error:
raise AugurError(str(error)) from error
return [treetime.utils.numeric_date(d) for d in ambig_date]
if value.startswith('[') and value.endswith(']') and len(value[1:-1].split(':'))==2:
# Treetime ambiguous date format, e.g. [2019.5:2020.5]
try:
return [float(x) for x in value[1:-1].split(':')]
except ValueError as error:
raise AugurError(str(error)) from error
try:
return treetime.utils.numeric_date(datetime.datetime.strptime(value, fmt))
except ValueError:
Expand Down
4 changes: 4 additions & 0 deletions tests/dates/test_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,7 @@ def test_get_numerical_dates_dict_error(self):
}
with pytest.raises(AugurError):
dates.get_numerical_dates(metadata)

def test_get_numerical_date_from_value_treetime_ambiguous_date_format(self):
assert dates.get_numerical_date_from_value("[2019.5:2020.5]") == [2019.5, 2020.5]
assert dates.get_numerical_date_from_value("[2013.543:2013.544]") == [2013.543, 2013.544]

0 comments on commit 854dc7d

Please sign in to comment.