Skip to content

Commit b2ab69e

Browse files
Merge pull request #188 from bioimage-io/export_old_versions
Enable to export old versions
2 parents 9e183ab + 3d61e05 commit b2ab69e

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

bioimageio/core/__main__.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def package(
5656
show_default=False,
5757
),
5858
verbose: bool = typer.Option(False, help="show traceback of exceptions"),
59-
) -> int:
59+
):
6060
# typer bug: typer returns empty tuple instead of None if weights_order_priority is not given
6161
weights_priority_order = weights_priority_order or None
6262

@@ -85,7 +85,7 @@ def test_model(
8585
weight_format: Optional[WeightFormatEnum] = typer.Option(None, help="The weight format to use."),
8686
devices: Optional[List[str]] = typer.Option(None, help="Devices for running the model."),
8787
decimal: int = typer.Option(4, help="The test precision."),
88-
) -> int:
88+
):
8989
# this is a weird typer bug: default devices are empty tuple although they should be None
9090
if len(devices) == 0:
9191
devices = None
@@ -126,7 +126,7 @@ def test_resource(
126126
weight_format: Optional[WeightFormatEnum] = typer.Option(None, help="(for model only) The weight format to use."),
127127
devices: Optional[List[str]] = typer.Option(None, help="(for model only) Devices for running the model."),
128128
decimal: int = typer.Option(4, help="(for model only) The test precision."),
129-
) -> int:
129+
):
130130
# this is a weird typer bug: default devices are empty tuple although they should be None
131131
if len(devices) == 0:
132132
devices = None
@@ -164,7 +164,7 @@ def predict_image(
164164
tiling: Optional[bool] = typer.Option(None, help="Whether to run prediction in tiling mode."),
165165
weight_format: Optional[WeightFormatEnum] = typer.Option(None, help="The weight format to use."),
166166
devices: Optional[List[str]] = typer.Option(None, help="Devices for running the model."),
167-
) -> int:
167+
):
168168

169169
if isinstance(padding, str):
170170
padding = json.loads(padding.replace("'", '"'))
@@ -203,7 +203,7 @@ def predict_images(
203203
tiling: Optional[bool] = typer.Option(None, help="Whether to run prediction in tiling mode."),
204204
weight_format: Optional[WeightFormatEnum] = typer.Option(None, help="The weight format to use."),
205205
devices: Optional[List[str]] = typer.Option(None, help="Devices for running the model."),
206-
) -> int:
206+
):
207207
input_files = glob(input_pattern)
208208
input_names = [os.path.split(infile)[1] for infile in input_files]
209209
output_files = [os.path.join(output_folder, fname) for fname in input_names]
@@ -246,7 +246,7 @@ def convert_torch_weights_to_onnx(
246246
opset_version: Optional[int] = typer.Argument(12, help="Onnx opset version."),
247247
use_tracing: bool = typer.Option(True, help="Whether to use torch.jit tracing or scripting."),
248248
verbose: bool = typer.Option(True, help="Verbosity"),
249-
) -> int:
249+
):
250250
ret_code = torch_converter.convert_weights_to_onnx(model_rdf, output_path, opset_version, use_tracing, verbose)
251251
sys.exit(ret_code)
252252

@@ -259,7 +259,7 @@ def convert_torch_weights_to_torchscript(
259259
),
260260
output_path: Path = typer.Argument(..., help="Where to save the torchscript weights."),
261261
use_tracing: bool = typer.Option(True, help="Whether to use torch.jit tracing or scripting."),
262-
) -> int:
262+
):
263263
ret_code = torch_converter.convert_weights_to_torchscript(model_rdf, output_path, use_tracing)
264264
sys.exit(ret_code)
265265

@@ -274,7 +274,7 @@ def convert_keras_weights_to_tensorflow(
274274
..., help="Path to the model resource description file (rdf.yaml) or zipped model."
275275
),
276276
output_path: Path = typer.Argument(..., help="Where to save the tensorflow weights."),
277-
) -> int:
277+
):
278278
ret_code = keras_converter.convert_weights_to_tensorflow_saved_model_bundle(model_rdf, output_path)
279279
sys.exit(ret_code)
280280

