Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing #3

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading