Skip to content

Commit

Permalink
Merge pull request #36 from awesome-spectral-indices/fm/tt
Browse files Browse the repository at this point in the history
Refactor of tests
  • Loading branch information
MartinuzziFrancesco authored Feb 17, 2024
2 parents c19da0e + aee5d90 commit a151eb5
Show file tree
Hide file tree
Showing 9 changed files with 334 additions and 100 deletions.
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@ julia = "1.6"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
DimensionalData = "0703355e-b756-11e9-17c0-8b28908087d0"
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
JuliaFormatter = "98e50ef6-434e-11e9-1051-2b60c6c9e899"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
YAXArrays = "c21b50f5-aa40-41ea-b809-c0f5e47bfa5c"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[targets]
test = ["Test", "SafeTestsets", "Aqua", "JET", "JuliaFormatter", "DataFrames", "YAXArrays", "Random"]
test = ["Test", "SafeTestsets", "Aqua", "JET", "JuliaFormatter", "DataFrames", "YAXArrays", "Random", "Combinatorics", "StatsBase", "DimensionalData"]
87 changes: 87 additions & 0 deletions test/DataFrames/compute_index.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Test
using SpectralIndices
using DataFrames
using Random
using Combinatorics
using StatsBase
Random.seed!(17)

function convert_to_kwargs(df::DataFrame)
kwargs = [(Symbol(band) => DataFrame(band => df[:, band])) for band in names(df)]
return kwargs
end

@testset "DataFrames compute_index single index tests: $idx_name" for (idx_name, idx) in indices
@testset "as Params" begin
if idx_name == "AVI" || idx_name == "TVI"
params = DataFrame(; N=[0.2, 0.2], R=[0.1, 0.1])
else
params = DataFrame([band => rand(10) for band in idx.bands])
end
result = compute_index(idx_name, params)
@test result isa DataFrame
@test names(result) == [idx_name]
end

@testset "as Kwargs" begin
if idx_name == "AVI" || idx_name == "TVI"
params = DataFrame(; N=[0.2, 0.2], R=[0.1, 0.1])
else
params = DataFrame([band => rand(10) for band in idx.bands])
end
result = compute_index(idx_name; convert_to_kwargs(params)...)
@test result isa DataFrame
@test names(result) == [idx_name]
end
end

msi = custom_key_combinations(indices, 2, 200)

@testset "DataFrames compute_index multiple indices tests: $idxs" for idxs in msi

if idxs[1] in ["AVI", "TVI"] && length(idxs) > 1
for i in 2:length(idxs)
if !(idxs[i] in ["AVI", "TVI"])
idxs[1], idxs[i] = idxs[i], idxs[1]
break
end
end
end

@testset "as Params" begin
params = DataFrame()
for idx_name in idxs
idx = indices[idx_name]
if idx_name == "AVI" || idx_name == "TVI"
params[!, "N"] = fill(0.2, 10)
params[!, "R"] = fill(0.1, 10)
else
for band in idx.bands
params[!, band] = rand(10)
end
end
end
result = compute_index(idxs, params)
#@test eltype(result) isa Float64 #TODO fix this
@test result isa DataFrame
@test names(result) == idxs
end

@testset "as Kwargs" begin
params = DataFrame()
for idx_name in idxs
idx = indices[idx_name]
if idx_name == "AVI" || idx_name == "TVI"
params[!, "N"] = fill(0.2, 10)
params[!, "R"] = fill(0.1, 10)
else
for band in idx.bands
params[!, band] = rand(10)
end
end
end
result = compute_index(idxs; convert_to_kwargs(params)...)
@test result isa DataFrame
@test names(result) == idxs
end
end
47 changes: 47 additions & 0 deletions test/DataFrames/compute_kernel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Test
using SpectralIndices
using DataFrames

function convert_to_kwargs(df::DataFrame)
kwargs = [(Symbol(band) => DataFrame(band => df[:, band])) for band in names(df)]
return kwargs
end

params = DataFrame(;
a=[1, 2], b=[3, 4], c=[1, 1], p=[2, 2], sigma=[5, 5]
)

@testset "linear test" begin
result = linear(params)
@test result isa DataFrame
end

@testset "poly test" begin
result = poly(params)
@test result isa DataFrame
end

@testset "RBF test" begin
result = RBF(params)
@test result isa DataFrame
end

@testset "DataFrames compute_kernel tests" begin
@testset "as Params" begin
lr = compute_kernel(linear, params)
@test lr isa DataFrame
pr = compute_kernel(poly, params)
@test pr isa DataFrame
rr = compute_kernel(RBF, params)
@test rr isa DataFrame
end

