kwcoco.util.delayed_ops.helpers module

kwcoco.util.delayed_ops.helpers.dequantize(quant_data, quantization)[source]

Helper for dequantization

Parameters
  • quant_data (ndarray) – data to dequantize

  • quantization (Dict[str, Any]) – quantization information dictionary to undo. Expected keys are: orig_type (str) orig_min (float) orig_max (float) quant_min (float) quant_max (float) nodata (None | int)

Returns

dequantized data

Return type

ndarray

Example

>>> quant_data = (np.random.rand(4, 4) * 256).astype(np.uint8)
>>> quantization = {
>>>     'orig_dtype': 'float32',
>>>     'orig_min': 0,
>>>     'orig_max': 1,
>>>     'quant_min': 0,
>>>     'quant_max': 255,
>>>     'nodata': None,
>>> }
>>> dequantize(quant_data, quantization)

Example

>>> quant_data = np.ones((4, 4), dtype=np.uint8)
>>> quantization = {
>>>     'orig_dtype': 'float32',
>>>     'orig_min': 0,
>>>     'orig_max': 1,
>>>     'quant_min': 1,
>>>     'quant_max': 1,
>>>     'nodata': None,
>>> }
>>> dequantize(quant_data, quantization)
kwcoco.util.delayed_ops.helpers.quantize_float01(imdata, old_min=0, old_max=1, quantize_dtype=<class 'numpy.int16'>)[source]

Note

Setting old_min / old_max indicates the possible extend of the input data (and it will be clipped to it). It does not mean that the input data has to have those min and max values, but it should be between them.

Example

>>> from kwcoco.util.delayed_ops.helpers import *  # NOQA
>>> # Test error when input is not nicely between 0 and 1
>>> imdata = (np.random.randn(32, 32, 3) - 1.) * 2.5
>>> quant1, quantization1 = quantize_float01(imdata, old_min=0, old_max=1)
>>> recon1 = dequantize(quant1, quantization1)
>>> error1 = np.abs((recon1 - imdata)).sum()
>>> print('error1 = {!r}'.format(error1))
>>> #
>>> for i in range(1, 20):
>>>     print('i = {!r}'.format(i))
>>>     quant2, quantization2 = quantize_float01(imdata, old_min=-i, old_max=i)
>>>     recon2 = dequantize(quant2, quantization2)
>>>     error2 = np.abs((recon2 - imdata)).sum()
>>>     print('error2 = {!r}'.format(error2))

Example

>>> # Test dequantize with uint8
>>> from kwcoco.util.util_delayed_poc import dequantize
>>> imdata = np.random.randn(32, 32, 3)
>>> quant1, quantization1 = quantize_float01(imdata, old_min=0, old_max=1, quantize_dtype=np.uint8)
>>> recon1 = dequantize(quant1, quantization1)
>>> error1 = np.abs((recon1 - imdata)).sum()
>>> print('error1 = {!r}'.format(error1))

Example

>>> # Test quantization with different signed / unsigned combos
>>> print(quantize_float01(None, 0, 1, np.int16))
>>> print(quantize_float01(None, 0, 1, np.int8))
>>> print(quantize_float01(None, 0, 1, np.uint8))
>>> print(quantize_float01(None, 0, 1, np.uint16))
class kwcoco.util.delayed_ops.helpers.AsciiDirectedGlyphs[source]

Bases: _AsciiBaseGlyphs

last = 'L-> '
mid = '|-> '
backedge = '<-'
class kwcoco.util.delayed_ops.helpers.AsciiUndirectedGlyphs[source]

Bases: _AsciiBaseGlyphs

last = 'L-- '
mid = '|-- '
backedge = '-'
class kwcoco.util.delayed_ops.helpers.UtfDirectedGlyphs[source]

Bases: _UtfBaseGlyphs

last = '└─╼ '
mid = '├─╼ '
backedge = '╾'
class kwcoco.util.delayed_ops.helpers.UtfUndirectedGlyphs[source]

Bases: _UtfBaseGlyphs

last = '└── '
mid = '├── '
backedge = '─'
kwcoco.util.delayed_ops.helpers.generate_network_text(graph, with_labels=True, sources=None, max_depth=None, ascii_only=False)[source]

Generate lines in the “network text” format

This works via a depth-first traversal of the graph and writing a line for each unique node encountered. Non-tree edges are written to the right of each node, and connection to a non-tree edge is indicated with an ellipsis. This representation works best when the input graph is a forest, but any graph can be represented.

This notation is original to networkx, although it is simple enough that it may be known in existing literature. See #5602 for details. The procedure is summarized as follows:

1. Given a set of source nodes (which can be specified, or automatically discovered via finding the (strongly) connected components and choosing one node with minimum degree from each), we traverse the graph in depth first order.

  1. Each reachable node will be printed exactly once on it’s own line.

  2. Edges are indicated in one of three ways:

    a. a parent “L-style” connection on the upper left. This corresponds to a traversal in the directed DFS tree.

    b. a backref “<-style” connection shown directly on the right. For directed graphs, these are drawn for any incoming edges to a node that is not a parent edge. For undirected graphs, these are drawn for only the non-parent edges that have already been represented (The edges that have not been represented will be handled in the recursive case).

    c. a child “L-style” connection on the lower right. Drawing of the children are handled recursively.

4. The children of each node (wrt the directed DFS tree) are drawn underneath and to the right of it. In the case that a child node has already been drawn the connection is replaced with an ellipsis (”…”) to indicate that there is one or more connections represented elsewhere.

5. If a maximum depth is specified, an edge to nodes past this maximum depth will be represented by an ellipsis.

