Coverage for src/bioimageio/core/_collection.py: 0%
54 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 14:35 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-20 14:35 +0000
1"""Collection utilities for bioimageio"""
3import json
4import warnings
5from collections.abc import Mapping, Sequence
6from functools import cache
7from typing import Literal, TypedDict
9from bioimageio.spec.common import Sha256
10from bioimageio.spec.utils import get_reader
12from ._settings import settings
15class IndexItemVersion(TypedDict):
16 version: str
17 source: str
18 sha256: Sha256
21class IndexItem(TypedDict):
22 id: str
23 type: str
24 versions: Sequence[IndexItemVersion]
27class Index(TypedDict):
28 items: Sequence[IndexItem]
29 total: int
30 count_per_type: Mapping[str, int]
31 timestamp: str
34class IdPartsEntry(TypedDict):
35 nouns: Mapping[str, str]
36 adjectives: Sequence[str]
39class CollectionConfig(TypedDict):
40 id_parts: Mapping[Literal["model", "dataset", "notebook"], IdPartsEntry]
41 reviewers: Mapping[str, Sequence[str]]
44@cache
45def load_json(url: str):
46 reader = get_reader(url)
47 return json.load(reader)
50def load_index() -> Index:
51 return load_json(settings.collection_index_url)
54def load_collection_config() -> CollectionConfig:
55 return load_json(settings.collection_config_url)
58@cache
59def load_hyphened_nouns() -> set[str]:
60 """get all nouns with hyphens that could be part of a nickname, e.g. 't-rex'"""
61 return {
62 noun
63 for id_parts in load_collection_config()["id_parts"].values()
64 for noun in id_parts.get("nouns", [])
65 if "-" in noun
66 }
69def lookup_from_index(source: str) -> tuple[str, dict[Literal["sha256"], Sha256]]:
70 index = load_index()
71 if source.startswith("bioimage-io/") and source.count("/") == 2:
72 version = source.split("/")[-1]
73 source = source[: -(len(version) + 1)]
74 else:
75 version = None
77 for item in index["items"]:
78 if item["id"] in (source, f"bioimage-io/{source}"):
79 v = item["versions"][-1]
80 if version is not None:
81 for v in item["versions"]:
82 if v["version"] == version:
83 break
84 else:
85 warnings.warn(
86 f"Version {version} not found in index, using latest version."
87 )
88 else:
89 continue
91 return v["source"], {"sha256": v["sha256"]}
93 return source, {}
96def get_resource_icon(nickname: str, rtype: str) -> str:
97 """Get emoji for a resource, matching to its nickname noun. nicknames are of the form "{adjective}-{noun}", e.g. "affable-shark"."""
98 if "-" not in nickname:
99 return " "
101 # remove hyphen from noun part of nickname, e.g. "laid-back-t-rex" -> "laid-back-trex"
102 for hyphened_noun in load_hyphened_nouns():
103 if nickname.endswith(hyphened_noun):
104 nickname = nickname[: -len(hyphened_noun)] + hyphened_noun.replace("-", "")
106 # last hyphen now sparates adjective and noun, e.g. "laid-back-trex" -> "laid-back" and "trex"
107 noun = nickname[nickname.rfind("-") + 1 :].replace("-", "")
108 try:
109 ret = {
110 k.replace("-", ""): v
111 for k, v in load_collection_config()["id_parts"][
112 rtype if rtype in ("model", "dataset", "notebook") else "notebook"
113 ]
114 .get("nouns", {})
115 .items()
116 }.get(noun, " ")
117 except Exception as e:
118 warnings.warn(f"Error getting icon for {rtype} {nickname}: {e}")
119 ret = " "
121 return ret