geowatch.utils.util_kwutil module

Functions that may be moved to kwutil

geowatch.utils.util_kwutil.distributed_subitems(items, max_num)[source]

Return a subset of items maximally distributed over the input index space. I.e. the chosen indexes are maximally dialted.

Parameters:

items (List | Dict) – an ordered indexable object

Returns:

a subset of the input with a length at most max_num.

Return type:

List | Dict

Todo

  • [ ] Find a better name

  • [ ] Figure out where this lives

Example

>>> from geowatch.utils.util_kwutil import *  # NOQA
>>> items = list(range(100))
>>> max_num = 5
>>> sub_items = distributed_subitems(items, max_num)
>>> print(sub_items)
[0, 25, 50, 75, 99]

Example

>>> from geowatch.utils.util_kwutil import *  # NOQA
>>> items = {chr(i): i for i in range(ord('a'), ord('a') + 26)}
>>> max_num = 5
>>> sub_items = distributed_subitems(items, max_num)
>>> print(sub_items)
{'a': 97, 'g': 103, 'n': 110, 't': 116, 'z': 122}
geowatch.utils.util_kwutil.farthest_from_previous(start, stop)[source]

Given a ordered list of items, incrementally yield indexes such that each new index maximizes the distance to all other previously chosen indexes.

Parameters:
  • start (int) – The inclusive starting index (typically 0)

  • stop (int) – The exclusive maximum index (typically len(items))

Yields:

int – the next chosen index in the series

Todo

  • [ ] Find a Better Name

Notes

  • This is an instance of farthest-point traversal in 1D.

References

CommandLine

xdoctest -m geowatch.utils.util_kwutil farthest_from_previous

Example

>>> from geowatch.utils.util_kwutil import *  # NOQA
>>> total = 10
>>> start, stop = 0, 10
>>> gen = farthest_from_previous(start, stop)
>>> result = list(gen)
>>> assert set(result) == set(range(start, stop))
>>> print(result)
[9, 0, 5, 2, 7, 1, 6, 3, 8, 4]