diff --git a/lib/galaxy/tool_util/verify/asserts/image.py b/lib/galaxy/tool_util/verify/asserts/image.py index d9972994d3f..daf4ab8e70c 100644 --- a/lib/galaxy/tool_util/verify/asserts/image.py +++ b/lib/galaxy/tool_util/verify/asserts/image.py @@ -320,19 +320,19 @@ CenterOfMassEps = Annotated[ ), ] Labels = Annotated[ - Optional[Union[str, List[int]]], + Optional[Union[str, List[Union[float, int]]]], AssertionParameter( "List of labels, separated by a comma. Labels *not* on this list will be excluded from consideration. Cannot be used in combination with ``exclude_labels``.", xml_type="xs:string", - json_type="typing.Optional[typing.List[int]]", + json_type=f"typing.Optional[typing.List[{JSON_STRICT_NUMBER}]]", ), ] ExcludeLabels = Annotated[ - Optional[Union[str, List[int]]], + Optional[Union[str, List[Union[float, int]]]], AssertionParameter( "List of labels to be excluded from consideration, separated by a comma. The primary usage of this attribute is to exclude the background of a label image. Cannot be used in combination with ``labels``.", xml_type="xs:string", - json_type="typing.Optional[typing.List[int]]", + json_type=f"typing.Optional[typing.List[{JSON_STRICT_NUMBER}]]", ), ] MeanObjectSize = Annotated[ @@ -734,8 +734,8 @@ def _get_image_labels( channel: Optional[Union[int, str]] = None, slice: Optional[Union[int, str]] = None, frame: Optional[Union[int, str]] = None, - labels: Optional[Union[str, List[int]]] = None, - exclude_labels: Optional[Union[str, List[int]]] = None, + labels: Labels = None, + exclude_labels: ExcludeLabels = None, ) -> Tuple["numpy.typing.NDArray", List[Any]]: """ Determines the unique labels in the output image or a specific channel. @@ -743,7 +743,7 @@ def _get_image_labels( assert labels is None or exclude_labels is None im_arr = _get_image(output_bytes, channel, slice, frame) - def cast_label(label): + def cast_label(label: str) -> Union[float, int]: label = label.strip() if numpy.issubdtype(im_arr.dtype, numpy.integer): return int(label) @@ -752,12 +752,12 @@ def _get_image_labels( raise AssertionError(f'Unsupported image label type: "{im_arr.dtype}"') # Determine labels present in the image. - present_labels = numpy.unique(im_arr) + present_labels: List[Any] = numpy.unique(im_arr).tolist() # Apply filtering due to `labels` (keep only those). if labels is None: labels = [] - if isinstance(labels, str): + elif isinstance(labels, str): labels = [cast_label(label) for label in labels.split(",") if len(label) > 0] if len(labels) > 0: present_labels = [label for label in present_labels if label in labels] @@ -765,7 +765,7 @@ def _get_image_labels( # Apply filtering due to `exclude_labels`. if exclude_labels is None: exclude_labels = [] - if isinstance(exclude_labels, str): + elif isinstance(exclude_labels, str): exclude_labels = [cast_label(label) for label in exclude_labels.split(",") if len(label) > 0] present_labels = [label for label in present_labels if label not in exclude_labels]