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

29 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-19 09:02 +0000

1"""These functions implement the logic of 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.spec import ( 

10 InvalidDescr, 

11 ResourceDescr, 

12 save_bioimageio_package, 

13 save_bioimageio_package_as_folder, 

14) 

15from bioimageio.spec.model.v0_5 import WeightsFormat 

16 

17from ._resource_tests import test_description 

18 

19WeightFormatArgAll = Literal[WeightsFormat, "all"] 

20WeightFormatArgAny = Literal[WeightsFormat, "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 decimal: int = 4, 

29) -> int: 

30 """test a bioimageio resource 

31 

32 Args: 

33 source: Path or URL to the bioimageio resource description file 

34 (bioimageio.yaml or rdf.yaml) or to a zipped resource 

35 weight_format: (model only) The weight format to use 

36 devices: Device(s) to use for testing 

37 decimal: Precision for numerical comparisons 

38 """ 

39 if isinstance(descr, InvalidDescr): 

40 descr.validation_summary.display() 

41 return 1 

42 

43 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 decimal=decimal, 

48 ) 

49 summary.display() 

50 return 0 if summary.status == "passed" else 1 

51 

52 

53def validate_format( 

54 descr: Union[ResourceDescr, InvalidDescr], 

55): 

56 """validate the meta data format of a bioimageio resource 

57 

58 Args: 

59 descr: a bioimageio resource description 

60 """ 

61 descr.validation_summary.display() 

62 return 0 if descr.validation_summary.status == "passed" else 1 

63 

64 

65def package( 

66 descr: ResourceDescr, path: Path, *, weight_format: WeightFormatArgAll = "all" 

67): 

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

69 

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

71 package as an unzipped folder instead. 

72 

73 Args: 

74 descr: a bioimageio resource description 

75 path: output path 

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

77 """ 

78 if isinstance(descr, InvalidDescr): 

79 descr.validation_summary.display() 

80 raise ValueError("resource description is invalid") 

81 

82 if weight_format == "all": 

83 weights_priority_order = None 

84 else: 

85 weights_priority_order = (weight_format,) 

86 

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

88 _ = save_bioimageio_package( 

89 descr, 

90 output_path=path, 

91 weights_priority_order=weights_priority_order, 

92 ) 

93 else: 

94 _ = save_bioimageio_package_as_folder( 

95 descr, 

96 output_path=path, 

97 weights_priority_order=weights_priority_order, 

98 ) 

99 return 0