Skip to content

Commit

Permalink
Disallow --rank with --subset
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbradley committed Nov 20, 2024
1 parent 94f1f73 commit a466465
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ When a list of PIL images is passed the index of the image will be filled in for
## Command Line Usage
```
bioclip predict [-h] [--format {table,csv}] [--output OUTPUT]
[--rank {kingdom,phylum,class,order,family,genus,species} | --cls CLS | --bins BINS]
[--subset SUBSET] [--k K] [--device DEVICE] image_file [image_file ...]
[--rank {kingdom,phylum,class,order,family,genus,species} | --cls CLS | --bins BINS | --subset SUBSET]
[--k K] [--device DEVICE] image_file [image_file ...]
bioclip embed [-h] [--device=DEVICE] [--output=OUTPUT] [IMAGE_FILE...]
Commands:
Expand All @@ -145,13 +145,13 @@ Options:
--format=FORMAT format of the output (table or csv) for predict mode [default: csv]
--rank {kingdom,phylum,class,order,family,genus,species}
rank of the classification, default: species (when)
--cls CLS classes to predict: either a comma separated list or a path to a text file of classes (one per line), when specified the
--rank and --bins arguments are not allowed.
--bins BINS path to CSV file with two columns with the first being classes and second being bin names, when specified the --cls
argument is not allowed.
--cls CLS classes to predict: either a comma separated list or a path to a text file of classes (one per line),
when specified the --rank, --bins, and --subset arguments are not allowed.
--bins BINS path to CSV file with two columns with the first being classes and second being bin names,
when specified the --rank, --cls, and --subset arguments are not allowed.
--subset SUBSET CSV used to subset the tree of life embeddings. CSV first column
must be named one of kingdom,phylum,class,order,family,genus,species.
When specified the --rank and --bins arguments are not allowed.
When specified the --rank, --bins, and --cls arguments are not allowed.
--k K number of top predictions to show, default: 5
--device=DEVICE device to use matrix math (cpu or cuda or mps) [default: cpu]
--output=OUTFILE print output to file OUTFILE [default: stdout]
Expand Down
10 changes: 4 additions & 6 deletions src/bioclip/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ def create_parser():
cls_group = predict_parser.add_mutually_exclusive_group(required=False)
cls_group.add_argument('--rank', choices=['kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species'],
help='rank of the classification, default: species (when)')
cls_help = "classes to predict: either a comma separated list or a path to a text file of classes (one per line), when specified the --rank and --bins arguments are not allowed."
cls_help = "classes to predict: either a comma separated list or a path to a text file of classes (one per line), when specified the --rank, --bins, and --subset arguments are not allowed."
cls_group.add_argument('--cls', help=cls_help)
cls_group.add_argument('--bins', help='path to CSV file with two columns with the first being classes and second being bin names, when specified the --cls argument is not allowed.')
cls_group.add_argument('--bins', help='path to CSV file with two columns with the first being classes and second being bin names, when specified the --rank, --cls, and --subset arguments are not allowed.')
subset_labels = ','.join(get_rank_labels())
SUBSET_HELP = f"path to CSV file used to subset the tree of life embeddings. CSV first column must be named one of {subset_labels}. When specified the --rank and --bins arguments are not allowed."
predict_parser.add_argument('--subset', help=SUBSET_HELP)
SUBSET_HELP = f"path to CSV file used to subset the tree of life embeddings. CSV first column must be named one of {subset_labels}. When specified the --rank, --bins, and --cls arguments are not allowed."
cls_group.add_argument('--subset', help=SUBSET_HELP)
predict_parser.add_argument('--k', type=int, help='number of top predictions to show, default: 5')

predict_parser.add_argument('--device', **device_arg)
Expand Down Expand Up @@ -148,8 +148,6 @@ def parse_args(input_args=None):
args.rank = Rank[args.rank.upper()]
if not args.k:
args.k = 5
elif args.subset:
raise ValueError("The --subset argument cannot be used with --cls or --bins.")
return args


Expand Down
9 changes: 7 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,18 @@ def test_parse_args(self):
self.assertEqual(args.k, 10)

# test error when using --cls with --subset
with self.assertRaises(ValueError):
with self.assertRaises(SystemExit):
parse_args(['predict', 'image.jpg', '--cls', 'class1,class2', '--subset', 'somefile.cvs'])

# test error when using --bins with --subset
with self.assertRaises(ValueError):
with self.assertRaises(SystemExit):
parse_args(['predict', 'image.jpg', '--bins', 'somefile.csv', '--subset', 'somefile.cvs'])

# test error when using --rank with --subset
with self.assertRaises(SystemExit):
parse_args(['predict', 'image.jpg', '--rank', 'class', '--subset', 'somefile.cvs'])


# example showing filename
args = parse_args(['predict', 'image.jpg', '--cls', 'classes.txt', '--k', '10'])
self.assertEqual(args.cls, 'classes.txt')
Expand Down

0 comments on commit a466465

Please sign in to comment.