23 lines
827 B
Python
23 lines
827 B
Python
from typing import Mapping, Iterable
|
|
|
|
from image_prediction.exceptions import UnexpectedLabelFormat
|
|
from image_prediction.label_mapper.mapper import LabelMapper
|
|
|
|
|
|
class IndexMapper(LabelMapper):
|
|
def __init__(self, labels: Mapping[int, str]):
|
|
self.__labels = labels
|
|
|
|
def __validate_index_label_format(self, index_label: int) -> None:
|
|
if not 0 <= index_label < len(self.__labels):
|
|
raise UnexpectedLabelFormat(
|
|
f"Received index label '{index_label}' that has no associated string label."
|
|
)
|
|
|
|
def __map_label(self, index_label: int) -> str:
|
|
self.__validate_index_label_format(index_label)
|
|
return self.__labels[index_label]
|
|
|
|
def map_labels(self, index_labels: Iterable[int]) -> Iterable[str]:
|
|
return map(self.__map_label, index_labels)
|