diff --git a/src/taggingbackends/data/labels.py b/src/taggingbackends/data/labels.py index c9301e2ccdd580152607548640483df261bd8c8e..a962b57bf18fae3ece4da2ab3ea82197daca793f 100644 --- a/src/taggingbackends/data/labels.py +++ b/src/taggingbackends/data/labels.py @@ -66,7 +66,9 @@ because label colors are also stored in the `labelspec` attribute. With taggingbackends==0.9, a related attribute was introduced: `secondarylabelspec`. To get a unique array of indexable labels, both primary -and secondary labels in a same array, use `full_label_list` instead. +and secondary labels in a same array, use either `encoding_label_list` or +`decoding_label_list` instead. The two properties typically equal, unless +attribute `decodingspec` is defined. """ class Labels: @@ -267,6 +269,7 @@ class Labels: self.labels, self.metadata = new_self.labels, new_self.metadata self.labelspec, self.units = new_self.labelspec, new_self.units self.secondarylabelspec = new_self.secondarylabelspec + # no `decodingspec` in label files self.tracking = new_self.tracking return self @@ -309,6 +312,7 @@ class Labels: self.units = data.get("units", {}) self.labelspec = data.get("labels", {}) self.secondarylabelspec = data.get("secondarylabels", []) + self.decodingspec = None # ensure it is not set self._tracking = data.get("dependencies", []) if isinstance(self._tracking, dict): self._tracking = [self._tracking] @@ -325,7 +329,7 @@ class Labels: List of str: all different labels including primary and secondary labels. """ @property - def full_label_list(self): + def encoding_label_list(self): if isinstance(self.labelspec, dict): labelset = self.labelspec['names'] else: @@ -334,6 +338,18 @@ class Labels: labelset = labelset + self.secondarylabelspec return labelset + """ + List of str: all different labels including primary and secondary labels. + """ + @property + def decoding_label_list(self): + labelspec = self.decodingspec + if labelspec is None: + return self.encoding_label_list + if self.secondarylabelspec: + labelset = labelset + self.secondarylabelspec + return labelset + """ Encode the text labels as indices (`int` or `list` of `int`). @@ -348,7 +364,7 @@ class Labels: elif isinstance(label, dict): encoded = {t: self.encode(l) for t, l in label.items()} else: - labelset = self.full_label_list + labelset = self.encoding_label_list if isinstance(label, str): encoded = labelset.index(label) + 1 elif isinstance(label, int): @@ -373,6 +389,13 @@ class Labels: defined by the tagger used to generate the label file. In the case of decoding a label file, `labelspec` only should be defined in the `Labels` object. + + To forget about these considerations, call `load_model_config` first if + processing the output of a tagger, or do not call that method otherwise. + + MaggotUBA's `predict_model` does not call `decode` but directly indexes into + `behavior_labels`. As a consequence, the above subtleties do not apply to + MaggotUBA. """ def decode(self, label=None): if label is None: @@ -382,10 +405,7 @@ class Labels: elif isinstance(label, dict): decoded = {t: self.decode(l) for t, l in label.items()} else: - if self.decodingspec is None: - labelset = self.full_label_list - else: - labelset = self.decodingspec + labelset = self.decoding_label_list if isinstance(label, int): decoded = labelset[label-1] elif isinstance(label, str):