bioimageio/core/build_spec/build_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ def build_model(
812812
kwargs = {k: v for k, v in optional_kwargs.items() if v is not None}
813813

814814
if attachments is not None:
815-
kwargs["attachments"] = model_spec.raw_nodes.Attachments(**attachments)
815+
kwargs["attachments"] = spec.rdf.raw_nodes.Attachments(**attachments)
816816
if dependencies is not None:
817817
kwargs["dependencies"] = _get_dependencies(dependencies, root)
818818
if maintainers is not None:

bioimageio/core/resource_io/io_.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,23 @@ def _replace_relative_paths_for_remote_source(
9595

9696

9797
def load_raw_resource_description(
98-
source: Union[dict, os.PathLike, IO, str, bytes, raw_nodes.URI, RawResourceDescription]
98+
source: Union[dict, os.PathLike, IO, str, bytes, raw_nodes.URI, RawResourceDescription],
99+
update_to_format: Optional[str] = "latest",
99100
) -> RawResourceDescription:
100101
"""load a raw python representation from a BioImage.IO resource description file (RDF).
101102
Use `load_resource_description` for a more convenient representation.
102103
103104
Args:
104105
source: resource description file (RDF)
106+
update_to_format: update resource to specific "major.minor" or "latest" format version; ignoring patch version.
105107
106108
Returns:
107109
raw BioImage.IO resource
108110
"""
109111
if isinstance(source, RawResourceDescription):
110112
return source
111113

112-
raw_rd = spec.load_raw_resource_description(source, update_to_current_format=True)
114+
raw_rd = spec.load_raw_resource_description(source, update_to_format=update_to_format)
113115
raw_rd = _replace_relative_paths_for_remote_source(raw_rd, raw_rd.root_path)
114116
return raw_rd
115117

@@ -133,7 +135,7 @@ def load_resource_description(
133135
if isinstance(source, ResourceDescription):
134136
return source
135137

136-
raw_rd = load_raw_resource_description(source)
138+
raw_rd = load_raw_resource_description(source, update_to_format="latest")
137139

138140
if weights_priority_order is not None:
139141
for wf in weights_priority_order:
@@ -181,26 +183,28 @@ def get_local_resource_package_content(
181183
def export_resource_package(
182184
source: Union[RawResourceDescription, os.PathLike, str, dict, raw_nodes.URI],
183185
*,
184-
output_path: Optional[os.PathLike] = None,
185-
weights_priority_order: Optional[Sequence[Union[str]]] = None,
186186
compression: int = ZIP_DEFLATED,
187187
compression_level: int = 1,
188+
output_path: Optional[os.PathLike] = None,
189+
update_to_format: Optional[str] = None,
190+
weights_priority_order: Optional[Sequence[Union[str]]] = None,
188191
) -> pathlib.Path:
189192
"""Package a BioImage.IO resource as a zip file.
190193
191194
Args:
192195
source: raw resource description, path, URI or raw data as dict
193-
output_path: file path to write package to
194-
weights_priority_order: If given only the first weights format present in the model is included.
195-
If none of the prioritized weights formats is found all are included.
196196
compression: The numeric constant of compression method.
197197
compression_level: Compression level to use when writing files to the archive.
198198
See https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile
199+
output_path: file path to write package to
200+
update_to_format: update resource to specific "major.minor" or "latest" format version; ignoring patch version.
201+
weights_priority_order: If given only the first weights format present in the model is included.
202+
If none of the prioritized weights formats is found all are included.
199203
200204
Returns:
201205
path to zipped BioImage.IO package in BIOIMAGEIO_CACHE_PATH or 'output_path'
202206
"""
203-
raw_rd = load_raw_resource_description(source)
207+
raw_rd = load_raw_resource_description(source, update_to_format=update_to_format)
204208
package_content = get_local_resource_package_content(raw_rd, weights_priority_order)
205209
if output_path is None:
206210
package_path = _get_tmp_package_path(raw_rd, weights_priority_order)

bioimageio/core/resource_io/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class TensorflowSavedModelBundleWeightsEntry(Node, model_raw_nodes.TensorflowSav
172172

173173

174174
@dataclass
175-
class Attachments(Node, model_raw_nodes.Attachments):
175+
class Attachments(Node, rdf_raw_nodes.Attachments):
176176
files: Union[_Missing, List[Path]] = missing
177177
unknown: Union[_Missing, Dict[str, Any]] = missing
178178

0 commit comments

Comments
 (0)