Skip to content

Commit

Permalink
Use ngettext to handle plurality
Browse files Browse the repository at this point in the history
Use the same method as "merge: Use singular vs. plural forms as
appropriate in error messages" (9a98647)
  • Loading branch information
victorlin committed Sep 24, 2024
1 parent 3a9fdbd commit b06ac37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions augur/filter/_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from augur.io.file import PANDAS_READ_CSV_OPTIONS, open_file
from augur.io.metadata import InvalidDelimiter, Metadata, read_metadata
from augur.io.sequences import read_sequences, write_sequences
from augur.io.print import print_err
from augur.io.print import print_err, _n
from augur.io.vcf import is_vcf as filename_is_vcf, write_vcf
from augur.types import EmptyOutputReportingMethod
from . import include_exclude_rules
Expand Down Expand Up @@ -427,7 +427,7 @@ def run(args):
total_strains_passed = len(valid_strains)
total_strains_filtered = len(metadata_strains) + num_excluded_by_lack_of_metadata - total_strains_passed

print_err(f"{total_strains_filtered} {'strain was' if total_strains_filtered == 1 else 'strains were'} dropped during filtering")
print_err(f"{total_strains_filtered} {_n('strain was', 'strains were', total_strains_filtered)} dropped during filtering")

if num_excluded_by_lack_of_metadata:
print_err(f"\t{num_excluded_by_lack_of_metadata} had no metadata")
Expand Down Expand Up @@ -457,13 +457,13 @@ def run(args):
parameters = {}

parameters["count"] = count
parameters["were"] = "was" if count == 1 else "were"
parameters["they"] = "it" if count == 1 else "they"
parameters["were"] = _n("was", "were", count)
parameters["they"] = _n("it", "they", count)
print_err("\t" + report_template_by_filter_name[filter_name].format(**parameters))

if (group_by and args.sequences_per_group) or args.subsample_max_sequences:
seed_txt = ", using seed {}".format(args.subsample_seed) if args.subsample_seed else ""
print_err(f"\t{num_excluded_subsamp} {'was' if num_excluded_subsamp == 1 else 'were'} dropped because of subsampling criteria{seed_txt}")
print_err(f"\t{num_excluded_subsamp} {_n('was', 'were', num_excluded_subsamp)} dropped because of subsampling criteria{seed_txt}")

if total_strains_passed == 0:
empty_results_message = "All samples have been dropped! Check filter rules and metadata file format."
Expand All @@ -476,4 +476,4 @@ def run(args):
else:
raise ValueError(f"Encountered unhandled --empty-output-reporting method {args.empty_output_reporting!r}")

print_err(f"{total_strains_passed} {'strain' if total_strains_passed == 1 else 'strains'} passed all filters")
print_err(f"{total_strains_passed} {_n('strain', 'strains', total_strains_passed)} passed all filters")
12 changes: 6 additions & 6 deletions augur/filter/subsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from augur.dates import get_year_month, get_year_week
from augur.errors import AugurError
from augur.io.metadata import METADATA_DATE_COLUMN
from augur.io.print import print_err
from augur.io.print import print_err, _n
from . import constants
from .weights_file import WEIGHTS_COLUMN, COLUMN_VALUE_FOR_DEFAULT_WEIGHT, get_default_weight, get_weighted_columns, read_weights_file

Expand Down Expand Up @@ -352,8 +352,8 @@ def get_weighted_group_sizes(
# Warn on any under-sampled groups
for _, row in weights.iterrows():
if row[INPUT_SIZE_COLUMN] < row[TARGET_SIZE_COLUMN]:
sequences = 'sequence' if row[TARGET_SIZE_COLUMN] == 1 else 'sequences'
are = 'is' if row[INPUT_SIZE_COLUMN] == 1 else 'are'
sequences = _n('sequence', 'sequences', row[TARGET_SIZE_COLUMN])
are = _n('is', 'are', row[INPUT_SIZE_COLUMN])
group = list(f'{col}={value!r}' for col, value in row[group_by].items())
print_err(f"WARNING: Targeted {row[TARGET_SIZE_COLUMN]} {sequences} for group {group} but only {row[INPUT_SIZE_COLUMN]} {are} available.")

Expand Down Expand Up @@ -411,7 +411,7 @@ def _drop_unused_groups(
extra_groups = set(weights.index) - valid_index
if extra_groups:
count = len(extra_groups)
unit = "group" if count == 1 else "groups"
unit = _n("group", "groups", count)
print_err(f"NOTE: Skipping {count} {unit} due to lack of entries in metadata.")
weights = weights[weights.index.isin(valid_index)]

Expand All @@ -427,8 +427,8 @@ def _adjust_weights_for_unweighted_columns(
) -> pd.DataFrame:
"""Adjust weights for unweighted columns to reflect equal weighting within each weighted group.
"""
columns = 'column' if len(unweighted_columns) == 1 else 'columns'
those = 'that' if len(unweighted_columns) == 1 else 'those'
columns = _n('column', 'columns', len(unweighted_columns))
those = _n('that', 'those', len(unweighted_columns))
print_err(f"NOTE: Weights were not provided for the {columns} {', '.join(repr(col) for col in unweighted_columns)}. Using equal weights across values in {those} {columns}.")

weights_grouped = weights.groupby(weighted_columns)
Expand Down

0 comments on commit b06ac37

Please sign in to comment.