Parameters
  • graph (nx.DiGraph | nx.Graph) – Graph to represent

  • with_labels (bool | str) – If True will use the “label” attribute of a node to display if it exists otherwise it will use the node value itself. If given as a string, then that attribte name will be used instead of “label”. Defaults to True.

  • sources (List) – Specifies which nodes to start traversal from. Note: nodes that are not reachable from one of these sources may not be shown. If unspecified, the minimal set of nodes needed to reach all others will be used.

  • max_depth (int | None) – The maximum depth to traverse before stopping. Defaults to None.

  • ascii_only (Boolean) – If True only ASCII characters are used to construct the visualization

Yields

str (a line of generated text)

kwcoco.util.delayed_ops.helpers.write_network_text(graph, path=None, with_labels=True, sources=None, max_depth=None, ascii_only=False, end='\n')[source]

Creates a nice text representation of a graph

This works via a depth-first traversal of the graph and writing a line for each unique node encountered. Non-tree edges are written to the right of each node, and connection to a non-tree edge is indicated with an ellipsis. This representation works best when the input graph is a forest, but any graph can be represented.

Parameters
  • graph (nx.DiGraph | nx.Graph) – Graph to represent

  • path (string or file or callable or None) – Filename or file handle for data output. if a function, then it will be called for each generated line. if None, this will default to “sys.stdout.write”

  • with_labels (bool | str) – If True will use the “label” attribute of a node to display if it exists otherwise it will use the node value itself. If given as a string, then that attribte name will be used instead of “label”. Defaults to True.

  • sources (List) – Specifies which nodes to start traversal from. Note: nodes that are not reachable from one of these sources may not be shown. If unspecified, the minimal set of nodes needed to reach all others will be used.

  • max_depth (int | None) – The maximum depth to traverse before stopping. Defaults to None.

  • ascii_only (Boolean) – If True only ASCII characters are used to construct the visualization

  • end (string) – The line ending characater

Example

>>> import networkx as nx
>>> graph = nx.balanced_tree(r=2, h=2, create_using=nx.DiGraph)
>>> write_network_text(graph)
╙── 0
    ├─╼ 1
    │   ├─╼ 3
    │   └─╼ 4
    └─╼ 2
        ├─╼ 5
        └─╼ 6
>>> # A near tree with one non-tree edge
>>> graph.add_edge(5, 1)
>>> write_network_text(graph)
╙── 0
    ├─╼ 1 ╾ 5
    │   ├─╼ 3
    │   └─╼ 4
    └─╼ 2
        ├─╼ 5
        │   └─╼  ...
        └─╼ 6
>>> graph = nx.cycle_graph(5)
>>> write_network_text(graph)
╙── 0
    ├── 1
    │   └── 2
    │       └── 3
    │           └── 4 ─ 0
    └──  ...
>>> graph = nx.generators.barbell_graph(4, 2)
>>> write_network_text(graph)
╙── 4
    ├── 5
    │   └── 6
    │       ├── 7
    │       │   ├── 8 ─ 6
    │       │   │   └── 9 ─ 6, 7
    │       │   └──  ...
    │       └──  ...
    └── 3
        ├── 0
        │   ├── 1 ─ 3
        │   │   └── 2 ─ 0, 3
        │   └──  ...
        └──  ...
>>> graph = nx.complete_graph(5, create_using=nx.Graph)
>>> write_network_text(graph)
╙── 0
    ├── 1
    │   ├── 2 ─ 0
    │   │   ├── 3 ─ 0, 1
    │   │   │   └── 4 ─ 0, 1, 2
    │   │   └──  ...
    │   └──  ...
    └──  ...
>>> graph = nx.complete_graph(3, create_using=nx.DiGraph)
>>> write_network_text(graph)
╙── 0 ╾ 1, 2
    ├─╼ 1 ╾ 2
    │   ├─╼ 2 ╾ 0
    │   │   └─╼  ...
    │   └─╼  ...
    └─╼  ...
kwcoco.util.delayed_ops.helpers.graph_str(graph, with_labels=True, sources=None, write=None, ascii_only=False)[source]

Creates a nice utf8 representation of a forest

This function has been superseded by nx.readwrite.text.generate_network_text(), which should be used instead.

Parameters
  • graph (nx.DiGraph | nx.Graph) – Graph to represent (must be a tree, forest, or the empty graph)

  • with_labels (bool) – If True will use the “label” attribute of a node to display if it exists otherwise it will use the node value itself. Defaults to True.

  • sources (List) – Mainly relevant for undirected forests, specifies which nodes to list first. If unspecified the root nodes of each tree will be used for directed forests; for undirected forests this defaults to the nodes with the smallest degree.

  • write (callable) – Function to use to write to, if None new lines are appended to a list and returned. If set to the print function, lines will be written to stdout as they are generated. If specified, this function will return None. Defaults to None.

  • ascii_only (Boolean) – If True only ASCII characters are used to construct the visualization

Returns

utf8 representation of the tree / forest

Return type

str | None

Example

>>> import networkx as nx
>>> graph = nx.balanced_tree(r=2, h=3, create_using=nx.DiGraph)
>>> print(graph_str(graph))
╙── 0
    ├─╼ 1
    │   ├─╼ 3
    │   │   ├─╼ 7
    │   │   └─╼ 8
    │   └─╼ 4
    │       ├─╼ 9
    │       └─╼ 10
    └─╼ 2
        ├─╼ 5
        │   ├─╼ 11
        │   └─╼ 12
        └─╼ 6
            ├─╼ 13
            └─╼ 14
>>> graph = nx.balanced_tree(r=1, h=2, create_using=nx.Graph)
>>> print(graph_str(graph))
╙── 0
    └── 1
        └── 2
>>> print(graph_str(graph, ascii_only=True))
+-- 0
    L-- 1
        L-- 2