Skip to content

Commit

Permalink
Merge pull request #376 from Circuitscape/RA/network_adv
Browse files Browse the repository at this point in the history
network: check if nodes start from zero
  • Loading branch information
ranjanan authored Dec 30, 2022
2 parents 5f1fcf5 + cd468eb commit dd314a7
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 19 deletions.
40 changes: 24 additions & 16 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end
function IncludeExcludePairs(V)
IncludeExcludePairs(:undef, V[], Matrix{V}(undef,0,0))
end
Base.isempty(x::IncludeExcludePairs) = isempty(x.include_pairs)
Base.isempty(t::IncludeExcludePairs) = t.mode == :undef

struct NetworkData{T,V} <: Data
coords::Tuple{Vector{V},Vector{V},Vector{T}}
Expand Down Expand Up @@ -66,17 +66,24 @@ function load_graph(V, gpath::String, ::Type{T}) where {T}
j[iter] = g[iter,2] + 1
end
end
i,j,v
i,j,v, min_node==0
end

read_focal_points(V, path::String) = V.(vec(readdlm(path)) .+ 1)
function read_focal_points(V, path::String)
ret = try
vec(readdlm(path, V))
catch
vec(V.(readdlm(path)))
end
minimum(ret) == 0 && (ret .+= 1)
ret
end

function read_point_strengths(T, path::String, inc = true)
a = readdlm(path, T)
if inc
@. a[:,1] = a[:,1] + 1
end
a
function read_point_strengths(T, path::String, starts_from_zero)
str = readdlm(path, T)
starts_from_zero = minimum(str[:,1]) == 0 || starts_from_zero
starts_from_zero && (str[:,1] .+= 1)
str
end

function read_cellmap(habitat_file::String, is_res::Bool, ::Type{T}) where {T}
Expand Down Expand Up @@ -370,7 +377,8 @@ function get_network_data(T, V, cfg)::NetworkData{T,V}

is_pairwise = cfg["scenario"] in PAIRWISE

i,j,v = load_graph(V, hab_file, T)
i,j,v,starts_from_zero = load_graph(V, hab_file, T)

if hab_is_res
v = 1 ./ v
end
Expand All @@ -382,14 +390,14 @@ function get_network_data(T, V, cfg)::NetworkData{T,V}
end

if !is_pairwise
source_map = read_point_strengths(T, source_file)
ground_map = read_point_strengths(T, ground_file)
source_list = read_point_strengths(T, source_file, starts_from_zero)
ground_list = read_point_strengths(T, ground_file, starts_from_zero)
else
source_map = Matrix{T}(undef,0,0)
ground_map = Matrix{T}(undef,0,0)
source_list = Matrix{T}(undef,0,0)
ground_list = Matrix{T}(undef,0,0)
end

NetworkData((i,j,v), fp, source_map, ground_map)
NetworkData((i,j,v), fp, source_list, ground_list)
end

function load_raster_data(T, V, cfg)::RasterData{T,V}
Expand Down Expand Up @@ -469,7 +477,7 @@ function load_raster_data(T, V, cfg)::RasterData{T,V}

# Variable source strengths
if use_var_source
strengths = read_point_strengths(T, var_source_file)
strengths = read_point_strengths(T, var_source_file, false)
else
strengths = Matrix{T}(undef, 0,0)
end
Expand Down
2 changes: 2 additions & 0 deletions src/network/advanced.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ function compute_advanced_data(data::NetworkData{T,V},
A = A + A'

cc = connected_components(SimpleWeightedGraph(A))
c = size(A,1)
csinfo("Graph has $c nodes and $(length(cc)) connected components", cfg["suppress_messages"] in TRUELIST)

t = @elapsed G = laplacian(A)
csinfo("Time taken to construct graph laplacian = $t", cfg["suppress_messages"] in TRUELIST)
Expand Down
2 changes: 2 additions & 0 deletions src/network/pairwise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ function compute_graph_data(data::NetworkData{T,V}, cfg)::GraphProblem{T,V} wher
A = A + A'

cc = connected_components(SimpleWeightedGraph(A))
c = size(A,1)
csinfo("Graph has $c nodes and $(length(cc)) connected components", cfg["suppress_messages"] in TRUELIST)

t = @elapsed G = laplacian(A)
csinfo("Time taken to construct graph laplacian = $t", cfg["suppress_messages"] in TRUELIST)
Expand Down
1 change: 0 additions & 1 deletion src/raster/onetoall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ function onetoall_kernel(data::RasterData{T,V}, flags, cfg)::Matrix{T} where {T,
points_unique = included_pairs.point_ids
prune_points!(points_rc, included_pairs.point_ids)
if use_variable_strengths
strengths[:,1] .-= 1
strengths = prune_strengths(strengths, included_pairs.point_ids)
end
end
Expand Down
1 change: 0 additions & 1 deletion src/raster/pairwise.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ function compute_graph_data_no_polygons(data::RasterData{T,V},
hbmeta, cellmap, cum, solver)

end
Base.isempty(t::IncludeExcludePairs) = t.mode == :undef

function generate_exclude_pairs(points_rc, included_pairs::IncludeExcludePairs{V}) where V

Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ function runtests(f = compute)

@testset "Raster One to All" begin
# Raster one to all test
for i in 1:13
for i in 1:13
@info("Testing oneToAllVerify$i")
r = f("input/raster/one_to_all/$i/oneToAllVerify$(i).ini")
x = readdlm("output_verify/oneToAllVerify$(i)_resistances.out")
Expand Down

0 comments on commit dd314a7

Please sign in to comment.