Coverage for mlprodict/onnxrt/ops_cpu/_op_classifier_string.py: 100%
14 statements
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-04 02:28 +0100
« prev ^ index » next coverage.py v7.1.0, created at 2023-02-04 02:28 +0100
1"""
2@file
3@brief Common class for classifiers supporting strings.
4"""
5import numpy
8class _ClassifierCommon:
9 """
10 Labels strings are not natively implemented in C++ runtime.
11 The class stores the strings labels, replaces them by
12 integer, calls the C++ codes and then replaces them by strings.
13 """
15 def _post_process_label_attributes(self):
16 """
17 Replaces string labels by int64 labels.
18 It creates attributes *_classlabels_int64s_string*.
19 """
20 name_int = 'classlabels_int64s' if hasattr(
21 self, 'classlabels_int64s') else 'classlabels_ints'
22 if (hasattr(self, 'classlabels_strings') and
23 len(self.classlabels_strings) > 0): # pylint: disable=E0203
24 if hasattr(self, name_int) and len(getattr(self, name_int)) != 0:
25 raise RuntimeError( # pragma: no cover
26 f"'{name_int}' must be empty if 'classlabels_strings' is not.")
27 setattr(self, name_int, numpy.arange(len(self.classlabels_strings), # pylint: disable=E0203
28 dtype=numpy.int64))
29 self._classlabels_int64s_string = self.classlabels_strings # pylint: disable=E0203
30 self.classlabels_strings = numpy.empty(
31 shape=(0, ), dtype=numpy.str_)
32 else:
33 self._classlabels_int64s_string = None
35 def _post_process_predicted_label(self, label, scores):
36 """
37 Replaces int64 predicted labels by the corresponding
38 strings.
39 """
40 if self._classlabels_int64s_string is not None:
41 label = numpy.array(
42 [self._classlabels_int64s_string[i] for i in label])
43 return label, scores