@testset "as Kwargs" begin
lr = compute_kernel(linear; convert_to_kwargs(params)...)
@test lr isa DataFrame
pr = compute_kernel(poly; convert_to_kwargs(params)...)
@test pr isa DataFrame
rr = compute_kernel(RBF; convert_to_kwargs(params)...)
@test rr isa DataFrame
end
end
124 changes: 124 additions & 0 deletions test/YAXArrays/compute_index.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using Test
using SpectralIndices
using YAXArrays
using DimensionalData
using Random
using Combinatorics
using StatsBase
Random.seed!(17)

function convert_to_kwargs(yaxarr::YAXArray)
var_names = lookup(yaxarr, :Variables)
kwargs = [(Symbol(var_name) => yaxarr[Variable=At(var_name)]) for var_name in var_names]
return kwargs
end

xdim = Dim{:x}(range(1, 10, length=10))
ydim = Dim{:x}(range(1, 10, length=15))

@testset "YAXArrays compute_index single index tests: $idx_name" for (idx_name, idx) in indices

@testset "as Params" begin
if idx_name == "AVI" || idx_name == "TVI"
nyx = YAXArray((xdim, ydim), fill(0.2, 10, 15))
ryx = YAXArray((xdim, ydim), fill(0.1, 10, 15))
bandsnames = Dim{:Variables}(["N", "R"])
params = concatenatecubes([nyx, ryx], bandsnames)
else
bands_dim = Dim{:Variables}(idx.bands)
data = cat([fill(rand(), 10, 15, 1) for _ in idx.bands]...; dims=3)
params = YAXArray((xdim, ydim, bands_dim), data)
end
result = compute_index(idx_name, params)
@test result isa YAXArray
@test size(result) == (length(xdim), length(ydim))
end

@testset "as Kwargs" begin
if idx_name == "AVI" || idx_name == "TVI"
nyx = YAXArray((xdim, ydim), fill(0.2, 10, 15))
ryx = YAXArray((xdim, ydim), fill(0.1, 10, 15))
bandsnames = Dim{:Variables}(["N", "R"])
params = concatenatecubes([nyx, ryx], bandsnames)
else
bands_dim = Dim{:Variables}(idx.bands)
data = cat([fill(rand(), 10, 15, 1) for _ in idx.bands]...; dims=3)
params = YAXArray((xdim, ydim, bands_dim), data)
end
result = compute_index(idx_name; convert_to_kwargs(params)...)
@test result isa YAXArray
@test size(result) == (length(xdim), length(ydim))
end
end

msi = custom_key_combinations(indices, 2, 200)

@testset "YAXArrays compute_index multiple indices tests: $idxs" for idxs in msi

if idxs[1] in ["AVI", "TVI"] && length(idxs) > 1
for i in 2:length(idxs)
if !(idxs[i] in ["AVI", "TVI"])
idxs[1], idxs[i] = idxs[i], idxs[1]
break
end
end
end

@testset "as Params" begin
yaxa_tmp = []
yaxa_names = String[]

for idx_name in idxs
idx = indices[idx_name]
if idx_name == "AVI" || idx_name == "TVI"
for band in ["N", "R"]
value = band == "N" ? 0.2 : 0.1
push!(yaxa_names, string(band))
data = fill(value, 10, 15)
push!(yaxa_tmp, YAXArray((xdim, ydim), data))
end
else
for band in idx.bands
append!(yaxa_names, [string(band)])
data = fill(rand(), 10, 15)
push!(yaxa_tmp, YAXArray((xdim, ydim), data))
end
end
end
unique_band_names = unique(yaxa_names)
unique_yaxas = yaxa_tmp[1:length(unique_band_names)] #sheesh, more elegant pls
params = concatenatecubes(unique_yaxas, Dim{:Variables}(unique_band_names))
result = compute_index(idxs, params)
@test result isa YAXArray
@test size(result) == (length(xdim), length(ydim), 2)
end

@testset "as Kwargs" begin
yaxa_tmp = []
yaxa_names = String[]

for idx_name in idxs
idx = indices[idx_name]
if idx_name == "AVI" || idx_name == "TVI"
for band in ["N", "R"]
value = band == "N" ? 0.2 : 0.1
push!(yaxa_names, string(band))
data = fill(value, 10, 15)
push!(yaxa_tmp, YAXArray((xdim, ydim), data))
end
else
for band in idx.bands
append!(yaxa_names, [string(band)])
data = fill(rand(), 10, 15)
push!(yaxa_tmp, YAXArray((xdim, ydim), data))
end
end
end
unique_band_names = unique(yaxa_names)
unique_yaxas = yaxa_tmp[1:length(unique_band_names)] #sheesh, more elegant pls
params = concatenatecubes(unique_yaxas, Dim{:Variables}(unique_band_names))
result = compute_index(idxs; convert_to_kwargs(params)...)
@test result isa YAXArray
@test size(result) == (length(xdim), length(ydim), 2)
end
end
54 changes: 54 additions & 0 deletions test/YAXArrays/compute_kernel.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Test
using SpectralIndices
using YAXArrays

