Graph Operations

NaiveNASlib.CompGraphType
CompGraph
CompGraph(input::AbstractVertex, output::AbstractVertex)
CompGraph(input::AbstractVector{<:AbstractVertex}, output::AbstractVertex)
CompGraph(input::AbstractVertex, output::AbstractVector{<:AbstractVertex})

Basic graph for computation. While not strictly neccessary to compute anything, it makes it easier to keep track of things.

Examples

julia> using NaiveNASlib

julia> v1 = inputvertex("in1", 1) + inputvertex("in2", 1);

julia> v2 = invariantvertex(x -> 3x, v1);

julia> CompGraph(inputs(v1), v2)(2,3) # (2 + 3) * 3
15

julia> CompGraph(inputs(v1), [v1, v2])(2,3)
(5, 15)
source
NaiveNASlib.verticesFunction
vertices(g::CompGraph)

Return an topologically sorted array of all vertices in the graph g.

Examples

julia> ins = InputVertex.(1:2);

julia> v1 = CompVertex(+, ins...);

julia> v2 = CompVertex(*, v1, ins[2]);

julia> graph = CompGraph(ins, v2);

julia> vertices(graph)
4-element Array{AbstractVertex,1}:
 InputVertex(1)
 InputVertex(2)
 CompVertex(+), inputs=[InputVertex(1), InputVertex(2)]
 CompVertex(*), inputs=[CompVertex(+), InputVertex(2)]
source
NaiveNASlib.findverticesFunction
findvertices(vname::AbstractString, g::CompGraph)

Return all vertices for which name(v) == vname`.

source
findvertices(vpat::Regex, g::CompGraph)

Return all vertices for which vpat matches name(v).

source
findvertices(predicate, g::CompGraph)

Return all vertices for which predicate(v) return true.

source
NaiveNASlib.graphsummaryFunction
graphsummary([io], graph, extracolumns...; [inputhl], [outputhl], kwargs...)

Prints a summary table of graph to io using PrettyTables.pretty_table.

Extra columns can be added to the table by providing any number of extracolumns which can be one of the following:

  • a function (or any callable object) which takes a vertex as input and returns the column content
  • a Pair where the first element is the column name and the other element is what previous bullet describes

The keyword arguments inputhl (default crayon"fg:black bg:249") and outputhl (default inputhl) can be used to set the highlighting of the inputs and outputs to graph respectively. If set to nothing no special highlighting will be used.

All other keyword arguments are forwarded to PrettyTables.pretty_table. Note that this allows for overriding the default formatting, alignment and highlighting.

API Stability

While this function is part of the public API for natural reasons, the exact shape of its output shall not be considered stable.

Base.show for CompGraphs just calls this function. This might change in the future.

Examples

julia> using NaiveNASlib

julia> g = let 
            v1 = "v1" >> inputvertex("in1", 1) + inputvertex("in2", 1)
            v2 = invariantvertex("v2", sin, v1)
            v3 = conc("v3", v1, v2; dims=1) 
            CompGraph(inputs(v1), v3)
        end;

julia> graphsummary(g)
┌────────────────┬───────────┬────────────────┬───────────────────┐
│ Graph Position │ Vertex Nr │ Input Vertices │ Op                │
├────────────────┼───────────┼────────────────┼───────────────────┤
│ Input          │ 1         │                │                   │
│ Input          │ 2         │                │                   │
│ Hidden         │ 3         │ 1,2            │ + (element wise)  │
│ Hidden         │ 4         │ 3              │ sin               │
│ Output         │ 5         │ 3,4            │ cat(x..., dims=1) │
└────────────────┴───────────┴────────────────┴───────────────────┘

julia> graphsummary(g, name, "input sizes" => nin, "output sizes" => nout)
┌────────────────┬───────────┬────────────────┬───────────────────┬──────┬─────────────┬──────────────┐
│ Graph Position │ Vertex Nr │ Input Vertices │ Op                │ Name │ input sizes │ output sizes │
├────────────────┼───────────┼────────────────┼───────────────────┼──────┼─────────────┼──────────────┤
│ Input          │ 1         │                │                   │ in1  │             │ 1            │
│ Input          │ 2         │                │                   │ in2  │             │ 1            │
│ Hidden         │ 3         │ 1,2            │ + (element wise)  │ v1   │ 1,1         │ 1            │
│ Hidden         │ 4         │ 3              │ sin               │ v2   │ 1           │ 1            │
│ Output         │ 5         │ 3,4            │ cat(x..., dims=1) │ v3   │ 1,1         │ 2            │
└────────────────┴───────────┴────────────────┴───────────────────┴──────┴─────────────┴──────────────┘
source