Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter: Fix groupby with incomplete dates #808

Merged
merged 2 commits into from
Dec 10, 2021
Merged
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
7 changes: 5 additions & 2 deletions augur/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,8 +914,11 @@ def get_groups_for_subsampling(strains, metadata, group_by=None):
else:
# replace date with year/month/day as nullable ints
date_cols = ['year', 'month', 'day']
df_dates = (metadata['date'].str.split('-', n=2, expand=True)
.set_axis(date_cols, axis=1))
df_dates = metadata['date'].str.split('-', n=2, expand=True)
df_dates = df_dates.set_axis(date_cols[:len(df_dates.columns)], axis=1)
missing_date_cols = set(date_cols) - set(df_dates.columns)
for col in missing_date_cols:
df_dates[col] = pd.NA
for col in date_cols:
df_dates[col] = pd.to_numeric(df_dates[col], errors='coerce').astype(pd.Int64Dtype())
metadata = pd.concat([metadata.drop('date', axis=1), df_dates], axis=1)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_filter_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,48 @@ def test_filter_groupby_no_strains(self, valid_metadata: pd.DataFrame):
group_by_strain, skipped_strains = get_groups_for_subsampling(strains, metadata, group_by=groups)
assert group_by_strain == {}
assert skipped_strains == []

def test_filter_groupby_only_year_provided(self, valid_metadata: pd.DataFrame):
groups = ['country', 'year']
metadata = valid_metadata.copy()
metadata['date'] = '2020'
strains = metadata.index.tolist()
group_by_strain, skipped_strains = get_groups_for_subsampling(strains, metadata, group_by=groups)
assert group_by_strain == {
'SEQ_1': ('A', 2020),
'SEQ_2': ('A', 2020),
'SEQ_3': ('B', 2020),
'SEQ_4': ('B', 2020),
'SEQ_5': ('B', 2020)
}
assert skipped_strains == []

def test_filter_groupby_month_with_only_year_provided(self, valid_metadata: pd.DataFrame):
groups = ['country', 'year', 'month']
metadata = valid_metadata.copy()
metadata['date'] = '2020'
strains = metadata.index.tolist()
group_by_strain, skipped_strains = get_groups_for_subsampling(strains, metadata, group_by=groups)
assert group_by_strain == {}
assert skipped_strains == [
{'strain': 'SEQ_1', 'filter': 'skip_group_by_with_ambiguous_month', 'kwargs': ''},
{'strain': 'SEQ_2', 'filter': 'skip_group_by_with_ambiguous_month', 'kwargs': ''},
{'strain': 'SEQ_3', 'filter': 'skip_group_by_with_ambiguous_month', 'kwargs': ''},
{'strain': 'SEQ_4', 'filter': 'skip_group_by_with_ambiguous_month', 'kwargs': ''},
{'strain': 'SEQ_5', 'filter': 'skip_group_by_with_ambiguous_month', 'kwargs': ''}
]

def test_filter_groupby_only_year_month_provided(self, valid_metadata: pd.DataFrame):
groups = ['country', 'year', 'month']
metadata = valid_metadata.copy()
metadata['date'] = '2020-01'
strains = metadata.index.tolist()
group_by_strain, skipped_strains = get_groups_for_subsampling(strains, metadata, group_by=groups)
assert group_by_strain == {
'SEQ_1': ('A', 2020, (2020, 1)),
'SEQ_2': ('A', 2020, (2020, 1)),
'SEQ_3': ('B', 2020, (2020, 1)),
'SEQ_4': ('B', 2020, (2020, 1)),
'SEQ_5': ('B', 2020, (2020, 1))
}
assert skipped_strains == []