Coverage for src/bioimageio/spec/generic/v0_2.py: 93%

188 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-17 16:20 +0000

1from __future__ import annotations 

2 

3import string 

4from collections.abc import Callable, Mapping, Sequence 

5from typing import ( 

6 TYPE_CHECKING, 

7 Annotated, 

8 Any, 

9 ClassVar, 

10 Literal, 

11 TypeVar, 

12 cast, 

13) 

14 

15import annotated_types 

16import pydantic 

17from annotated_types import Len, LowerCase, MaxLen 

18from pydantic import ( 

19 EmailStr, 

20 Field, 

21 RootModel, 

22 ValidationInfo, 

23 field_validator, 

24 model_validator, 

25) 

26from typing_extensions import Self, assert_never 

27 

28from .._internal.common_nodes import Node, ResourceDescrBase 

29from .._internal.constants import TAG_CATEGORIES 

30from .._internal.field_warning import as_warning, issue_warning, warn 

31from .._internal.io import ( 

32 BioimageioYamlContent, 

33 WithSuffix, 

34 YamlValue, 

35 wo_special_file_name, 

36) 

37from .._internal.io_packaging import FileSource_package, include_in_package 

38from .._internal.type_guards import is_sequence 

39from .._internal.types import ( 

40 DeprecatedLicenseId, 

41 FilePath, 

42 FileSource, 

43 LicenseId, 

44 NotEmpty, 

45) 

46from .._internal.types import Doi as Doi 

47from .._internal.types import OrcidId as OrcidId 

48from .._internal.types import RelativeFilePath as RelativeFilePath 

49from .._internal.url import HttpUrl as HttpUrl 

50from .._internal.validated_string import ValidatedString 

51from .._internal.validator_annotations import AfterValidator, RestrictCharacters 

52from .._internal.version_type import Version as Version 

53from ._v0_2_converter import convert_from_older_format as _convert_from_older_format 

54 

55 

56class ResourceId(ValidatedString): 

57 root_model: ClassVar[type[RootModel[Any]]] = RootModel[ 

58 Annotated[ 

59 NotEmpty[str], 

60 AfterValidator(str.lower), # convert upper case on the fly 

61 RestrictCharacters(string.ascii_lowercase + string.digits + "_-/."), 

62 annotated_types.Predicate( 

63 lambda s: not (s.startswith("/") or s.endswith("/")) 

64 ), 

65 ] 

66 ] 

67 

68 

69KNOWN_SPECIFIC_RESOURCE_TYPES = ( 

70 "application", 

71 "collection", 

72 "dataset", 

73 "model", 

74 "notebook", 

75) 

76 

77VALID_COVER_IMAGE_EXTENSIONS = ( 

78 ".gif", 

79 ".jpeg", 

80 ".jpg", 

81 ".png", 

82 ".svg", 

83 ".tif", 

84 ".tiff", 

85) 

86 

87 

88_FileSource_cover = Annotated[ 

89 FileSource_package, 

90 WithSuffix(VALID_COVER_IMAGE_EXTENSIONS, case_sensitive=False), 

91] 

92 

93 

94class AttachmentsDescr(Node, extra="allow"): 

95 files: list[FileSource_package] = Field( 

96 default_factory=cast(Callable[[], list[FileSource]], list) 

97 ) 

98 """File attachments""" 

99 

100 

101def _remove_slashes(s: str): 

102 return s.replace("/", "").replace("\\", "") 

103 

104 

105class Uploader(Node): 

106 email: EmailStr 

107 """Email""" 

108 name: Annotated[str, AfterValidator(_remove_slashes)] | None = None 

109 """name""" 

110 

111 

112class _Person(Node): 

113 affiliation: str | None = None 

114 """Affiliation""" 

115 

116 email: EmailStr | None = None 

117 """Email""" 

118 

119 orcid: Annotated[OrcidId | None, Field(examples=["0000-0001-2345-6789"])] = None 

120 """An [ORCID iD](https://support.orcid.org/hc/en-us/sections/360001495313-What-is-ORCID 

121 ) in hyphenated groups of 4 digits, (and [valid]( 

122 https://support.orcid.org/hc/en-us/articles/360006897674-Structure-of-the-ORCID-Identifier 

123 ) as per ISO 7064 11,2.) 

124 """ 

125 

126 

127class Author(_Person): 

128 name: Annotated[str, AfterValidator(_remove_slashes)] 

129 github_user: str | None = None # TODO: validate github_user 

130 

