module tools.graphs

Inheritance diagram of mlprodict.tools.graphs

Short summary

module mlprodict.tools.graphs

Alternative to dot to display a graph.

Classes

class

truncated documentation

AdjacencyGraphDisplay

Structure which contains the necessary information to display a graph using an adjacency matrix.

BiGraph

BiGraph representation.

Functions

function

truncated documentation

make_hash_bytes

Creates a hash of length length.

onnx2bigraph

Converts an ONNX graph into a graph representation, edges, vertices.

onnx_graph_distance

Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. …

Static Methods

staticmethod

truncated documentation

_onnx2bigraph_basic

Implements graph type ‘basic’ for function onnx2bigraph().

_onnx2bigraph_simplified

Implements graph type ‘simplified’ for function onnx2bigraph().

onnx_graph_distance

Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. …

Methods

method

truncated documentation

__getitem__

Returns a vertex is key is a string or an edge if it is a tuple.

__init__

__init__

__iter__

Iterates over actions.

__iter__

Iterates over all vertices and edges. It produces 3-uples:

__str__

usual

__str__

usual

add

Adds an action to display the graph.

adjacency_matrix

Builds an adjacency matrix.

display_structure

Creates a display structure which contains all the necessary steps to display a graph.

order

Order nodes. Depth first. Returns a sequence of keys of mixed v1, v2.

order_vertices

Orders the vertices from the input to the output.

summarize

Creates a text summary of the graph.

to_text

Displays the graph as a single string. See onnx2bigraph() to see how the result looks like.

Documentation

Alternative to dot to display a graph.

New in version 0.7.

source on GitHub

class mlprodict.tools.graphs.AdjacencyGraphDisplay

Bases: object

Structure which contains the necessary information to display a graph using an adjacency matrix.

New in version 0.7.

source on GitHub

class Action(x, y, kind, label, orientation=None)

Bases: object

One action to do.

__init__(x, y, kind, label, orientation=None)
__repr__()

usual

__init__()
__iter__()

Iterates over actions.

__str__()

usual

add(x, y, kind, label, orientation=None)

Adds an action to display the graph.

Parameters
  • x – x coordinate

  • y – y coordinate

  • kind‘cross’ or ‘text’

  • label – specific to kind

  • orientation – a 2-uple (i,j) where i or j in {-1,0,1}

source on GitHub

to_text()

Displays the graph as a single string. See onnx2bigraph to see how the result looks like.

Returns

str

source on GitHub

class mlprodict.tools.graphs.BiGraph(v0, v1, edges)

Bases: object

BiGraph representation.

New in version 0.7.

source on GitHub

Parameters
  • v0 – first set of vertices (dictionary)

  • v1 – second set of vertices (dictionary)

  • edges – edges

source on GitHub

class A(kind)

Bases: object

Additional information for a vertex or an edge.

__init__(kind)
__repr__()

Return repr(self).

class B(name, content, onnx_name)

Bases: object

Additional information for a vertex or an edge.

__init__(name, content, onnx_name)
__repr__()

Return repr(self).

__getitem__(key)

Returns a vertex is key is a string or an edge if it is a tuple.

Parameters

key – vertex or edge name

Returns

value

source on GitHub

__init__(v0, v1, edges)
Parameters
  • v0 – first set of vertices (dictionary)

  • v1 – second set of vertices (dictionary)

  • edges – edges

source on GitHub

__iter__()

Iterates over all vertices and edges. It produces 3-uples:

  • 0, name, A: vertices in v0

  • 1, name, A: vertices in v1

  • -1, name, A: edges

source on GitHub

__str__()

usual

source on GitHub

static _onnx2bigraph_basic(model_onnx, recursive=False)

Implements graph type ‘basic’ for function onnx2bigraph.

source on GitHub

static _onnx2bigraph_simplified(model_onnx, recursive=False)

Implements graph type ‘simplified’ for function onnx2bigraph.

