Coverage for src/bioimageio/spec/generic/_v0_2_converter.py: 83%
75 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 16:20 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-17 16:20 +0000
1from __future__ import annotations
3import collections.abc
4from collections.abc import Mapping
5from typing import Any
7from .._internal.io import BioimageioYamlContent
8from .._internal.type_guards import is_mapping
11def convert_from_older_format(data: BioimageioYamlContent) -> None:
12 """convert raw RDF data of an older format where possible"""
13 # check if we have future format version
14 if "format_version" not in data:
15 return
17 fv = data["format_version"]
18 if isinstance(fv, str) and tuple(map(int, fv.split(".")[:2])) > (0, 2):
19 return
21 # we unofficially accept strings as author entries
22 authors = data.get("authors")
23 if isinstance(authors, list):
24 data["authors"] = [{"name": a} if isinstance(a, str) else a for a in authors]
26 if data.get("format_version") in ("0.2.0", "0.2.1"):
27 data["format_version"] = "0.2.2"
29 if data.get("format_version") == "0.2.2":
30 remove_slashes_from_names(data)
31 data["format_version"] = "0.2.3"
33 if data.get("format_version") == "0.2.3":
34 if isinstance(config := data.get("config"), dict) and isinstance(
35 bconfig := config.get("bioimageio"), dict
36 ):
37 if (nickname := bconfig.get("nickname")) is not None:
38 data["id"] = nickname
40 if (nickname_icon := bconfig.get("nickname_icon")) is not None:
41 data["id_emoji"] = nickname_icon
43 data["format_version"] = "0.2.4"
45 remove_doi_prefix(data)
46 remove_gh_prefix(data)
49def remove_slashes_from_names(data: dict[Any, Any]) -> None:
50 NAME = "name"
51 if NAME in data and isinstance(data[NAME], str):
52 data[NAME] = data[NAME].replace("/", "").replace("\\", "")
54 # update authors and maintainers
55 def rm_slashes_in_person_name(
56 person: Any | Mapping[Any | str, Any],
57 ) -> Any:
58 if not is_mapping(person):
59 return person
61 new_person = dict(person)
62 if isinstance(n := person.get(NAME), str):
63 new_person[NAME] = n.replace("/", "").replace("\\", "")
65 return new_person
67 for group in ("authors", "maintainers"):
68 persons = data.get(group)
69 if isinstance(persons, collections.abc.Sequence):
70 data[group] = [rm_slashes_in_person_name(p) for p in persons] # type: ignore
73DOI_PREFIXES = ("https://doi.org/", "http://dx.doi.org/")
76def remove_doi_prefix(data: BioimageioYamlContent) -> None:
77 """we unofficially accept DOIs starting with "https://doi.org/" here we remove this prefix"""
78 cite = data.get("cite")
79 if isinstance(cite, collections.abc.Sequence):
80 new_cite = list(cite)
81 for i in range(len(new_cite)):
82 cite_entry = new_cite[i]
83 if not isinstance(cite_entry, collections.abc.Mapping):
84 continue
86 doi = cite_entry.get("doi")
87 if not isinstance(doi, str):
88 continue
90 for doi_prefix in DOI_PREFIXES:
91 if doi.startswith(doi_prefix):
92 doi = doi[len(doi_prefix) :]
93 break
94 else:
95 continue
97 new_cite_entry = dict(cite_entry)
98 new_cite_entry["doi"] = doi
99 new_cite[i] = new_cite_entry
101 data["cite"] = new_cite
104def remove_gh_prefix(data: BioimageioYamlContent) -> None:
105 def rm_gh(field_name: str):
106 authors = data.get(field_name)
107 if not isinstance(authors, list):
108 return
110 for a in authors:
111 if (
112 isinstance(a, dict)
113 and "github_user" in a
114 and isinstance(a["github_user"], str)
115 and a["github_user"].startswith("https://github.com/")
116 ):
117 a["github_user"] = a["github_user"][len("https://github.com/") :]
119 rm_gh("authors")
120 rm_gh("maintainers")