131 

132class Maintainer(_Person): 

133 name: Annotated[str, AfterValidator(_remove_slashes)] | None = None 

134 github_user: str 

135 

136 

137class BadgeDescr(Node): 

138 """A custom badge""" 

139 

140 label: Annotated[str, Field(examples=["Open in Colab"])] 

141 """badge label to display on hover""" 

142 

143 icon: Annotated[ 

144 Annotated[ 

145 FilePath | RelativeFilePath, 

146 AfterValidator(wo_special_file_name), 

147 include_in_package, 

148 ] 

149 | HttpUrl 

150 | pydantic.HttpUrl 

151 | None, 

152 Field(examples=["https://colab.research.google.com/assets/colab-badge.svg"]), 

153 ] = None 

154 """badge icon (included in bioimage.io package if not a URL)""" 

155 

156 url: Annotated[ 

157 HttpUrl, 

158 Field( 

159 examples=[ 

160 "https://colab.research.google.com/github/HenriquesLab/ZeroCostDL4Mic/blob/master/Colab_notebooks/U-net_2D_ZeroCostDL4Mic.ipynb" 

161 ] 

162 ), 

163 ] 

164 """target URL""" 

165 

166 

167class CiteEntry(Node): 

168 text: str 

169 """free text description""" 

170 

171 doi: Doi | None = None 

172 """A digital object identifier (DOI) is the prefered citation reference. 

173 See https://www.doi.org/ for details. (alternatively specify `url`)""" 

174 

175 @field_validator("doi", mode="before") 

176 @classmethod 

177 def accept_prefixed_doi(cls, doi: Any) -> Any: 

178 if isinstance(doi, str): 

179 for doi_prefix in ("https://doi.org/", "http://dx.doi.org/"): 

180 if doi.startswith(doi_prefix): 

181 doi = doi[len(doi_prefix) :] 

182 break 

183 

184 return doi 

185 

186 url: str | None = None 

187 """URL to cite (preferably specify a `doi` instead)""" 

188 

189 @model_validator(mode="after") 

190 def _check_doi_or_url(self) -> Self: 

191 if not self.doi and not self.url: 

192 raise ValueError("Either 'doi' or 'url' is required") 

193 

194 return self 

195 

196 

197class LinkedResource(Node): 

198 """Reference to a bioimage.io resource""" 

199 

200 id: ResourceId 

201 """A valid resource `id` from the bioimage.io collection.""" 

202 

203 version_number: int | None = None 

204 """version number (n-th published version, not the semantic version) of linked resource""" 

205 

206 

207class GenericModelDescrBase(ResourceDescrBase): 

208 """Base for all resource descriptions including of model descriptions""" 

209 

210 name: Annotated[ 

211 NotEmpty[str], warn(Annotated[str, MaxLen(128)], "Longer than 128 characters.") 

212 ] 

213 """A human-friendly name of the resource description""" 

214 

215 description: str 

216 

217 covers: list[_FileSource_cover] = Field( 

218 default_factory=cast(Callable[[], list[_FileSource_cover]], list), 

219 examples=[["cover.png"]], 

220 description=( 

221 "Cover images. Please use an image smaller than 500KB and an aspect" 

222 " ratio width to height of 2:1.\nThe supported image formats are:" 

223 f" {VALID_COVER_IMAGE_EXTENSIONS}" 

224 ), 

225 ) 

226 """Cover images. Please use an image smaller than 500KB and an aspect ratio width to height of 2:1.""" 

227 

228 id_emoji: ( 

229 Annotated[str, Len(min_length=1, max_length=1), Field(examples=["🦈", "🦥"])] 

230 | None 

231 ) = None 

232 """UTF-8 emoji for display alongside the `id`.""" 

233 

234 authors: list[Author] = Field( # pyright: ignore[reportUnknownVariableType] 

235 default_factory=list 

236 ) 

237 """The authors are the creators of the RDF and the primary points of contact.""" 

238 

239 @field_validator("authors", mode="before") 

240 @classmethod 

241 def accept_author_strings(cls, authors: Any | Sequence[Any]) -> Any: 

242 """we unofficially accept strings as author entries""" 

243 if is_sequence(authors): 

244 authors = [{"name": a} if isinstance(a, str) else a for a in authors] 

245 

246 if not authors: 

247 issue_warning("missing", value=authors, field="authors") 

248 

249 return authors 

250 

251 attachments: AttachmentsDescr | None = None 

252 """file and other attachments""" 

253 

