Spaces:
Running
Running
Theivaprakasham Hari commited on
Commit ·
90f3f7a
1
Parent(s): 66634a1
added
Browse filesFiles changed (15)
- .gitattributes+1
- app.pyAdded+276
- embed_texts.shAdded+12
- example1_Pararge_aegeria.jpgAdded
- gitattributesAdded+39
- gitignoreAdded+2
- lib.pyAdded+170
- make_txt_embedding.pyAdded+193
- name_lookup.jsonAdded+3
- requirements.txtAdded+4
- templates.pyAdded+82
- test_lib.pyAdded+481
- txt_emb.npyAdded+3
- txt_emb_species.jsonAdded+3
- txt_emb_species.npyAdded+3
5 unmodified lines678910115 unmodified lines*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text5 unmodified lines67891011125 unmodified lines*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.json filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276import collectionsimport heapqimport jsonimport osimport logging
import gradio as grimport numpy as npimport torchimport torch.nn.functional as Ffrom open_clip import create_model, get_tokenizerfrom torchvision import transforms
from templates import openai_imagenet_template
log_format = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"logging.basicConfig(level=logging.INFO, format=log_format)logger = logging.getLogger()
model_str = "hf-hub:imageomics/bioclip"tokenizer_str = "ViT-B-16"
txt_emb_npy = r"txt_emb_species.npy"txt_names_json = r"txt_emb_species.json"
min_prob = 1e-9k = 5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
preprocess_img = transforms.Compose( [ transforms.ToTensor(), transforms.Resize((224, 224), antialias=True), transforms.Normalize( mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711), ), ])
ranks = ("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
open_domain_examples = [
['example1_Pararge_aegeria.jpg', "Species"]
]zero_shot_examples = [ ['example1_Pararge_aegeria.jpg', "Pararge aegeria \nPieris brassicae \nSatyrium w-album \nDanaus chrysippus"] ]
def indexed(lst, indices): return [lst[i] for i in indices]
@torch.no_grad()def get_txt_features(classnames, templates): all_features = [] for classname in classnames: txts = [template(classname) for template in templates] txts = tokenizer(txts).to(device) txt_features = model.encode_text(txts) txt_features = F.normalize(txt_features, dim=-1).mean(dim=0) txt_features /= txt_features.norm() all_features.append(txt_features) all_features = torch.stack(all_features, dim=1) return all_features
@torch.no_grad()def zero_shot_classification(img, cls_str: str) -> dict[str, float]: classes = [cls.strip() for cls in cls_str.split("\n") if cls.strip()] txt_features = get_txt_features(classes, openai_imagenet_template)
img = preprocess_img(img).to(device) img_features = model.encode_image(img.unsqueeze(0)) img_features = F.normalize(img_features, dim=-1)
logits = (model.logit_scale.exp() * img_features @ txt_features).squeeze() probs = F.softmax(logits, dim=0).to("cpu").tolist() return {cls: prob for cls, prob in zip(classes, probs)}
def format_name(taxon, common): taxon = " ".join(taxon) if not common: return taxon return f"{taxon} ({common})"
@torch.no_grad()def open_domain_classification(img, rank: int) -> dict[str, float]: """ Predicts from the entire tree of life. If targeting a higher rank than species, then this function predicts among all species, then sums up species-level probabilities for the given rank. """ img = preprocess_img(img).to(device) img_features = model.encode_image(img.unsqueeze(0)) img_features = F.normalize(img_features, dim=-1)
logits = (model.logit_scale.exp() * img_features @ txt_emb).squeeze() probs = F.softmax(logits, dim=0)
# If predicting species, no need to sum probabilities. if rank + 1 == len(ranks): topk = probs.topk(k) return { format_name(*txt_names[i]): prob for i, prob in zip(topk.indices, topk.values) }
# Sum up by the rank output = collections.defaultdict(float) for i in torch.nonzero(probs > min_prob).squeeze(): output[" ".join(txt_names[i][0][: rank + 1])] += probs[i]
topk_names = heapq.nlargest(k, output, key=output.get)
return {name: output[name] for name in topk_names}
def change_output(choice): return gr.Label(num_top_classes=k, label=ranks[choice], show_label=True, value=None)
js = """function createGradioAnimation() { var container = document.createElement('div'); container.id = 'gradio-animation'; container.style.fontSize = '2em'; container.style.fontWeight = 'bold'; container.style.textAlign = 'center'; container.style.marginBottom = '20px';
var text = 'Global Species Identifier: Powered by Artificial Intelligence'; for (var i = 0; i < text.length; i++) { (function(i){ setTimeout(function(){ var letter = document.createElement('span'); letter.style.opacity = '0'; letter.style.transition = 'opacity 0.5s'; letter.innerText = text[i];
container.appendChild(letter);
setTimeout(function() { letter.style.opacity = '1'; }, 50); }, i * 50); })(i); }
var gradioContainer = document.querySelector('.gradio-container'); gradioContainer.insertBefore(container, gradioContainer.firstChild);
return 'Animation created';}"""
if __name__ == "__main__": logger.info("Starting.") model = create_model(model_str, output_dict=True, require_pretrained=True) model = model.to(device) logger.info("Created model.")
# model = torch.compile(model) logger.info("Compiled model.")
tokenizer = get_tokenizer(tokenizer_str)
txt_emb = torch.from_numpy(np.load(txt_emb_npy, mmap_mode="r")).to(device) with open(txt_names_json) as fd: txt_names = json.load(fd)
done = txt_emb.any(axis=0).sum().item() total = txt_emb.shape[1] status_msg = "" if done != total: status_msg = f"{done}/{total} ({done / total * 100:.1f}%) indexed"
with gr.Blocks(title='Global Species Identifier: Powered by Artificial Intelligence', css="footer {visibility: hidden}", js=js) as app:
gr.Markdown( """ Upload an image of any plant, animal, or other organism, and our Artificial Intelligence-powered tool will identify the species. Our database covers species from around the world, aiming to support biodiversity awareness and conservation efforts.
Features include:- **Instant identification** of plants, animals, and other organisms.- **Detailed information** on species, including habitat, distribution, and conservation status.- An **interactive, user-friendly interface** designed for both experts and enthusiasts.- **Continuous learning and improvement** of AI models to expand the app's knowledge base and accuracy.
Join us in exploring the diversity of life on Earth, powered by the intelligence of technology. Start your journey of discovery today!
""") img_input = gr.Image()
with gr.Tab("Open-Ended"): with gr.Row(): with gr.Column(): rank_dropdown = gr.Dropdown( label="Taxonomic Rank", info="Which taxonomic rank to predict. Fine-grained ranks (genus, species) are more challenging.", choices=ranks, value="Species", type="index", ) open_domain_btn = gr.Button("Submit", variant="primary") with gr.Column(): open_domain_output = gr.Label( num_top_classes=k, label="Prediction", show_label=True, value=None, )
with gr.Row(): gr.Examples( examples=open_domain_examples, inputs=[img_input, rank_dropdown], cache_examples=True, fn=open_domain_classification, outputs=[open_domain_output], )
with gr.Tab("Zero-Shot"): with gr.Row(): with gr.Column(): classes_txt = gr.Textbox( placeholder= "Pararge aegeria \nPieris brassicae \nSatyrium w-album \nDanaus chrysippus\n...", lines=3, label="Classes", show_label=True, info="Use taxonomic names where possible; include common names if possible.", ) zero_shot_btn = gr.Button("Submit", variant="primary")
with gr.Column(): zero_shot_output = gr.Label( num_top_classes=k, label="Prediction", show_label=True )
with gr.Row(): gr.Examples( examples=zero_shot_examples, inputs=[img_input, classes_txt], cache_examples=True, fn=zero_shot_classification, outputs=[zero_shot_output], )
rank_dropdown.change( fn=change_output, inputs=rank_dropdown, outputs=[open_domain_output] )
open_domain_btn.click( fn=open_domain_classification, inputs=[img_input, rank_dropdown], outputs=[open_domain_output], )
zero_shot_btn.click( fn=zero_shot_classification, inputs=[img_input, classes_txt], outputs=zero_shot_output, )
app.queue(max_size=20) app.launch(show_api=False)123456789101112#!/usr/bin/env bash#SBATCH --nodes=1#SBATCH --account=PAS2136#SBATCH --gpus-per-node=1#SBATCH --ntasks-per-node=10#SBATCH --job-name=embed-treeoflife#SBATCH --time=12:00:00#SBATCH --partition=gpu
python make_txt_embedding.py \ --catalog-path /fs/ess/PAS2136/open_clip/data/evobio10m-v3.3/predicted-statistics.csv \ --out-path text_emb.bin123456789101112131415161718192021222324252627282930313233343536373839*.7z filter=lfs diff=lfs merge=lfs -text*.arrow filter=lfs diff=lfs merge=lfs -text*.bin filter=lfs diff=lfs merge=lfs -text*.bz2 filter=lfs diff=lfs merge=lfs -text*.ckpt filter=lfs diff=lfs merge=lfs -text*.ftz filter=lfs diff=lfs merge=lfs -text*.gz filter=lfs diff=lfs merge=lfs -text*.h5 filter=lfs diff=lfs merge=lfs -text*.joblib filter=lfs diff=lfs merge=lfs -text*.lfs.* filter=lfs diff=lfs merge=lfs -text*.mlmodel filter=lfs diff=lfs merge=lfs -text*.model filter=lfs diff=lfs merge=lfs -text*.msgpack filter=lfs diff=lfs merge=lfs -text*.npy filter=lfs diff=lfs merge=lfs -text*.npz filter=lfs diff=lfs merge=lfs -text*.onnx filter=lfs diff=lfs merge=lfs -text*.ot filter=lfs diff=lfs merge=lfs -text*.parquet filter=lfs diff=lfs merge=lfs -text*.pb filter=lfs diff=lfs merge=lfs -text*.pickle filter=lfs diff=lfs merge=lfs -text*.pkl filter=lfs diff=lfs merge=lfs -text*.pt filter=lfs diff=lfs merge=lfs -text*.pth filter=lfs diff=lfs merge=lfs -text*.rar filter=lfs diff=lfs merge=lfs -text*.safetensors filter=lfs diff=lfs merge=lfs -textsaved_model/**/* filter=lfs diff=lfs merge=lfs -text*.tar.* filter=lfs diff=lfs merge=lfs -text*.tar filter=lfs diff=lfs merge=lfs -text*.tflite filter=lfs diff=lfs merge=lfs -text*.tgz filter=lfs diff=lfs merge=lfs -text*.wasm filter=lfs diff=lfs merge=lfs -text*.xz filter=lfs diff=lfs merge=lfs -text*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text
*.json filter=lfs diff=lfs merge=lfs -text*.jpeg filter=lfs diff=lfs merge=lfs -text*.png filter=lfs diff=lfs merge=lfs -text12.venv/__pycache__/123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170"""Mostly a TaxonomicTree class that implements a taxonomy and some helpers for easilywalking and looking in the tree.
A tree is an arrangement of TaxonomicNodes.
"""
import itertoolsimport json
class TaxonomicNode: __slots__ = ("name", "index", "root", "_children")
def __init__(self, name, index, root): self.name = name self.index = index self.root = root self._children = {}
def add(self, name): added = 0 if not name: return added
first, rest = name[0], name[1:] if first not in self._children: self._children[first] = TaxonomicNode(first, self.root.size, self.root) self.root.size += 1
self._children[first].add(rest)
def children(self, name): if not name: return set((child.name, child.index) for child in self._children.values())
first, rest = name[0], name[1:] if first not in self._children: return set()
return self._children[first].children(rest)
def descendants(self, prefix=None): """Iterates over all values in the subtree that match prefix."""
if not prefix: yield (self.name,), self.index for child in self._children.values(): for name, i in child.descendants(): yield (self.name, *name), i return
first, rest = prefix[0], prefix[1:] if first not in self._children: return
for name, i in self._children[first].descendants(rest): yield (self.name, *name), i
def values(self): """Iterates over all (name, i) pairs in the tree.""" yield (self.name,), self.index
for child in self._children.values(): for name, index in child.values(): yield (self.name, *name), index
@classmethod def from_dict(cls, dct, root): node = cls(dct["name"], dct["index"], root) node._children = { child["name"]: cls.from_dict(child, root) for child in dct["children"] } return node
class TaxonomicTree: """ Efficient structure for finding taxonomic names and their descendants. Also returns an integer index i for each possible name. """
def __init__(self): self.kingdoms = {} self.size = 0
def add(self, name: list[str]): if not name: return
first, rest = name[0], name[1:] if first not in self.kingdoms: self.kingdoms[first] = TaxonomicNode(first, self.size, self) self.size += 1
self.kingdoms[first].add(rest)
def children(self, name=None): if not name: return set( (kingdom.name, kingdom.index) for kingdom in self.kingdoms.values() )
first, rest = name[0], name[1:] if first not in self.kingdoms: return set()
return self.kingdoms[first].children(rest)
def descendants(self, prefix=None): """Iterates over all values in the tree that match prefix.""" if not prefix: # Give them all the subnodes for kingdom in self.kingdoms.values(): yield from kingdom.descendants()
return
first, rest = prefix[0], prefix[1:] if first not in self.kingdoms: return
yield from self.kingdoms[first].descendants(rest)
def values(self): """Iterates over all (name, i) pairs in the tree.""" for kingdom in self.kingdoms.values(): yield from kingdom.values()
def __len__(self): return self.size
@classmethod def from_dict(cls, dct): tree = cls() tree.kingdoms = { kingdom["name"]: TaxonomicNode.from_dict(kingdom, tree) for kingdom in dct["kingdoms"] } tree.size = dct["size"] return tree
class TaxonomicJsonEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, TaxonomicNode): return { "name": obj.name, "index": obj.index, "children": list(obj._children.values()), } elif isinstance(obj, TaxonomicTree): return { "kingdoms": list(obj.kingdoms.values()), "size": obj.size, } else: super().default(self, obj)
def batched(iterable, n): # batched('ABCDEFG', 3) --> ABC DEF G if n < 1: raise ValueError("n must be at least one") it = iter(iterable) while batch := tuple(itertools.islice(it, n)): yield zip(*batch)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193"""Makes the entire set of text emebeddings for all possible names in the tree of life. Uses the catalog.csv file from TreeOfLife-10M."""import argparseimport csvimport jsonimport osimport logging
import numpy as npimport torchimport torch.nn.functional as F
from open_clip import create_model, get_tokenizerfrom tqdm import tqdm
import libfrom templates import openai_imagenet_template
log_format = "[%(asctime)s] [%(levelname)s] [%(name)s] %(message)s"logging.basicConfig(level=logging.INFO, format=log_format)logger = logging.getLogger()
model_str = "hf-hub:imageomics/bioclip"tokenizer_str = "ViT-B-16"device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
ranks = ("Kingdom", "Phylum", "Class", "Order", "Family", "Genus", "Species")
@torch.no_grad()def write_txt_features(name_lookup): if os.path.isfile(args.out_path): all_features = np.load(args.out_path) else: all_features = np.zeros((512, len(name_lookup)), dtype=np.float32)
batch_size = args.batch_size // len(openai_imagenet_template) for batch, (names, indices) in enumerate( tqdm( lib.batched(name_lookup.values(), batch_size), desc="txt feats", total=len(name_lookup) // batch_size, ) ): # Skip if any non-zero elements if all_features[:, indices].any(): logger.info(f"Skipping batch {batch}") continue
txts = [ template(name) for name in names for template in openai_imagenet_template ] txts = tokenizer(txts).to(device) txt_features = model.encode_text(txts) txt_features = torch.reshape( txt_features, (len(names), len(openai_imagenet_template), 512) ) txt_features = F.normalize(txt_features, dim=2).mean(dim=1) txt_features /= txt_features.norm(dim=1, keepdim=True) all_features[:, indices] = txt_features.T.cpu().numpy()
if batch % 100 == 0: np.save(args.out_path, all_features)
np.save(args.out_path, all_features)
def convert_txt_features_to_avgs(name_lookup): assert os.path.isfile(args.out_path)
# Put that big boy on the GPU. We're going fast. all_features = torch.from_numpy(np.load(args.out_path)).to(device) logger.info("Loaded text features from disk to %s.", device)
names_by_rank = [set() for rank in ranks] for name, index in tqdm(name_lookup.values()): i = len(name) - 1 names_by_rank[i].add((name, index))
zeroed = 0 for i, rank in reversed(list(enumerate(ranks))): if rank == "Species": continue for name, index in tqdm(names_by_rank[i], desc=rank): species = tuple( zip( *( (d, i) for d, i in name_lookup.descendants(prefix=name) if len(d) >= 6 ) ) ) if not species: logger.warning("No species for %s.", " ".join(name)) all_features[:, index] = 0.0 zeroed += 1 continue
values, indices = species mean = all_features[:, indices].mean(dim=1) all_features[:, index] = F.normalize(mean, dim=0)
out_path, ext = os.path.splitext(args.out_path) np.save(f"{out_path}_avgs{ext}", all_features.cpu().numpy()) if zeroed: logger.warning( "Zeroed out %d nodes because they didn't have any genus or species-level labels.", zeroed, )
def convert_txt_features_to_species_only(name_lookup): assert os.path.isfile(args.out_path)
all_features = np.load(args.out_path) logger.info("Loaded text features from disk.")
species = [(d, i) for d, i in name_lookup.descendants() if len(d) == 7] species_features = np.zeros((512, len(species)), dtype=np.float32) species_names = [""] * len(species)
for new_i, (name, old_i) in enumerate(tqdm(species)): species_features[:, new_i] = all_features[:, old_i] species_names[new_i] = name
out_path, ext = os.path.splitext(args.out_path) np.save(f"{out_path}_species{ext}", species_features) with open(f"{out_path}_species.json", "w") as fd: json.dump(species_names, fd, indent=2)
def get_name_lookup(catalog_path, cache_path): if os.path.isfile(cache_path): with open(cache_path) as fd: lookup = lib.TaxonomicTree.from_dict(json.load(fd)) return lookup
lookup = lib.TaxonomicTree()
with open(catalog_path) as fd: reader = csv.DictReader(fd) for row in tqdm(reader, desc="catalog"): name = [ row["kingdom"], row["phylum"], row["class"], row["order"], row["family"], row["genus"], row["species"], ] if any(not value for value in name): name = name[: name.index("")] lookup.add(name)
with open(args.name_cache_path, "w") as fd: json.dump(lookup, fd, cls=lib.TaxonomicJsonEncoder)
return lookup
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument( "--catalog-path", help="Path to the catalog.csv file from TreeOfLife-10M.", required=True, ) parser.add_argument("--out-path", help="Path to the output file.", required=True) parser.add_argument( "--name-cache-path", help="Path to the name cache file.", default="name_lookup.json", ) parser.add_argument("--batch-size", help="Batch size.", default=2**15, type=int) args = parser.parse_args()
name_lookup = get_name_lookup(args.catalog_path, cache_path=args.name_cache_path) logger.info("Got name lookup.")
model = create_model(model_str, output_dict=True, require_pretrained=True) model = model.to(device) logger.info("Created model.") model = torch.compile(model) logger.info("Compiled model.")
tokenizer = get_tokenizer(tokenizer_str) write_txt_features(name_lookup) convert_txt_features_to_avgs(name_lookup) convert_txt_features_to_species_only(name_lookup)123version https://git-lfs.github.com/spec/v1oid sha256:20d731d9d901f1c17927187bc87e4a2513279845a1a6ba5982dbf779f2ac1434size 264628581234open_clip_torchtorchvisiontorchgradio12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182openai_imagenet_template = [ lambda c: f"a bad photo of a {c}.", lambda c: f"a photo of many {c}.", lambda c: f"a sculpture of a {c}.", lambda c: f"a photo of the hard to see {c}.", lambda c: f"a low resolution photo of the {c}.", lambda c: f"a rendering of a {c}.", lambda c: f"graffiti of a {c}.", lambda c: f"a bad photo of the {c}.", lambda c: f"a cropped photo of the {c}.", lambda c: f"a tattoo of a {c}.", lambda c: f"the embroidered {c}.", lambda c: f"a photo of a hard to see {c}.", lambda c: f"a bright photo of a {c}.", lambda c: f"a photo of a clean {c}.", lambda c: f"a photo of a dirty {c}.", lambda c: f"a dark photo of the {c}.", lambda c: f"a drawing of a {c}.", lambda c: f"a photo of my {c}.", lambda c: f"the plastic {c}.", lambda c: f"a photo of the cool {c}.", lambda c: f"a close-up photo of a {c}.", lambda c: f"a black and white photo of the {c}.", lambda c: f"a painting of the {c}.", lambda c: f"a painting of a {c}.", lambda c: f"a pixelated photo of the {c}.", lambda c: f"a sculpture of the {c}.", lambda c: f"a bright photo of the {c}.", lambda c: f"a cropped photo of a {c}.", lambda c: f"a plastic {c}.", lambda c: f"a photo of the dirty {c}.", lambda c: f"a jpeg corrupted photo of a {c}.", lambda c: f"a blurry photo of the {c}.", lambda c: f"a photo of the {c}.", lambda c: f"a good photo of the {c}.", lambda c: f"a rendering of the {c}.", lambda c: f"a {c} in a video game.", lambda c: f"a photo of one {c}.", lambda c: f"a doodle of a {c}.", lambda c: f"a close-up photo of the {c}.", lambda c: f"a photo of a {c}.", lambda c: f"the origami {c}.", lambda c: f"the {c} in a video game.", lambda c: f"a sketch of a {c}.", lambda c: f"a doodle of the {c}.", lambda c: f"a origami {c}.", lambda c: f"a low resolution photo of a {c}.", lambda c: f"the toy {c}.", lambda c: f"a rendition of the {c}.", lambda c: f"a photo of the clean {c}.", lambda c: f"a photo of a large {c}.", lambda c: f"a rendition of a {c}.", lambda c: f"a photo of a nice {c}.", lambda c: f"a photo of a weird {c}.", lambda c: f"a blurry photo of a {c}.", lambda c: f"a cartoon {c}.", lambda c: f"art of a {c}.", lambda c: f"a sketch of the {c}.", lambda c: f"a embroidered {c}.", lambda c: f"a pixelated photo of a {c}.", lambda c: f"itap of the {c}.", lambda c: f"a jpeg corrupted photo of the {c}.", lambda c: f"a good photo of a {c}.", lambda c: f"a plushie {c}.", lambda c: f"a photo of the nice {c}.", lambda c: f"a photo of the small {c}.", lambda c: f"a photo of the weird {c}.", lambda c: f"the cartoon {c}.", lambda c: f"art of the {c}.", lambda c: f"a drawing of the {c}.", lambda c: f"a photo of the large {c}.", lambda c: f"a black and white photo of a {c}.", lambda c: f"the plushie {c}.", lambda c: f"a dark photo of a {c}.", lambda c: f"itap of a {c}.", lambda c: f"graffiti of the {c}.", lambda c: f"a toy {c}.", lambda c: f"itap of my {c}.", lambda c: f"a photo of a cool {c}.", lambda c: f"a photo of a small {c}.", lambda c: f"a tattoo of the {c}.",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481import lib
def test_taxonomiclookup_empty(): lookup = lib.TaxonomicTree() assert lookup.size == 0
def test_taxonomiclookup_kingdom_size(): lookup = lib.TaxonomicTree()
lookup.add(("Animalia",))
assert lookup.size == 1
def test_taxonomiclookup_genus_size(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", ) )
assert lookup.size == 6
def test_taxonomictree_kingdom_children(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", ) )
expected = set([("Animalia", 0)]) actual = lookup.children() assert actual == expected
def test_taxonomiclookup_children_of_animal_only_birds(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", "leucocephalus", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "scutulata", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "plesseni", ) )
actual = lookup.children(("Animalia",)) expected = set([("Chordata", 1)]) assert actual == expected
def test_taxonomiclookup_children_of_animal(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", "leucocephalus", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "scutulata", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "plesseni", ) ) lookup.add( ( "Animalia", "Chordata", "Mammalia", "Primates", "Hominidae", "Gorilla", "gorilla", ) ) lookup.add( ( "Animalia", "Arthropoda", "Insecta", "Hymenoptera", "Apidae", "Bombus", "balteatus", ) )
actual = lookup.children(("Animalia",)) expected = set([("Chordata", 1), ("Arthropoda", 17)]) assert actual == expected
def test_taxonomiclookup_children_of_chordata(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", "leucocephalus", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "scutulata", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "plesseni", ) ) lookup.add( ( "Animalia", "Chordata", "Mammalia", "Primates", "Hominidae", "Gorilla", "gorilla", ) ) lookup.add( ( "Animalia", "Arthropoda", "Insecta", "Hymenoptera", "Apidae", "Bombus", "balteatus", ) )
actual = lookup.children(("Animalia", "Chordata")) expected = set([("Aves", 2), ("Mammalia", 12)]) assert actual == expected
def test_taxonomiclookup_children_of_strigiformes(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", "leucocephalus", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "scutulata", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "plesseni", ) ) lookup.add( ( "Animalia", "Chordata", "Mammalia", "Primates", "Hominidae", "Gorilla", "gorilla", ) ) lookup.add( ( "Animalia", "Arthropoda", "Insecta", "Hymenoptera", "Apidae", "Bombus", "balteatus", ) )
actual = lookup.children(("Animalia", "Chordata", "Aves", "Strigiformes")) expected = set([("Strigidae", 8)]) assert actual == expected
def test_taxonomiclookup_children_of_ninox(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", "leucocephalus", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "scutulata", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "plesseni", ) ) lookup.add( ( "Animalia", "Chordata", "Mammalia", "Primates", "Hominidae", "Gorilla", "gorilla", ) ) lookup.add( ( "Animalia", "Arthropoda", "Insecta", "Hymenoptera", "Apidae", "Bombus", "balteatus", ) )
actual = lookup.children( ("Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox") ) expected = set([("scutulata", 10), ("plesseni", 11)]) assert actual == expected
def test_taxonomiclookup_children_of_gorilla(): lookup = lib.TaxonomicTree()
lookup.add( ( "Animalia", "Chordata", "Aves", "Accipitriformes", "Accipitridae", "Halieaeetus", "leucocephalus", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "scutulata", ) ) lookup.add( ( "Animalia", "Chordata", "Aves", "Strigiformes", "Strigidae", "Ninox", "plesseni", ) ) lookup.add( ( "Animalia", "Chordata", "Mammalia", "Primates", "Hominidae", "Gorilla", "gorilla", ) ) lookup.add( ( "Animalia", "Arthropoda", "Insecta", "Hymenoptera", "Apidae", "Bombus", "balteatus", ) )
actual = lookup.children( ( "Animalia", "Chordata", "Mammalia", "Primates", "Hominidae", "Gorilla", "gorilla", ) ) expected = set() assert actual == expected
def test_taxonomictree_descendants_last(): lookup = lib.TaxonomicTree()
lookup.add(("A", "B", "C", "D", "E", "F", "G"))
actual = list(lookup.descendants(("A", "B", "C", "D", "E", "F", "G")))
expected = [ (("A", "B", "C", "D", "E", "F", "G"), 6), ] assert actual == expected
def test_taxonomictree_descendants_entire_tree(): lookup = lib.TaxonomicTree()
lookup.add(("A", "B"))
actual = list(lookup.descendants())
expected = [ (("A",), 0), (("A", "B"), 1), ] assert actual == expected
def test_taxonomictree_descendants_entire_tree_with_prefix(): lookup = lib.TaxonomicTree()
lookup.add(("A", "B"))
actual = list(lookup.descendants(prefix=("A",)))
expected = [ (("A",), 0), (("A", "B"), 1), ] assert actual == expected
def test_taxonomictree_descendants_general(): lookup = lib.TaxonomicTree()
lookup.add(("A", "B", "C", "D", "E", "F", "G"))
actual = list(lookup.descendants(("A", "B", "C", "D")))
expected = [ (("A", "B", "C", "D"), 3), (("A", "B", "C", "D", "E"), 4), (("A", "B", "C", "D", "E", "F"), 5), (("A", "B", "C", "D", "E", "F", "G"), 6), ] assert actual == expected123version https://git-lfs.github.com/spec/v1oid sha256:b4a3c3412c3dae49cf92cc760aba5ee84227362adf1eb08f04dd50ee2a756e43size 969818240123version https://git-lfs.github.com/spec/v1oid sha256:844e6fabc06cac072214d566b78f40825b154efa9479eb11285030ca038b2ecesize 65731052123version https://git-lfs.github.com/spec/v1oid sha256:91ce02dff2433222e3138b8bf7eefa1dd74b30f4d406c16cd3301f66d65ab4edsize 787435648.gitattributes+1
app.pyAdded+276
embed_texts.shAdded+12
example1_Pararge_aegeria.jpgAdded

gitattributesAdded+39
gitignoreAdded+2
lib.pyAdded+170
make_txt_embedding.pyAdded+193
name_lookup.jsonAdded+3
requirements.txtAdded+4
templates.pyAdded+82
test_lib.pyAdded+481
txt_emb.npyAdded+3
txt_emb_species.jsonAdded+3
txt_emb_species.npyAdded+3