Merge the annotation at given index with next or previous one.
if direction > 0:
annidx: [beginidx, endidx, labelsidx]
nextann: [beginn, endn, labelsn]
result: [beginidx, endn, labelsidx + labelsn]
if direction < 0:
prevann: [beginp, endp, labelsp]
annidx: [beginidx, endidx, labelsidx]
result: [beginp, endidx, labelsp + labelsidx]
Parameters
- idx: (int) Index of the annotation in the list
- direction: (int) Positive for next, Negative for previous
Returns
- (bool) False if direction does not match with index
Raises
Exception if merged annotation can't be deleted of the tier
View Source
def merge(self, idx, direction):
"""Merge the annotation at given index with next or previous one.
if direction > 0:
ann_idx: [begin_idx, end_idx, labels_idx]
next_ann: [begin_n, end_n, labels_n]
result: [begin_idx, end_n, labels_idx + labels_n]
if direction < 0:
prev_ann: [begin_p, end_p, labels_p]
ann_idx: [begin_idx, end_idx, labels_idx]
result: [begin_p, end_idx, labels_p + labels_idx]
:param idx: (int) Index of the annotation in the list
:param direction: (int) Positive for next, Negative for previous
:return: (bool) False if direction does not match with index
:raise: Exception if merged annotation can't be deleted of the tier
"""
if self.is_point() is True:
raise AnnDataTypeError(self.get_name(), 'Interval, Disjoint')
try:
ann = self.__ann[idx]
except IndexError:
raise AnnDataIndexError(idx)
if direction > 0:
merge_idx = idx + 1
if merge_idx == len(self.__ann):
return False
else:
if idx == 0:
return False
merge_idx = idx - 1
merge_ann = self.__ann[merge_idx]
merge_labels = [l.copy() for l in merge_ann.get_labels()]
merge_loc = merge_ann.get_location().get_best()
localization = ann.get_location().get_best()
copied_loc = localization.copy()
labels = self.__ann[idx].get_labels()
try:
if direction > 0:
localization.set_end(merge_loc.get_end())
new_labels = labels + merge_labels
else:
localization.set_begin(merge_loc.get_begin())
new_labels = merge_labels + labels
self.validate()
ann.set_labels(new_labels)
self.pop(merge_idx)
except:
ann.set_best_localization(copied_loc)
ann.set_labels(labels)
raise
return True