-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a DiagTree to Graph transformation
- Loading branch information
Showing
3 changed files
with
77 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
""" | ||
function Graph!(d::DiagTree.Diagram{W}; map=Dict{Int,DiagTree.DiagramId}()) where {W} | ||
Converts a DiagTree `d` into a Graph, storing the diagram information (which is a DiagramId object) in a Graph.id to DiagramId dictionary ``map". | ||
# Arguments: | ||
- `d`: DiagTree.Diagram object. | ||
- `map`: map between the Graph.id and DiagramId. It will be updated with the new nodes and leaves contained in the DiagTree.Diagram `d`. | ||
# Example: | ||
```julia-repl | ||
julia> para = DiagParaF64(type = Ver4Diag, innerLoopNum=2); | ||
julia> ver4=Parquet.build(para) | ||
2×5 DataFrame | ||
Row │ diagram extT hash response type | ||
│ Diagram… Tuple… Int64 Response Analytic… | ||
─────┼───────────────────────────────────────────────────────────────────────────── | ||
1 │ 5978:↑↑Dyn#[0, 0, 0, 0],t(1, 1, … (1, 1, 1, 1) 5978 UpUp Dynamic | ||
2 │ 5979:↑↓Dyn#[0, 0, 0, 0],t(1, 1, … (1, 1, 1, 1) 5979 UpDown Dynamic | ||
julia> d = ver4.diagram[1] # take the first diagram | ||
5978:↑↑Dyn#[0, 0, 0, 0],t(1, 1, 1, 1)=0.0=⨁ (5026, 5071, 5137, 5146, 5175, 5220, 5312, 5321, 5350, 5396, 5463, 5473, 5503, 5549, 5642, 5652, 5682, 5793, 5831, 5968) | ||
julia> root = FrontEnds.Graph!(d) | ||
``` | ||
""" | ||
function Graph!(d::DiagTree.Diagram{W}; map=Dict{Int,DiagTree.DiagramId}()) where {W} | ||
|
||
function op(o) | ||
if o isa DiagTree.Sum | ||
return ComputationalGraphs.Sum() | ||
elseif o isa DiagTree.Prod | ||
return ComputationalGraphs.Prod() | ||
else | ||
error("Unknown operator: $o") | ||
end | ||
end | ||
|
||
subgraph = ComputationalGraphs.Graph{W,W}[] | ||
for g in d.subdiagram | ||
res, map = Graph!(g; map=map) | ||
push!(subgraph, res) | ||
end | ||
|
||
tree = ComputationalGraphs.Graph(subgraph; subgraph_factors=ones(W, length(d.subdiagram)), name=String(d.name), operator=op(d.operator), orders=d.id.order, ftype=W, wtype=W, weight=d.weight) | ||
|
||
root = ComputationalGraphs.Graph([tree,]; subgraph_factors=[d.factor,], name=tree.name, orders=tree.orders, ftype=W, wtype=W, weight=d.weight * d.factor) | ||
|
||
@assert haskey(map, root.id) == false "DiagramId already exists in map: $(root.id)" | ||
@assert haskey(map, tree.id) == false "DiagramId already exists in map: $(tree.id)" | ||
map[root.id] = d.id | ||
map[tree.id] = d.id | ||
|
||
return root, map | ||
end |
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