Skip to content

Commit 34f499a

Browse files
author
Vincent Landau
committed
add more tests to improve coverage
1 parent 28e4b69 commit 34f499a

File tree

6 files changed

+152
-2
lines changed

6 files changed

+152
-2
lines changed

test/lg_interface.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using SpatialGraphs, Test, GeoData, LightGraphs, SimpleWeightedGraphs
2+
## This script tests methods for the LightGraph interface that aren't already
3+
## used in other tests
4+
5+
A_array = Array{Float32}(undef, (3, 4, 1))
6+
A_array[:,:,:] = [1, 3, 2, 0.5, 10, 8, 5, -9999, 3, 1, 2, 6]
7+
x = X(1:4)
8+
y = Y(1:3)
9+
band = Band(1:1)
10+
11+
weight_raster = GeoArray(A_array, (y, x, band), missingval = -9999)
12+
rasgraph = weightedrastergraph(weight_raster)
13+
14+
@test nv(rasgraph) == maximum(rasgraph.vertex_raster)
15+
num_edge = ne(rasgraph)
16+
@test vertices(rasgraph) == 1:maximum(rasgraph.vertex_raster)
17+
@test eltype(rasgraph) == Int # Should always be Int64 (or int32 on 32-bit)
18+
@test edgetype(rasgraph) == SimpleWeightedEdge{eltype(rasgraph), eltype(A_array)}
19+
20+
for i in 1:maximum(rasgraph.vertex_raster)
21+
@test has_vertex(rasgraph, i)
22+
end
23+
24+
# In undirected graph incoming neighbors should equal outgoing
25+
@test inneighbors(rasgraph, 1) == outneighbors(rasgraph, 1)
26+
27+
empty = zero(rasgraph)
28+
29+
add_vertices!(empty, maximum(rasgraph.vertex_raster))
30+
add_edge!(empty, 1, 4, Float32(0.5))
31+
32+
@test nv(empty) == maximum(rasgraph.vertex_raster)
33+
@test has_edge(empty, 1, 4)

test/runtests.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Test, SpatialGraphs
1+
using GeoData, LightGraphs, SimpleWeightedGraphs, SpatialGraphs, Test
2+
3+
@testset "Simple Raster Graph Construction" begin
4+
include("simplerastergraphs.jl")
5+
end
26

37
@testset "Weighted Raster Graph Construction" begin
48
include("weightedrastergraphs.jl")
@@ -10,4 +14,21 @@ end
1014

1115
@testset "Weighted Raster DiGraph Construction" begin
1216
include("weightedrasterdigraphs.jl")
13-
end
17+
end
18+
19+
@testset "Checking LightGraphs Interface" begin
20+
include("lg_interface.jl")
21+
end
22+
23+
printstyled("Checking that Base.show works...\n", bold = true)
24+
25+
A_array = Array{Float64}(undef, (3, 4, 1))
26+
A_array[:,:,:] = [1, 3, 2, 0.5, 10, 8, 5, -9999, 3, 1, 2, 6]
27+
x = X(1:4)
28+
y = Y(1:3)
29+
band = Band(1:1)
30+
31+
weight_raster = GeoArray(A_array, (y, x, band), missingval = -9999)
32+
rasgraph = weightedrastergraph(weight_raster)
33+
show(rasgraph);print("\n")
34+

test/simplerasterdigraphs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ for i in 1:length(graph_edges)
5252
col_diff = abs(source_coords[2] - dest_coords[2])
5353
@test col_diff <= 1
5454
end
55+
56+
@test is_directed(rasgraph)
57+
@test zero(rasgraph).vertex_raster == rasgraph.vertex_raster

test/simplerastergraphs.jl

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using GeoData, LightGraphs, SimpleWeightedGraphs, SpatialGraphs, Test
2+
3+
array = Array{Int}(undef, (3, 4, 1))
4+
array[:,:,:] = [1, 1, 1, 1, 2, 2, 2, -9999, 2, 3, 3, 3]
5+
6+
x = X(1:4)
7+
y = Y(1:3)
8+
band = Band(1:1)
9+
10+
raster = GeoArray(array, (y, x, band), missingval = -9999)
11+
12+
compare = ==
13+
rasgraph = simplerastergraph(
14+
raster,
15+
directed = false,
16+
condition = compare
17+
)
18+
19+
# no vertices in NoData pixels?
20+
@test (rasgraph.vertex_raster .== 0) ==
21+
((raster .== raster.missingval) .|
22+
isnan.(raster))
23+
24+
# Is the number of of vertices correct, and is the range of values correct?
25+
@test sort(collect(rasgraph.vertex_raster[rasgraph.vertex_raster .!= 0])) ==
26+
collect(1:sum(
27+
(raster .!= raster.missingval) .&
28+
(!).(isnan.(raster))
29+
))
30+
31+
graph_edges = collect(edges(rasgraph))
32+
33+
# Test that the edges are correct and have proper weights
34+
for i in 1:length(graph_edges)
35+
source_i = src(graph_edges[i])
36+
dest_i = dst(graph_edges[i])
37+
38+
source_coords = findall(rasgraph.vertex_raster .== source_i)[1]
39+
dest_coords = findall(rasgraph.vertex_raster .== dest_i)[1]
40+
41+
# Check that condition is met
42+
@test compare(
43+
raster[source_coords],
44+
raster[dest_coords]
45+
)
46+
47+
# Test that source row is within 1 step of dest row
48+
row_diff = abs(source_coords[1] - dest_coords[1])
49+
@test row_diff <= 1
50+
51+
# Test that source column is within 1 step of dest row
52+
col_diff = abs(source_coords[2] - dest_coords[2])
53+
@test col_diff <= 1
54+
end
55+
56+
@test is_directed(rasgraph) == false
57+
@test zero(rasgraph).vertex_raster == rasgraph.vertex_raster

test/weightedrasterdigraphs.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,6 @@ for i in 1:length(graph_edges)
6868
weight_raster[dest_coords])
6969
end
7070
end
71+
72+
@test is_directed(rasgraph)
73+
@test zero(rasgraph).vertex_raster == rasgraph.vertex_raster

test/weightedrastergraphs.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,36 @@ for i in 1:length(graph_edges)
5656
weight_raster[dest_coords])
5757
end
5858
end
59+
60+
# check that no edges exist that shouldn't
61+
vertex_raster = rasgraph.vertex_raster
62+
for src_row in 1:size(vertex_raster)[1]
63+
for src_col in 1:size(vertex_raster)[2]
64+
for dst_row in 1:size(vertex_raster)[1]
65+
for dst_col in 1:size(vertex_raster)[2]
66+
if ((vertex_raster[src_row, src_col] == 0) |
67+
(vertex_raster[dst_row, dst_col] == 0))
68+
continue
69+
elseif ((abs(src_row - dst_row) <= 1) &
70+
(abs(src_col - dst_col) <= 1)) & (
71+
(abs(src_row - dst_row) > 0) |
72+
(abs(src_col - dst_col) > 0))
73+
@test has_edge(
74+
rasgraph,
75+
vertex_raster[src_row, src_col],
76+
vertex_raster[dst_row, dst_col]
77+
) == true
78+
else
79+
@test (has_edge(
80+
rasgraph,
81+
vertex_raster[src_row, src_col],
82+
vertex_raster[dst_row, dst_col]
83+
) == false)
84+
end
85+
end
86+
end
87+
end
88+
end
89+
90+
@test is_directed(rasgraph) == false
91+
@test zero(rasgraph).vertex_raster == rasgraph.vertex_raster

0 commit comments

Comments
 (0)