Skip to content

Commit

Permalink
Merge pull request #3 from awesome-spectral-indices/fm/tests
Browse files Browse the repository at this point in the history
Testing
  • Loading branch information
MartinuzziFrancesco authored Dec 12, 2023
2 parents 8aad26a + 24c5fbd commit 7648c8b
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SpectralIndices"
uuid = "df0093a1-273d-40bc-819a-796ec3476907"
authors = ["MartinuzziFrancesco <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
5 changes: 2 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ makedocs(;
)

deploydocs(;
repo="github.com/awesome-spectral-indices/SpectralIndices.jl.git",
push_preview=true,
)
repo="github.com/awesome-spectral-indices/SpectralIndices.jl.git", push_preview=true
)
14 changes: 12 additions & 2 deletions src/axioms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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...)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"]
Expand Down
6 changes: 6 additions & 0 deletions src/compute.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down
31 changes: 31 additions & 0 deletions test/axioms.jl
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 18 additions & 0 deletions test/compute.jl
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions test/utils.jl
Original file line number Diff line number Diff line change
@@ -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

2 comments on commit 7648c8b

@MartinuzziFrancesco
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96950

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" 7648c8b113500f44e33ad5cbf5d3d9043c3b9458
git push origin v0.1.1

Please sign in to comment.