kwcoco._helpers

These items were split out of coco_dataset.py which is becoming too big

These are helper data structures used to do things like auto-increment ids, recycle ids, do renaming, extend sortedcontainers etc…

Module Contents

Classes

_NextId

Helper class to tracks unused ids for new items

_ID_Remapper

Helper to recycle ids for unions.

UniqueNameRemapper

helper to ensure names will be unique by appending suffixes

SortedSetQuiet

Sorted set is a sorted mutable set.

Functions

_lut_frame_index(imgs, gid)

_delitems(items, remove_idxs, thresh=750)

Parameters
  • items (List) -- list which will be modified

class kwcoco._helpers._NextId(parent)[source]

Bases: object

Helper class to tracks unused ids for new items

_update_unused(self, key)[source]

Scans for what the next safe id can be for key

get(self, key)[source]

Get the next safe item id for key

class kwcoco._helpers._ID_Remapper(reuse=False)[source]

Bases: object

Helper to recycle ids for unions.

For each dataset we create a mapping between each old id and a new id. If possible and reuse=True we allow the new id to match the old id. After each dataset is finished we mark all those ids as used and subsequent new-ids cannot be chosen from that pool.

Parameters

reuse (bool) – if True we are allowed to reuse ids as long as they haven’t been used before.

Example

>>> video_trackids = [[1, 1, 3, 3, 200, 4], [204, 1, 2, 3, 3, 4, 5, 9]]
>>> self = _ID_Remapper(reuse=True)
>>> for tids in video_trackids:
>>>     new_tids = [self.remap(old_tid) for old_tid in tids]
>>>     self.block_seen()
>>>     print('new_tids = {!r}'.format(new_tids))
new_tids = [1, 1, 3, 3, 200, 4]
new_tids = [204, 205, 2, 206, 206, 207, 5, 9]
>>> #
>>> self = _ID_Remapper(reuse=False)
>>> for tids in video_trackids:
>>>     new_tids = [self.remap(old_tid) for old_tid in tids]
>>>     self.block_seen()
>>>     print('new_tids = {!r}'.format(new_tids))
new_tids = [0, 0, 1, 1, 2, 3]
new_tids = [4, 5, 6, 7, 7, 8, 9, 10]
remap(self, old_id)[source]

Convert a old-id into a new-id. If self.reuse is True then we will return the same id if it hasn’t been blocked yet.

block_seen(self)[source]

Mark all seen ids as unable to be used. Any ids sent to remap will now generate new ids.

next_id(self)[source]

Generate a new id that hasnt been used yet

class kwcoco._helpers.UniqueNameRemapper[source]

Bases: object

helper to ensure names will be unique by appending suffixes

Example

>>> from kwcoco.coco_dataset import *  # NOQA
>>> self = UniqueNameRemapper()
>>> assert self.remap('foo') == 'foo'
>>> assert self.remap('foo') == 'foo_v001'
>>> assert self.remap('foo') == 'foo_v002'
>>> assert self.remap('foo_v001') == 'foo_v003'
remap(self, name)[source]
kwcoco._helpers._lut_frame_index(imgs, gid)[source]
class kwcoco._helpers.SortedSetQuiet(iterable=None, key=None)[source]

Bases: sortedcontainers.SortedSet

Sorted set is a sorted mutable set.

Sorted set values are maintained in sorted order. The design of sorted set is simple: sorted set uses a set for set-operations and maintains a sorted list of values.

Sorted set values must be hashable and comparable. The hash and total ordering of values must not change while they are stored in the sorted set.

Mutable set methods:

  • SortedSet.__contains__()

  • SortedSet.__iter__()

  • SortedSet.__len__()

  • SortedSet.add()

  • SortedSet.discard()

Sequence methods:

  • SortedSet.__getitem__()

  • SortedSet.__delitem__()

  • SortedSet.__reversed__()

Methods for removing values:

  • SortedSet.clear()

  • SortedSet.pop()

  • SortedSet.remove()

Set-operation methods:

  • SortedSet.difference()

  • SortedSet.difference_update()

  • SortedSet.intersection()

  • SortedSet.intersection_update()

  • SortedSet.symmetric_difference()

  • SortedSet.symmetric_difference_update()

  • SortedSet.union()

  • SortedSet.update()

Methods for miscellany:

  • SortedSet.copy()

  • SortedSet.count()

  • SortedSet.__repr__()

  • SortedSet._check()

Sorted list methods available:

  • SortedList.bisect_left()

  • SortedList.bisect_right()

  • SortedList.index()

  • SortedList.irange()

  • SortedList.islice()

  • SortedList._reset()

Additional sorted list methods available, if key-function used:

  • SortedKeyList.bisect_key_left()

  • SortedKeyList.bisect_key_right()

  • SortedKeyList.irange_key()

Sorted set comparisons use subset and superset relations. Two sorted sets are equal if and only if every element of each sorted set is contained in the other (each is a subset of the other). A sorted set is less than another sorted set if and only if the first sorted set is a proper subset of the second sorted set (is a subset, but is not equal). A sorted set is greater than another sorted set if and only if the first sorted set is a proper superset of the second sorted set (is a superset, but is not equal).

__repr__(self)[source]

Return string representation of sorted set.

ss.__repr__() <==> repr(ss)

Returns

string representation

kwcoco._helpers._delitems(items, remove_idxs, thresh=750)[source]
Parameters
  • items (List) – list which will be modified

  • remove_idxs (List[int]) – integers to remove (MUST BE UNIQUE)