From 854dc7de9f99809e6187fbe79f9cf481cc0cd607 Mon Sep 17 00:00:00 2001 From: Cornelius Roemer Date: Mon, 4 Sep 2023 13:49:13 +0200 Subject: [PATCH] feat: support treetime ambiguous date format 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 df94593d3f1fb9795333916f462209f78545c39c) --- augur/dates/__init__.py | 6 ++++++ tests/dates/test_dates.py | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/augur/dates/__init__.py b/augur/dates/__init__.py index 49ad132e1..a3593449d 100644 --- a/augur/dates/__init__.py +++ b/augur/dates/__init__.py @@ -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: diff --git a/tests/dates/test_dates.py b/tests/dates/test_dates.py index 1427ea3fa..ed7b3fb90 100644 --- a/tests/dates/test_dates.py +++ b/tests/dates/test_dates.py @@ -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]