254 cite: list[CiteEntry] = Field( # pyright: ignore[reportUnknownVariableType] 

255 default_factory=list 

256 ) 

257 """citations""" 

258 

259 @field_validator("cite", mode="after") 

260 @classmethod 

261 def _warn_empty_cite(cls, value: Any): 

262 if not value: 

263 issue_warning("missing", value=value, field="cite") 

264 

265 return value 

266 

267 config: Annotated[ 

268 dict[str, YamlValue], 

269 Field( 

270 examples=[ 

271 { 

272 "bioimageio": { 

273 "my_custom_key": 3837283, 

274 "another_key": {"nested": "value"}, 

275 }, 

276 "imagej": {"macro_dir": "path/to/macro/file"}, 

277 } 

278 ], 

279 ), 

280 ] = Field(default_factory=dict) 

281 """A field for custom configuration that can contain any keys not present in the RDF spec. 

282 This means you should not store, for example, a github repo URL in `config` since we already have the 

283 `git_repo` field defined in the spec. 

284 Keys in `config` may be very specific to a tool or consumer software. To avoid conflicting definitions, 

285 it is recommended to wrap added configuration into a sub-field named with the specific domain or tool name, 

286 for example: 

287 ```yaml 

288 config: 

289 bioimageio: # here is the domain name 

290 my_custom_key: 3837283 

291 another_key: 

292 nested: value 

293 imagej: # config specific to ImageJ 

294 macro_dir: path/to/macro/file 

295 ``` 

296 If possible, please use [`snake_case`](https://en.wikipedia.org/wiki/Snake_case) for keys in `config`. 

297 You may want to list linked files additionally under `attachments` to include them when packaging a resource 

298 (packaging a resource means downloading/copying important linked files and creating a ZIP archive that contains 

299 an altered rdf.yaml file with local references to the downloaded files)""" 

300 

301 download_url: HttpUrl | None = None 

302 """URL to download the resource from (deprecated)""" 

303 

304 git_repo: Annotated[ 

305 str | None, 

306 Field( 

307 examples=[ 

308 "https://github.com/bioimage-io/spec-bioimage-io/tree/main/example_descriptions/models/unet2d_nuclei_broad" 

309 ], 

310 ), 

311 ] = None 

312 """A URL to the Git repository where the resource is being developed.""" 

313 

314 icon: Annotated[str, Len(min_length=1, max_length=2)] | FileSource | None = None 

315 """An icon for illustration""" 

316 

317 links: Annotated[ 

318 list[str], 

319 Field( 

320 examples=[ 

321 ( 

322 "ilastik/ilastik", 

323 "deepimagej/deepimagej", 

324 "zero/notebook_u-net_3d_zerocostdl4mic", 

325 ) 

326 ], 

327 ), 

328 ] = Field(default_factory=list) 

329 """IDs of other bioimage.io resources""" 

330 

331 uploader: Uploader | None = None 

332 """The person who uploaded the model (e.g. to bioimage.io)""" 

333 

334 # TODO: (py>3.8) remove pyright ignore 

335 maintainers: list[Maintainer] = Field( # pyright: ignore[reportUnknownVariableType] 

336 default_factory=list 

337 ) 

338 """Maintainers of this resource. 

339 If not specified `authors` are maintainers and at least some of them should specify their `github_user` name""" 

340 

341 rdf_source: FileSource | None = None 

342 """Resource description file (RDF) source; used to keep track of where an rdf.yaml was loaded from. 

343 Do not set this field in a YAML file.""" 

344 

345 tags: Annotated[ 

346 list[str], 

347 Field(examples=[("unet2d", "pytorch", "nucleus", "segmentation", "dsb2018")]), 

348 ] = Field(default_factory=list) 

349 """Associated tags""" 

350 

351 @as_warning 

352 @field_validator("tags") 

353 @classmethod 

354 def warn_about_tag_categories( 

355 cls, value: list[str], info: ValidationInfo 

356 ) -> list[str]: 

357 categories = TAG_CATEGORIES.get(info.data["type"], {}) 

358 missing_categories: list[Mapping[str, Sequence[str]]] = [] 

359 for cat, entries in categories.items(): 

360 if not any(e in value for e in entries): 

361 missing_categories.append({cat: entries}) 

362 

363 if missing_categories: 

364 raise ValueError( 

365 "Missing tags from bioimage.io categories: {missing_categories}" 

366 ) 

367 

368 return value 

369 

370 version: Version | None = None 

