Skip to content

Commit

Permalink
Support bool type in --query-columns
Browse files Browse the repository at this point in the history
  • Loading branch information
victorlin committed Feb 9, 2024
1 parent bd35393 commit 423b3b6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions augur/filter/include_exclude_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ def filter_by_query(metadata: pd.DataFrame, query: str, column_types: Optional[D
metadata_copy[column] = pd.to_numeric(metadata_copy[column], errors='raise', downcast='float')
except ValueError as e:
raise AugurError(f"Failed to convert value in column {column!r} to float. {e}")
elif dtype == 'bool':
try:
metadata_copy[column] = metadata_copy[column].map(_string_to_boolean)
except ValueError as e:
raise AugurError(f"Failed to convert value in column {column!r} to bool. {e}")
elif dtype == 'str':
metadata_copy[column] = metadata_copy[column].astype('str', errors='ignore')

Expand Down
2 changes: 1 addition & 1 deletion augur/filter/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def write_metadata_based_outputs(input_metadata_path: str, delimiters: Sequence[


# These are the types accepted in the following function.
ACCEPTED_TYPES = {'int', 'float', 'str'}
ACCEPTED_TYPES = {'int', 'float', 'bool', 'str'}

def column_type_pair(input: str):
"""Get a 2-tuple for column name to type.
Expand Down
10 changes: 10 additions & 0 deletions tests/functional/filter/cram/filter-query-columns.t
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ Specifying category:float does not work.
> --output-strains filtered_strains.txt
ERROR: Failed to convert value in column 'category' to float. Unable to parse string "A" at position 0
[2]

Specifying category:bool also does not work.

$ ${AUGUR} filter \
> --metadata metadata.tsv \
> --query "coverage >= 0.95 & category == 'B'" \
> --query-columns category:bool \
> --output-strains filtered_strains.txt
ERROR: Failed to convert value in column 'category' to bool. Unable to convert 'A' to a boolean value.
[2]

0 comments on commit 423b3b6

Please sign in to comment.