function convert_to_kwargs(yaxarr::YAXArray)
var_names = lookup(yaxarr, :Variables)
kwargs = [(Symbol(var_name) => yaxarr[Variable=At(var_name)]) for var_name in var_names]
return kwargs
end

axlist = (
Dim{:Lon}(1:5),
Dim{:Lat}(1:5),
Dim{:Variables}(["a", "b", "c", "p", "sigma"]),
)
data = rand(5, 5, 5)

params = YAXArray(axlist, data)

@testset "linear test" begin
result = linear(params)
@test result isa YAXArray
end

@testset "poly test" begin
result = poly(params)
@test result isa YAXArray
end

@testset "RBF test" begin
result = RBF(params)
@test result isa YAXArray
end

@testset "YAXArrays compute_kernel tests" begin
@testset "as Params" begin
lr = compute_kernel(linear, params)
@test lr isa YAXArray
pr = compute_kernel(poly, params)
@test pr isa YAXArray
rr = compute_kernel(RBF, params)
@test rr isa YAXArray
end

@testset "as Kwargs" begin
lr = compute_kernel(linear; convert_to_kwargs(params)...)
@test lr isa YAXArray
pr = compute_kernel(poly; convert_to_kwargs(params)...)
@test pr isa YAXArray
rr = compute_kernel(RBF; convert_to_kwargs(params)...)
@test rr isa YAXArray
end
end

53 changes: 0 additions & 53 deletions test/compute_index.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Test
using SpectralIndices
using DataFrames
using YAXArrays
using Random
Random.seed!(17)
Expand Down Expand Up @@ -98,55 +97,3 @@ end
@test size(result) == (10,)
end
end

@testset "DataFrame Tests" begin
@testset "Single Index as Params" begin
df_single = DataFrame(; N=[0.643, 0.56], R=[0.175, 0.22])
result_single = compute_index("NDVI", df_single)
@test size(result_single, 1) == 2
@test size(result_single, 2) == 1
@test names(result_single) == ["NDVI"]
@test typeof(result_single[!, "NDVI"][1]) == Float64
end

@testset "Single Index as kwargs" begin
dfn_single = DataFrame(; N=[0.643, 0.56])
dfr_single = DataFrame(; R=[0.175, 0.22])
result_single2 = compute_index("NDVI"; N=dfn_single, R=dfr_single)
@test size(result_single2, 1) == 2
@test size(result_single2, 2) == 1
@test names(result_single2) == ["NDVI"]
@test typeof(result_single2[!, "NDVI"][1]) == Float64
end

@testset "Multiple Indices as Params" begin
df_multiple = DataFrame(; N=[0.643, 0.56], R=[0.175, 0.22], L=[0.5, 0.4])
result_multiple = compute_index(["NDVI", "SAVI"], df_multiple)
@test size(result_multiple, 1) == 2
@test size(result_multiple, 2) == 2
@test names(result_multiple) == ["NDVI", "SAVI"]
@test typeof(result_multiple[!, "NDVI"][1]) == Float64
@test typeof(result_multiple[!, "SAVI"][1]) == Float64
end
end

@testset "YAXArray Tests" begin
# Setup YAXArrays
axes = (Dim{:Lon}(1:5), Dim{:Lat}(1:5), Dim{:Time}(1:10))
nds = YAXArray(axes, fill(0.643, (5, 5, 10)))
rds = YAXArray(axes, fill(0.175, (5, 5, 10)))
lds = YAXArray(axes, fill(0.5, (5, 5, 10)))

@testset "Single Index Tests" begin
nr_ds = concatenatecubes([nds, rds], Dim{:Variables}(["N", "R"]))
result_yaxa_single = compute_index("NDVI", nr_ds)
@test result_yaxa_single isa YAXArray
@test size(result_yaxa_single) == size(rds) == size(nds)
end
@testset "Multiple Indices Tests" begin
nrl_ds = concatenatecubes([nds, rds, lds], Dim{:Variables}(["N", "R", "L"]))
result_yaxa_multiple = compute_index(["NDVI", "SAVI"], nrl_ds)
@test result_yaxa_multiple isa YAXArray
@test size(result_yaxa_multiple)[1:(end - 1)] == size(rds) == size(nds) == size(lds)
end
end
Loading

0 comments on commit a151eb5

Please sign in to comment.