-
Notifications
You must be signed in to change notification settings - Fork 1
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
Visualize AbstractTensorNetworks
interactively with ECharts
#211
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
005951d
Init visualization in VSCode with ECharts
mofeing 55a5070
Remove Pkg.Artifacts for now
mofeing 99d7833
Locally provide `echarts.js` library
mofeing b0e4149
Some aesthetic enhancements
mofeing 92d8701
Remove Graphs from weakdeps
mofeing b069696
Support `text/html` MIME
mofeing 7b28929
Fix symbol clash with Graphs
mofeing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using ECharts_jll | ||
using Graphs: Graphs, vertices, edges | ||
|
||
function graph_representation(tn::AbstractTensorNetwork) | ||
hypermap = Tenet.hyperflatten(tn) | ||
if !isempty(hypermap) | ||
tn = transform(tn, Tenet.HyperFlatten) | ||
end | ||
tensormap = IdDict(tensor => i for (i, tensor) in enumerate(tensors(tn))) | ||
|
||
graph = Graphs.SimpleGraph(length(tensors(tn))) | ||
for i in setdiff(inds(tn; set=:inner), inds(tn; set=:hyper)) | ||
a, b = tensors(tn; intersects=i) | ||
Graphs.add_edge!(graph, tensormap[a], tensormap[b]) | ||
end | ||
|
||
# TODO recognise `copytensors` by using `DeltaArray` or `Diagonal` representations | ||
hypernodes = findall(tensor -> any(flatinds -> issetequal(inds(tensor), flatinds), keys(hypermap)), tensors(tn)) | ||
ghostnodes = map(inds(tn; set=:open)) do index | ||
# create new ghost node | ||
Graphs.add_vertex!(graph) | ||
node = Graphs.nv(graph) | ||
|
||
# connect ghost node | ||
tensor = only(tn.indexmap[index]) | ||
Graphs.add_edge!(graph, node, tensormap[tensor]) | ||
|
||
return node | ||
end | ||
|
||
return tn, graph, tensormap, hypermap, hypernodes, ghostnodes | ||
end | ||
|
||
Base.show(io::IO, ::MIME"text/html", @nospecialize(tn::AbstractTensorNetwork)) = show(io, MIME"juliavscode/html"(), tn) | ||
function Base.show(io::IO, ::MIME"juliavscode/html", @nospecialize(tn::AbstractTensorNetwork)) | ||
tn, graph, tensormap, hypermap, hypernodes, ghostnodes = graph_representation(tn) | ||
hypermap = Dict(Iterators.flatten([[i => v for i in k] for (k, v) in hypermap])) | ||
|
||
appid = gensym("tenet-graph") | ||
return print( | ||
io, | ||
""" | ||
<script type="text/javascript">$( | ||
read(joinpath(dirname(ECharts_jll.echarts), "echarts.min.js"), String) | ||
)</script> | ||
<div id="$appid" style="width: 600px; height: 600px;"></div> | ||
<script> | ||
var chart = echarts.init(document.getElementById('$appid'), null, {renderer: 'svg'}); | ||
|
||
option = { | ||
series: [ | ||
{ | ||
type: 'graph', | ||
layout: 'force', | ||
animation: false, | ||
darkMode: 'auto', | ||
left: '5%', | ||
top: '5%', | ||
width: '95%', | ||
height: '95%', | ||
draggable: true, | ||
force: { | ||
gravity: 0, | ||
repulsion: 100, | ||
edgeLength: 4 | ||
}, | ||
data: [$(join(map(vertices(graph)) do v | ||
return "{ " * | ||
"name: '$v', " * | ||
"symbol: $(if v ∈ ghostnodes | ||
"'none'" | ||
elseif v ∈ hypernodes | ||
"'diamond'" | ||
else | ||
"'circle'" | ||
end), " * | ||
"symbolSize: $(v ∈ ghostnodes ? "0" : "8")" * | ||
" }" | ||
end, ", "))], | ||
edges: [$(join( | ||
map(inds(tn)) do i | ||
nodes = tensors(tn; intersects=i) | ||
if length(nodes) == 1 | ||
# TODO | ||
v = tensormap[only(nodes)] | ||
return "{ source: '$v', target: '$v', name: '$i' }" | ||
elseif length(nodes) == 2 | ||
a, b = nodes | ||
index = get(hypermap, i, i) | ||
return "{ source: '$(tensormap[a])', target: '$(tensormap[b])', name: '$index' }" | ||
end | ||
end | ||
, ','))], | ||
edgeLabel: { | ||
show: false, | ||
formatter: function (params) { | ||
return params.data.name; | ||
} | ||
}, | ||
emphasis: { | ||
label: false, | ||
edgeLabel: { | ||
show: true, | ||
fontSize: 20, | ||
}, | ||
lineStyle: { | ||
width: 3 | ||
}, | ||
}, | ||
} | ||
] | ||
}; | ||
|
||
option && chart.setOption(option); | ||
</script> | ||
""", | ||
) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be an extension? Since we have the visualization libraries
GraphMakie
andMakie
as an extension too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point but in this case no. The reason is that package extensions are there to avoid precompile and load time overheads.
In this case, ECharts is a JavaScript library (so only gets loaded on the web browser/notebook/VSCode, it doesn't get loaded to the Julia session). Furthermore, JLL packages don't load anything to memory... they are just wrappers over some "binary" files stored on disk. And the minified file just weights 1MiB...