Skip to content
Snippets Groups Projects
Commit 4499a7fc authored by xo30xoqa's avatar xo30xoqa
Browse files

Fixed broken tests

Added tests for dataframe data collection (#64), refactored
`smalltestlandscape` into `inittestmodel`.
parent fddae638
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
end end
@testset "Output functions" begin @testset "Output functions" begin
model = smalltestlandscape() model = inittestmodel()
# test that the output directory is created with all files # test that the output directory is created with all files
outdir = @param(core.outdir) outdir = @param(core.outdir)
Ps.createdatadir(outdir, @param(core.overwrite)) Ps.createdatadir(outdir, @param(core.overwrite))
...@@ -37,12 +37,12 @@ end ...@@ -37,12 +37,12 @@ end
Ps.createdatadir(outdir, @param(core.overwrite))) Ps.createdatadir(outdir, @param(core.overwrite)))
@test_throws ErrorException Ps.createdatadir(outdir, false) @test_throws ErrorException Ps.createdatadir(outdir, false)
# test that creating a DataOutput works, and outputs data with the required frequency # test that creating a DataOutput works, and outputs data with the required frequency
testoutput(model) = string(model.date)*"\n" testoutput(model) = [[string(model.date)]]
Ps.newdataoutput!(model, "never.csv", "Date", testoutput, "never") Ps.newdataoutput!(model, "never", ["Date"], testoutput, "never")
Ps.newdataoutput!(model, "daily.csv", "Date", testoutput, "daily") Ps.newdataoutput!(model, "daily", ["Date"], testoutput, "daily")
Ps.newdataoutput!(model, "monthly.csv", "Date", testoutput, "monthly") Ps.newdataoutput!(model, "monthly", ["Date"], testoutput, "monthly")
Ps.newdataoutput!(model, "yearly.csv", "Date", testoutput, "yearly") Ps.newdataoutput!(model, "yearly", ["Date"], testoutput, "yearly")
Ps.newdataoutput!(model, "end.csv", "Date", testoutput, "end") Ps.newdataoutput!(model, "end", ["Date"], testoutput, "end")
@param(core.enddate) = Date(2023,2,1) @param(core.enddate) = Date(2023,2,1)
@test_logs((:info, "Simulated 366 days."), @test_logs((:info, "Simulated 366 days."),
match_mode=:any, match_mode=:any,
...@@ -51,12 +51,15 @@ end ...@@ -51,12 +51,15 @@ end
@test !isfile(joinpath(outdir, "never.csv")) @test !isfile(joinpath(outdir, "never.csv"))
@test isfile(joinpath(outdir, "daily.csv")) @test isfile(joinpath(outdir, "daily.csv"))
@test countlines(joinpath(outdir, "daily.csv")) == 367 @test countlines(joinpath(outdir, "daily.csv")) == 367
@test size(model.datatables["daily"]) == (366, 1)
@test isfile(joinpath(outdir, "monthly.csv")) @test isfile(joinpath(outdir, "monthly.csv"))
@test countlines(joinpath(outdir, "monthly.csv")) == 14 @test countlines(joinpath(outdir, "monthly.csv")) == 14
@test size(model.datatables["monthly"]) == (13, 1)
@test isfile(joinpath(outdir, "yearly.csv")) @test isfile(joinpath(outdir, "yearly.csv"))
@test countlines(joinpath(outdir, "yearly.csv")) == 3 @test countlines(joinpath(outdir, "yearly.csv")) == 3
@test size(model.datatables["yearly"]) == (2, 1)
@test isfile(joinpath(outdir, "end.csv")) @test isfile(joinpath(outdir, "end.csv"))
@test countlines(joinpath(outdir, "end.csv")) == 2 @test countlines(joinpath(outdir, "end.csv")) == 2
@test size(model.datatables["end"]) == (1, 1)
rm(outdir, force=true, recursive=true) rm(outdir, force=true, recursive=true)
#TODO test dataframe output
end end
...@@ -4,13 +4,7 @@ ...@@ -4,13 +4,7 @@
### ###
@testset "Landscape initialisation" begin @testset "Landscape initialisation" begin
# initialise the landscape part of the model model = inittestmodel(false)
landscape = Ps.initlandscape(TESTSETTINGS["world.landcovermap"],
TESTSETTINGS["world.farmfieldsmap"])
space = GridSpace(size(landscape), periodic=false)
properties = Dict{Symbol,Any}(:landscape=>landscape, :settings=>TESTSETTINGS,
:date=>TESTSETTINGS["core.startdate"])
model = AgentBasedModel(FarmPlot, space, properties=properties, warn=false)
# these tests are specific to the Jena maps # these tests are specific to the Jena maps
@test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.initfields!(model) @test_logs (:info, "Initialised 2092 farm plots.") match_mode=:any Ps.initfields!(model)
@test size(model.landscape) == (1754, 1602) @test size(model.landscape) == (1754, 1602)
...@@ -26,7 +20,7 @@ ...@@ -26,7 +20,7 @@
end end
@testset "Event system" begin @testset "Event system" begin
model = smalltestlandscape() model = inittestmodel()
createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Ps.tillage) createevent!(model, [(1,1), (1,2), (1,3), (2,1), (2,3)], Ps.tillage)
createevent!(model, [(1,1), (1,2), (1,3), (2,2)], Ps.sowing, 2) createevent!(model, [(1,1), (1,2), (1,3), (2,2)], Ps.sowing, 2)
@test model.landscape[1,1].events == [Ps.tillage, Ps.sowing] @test model.landscape[1,1].events == [Ps.tillage, Ps.sowing]
...@@ -47,7 +41,7 @@ end ...@@ -47,7 +41,7 @@ end
end end
@testset "Landscape functions" begin @testset "Landscape functions" begin
model = smalltestlandscape() model = inittestmodel()
@test Ps.distanceto((2,3), model, Ps.forest) == 0 @test Ps.distanceto((2,3), model, Ps.forest) == 0
@test Ps.distanceto((2,3), model, Ps.grass) == 2 @test Ps.distanceto((2,3), model, Ps.grass) == 2
@test Ps.distanceto((2,3), model, Ps.water) == 4 @test Ps.distanceto((2,3), model, Ps.water) == 4
...@@ -59,7 +53,7 @@ end ...@@ -59,7 +53,7 @@ end
@testset "Weather initialisation" begin @testset "Weather initialisation" begin
# these tests are specific to the Jena weather file # these tests are specific to the Jena weather file
model = smalltestlandscape() model = inittestmodel()
@test_logs((:warn, "There are missing days in the weather input file."), @test_logs((:warn, "There are missing days in the weather input file."),
Ps.initweather(TESTSETTINGS["world.weatherfile"], Ps.initweather(TESTSETTINGS["world.weatherfile"],
Date("2022-01-01"), Date("2023-12-31"))) Date("2022-01-01"), Date("2023-12-31")))
......
...@@ -5,15 +5,17 @@ ...@@ -5,15 +5,17 @@
@testset "Habitat macros" begin @testset "Habitat macros" begin
# set up the testing landscape # set up the testing landscape
model = smalltestlandscape() model = inittestmodel()
model.landscape[6,6] = Pixel(Ps.agriculture, 1, []) model.landscape[6,6] = Pixel(Ps.agriculture, 1, [])
species::Dict{String,Any} = Dict("name"=>"test_animal") species::Dict{String,Any} = Dict("name"=>"test_animal")
add_agent!((6,6), FarmPlot, model, [(6,6)], Ps.wheat, 1.2, 3.4) #FIXME add_agent!((6,6), FarmPlot, model,
[(6,6)], model.crops["winter wheat"], Ps.janfirst,
0.0, 0.0, 0.0, 0.0, Vector{Ps.EventType}())
add_agent!((3,3), Animal, model, species, Ps.male, 1) add_agent!((3,3), Animal, model, species, Ps.male, 1)
add_agent!((4,4), Animal, model, species, Ps.female, 1) add_agent!((4,4), Animal, model, species, Ps.female, 1)
# create a set of habitat descriptors # create a set of habitat descriptors
h1 = @habitat(@landcover() == Ps.water) h1 = @habitat(@landcover() == Ps.water)
h2 = @habitat(@cropname() == "wheat" && h2 = @habitat(@cropname() == "winter wheat" &&
@cropheight() < 2) @cropheight() < 2)
h3 = @habitat(@distanceto(Ps.water) > 2 && h3 = @habitat(@distanceto(Ps.water) > 2 &&
@distancetoedge() <= 2) @distancetoedge() <= 2)
...@@ -32,7 +34,7 @@ ...@@ -32,7 +34,7 @@
end end
@testset "Species initialisation" begin @testset "Species initialisation" begin
model = smalltestlandscape() model = inittestmodel()
spec = "test_animal" spec = "test_animal"
species::Dict{String,Any} = Dict("name"=>spec) species::Dict{String,Any} = Dict("name"=>spec)
# create a set of initialisation functions # create a set of initialisation functions
...@@ -68,7 +70,7 @@ end ...@@ -68,7 +70,7 @@ end
@testset "Species macros" begin @testset "Species macros" begin
# create a model landscape and a test species # create a model landscape and a test species
model = smalltestlandscape() model = inittestmodel()
@species Mermaid begin @species Mermaid begin
ageofmaturity = 2 ageofmaturity = 2
...@@ -139,7 +141,7 @@ end ...@@ -139,7 +141,7 @@ end
@testset "Insect submodel" begin @testset "Insect submodel" begin
# create a set of pixels and dates for testing # create a set of pixels and dates for testing
model = smalltestlandscape() model = inittestmodel()
date1 = Date("2023-05-08") # day 128 (season begin) date1 = Date("2023-05-08") # day 128 (season begin)
date2 = Date("2023-07-06") # day 187 (insect max) date2 = Date("2023-07-06") # day 187 (insect max)
date3 = Date("2023-09-27") # day 270 (season end) date3 = Date("2023-09-27") # day 270 (season end)
......
...@@ -8,6 +8,7 @@ Pkg.activate("..") ...@@ -8,6 +8,7 @@ Pkg.activate("..")
using Agents using Agents
using Dates using Dates
using DataFrames
using Logging using Logging
using LoggingExtras using LoggingExtras
using Persefone using Persefone
...@@ -20,6 +21,37 @@ const Ps = Persefone ...@@ -20,6 +21,37 @@ const Ps = Persefone
const TESTPARAMETERS = joinpath(pkgdir(Persefone), "test/test_parameters.toml") const TESTPARAMETERS = joinpath(pkgdir(Persefone), "test/test_parameters.toml")
const TESTSETTINGS = Ps.getsettings(TESTPARAMETERS) const TESTSETTINGS = Ps.getsettings(TESTPARAMETERS)
"""
Initialise an AgentBasedModel for testing purposes.
`smallmap`: use a hypothetical small landscape rather than a real one?
"""
function inittestmodel(smallmap=true)
if smallmap
landscape = smalltestlandscape()
else
landscape = Ps.initlandscape(TESTSETTINGS["world.landcovermap"],
TESTSETTINGS["world.farmfieldsmap"])
end
space = GridSpace(size(landscape), periodic=false)
weather = Ps.initweather(TESTSETTINGS["world.weatherfile"],
TESTSETTINGS["core.startdate"],
TESTSETTINGS["core.enddate"])
crops = Ps.readcropparameters(TESTSETTINGS["crop.cropfile"],
TESTSETTINGS["crop.growthfile"])
properties = Dict{Symbol,Any}(:date=>TESTSETTINGS["core.startdate"],
:landscape=>landscape,
:weather=>weather,
:crops=>crops,
:events=>Vector{FarmEvent}(),
:logger=>global_logger(),
:dataoutputs=>Vector{DataOutput}(),
:datatables=>Dict{String,DataFrame}(),
:settings=>TESTSETTINGS)
return AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties,
rng=StableRNG(TESTSETTINGS["core.seed"]), warn=false)
end
""" """
smalltestlandscape() smalltestlandscape()
...@@ -41,22 +73,7 @@ function smalltestlandscape() ...@@ -41,22 +73,7 @@ function smalltestlandscape()
end end
end end
landscape[6,4] = Pixel(Ps.water, 0, []) landscape[6,4] = Pixel(Ps.water, 0, [])
space = GridSpace(size(landscape), periodic=false) landscape
weather = Ps.initweather(TESTSETTINGS["world.weatherfile"],
TESTSETTINGS["core.startdate"],
TESTSETTINGS["core.enddate"])
crops = Ps.readcropparameters(TESTSETTINGS["crop.cropfile"],
TESTSETTINGS["crop.growthfile"])
properties = Dict{Symbol,Any}(:date=>TESTSETTINGS["core.startdate"],
:landscape=>landscape,
:weather=>weather,
:crops=>crops,
:events=>Vector{FarmEvent}(),
:logger=>global_logger(),
:dataoutputs=>Vector{DataOutput}(),
:settings=>TESTSETTINGS)
return AgentBasedModel(Union{Farmer,Animal,FarmPlot}, space, properties=properties,
rng=StableRNG(TESTSETTINGS["core.seed"]), warn=false)
end end
@testset "Persefone Tests" begin @testset "Persefone Tests" begin
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment