-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from awesome-spectral-indices/fm/tt
Refactor of tests
- Loading branch information
Showing
9 changed files
with
334 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.