371 """The version of the resource following SemVer 2.0.""" 

372 

373 version_number: int | None = None 

374 """version number (n-th published version, not the semantic version)""" 

375 

376 

377class GenericDescrBase(GenericModelDescrBase): 

378 """Base for all resource descriptions except for the model descriptions""" 

379 

380 implemented_format_version: ClassVar[Literal["0.2.4"]] = "0.2.4" 

381 if TYPE_CHECKING: 

382 format_version: Literal["0.2.4"] = "0.2.4" 

383 else: 

384 format_version: Literal["0.2.4"] 

385 """The format version of this resource specification 

386 (not the `version` of the resource description) 

387 When creating a new resource always use the latest micro/patch version described here. 

388 The `format_version` is important for any consumer software to understand how to parse the fields. 

389 """ 

390 

391 @model_validator(mode="before") 

392 @classmethod 

393 def _convert_from_older_format( 

394 cls, data: BioimageioYamlContent, / 

395 ) -> BioimageioYamlContent: 

396 _convert_from_older_format(data) 

397 return data 

398 

399 badges: list[BadgeDescr] = Field( # pyright: ignore[reportUnknownVariableType] 

400 default_factory=list 

401 ) 

402 """badges associated with this resource""" 

403 

404 documentation: Annotated[ 

405 FileSource | None, 

406 Field( 

407 examples=[ 

408 "https://raw.githubusercontent.com/bioimage-io/spec-bioimage-io/main/example_descriptions/models/unet2d_nuclei_broad/README.md", 

409 "README.md", 

410 ], 

411 ), 

412 ] = None 

413 """URL or relative path to a markdown file with additional documentation. 

414 The recommended documentation file name is `README.md`. An `.md` suffix is mandatory.""" 

415 

416 license: Annotated[ 

417 LicenseId | DeprecatedLicenseId | str | None, 

418 Field(union_mode="left_to_right", examples=["CC0-1.0", "MIT", "BSD-2-Clause"]), 

419 ] = None 

420 """A [SPDX license identifier](https://spdx.org/licenses/). 

421 We do not support custom license beyond the SPDX license list, if you need that please 

422 [open a GitHub issue](https://github.com/bioimage-io/spec-bioimage-io/issues/new/choose 

423 ) to discuss your intentions with the community.""" 

424 

425 @field_validator("license", mode="after") 

426 @classmethod 

427 def deprecated_spdx_license( 

428 cls, value: LicenseId | DeprecatedLicenseId | str | None 

429 ): 

430 if isinstance(value, LicenseId): 

431 pass 

432 elif value is None: 

433 issue_warning("missing", value=value, field="license") 

434 elif isinstance(value, DeprecatedLicenseId): 

435 issue_warning( 

436 "'{value}' is a deprecated license identifier.", 

437 value=value, 

438 field="license", 

439 ) 

440 elif isinstance(value, str): 

441 issue_warning( 

442 "'{value}' is an unknown license identifier.", 

443 value=value, 

444 field="license", 

445 ) 

446 else: 

447 assert_never(value) 

448 

449 return value 

450 

451 

452ResourceDescrType = TypeVar("ResourceDescrType", bound=GenericDescrBase) 

453 

454 

455class GenericDescr(GenericDescrBase, extra="ignore"): 

456 """Specification of the fields used in a generic bioimage.io-compliant resource description file (RDF). 

457 

458 An RDF is a YAML file that describes a resource such as a model, a dataset, or a notebook. 

459 Note that those resources are described with a type-specific RDF. 

460 Use this generic resource description, if none of the known specific types matches your resource. 

461 """ 

462 

463 type: Annotated[str, LowerCase, Field(frozen=True)] = "generic" 

464 """The resource type assigns a broad category to the resource.""" 

465 

466 id: ( 

467 Annotated[ResourceId, Field(examples=["affable-shark", "ambitious-sloth"])] 

468 | None 

469 ) = None 

470 """bioimage.io-wide unique resource identifier 

471 assigned by bioimage.io; version **un**specific.""" 

472 

473 source: HttpUrl | None = None 

474 """The primary source of the resource""" 

475 

476 @field_validator("type", mode="after") 

477 @classmethod 

478 def check_specific_types(cls, value: str) -> str: 

479 if value in KNOWN_SPECIFIC_RESOURCE_TYPES: 

480 raise ValueError( 

481 f"Use the {value} description instead of this generic description for" 

482 + f" your '{value}' resource." 

483 ) 

484 

485 return value