diff --git a/Project.toml b/Project.toml index b39a60b..41e24c5 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "SpectralIndices" uuid = "df0093a1-273d-40bc-819a-796ec3476907" authors = ["MartinuzziFrancesco "] -version = "0.1.0" +version = "0.1.1" [deps] Dates = "ade2ca70-3891-5945-98fb-dc099432e06a" diff --git a/docs/make.jl b/docs/make.jl index c95e842..8a80045 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -14,6 +14,5 @@ makedocs(; ) deploydocs(; - repo="github.com/awesome-spectral-indices/SpectralIndices.jl.git", - push_preview=true, -) \ No newline at end of file + repo="github.com/awesome-spectral-indices/SpectralIndices.jl.git", push_preview=true +) diff --git a/src/axioms.jl b/src/axioms.jl index 225bec1..f795182 100644 --- a/src/axioms.jl +++ b/src/axioms.jl @@ -55,8 +55,11 @@ A `SpectralIndex` object containing the specified index information. ```julia-repl julia> indices["NIRv"] + ``` + Or, accessing directly the provided Dict of spectral indices: + ```julia-repl NIRv ``` @@ -130,10 +133,12 @@ parameters, and optional keyword arguments. ```julia-repl julia> compute(NDVI; N=0.643, R=0.175) + ``` ```julia-repl julia> compute(NDVI; N=fill(0.643, (5, 5)), R=fill(0.175, (5, 5))) + ``` """ function compute(si::SpectralIndex, params::Dict=Dict(); kwargs...) @@ -192,12 +197,15 @@ platform_band = PlatformBand(platform_band_dict) ``` Or, accessing directly the provided Dict of platforms: + ```julia-repl julia> bands["B"].platforms["sentinel2a"] + ``` ```julia-repl julia> bands["B"].platforms["sentinel2a"].wavelength + ``` """ function PlatformBand(platform_band::Dict) @@ -277,12 +285,14 @@ band_dict = Dict{String, Any}( ) band = Band(band_dict) -```` +``` + Or, using the provided bands + ```julia-repl julia> bands["B"].long_name -``` +``` """ function Band(band::Dict{String,Any}) short_name = band["short_name"] diff --git a/src/compute.jl b/src/compute.jl index 047e669..8ed34f4 100644 --- a/src/compute.jl +++ b/src/compute.jl @@ -23,28 +23,34 @@ based on the provided index name, parameters, and optional keyword arguments. ```julia-repl julia> compute_index("NDVI"; N=0.643, R=0.175) + ``` ```julia-repl julia> compute_index("NDVI"; N=fill(0.643, (5, 5)), R=fill(0.175, (5, 5))) + ``` ```julia-repl julia> compute_index("NDVI"; N=fill(0.643, 5), R=fill(0.175, 5)) + ``` ```julia-repl julia> compute_index(["NDVI", "SAVI"]; N=fill(0.643, 5), R=fill(0.175, 5), L=fill(0.5, 5)) + ``` ```julia-repl julia> compute_index(["NDVI", "SAVI"]; N=0.643, R=0.175, L=0.5) + ``` ```julia-repl julia> compute_index( ["NDVI", "SAVI"]; N=fill(0.643, (5, 5)), R=fill(0.175, (5, 5)), L=fill(0.5, (5, 5)) ) + ``` """ function compute_index(index::String, params::Dict=Dict(), online::Bool=false; kwargs...) diff --git a/test/axioms.jl b/test/axioms.jl new file mode 100644 index 0000000..a57fd82 --- /dev/null +++ b/test/axioms.jl @@ -0,0 +1,31 @@ +using SpectralIndices, Dates + +custom_index = Dict( + "short_name" => "CI", + "long_name" => "Custom Index", + "bands" => ["C", "I"], + "application_domain" => "Vegetation", + "reference" => "Doe et al., 1984", + "formula" => "(C - I) / (C + I)", + "date_of_addition" => "1984-01-01", + "contributor" => "John Doe", + "platforms" => ["Landsat 8", "MODIS"], +) +si = SpectralIndex(custom_index) +@test si.short_name == "CI" +@test si.long_name == "Custom Index" +@test si.bands == ["C", "I"] +@test si.application_domain == "Vegetation" +@test si.reference == "Doe et al., 1984" +@test si.formula == "(C-I)/(C+I)" +@test si.date_of_addition == Dates.Date("1984-01-01") +@test si.contributor == "John Doe" +@test si.platforms == ["Landsat 8", "MODIS"] + +computed_index = si(0.6, 0.2) +@test computed_index ≈ 0.5 atol = 0.01 + +C_vals = fill(0.6, 5) +I_vals = fill(0.2, 5) +computed_array_index = si.(C_vals, I_vals) +@test all(computed_array_index .≈ 0.5) diff --git a/test/compute.jl b/test/compute.jl new file mode 100644 index 0000000..9e43485 --- /dev/null +++ b/test/compute.jl @@ -0,0 +1,18 @@ +using SpectralIndices + +expected_ndvi_value_f64 = 0.5721271393643031 +expected_savi_value_f64 = 0.5326251896813354 + +# Basics +@test compute_index("NDVI"; N=0.643, R=0.175) == expected_ndvi_value_f64 +# Type handling +@test compute_index("NDVI"; N=fill(0.643, 5), R=fill(0.175, 5)) isa Array +# Corner cases +#@test compute_index("NDVI"; N=0.0, R=0.0) == NaN +#Input validation +@test_throws AssertionError compute_index("InvalidIndex"; N=0.5, R=0.5) +# Multiple indices +results = compute_index(["NDVI", "SAVI"]; N=0.643, R=0.175, L=0.5) +@test length(results) == 2 +@test results[1] == expected_ndvi_value_f64 # Replace with correct value +@test results[2] == expected_savi_value_f64 # Replace with correct value diff --git a/test/runtests.jl b/test/runtests.jl index 8b13789..0f74bc9 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1 +1,14 @@ +using SafeTestsets +using Test +@testset "Axioms" begin + @safetestset "Axioms" begin + include("axioms.jl") + end + @safetestset "Compute" begin + include("compute.jl") + end + @safetestset "Utils" begin + include("utils.jl") + end +end diff --git a/test/utils.jl b/test/utils.jl new file mode 100644 index 0000000..8546be1 --- /dev/null +++ b/test/utils.jl @@ -0,0 +1,33 @@ +using SpectralIndices + +# Test correctness +@test SpectralIndices._load_json("spectral-indices-dict.json") isa Dict +# Test missing +@test_throws ErrorException SpectralIndices._load_json("non_existing_file.json") + +# Test offline +@test SpectralIndices._get_indices() isa Dict +# Test online +@test SpectralIndices._get_indices(true) isa Dict + +params = Dict("N" => 0.6, "R" => 0.3) +# Test correctness +@test SpectralIndices._check_params(NDVI, params) === nothing +# Test missing +@test_throws ArgumentError SpectralIndices._check_params(NDVI, Dict("N" => 0.6)) + +locals = Dict("x" => 2, "y" => 3) +expression = "x + y" + +# Test correct evaluation +@test SpectralIndices.parse_eval_dict(expression, locals) == 5 + +name = "test_func" +expr = :(x + y) +args = (:x, :y) + +# Create a test function +test_func = SpectralIndices._build_function(name, expr, args...) + +# Test the created function +@test test_func(2, 3) == 5