source on GitHub

adjacency_matrix()

Builds an adjacency matrix.

Returns

matrix, list of row vertices, list of column vertices

source on GitHub

display_structure(grid=5, distance=5)

Creates a display structure which contains all the necessary steps to display a graph.

Parameters
  • grid – align text to this grid

  • distance – distance to the text

Returns

instance of AdjacencyGraphDisplay

source on GitHub

static onnx_graph_distance(onx1, onx2, verbose=0, fLOG=<built-in function print>)

Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. The function relies on package mlstatpy.

Parameters
  • onx1 – first graph (ONNX graph or model file name)

  • onx2 – second graph (ONNX graph or model file name)

  • verbose – verbosity

  • fLOG – logging function

Returns

distance and differences

Warning

This is very experimental and very slow.

New in version 0.7.

source on GitHub

order()

Order nodes. Depth first. Returns a sequence of keys of mixed v1, v2.

source on GitHub

order_vertices()

Orders the vertices from the input to the output.

Returns

dictionary {vertex name: order}

source on GitHub

summarize()

Creates a text summary of the graph.

source on GitHub

mlprodict.tools.graphs.make_hash_bytes(data, length=20)

Creates a hash of length length.

source on GitHub

mlprodict.tools.graphs.onnx2bigraph(model_onnx, recursive=False, graph_type='basic')

Converts an ONNX graph into a graph representation, edges, vertices.

Parameters
  • model_onnx – ONNX graph

  • recursive – dig into subgraphs too

  • graph_type – kind of graph it creates

Returns

see @cl BiGraph

About graph_type:

  • ‘basic’: basic graph structure, it returns an instance

    of type BiGraph. The structure keeps the original names.

  • ‘simplified’: simplifed graph structure, names are removed

    as they could be prevent the algorithm to find any matching.

Displays an ONNX graph as text

The function uses an adjacency matrix of the graph. Results are displayed by rows, operator by columns. Results kinds are shows on the left, their names on the right. Operator types are displayed on the top, their names on the bottom.

<<<

import numpy
from skl2onnx.algebra.onnx_ops import OnnxAdd, OnnxSub
from mlprodict.onnx_conv import to_onnx
from mlprodict.tools import get_opset_number_from_onnx
from mlprodict.tools.graphs import onnx2bigraph

idi = numpy.identity(2).astype(numpy.float32)
opv = get_opset_number_from_onnx()
A = OnnxAdd('X', idi, op_version=opv)
B = OnnxSub(A, 'W', output_names=['Y'], op_version=opv)
onx = B.to_onnx({'X': idi, 'W': idi})
bigraph = onnx2bigraph(onx)
graph = bigraph.display_structure()
text = graph.to_text()
print(text)

>>>

    /usr/local/lib/python3.9/site-packages/onnx/numpy_helper.py:93: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
    Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
      if arr.dtype == np.object:
                          A  S                    
                          d  u                    
                          d  b                    
                                                  
                                                  
                                                  
                                                  
        Init              I1             Ad_Addcst
     Input-1                 I1          W        
     Input-0              I0             X        
       inout              O0 I0          Ad_C0    
    Output-0                 O0          Y        
                                                  
                                                  
                                                  
                                                  
                                                  
                          A  S                    
                          d  u                    
                          _  _                    
                          A  S                    
                          d  u                    
                          d  b

New in version 0.7.

source on GitHub

mlprodict.tools.graphs.onnx_graph_distance(onx1, onx2, verbose=0, fLOG=<built-in function print>)

Computes a distance between two ONNX graphs. They must not be too big otherwise this function might take for ever. The function relies on package mlstatpy.

Parameters
  • onx1 – first graph (ONNX graph or model file name)

  • onx2 – second graph (ONNX graph or model file name)

  • verbose – verbosity

  • fLOG – logging function

Returns

distance and differences

Warning

This is very experimental and very slow.

New in version 0.7.

source on GitHub