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

Added optional keyword parameter to gplot for plotting Chain Graphs #110

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions src/lines.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
function hasReverseEdge(g, e)
return has_edge(g,dst(e),src(e))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return has_edge(g,dst(e),src(e))
return has_edge(g, dst(e), src(e))

end

function filterUndef(array)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function filterUndef(array)
function filterAssigned(array)

or filterDefined.

Reason is, that when one uses the filter function then predicate specifies what should stay in the array and not what should be filtered out.

result = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result = []
result = eltype(array)[]

for i in 1:length(array)
if isassigned(array,i)
if result == []
result = typeof(array[i])[]
end
Comment on lines +9 to +11
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if result == []
result = typeof(array[i])[]
end

We can remove this as we can already initialize the list correctly above.

push!(result,array[i])
end
end
return result
end

"""
Return lines and arrow heads
"""
function graphline(g, locs_x, locs_y, nodesize::Vector{T}, arrowlength, angleoffset) where {T<:Real}
function graphline(g, locs_x, locs_y, nodesize::Vector{T}, arrowlength, angleoffset, chainGraph) where {T<:Real}
lines = Array{Vector{Tuple{Float64,Float64}}}(undef, ne(g))
arrows = Array{Vector{Tuple{Float64,Float64}}}(undef, ne(g))
for (e_idx, e) in enumerate(edges(g))
Expand All @@ -15,13 +32,21 @@ function graphline(g, locs_x, locs_y, nodesize::Vector{T}, arrowlength, angleoff
endx = locs_x[j] + nodesize[j]*cos(θ+π)
endy = locs_y[j] + nodesize[j]*sin(θ+π)
lines[e_idx] = [(startx, starty), (endx, endy)]
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
if chainGraph
if !hasReverseEdge(g,e)
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end
else
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end
Comment on lines +35 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if chainGraph
if !hasReverseEdge(g,e)
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end
else
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end
if !(chainGraph && !hasReverseEdge(g, e))
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end

end
arrows = chainGraph ? filterUndef(arrows) : arrows
lines, arrows
end

function graphline(g::AbstractGraph{T}, locs_x, locs_y, nodesize::Real, arrowlength, angleoffset) where {T<:Integer}
function graphline(g::AbstractGraph{T}, locs_x, locs_y, nodesize::Real, arrowlength, angleoffset, chainGraph) where {T<:Integer}
lines = Array{Vector{Tuple{Float64,Float64}}}(undef, ne(g))
arrows = Array{Vector{Tuple{Float64,Float64}}}(undef, ne(g))
for (e_idx, e) in enumerate(edges(g))
Expand All @@ -35,9 +60,17 @@ function graphline(g::AbstractGraph{T}, locs_x, locs_y, nodesize::Real, arrowlen
endx = locs_x[j] + nodesize*cos(θ+π)
endy = locs_y[j] + nodesize*sin(θ+π)
lines[e_idx] = [(startx, starty), (endx, endy)]
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
if chainGraph
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You seem to have used tabs here. For consistency with the rest of the project we should spaces instead.

if !hasReverseEdge(g,e)
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end
else
arr1, arr2 = arrowcoords(θ, endx, endy, arrowlength, angleoffset)
arrows[e_idx] = [arr1, (endx, endy), arr2]
end
end
arrows = chainGraph ? filterUndef(arrows) : arrows
lines, arrows
end

Expand Down
5 changes: 4 additions & 1 deletion src/plot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Equal to 0 for undirected graphs. Default: `0.1` for the directed graphs
`arrowangleoffset`
Optional. Angular width in radians for the arrows. Default: `π/9 (20 degrees)`

`chainGraph`
Optional. Hides arrows for bidirectional edges in a directed graph. Default: false
"""
function gplot(g::AbstractGraph{T},
locs_x_in::Vector{R}, locs_y_in::Vector{R};
Expand All @@ -110,6 +112,7 @@ function gplot(g::AbstractGraph{T},
arrowlengthfrac = is_directed(g) ? 0.1 : 0.0,
arrowangleoffset = π / 9.0,
linetype = "straight",
chainGraph = false,
outangle = pi/5) where {T <:Integer, R <: Real}

length(locs_x_in) != length(locs_y_in) && error("Vectors must be same length")
Expand Down Expand Up @@ -204,7 +207,7 @@ function gplot(g::AbstractGraph{T},
end
else
if arrowlengthfrac > 0.0
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset)
lines_cord, arrows_cord = graphline(g, locs_x, locs_y, nodesize, arrowlengthfrac, arrowangleoffset, chainGraph)
lines = line(lines_cord)
arrows = line(arrows_cord)
else
Expand Down
Binary file added test/data/chainGraphFalse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/chainGraphSizeFalse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/chainGraphSizeTrue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/data/chainGraphTrue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,21 @@ end
@test test_images(VisualTest(plot_and_save2, refimg2), popup=!istravis) |> save_comparison |> success

end

@testset "ChainGraph" begin
g = SimpleDiGraph([ 0 1 0 1 ; 1 0 1 0 ; 1 0 0 0 ; 1 0 1 0])
refimg1 = joinpath(datadir, "chainGraphTrue.png")
refimg2 = joinpath(datadir, "chainGraphFalse.png")
plot_and_save1(fname) = plot_and_save(fname, g, chainGraph = true)
plot_and_save2(fname) = plot_and_save(fname, g, chainGraph = false)
@test test_images(VisualTest(plot_and_save1, refimg1), popup=!istravis) |> save_comparison |> success
@test test_images(VisualTest(plot_and_save2, refimg2), popup=!istravis) |> save_comparison |> success

g2 = SimpleDiGraph([ 0 1 0 0 0; 0 0 1 1 1; 0 1 0 1 1; 0 1 1 0 1; 0 1 1 1 0])
refimg3 = joinpath(datadir, "chainGraphSizeTrue.png")
refimg4 = joinpath(datadir, "chainGraphSizeFalse.png")
plot_and_save3(fname) = plot_and_save(fname, g2, chainGraph=true, nodesize = [0.6; 1.0; 1.0; 1.0; 1.0])
plot_and_save4(fname) = plot_and_save(fname, g2, chainGraph=false, nodesize = [0.6; 1.0; 1.0; 1.0; 1.0])
@test test_images(VisualTest(plot_and_save3, refimg3), popup=!istravis) |> save_comparison |> success
@test test_images(VisualTest(plot_and_save4, refimg4), popup=!istravis) |> save_comparison |> success
end