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

Add utility to create cumulative current maps #115

Merged
merged 2 commits into from
Apr 25, 2018
Merged
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
79 changes: 78 additions & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export model_problem,
test_problem
test_problem,
accumulate_current_maps,
calculate_cum_current_maps,
calculate_max_current_maps

"""
Construct nodemap specific to a connected component
Expand Down Expand Up @@ -100,7 +103,81 @@ function get_output_flags(cfg)
compress_grids, log_transform_maps)
end

# Helps start new processes from the INI file
function myaddprocs(n)
addprocs(n)
@everywhere eval(:(using Circuitscape))
end

# Reads the directory with the current maps
# and accumulates all current maps
function accumulate_current_maps(path, f)
dir = dirname(path)
base = basename(path)

# If base file has a dot
name = split(base, '.')[1]

cmap_list = readdir(dir) |>
x -> filter(y -> startswith(y, "$(name)_"), x) |>
x -> filter(y -> contains(y, "_curmap_"), x)
isempty(cmap_list) && return

headers = ""
first_file = joinpath(dir, cmap_list[1])
nrow = 0
ncol = 0

# Read the headers from the first file
open(first_file, "r") do f

# Get num cols
str = readline(f)
headers = headers * str * "\n"
ncol = split(str)[2] |> x -> parse(Int, x)

# Get num rows
str = readline(f)
headers = headers * str * "\n"
nrow = split(str)[2] |> x -> parse(Int, x)

# Just append the rest
for i = 3:6
headers = headers * readline(f) * "\n"
end
end

accum = zeros(nrow, ncol)
for file in cmap_list
csinfo("Accumulating $file")
cmap_path = joinpath(dir, file)
cmap = readdlm(cmap_path, skipstart = 6)
f_in_place!(accum, cmap, f)
end
for i in eachindex(accum)
if accum[i] < -9999
accum[i] = -9999
end
end

name = if isequal(f, +)
"cum"
elseif isequal(f, max)
"max"
end

accum_path = joinpath(dir, name * "_$(name)_curmap.asc")
csinfo("Writing to $accum_path")
open(accum_path, "w") do f
write(f, headers)
writedlm(f, round.(accum, 8), ' ')
end

end

function f_in_place!(accum, cmap, f)
accum .= f.(accum, cmap)
end

calculate_cum_current_maps(path) = accumulate_current_maps(path, +)
calculate_max_current_maps(path) = accumulate_current_maps(path, max)
2 changes: 1 addition & 1 deletion test/compare_output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function compare_all_output(str)

end

list_of_files(str, pref) = readdir(pref) |> y -> filter(x -> startswith(x, str), y)
list_of_files(str, pref) = readdir(pref) |> y -> filter(x -> startswith(x, "$(str)_"), y)
generate_lists(str) = list_of_files(str, "output/"), list_of_files(str, "output_verify/")
read_branch_currents(str) = readdlm(str)
read_node_currents(str) = readdlm(str)
Expand Down
14 changes: 7 additions & 7 deletions test/output_verify/sgVerify13_cum_curmap.asc
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ xllcorner 1.0
yllcorner 1.0
cellsize 1.0
NODATA_value -9999
2.089490232 1.298758624 1.298758488 1.373804185 1.101848817 0 0 0 2.225223299 3.633008265
0.880221949 0 0 0.6084204474 1.635223393 2.241510011 2.108250501 2.322789333 3.22525816 2.349119614
0.8802219367 0.4909988629 0.4233619249 0.5243430599 0 2.689625687 2.232807643 2.46287694 3.824104077 3.095566935
0.3892230027 0.4783190821 0.5738108754 0.9367029631 0.9367029266 2.653629619 0 0 4.218353589 4.869719782
0 0 0 0 0 2.000000581 0 0 3.000000128 0
0 0 0 0 0 2.000000569 0 0 0 0
0 0 0 0 0 4.519092106e-07 0 0 0 0
2.044037243 1.209661117 1.209661316 1.24659036 0.9380350996 0 0 0 0.818918643 1.162821409
0.878413185 0 0 0.4761611522 1.35051523 1.633042696 1.108143365 1.071244169 1.226906459 0.5860314587
0.8784132355 0.4901211344 0.4120352548 0.4408382726 0 1.851181938 1.150041093 1.0717009 1.186836301 0.5805456336
0.3882920652 0.4769378899 0.5715689367 0.9309241284 0.9309242376 2.324020179 0 0 1.062598272 0.4344830572
0 0 0 0 0 2.000000301 0 0 1.000000077 0
0 0 0 0 0 2.000000427 0 0 0 0
0 0 0 0 0 2.613089854e-07 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ str = f == compute ? "Double" : "Single"
for i = 1:3
info("Testing sgNetworkVerify$i")
r = f("input/network/sgNetworkVerify$(i).ini")
calculate_cum_current_maps("output/sgNetworkVerify$(i).out")
x = readdlm("output_verify/sgNetworkVerify$(i)_resistances.out")
valx = x[2:end, 2:end]
valr = r[2:end, 2:end]
Expand All @@ -39,6 +40,7 @@ end
for i = 1:3
info("Testing mgNetworkVerify$i")
r = f("input/network/mgNetworkVerify$(i).ini")
calculate_cum_current_maps("output/mgNetworkVerify$(i).out")
x = readdlm("output_verify/mgNetworkVerify$(i)_voltages.txt")
x[:,1] = x[:,1] + 1
@test sum(abs2, x - r) < 1e-6
Expand All @@ -53,6 +55,7 @@ end
for i = 1:15
info("Testing sgVerify$i")
r = f("input/raster/pairwise/$i/sgVerify$(i).ini")
calculate_cum_current_maps("output/sgVerify$(i).out")
x = readdlm("output_verify/sgVerify$(i)_resistances.out")
# x = x[2:end, 2:end]
@test sum(abs2, x - r) < 1e-6
Expand All @@ -66,6 +69,7 @@ end
for i in 1:5
info("Testing mgVerify$i")
r = f("input/raster/advanced/$i/mgVerify$(i).ini")
calculate_cum_current_maps("output/mgVerify$(i).out")
x = readdlm("output_verify/mgVerify$(i)_voltmap.asc"; skipstart = 6)
@test sum(abs2, x - r) < 1e-5
# compare_all_output("mgVerify$(i)")
Expand All @@ -78,6 +82,7 @@ end
for i in 1:13
info("Testing oneToAllVerify$i")
r = f("input/raster/one_to_all/$i/oneToAllVerify$(i).ini")
calculate_cum_current_maps("output/oneToAllVerify$(i).out")
x = readdlm("output_verify/oneToAllVerify$(i)_resistances.out")
# x = x[:,2]
@test sum(abs2, x - r) < 1e-6
Expand All @@ -91,6 +96,7 @@ end
for i in 1:12
info("Testing allToOneVerify$i")
r = f("input/raster/all_to_one/$i/allToOneVerify$(i).ini")
calculate_cum_current_maps("output/allToOneVerify$(i).out")
x = readdlm("output_verify/allToOneVerify$(i)_resistances.out")
# x = x[:,2]

Expand Down