diff --git a/src/taggingbackends/data/labels.py b/src/taggingbackends/data/labels.py index f9402f502e3a8ee746d1bd54bd07cd57fea41206..e2b93ad59c98dc9e2e86584be438592b5bcf8bb7 100644 --- a/src/taggingbackends/data/labels.py +++ b/src/taggingbackends/data/labels.py @@ -60,9 +60,13 @@ def retagged_trxmat_confusion_matrix(label_file): ``` The `labelspec` attribute is assumed to be a list, which is valid for *.label* -generated by automatic tagging. -Manual tagging will store label names as `Labels.labelspec['names']`, because -label colors are also stored in the `labelspec` attribute. +files generated by automatic tagging. +In contrast, manual tagging stores label names as `Labels.labelspec['names']`, +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. """ class Labels: @@ -77,6 +81,7 @@ class Labels: # self.labels, self.metadata = labels, metadata self.labelspec, self.units = labelspec, units + self.secondarylabelspec = None self._tracking = tracking self._input_labels = None # @@ -258,6 +263,7 @@ class Labels: new_self = json.load(file, cls=LabelDecoder) 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 self.tracking = new_self.tracking return self @@ -275,6 +281,8 @@ class Labels: data["units"] = self.units if self.labelspec: data["labels"] = self.labelspec + if self.secondarylabelspec: + data["secondarylabels"] = self.secondarylabelspec if self.tracking: if isinstance(self.tracking, dict): data["dependencies"] = self.tracking @@ -297,6 +305,7 @@ class Labels: run = self.metadata.pop("id") self.units = data.get("units", {}) self.labelspec = data.get("labels", {}) + self.secondarylabelspec = data.get("secondarylabels", []) self._tracking = data.get("dependencies", []) if isinstance(self._tracking, dict): self._tracking = [self._tracking] @@ -309,6 +318,19 @@ class Labels: for timestamp, label in zip(track["t"], track["labels"])} return self + """ + List of str: all different labels including primary and secondary labels. + """ + @property + def full_label_list(self): + if isinstance(self.labelspec, dict): + labelset = self.labelspec['names'] + else: + labelset = self.labelspec + if self.secondarylabelspec: + labelset = labelset + self.secondarylabelspec + return labelset + """ Encode the text labels as indices (`int` or `list` of `int`). @@ -323,10 +345,7 @@ class Labels: elif isinstance(label, dict): encoded = {t: self.encode(l) for t, l in label.items()} else: - if isinstance(self.labelspec, dict): - labelset = self.labelspec['names'] - else: - labelset = self.labelspec + labelset = self.full_label_list if isinstance(label, str): encoded = labelset.index(label) + 1 elif isinstance(label, int): @@ -349,10 +368,7 @@ class Labels: elif isinstance(label, dict): decoded = {t: self.decode(l) for t, l in label.items()} else: - if isinstance(self.labelspec, dict): - labelset = self.labelspec['names'] - else: - labelset = self.labelspec + labelset = self.full_label_list if isinstance(label, int): decoded = labelset[label-1] elif isinstance(label, str):