Skip to content

Commit 145cc20

Browse files
committed
more explicit handling of format_version arg
1 parent a085d65 commit 145cc20

File tree

3 files changed

+58
-28
lines changed

3 files changed

+58
-28
lines changed

src/bioimageio/spec/_description.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,43 +100,43 @@ def dump_description(
100100
{
101101
"0.2": GenericDescr_v0_2,
102102
"0.3": GenericDescr_v0_3,
103-
None: GenericDescr,
103+
"latest": GenericDescr,
104104
}
105105
),
106106
"generic": MappingProxyType(
107107
{
108108
"0.2": GenericDescr_v0_2,
109109
"0.3": GenericDescr_v0_3,
110-
None: GenericDescr,
110+
"latest": GenericDescr,
111111
}
112112
),
113113
"application": MappingProxyType(
114114
{
115115
"0.2": ApplicationDescr_v0_2,
116116
"0.3": ApplicationDescr_v0_3,
117-
None: ApplicationDescr,
117+
"latest": ApplicationDescr,
118118
}
119119
),
120120
"dataset": MappingProxyType(
121121
{
122122
"0.2": DatasetDescr_v0_2,
123123
"0.3": DatasetDescr_v0_3,
124-
None: DatasetDescr,
124+
"latest": DatasetDescr,
125125
}
126126
),
127127
"notebook": MappingProxyType(
128128
{
129129
"0.2": NotebookDescr_v0_2,
130130
"0.3": NotebookDescr_v0_3,
131-
None: NotebookDescr,
131+
"latest": NotebookDescr,
132132
}
133133
),
134134
"model": MappingProxyType(
135135
{
136136
"0.3": ModelDescr_v0_4,
137137
"0.4": ModelDescr_v0_4,
138138
"0.5": ModelDescr_v0_5,
139-
None: ModelDescr,
139+
"latest": ModelDescr,
140140
}
141141
),
142142
}
@@ -184,8 +184,14 @@ def build_description(
184184
Args:
185185
content: loaded rdf.yaml file (loaded with YAML, not bioimageio.spec)
186186
context: validation context to use during validation
187-
format_version: (optional) use this argument to load the resource and
188-
convert its metadata to a higher format_version
187+
format_version:
188+
(optional) use this argument to load the resource and
189+
convert its metadata to a higher format_version.
190+
Note:
191+
- Use "latest" to convert to the latest available format version.
192+
- Use "discover" to use the format version specified in the RDF.
193+
- Only considers major.minor format version, ignores patch version.
194+
- Conversion to lower format versions is not supported.
189195
190196
Returns:
191197
An object holding all metadata of the bioimage.io resource
@@ -213,7 +219,13 @@ def validate_format(
213219
214220
Args:
215221
data: Dictionary holding the raw bioimageio.yaml content.
216-
format_version: Format version to (update to and) use for validation.
222+
format_version:
223+
Format version to (update to and) use for validation.
224+
Note:
225+
- Use "latest" to convert to the latest available format version.
226+
- Use "discover" to use the format version specified in the RDF.
227+
- Only considers major.minor format version, ignores patch version.
228+
- Conversion to lower format versions is not supported.
217229
context: Validation context, see `bioimagieo.spec.ValidationContext`
218230
219231
Note:

src/bioimageio/spec/_description_impl.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,34 @@ def get_rd_class_impl(
2626
Optional[str], Mapping[Optional[str], Type[ResourceDescrT]]
2727
],
2828
) -> Type[ResourceDescrT]:
29+
"""get the resource description class for the given type and format version"""
2930
assert None in descriptions_map
30-
assert all(None in version_map for version_map in descriptions_map.values())
31+
assert all("latest" in version_map for version_map in descriptions_map.values())
3132
assert all(
32-
fv is None or fv.count(".") == 1
33+
fv == "latest" or fv.count(".") == 1
3334
for version_map in descriptions_map.values()
3435
for fv in version_map
3536
)
3637
if not isinstance(typ, str) or typ not in descriptions_map:
3738
typ = None
3839

3940
format_version = str(format_version)
40-
if (ndots := format_version.count(".")) == 0:
41+
if format_version == "latest" or (ndots := format_version.count(".")) == 1:
42+
use_format_version = format_version
43+
elif ndots == 0:
4144
use_format_version = format_version + ".0"
42-
elif ndots == 2:
43-
use_format_version = format_version[: format_version.rfind(".")]
4445
else:
45-
use_format_version = None
46+
assert ndots > 1
47+
use_format_version = format_version[: format_version.rfind(".")]
4648

4749
descr_versions = descriptions_map[typ]
48-
return descr_versions.get(use_format_version, descr_versions[None])
50+
if use_format_version not in descr_versions:
51+
raise ValueError(
52+
f"Unsupported format version '{format_version}' for type '{typ}'. "
53+
f"Supported format versions are: {', '.join(sorted(fv for fv in descr_versions if fv is not None))}"
54+
)
55+
56+
return descr_versions[use_format_version]
4957

5058

5159
def build_description_impl(

src/bioimageio/spec/_io.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,28 @@ def load_description(
6565
"""load a bioimage.io resource description
6666
6767
Args:
68-
source: Path or URL to an rdf.yaml or a bioimage.io package
69-
(zip-file with rdf.yaml in it).
70-
format_version: (optional) Use this argument to load the resource and
71-
convert its metadata to a higher format_version.
72-
perform_io_checks: Wether or not to perform validation that requires file io,
73-
e.g. downloading a remote files. The existence of local
74-
absolute file paths is still being checked.
75-
known_files: Allows to bypass download and hashing of referenced files
76-
(even if perform_io_checks is True).
77-
Checked files will be added to this dictionary
78-
with their SHA-256 value.
79-
sha256: Optional SHA-256 value of **source**
68+
source:
69+
Path or URL to an rdf.yaml or a bioimage.io package
70+
(zip-file with rdf.yaml in it).
71+
format_version:
72+
(optional) Use this argument to load the resource and
73+
convert its metadata to a higher format_version.
74+
Note:
75+
- Use "latest" to convert to the latest available format version.
76+
- Use "discover" to use the format version specified in the RDF.
77+
- Only considers major.minor format version, ignores patch version.
78+
- Conversion to lower format versions is not supported.
79+
perform_io_checks:
80+
Wether or not to perform validation that requires file io,
81+
e.g. downloading a remote files. The existence of local
82+
absolute file paths is still being checked.
83+
known_files:
84+
Allows to bypass download and hashing of referenced files
85+
(even if perform_io_checks is True).
86+
Checked files will be added to this dictionary
87+
with their SHA-256 value.
88+
sha256:
89+
Optional SHA-256 value of **source**
8090
8191
Returns:
8292
An object holding all metadata of the bioimage.io resource

0 commit comments

Comments
 (0)