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