Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json exporting #82

Open
jarlebring opened this issue Feb 7, 2024 · 2 comments
Open

json exporting #82

jarlebring opened this issue Feb 7, 2024 · 2 comments

Comments

@jarlebring
Copy link
Collaborator

jarlebring commented Feb 7, 2024

For presenting and visualization of our work, it would be cool to generate 3d-force-graphs of the computation graphs using

https://github.com/vasturiano/3d-force-graph/

We need

a) functionality to export the computation graph to a json format. This could either be a separate function 1) export_json or 2) a part of gen_code with LangJSON. At the moment I would say option 1) since it would not need the memory management functionality.

b) some javascript that reads the json and encodes operations / coefficients etc graphically in the 3d-force-graph format wanted in that library

Point a) should be rather straightforward if we add dependence JSON.jl following this:

using JSON

nodes=Vector{Any}();
links=Vector{Any}();
for n in keys(g.nodes)
    push!(nodes,Dict("id" => sting(n), "operation" => string(g.operations[n]))); 
    for (i,n2) = enumerate(g.parents[n])
        d=Dict{String,Any}("source" => string(n),
                           "target" => string(n2),
                          "weight" => string(2);
      push!(links,d)
    end
end
json=Dict("nodes" => nodes, "links" => links);
open("comp_tree.json","w") do f
    JSON.print(f, json)
end

Javascript examples:
https://people.kth.se/~eliasj/watkins-graph-small.html
https://people.kth.se/~eliasj/large-graph.html

@jarlebring
Copy link
Collaborator Author

jarlebring commented Feb 7, 2024

More thoughts: One could ask if this functionality should be in the package or somewhere else. The package is now kind of clean in terms of what it can/should do (in the same sense that BLAS and LAPACK are clean). Adding visualizations is yet another feature... On the other hand, generate json seems to be not so much code and focused on the data structure rather than visualization, so if we can restrict it to json-generation (not javascript generation) it is maybe still okay.

@jarlebring
Copy link
Collaborator Author

As an intermediate step, I suggest we add the functionality to compute the adjacency matrix.

The standardized way for the function is adjacency_matrix(g). https://juliagraphs.org/Graphs.jl/stable/algorithms/linalg/#Graphs.LinAlg.adjacency_matrix (I don't think we should add dependency on Graphs.jl.)

Something like:

function adjecency_matrix(g)
    (nodes,_)=get_topo_order(g)
    # Include the basic fundamental nodes
    pushfirst!(nodes,:I)
    pushfirst!(nodes,:A)
    m=size(nodes,1);
    A=zeros(m,m);
    for (i,n) in enumerate(nodes)
        children=GraphMatFun.get_children(g,n)
        children_idx=map(i-> findfirst(nodes .== i), children);
        # Set all children to value 1.
        A[i,children_idx] .= 1;
    end
    # A will be a sparse non-symmetric matrix with ones and zeros
    return A;
end

Then we can directly use graphplots

julia> using GraphRecipes, Plots
julia> (g,_)=graph_sastre_poly([randn(8);1])
julia> A=adjecency_matrix(g);
julia> graphplot(A, names=["I"; "A"; string.(get_topo_order(g)[1])])

Screenshot_20241013_093552

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant