Coverage for bioimageio/core/commands.py: 77%

31 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-01 09:51 +0000

1"""These functions are used in the bioimageio command line interface 

2defined in `bioimageio.core.cli`.""" 

3 

4from pathlib import Path 

5from typing import Optional, Sequence, Union 

6 

7from typing_extensions import Literal 

8 

9from bioimageio.core.common import SupportedWeightsFormat 

10from bioimageio.spec import ( 

11 InvalidDescr, 

12 ResourceDescr, 

13 save_bioimageio_package, 

14 save_bioimageio_package_as_folder, 

15) 

16 

17from ._resource_tests import test_description 

18 

19WeightFormatArgAll = Literal[SupportedWeightsFormat, "all"] 

20WeightFormatArgAny = Literal[SupportedWeightsFormat, "any"] 

21 

22 

23def test( 

24 descr: Union[ResourceDescr, InvalidDescr], 

25 *, 

26 weight_format: WeightFormatArgAll = "all", 

27 devices: Optional[Union[str, Sequence[str]]] = None, 

28 summary: Union[ 

29 Literal["display"], Path, Sequence[Union[Literal["display"], Path]] 

30 ] = "display", 

31 runtime_env: Union[ 

32 Literal["currently-active", "as-described"], Path 

33 ] = "currently-active", 

34 determinism: Literal["seed_only", "full"] = "seed_only", 

35) -> int: 

36 """Test a bioimageio resource. 

37 

38 Arguments as described in `bioimageio.core.cli.TestCmd` 

39 """ 

40 if isinstance(descr, InvalidDescr): 

41 test_summary = descr.validation_summary 

42 else: 

43 test_summary = test_description( 

44 descr, 

45 weight_format=None if weight_format == "all" else weight_format, 

46 devices=[devices] if isinstance(devices, str) else devices, 

47 runtime_env=runtime_env, 

48 determinism=determinism, 

49 ) 

50 

51 _ = test_summary.log(summary) 

52 return 0 if test_summary.status == "passed" else 1 

53 

54 

55def validate_format( 

56 descr: Union[ResourceDescr, InvalidDescr], 

57 summary: Union[Path, Sequence[Path]] = (), 

58): 

59 """DEPRECATED; Access the existing `validation_summary` attribute instead. 

60 validate the meta data format of a bioimageio resource 

61 

62 Args: 

63 descr: a bioimageio resource description 

64 """ 

65 _ = descr.validation_summary.save(summary) 

66 return 0 if descr.validation_summary.status in ("valid-format", "passed") else 1 

67 

68 

69# TODO: absorb into `save_bioimageio_package` 

70def package( 

71 descr: ResourceDescr, 

72 path: Path, 

73 *, 

74 weight_format: WeightFormatArgAll = "all", 

75): 

76 """Save a resource's metadata with its associated files. 

77 

78 Note: If `path` does not have a `.zip` suffix this command will save the 

79 package as an unzipped folder instead. 

80 

81 Args: 

82 descr: a bioimageio resource description 

83 path: output path 

84 weight-format: include only this single weight-format (if not 'all'). 

85 """ 

86 if isinstance(descr, InvalidDescr): 

87 logged = descr.validation_summary.save() 

88 msg = f"Invalid {descr.type} description." 

89 if logged: 

90 msg += f" Details saved to {logged}." 

91 

92 raise ValueError(msg) 

93 

94 if weight_format == "all": 

95 weights_priority_order = None 

96 else: 

97 weights_priority_order = (weight_format,) 

98 

99 if path.suffix == ".zip": 

100 _ = save_bioimageio_package( 

101 descr, 

102 output_path=path, 

103 weights_priority_order=weights_priority_order, 

104 ) 

105 else: 

106 _ = save_bioimageio_package_as_folder( 

107 descr, 

108 output_path=path, 

109 weights_priority_order=weights_priority_order, 

110 ) 

111 return 0