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

Basic all around fixes #22

Merged
merged 3 commits into from
Jan 23, 2024
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
25 changes: 19 additions & 6 deletions src/axioms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,25 @@ function (si::SpectralIndex)(args::Number...)
return arg_type(result)
end

function Base.show(io::IO, index::SpectralIndex)
println(io, index.short_name, ": ", index.long_name)
println(io, "Application domain: ", index.application_domain)
println(io, "Bands/Parameters: ", index.bands)
println(io, "Formula: ", index.formula)
return println(io, "Reference: ", index.reference)
# Machine-readable output
function Base.show(io::IO, si::SpectralIndex)
print(io, "SpectralIndex(")
println(io, "short_name: $(si.short_name),")
println(io, "long_name: $(si.long_name),")
println(io, "application_domain: $(si.application_domain),")
println(io, "bands: $(si.bands),")
println(io, "formula: $(si.formula),")
println(io, "reference: $(si.reference)")
return println(io, ")")
end

# Human-readable output
function Base.show(io::IO, ::MIME"text/plain", si::SpectralIndex)
println(io, "$(si.short_name): $(si.long_name)")
println(io, "* Application Domain: $(si.application_domain)")
println(io, "* Bands/Parameters: $(si.bands)")
println(io, "* Formula: $(si.formula)")
return println(io, "* Reference: $(si.reference)")
end

"""
Expand Down
8 changes: 6 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ indices = _get_indices(true)
'''
```
"""
function _get_indices(online::Bool=false)
function _get_indices(
online::Bool=false;
filename="spectral-indices-dict.json",
fileloc=joinpath(dirname(@__FILE__), "..", "data", filename),
)
if online
indices_loc = Downloads.download(
"https://raw.githubusercontent.com/awesome-spectral-indices/awesome-spectral-indices/main/output/spectral-indices-dict.json",
string(pwd(), "spectral-indices-dict.json"),
fileloc,
)
indices = JSON.parsefile(indices_loc)
else
Expand Down
24 changes: 24 additions & 0 deletions test/axioms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,27 @@ 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)

# Test Human-readable Output
@test begin
io_buffer = IOBuffer()
show(io_buffer, MIME("text/plain"), si)
human_readable_output = String(take!(io_buffer))
expected_human_readable_output = """
CI: Custom Index
* Application Domain: Vegetation
* Bands/Parameters: ["C", "I"]
* Formula: (C-I)/(C+I)
* Reference: Doe et al., 1984
"""
human_readable_output == expected_human_readable_output
end

# Test Machine-readable Output
@test begin
io_buffer = IOBuffer()
show(io_buffer, si)
machine_readable_output = String(take!(io_buffer))
expected_machine_readable_output = "SpectralIndex(short_name: CI,\nlong_name: Custom Index,\napplication_domain: Vegetation,\nbands: [\"C\", \"I\"],\nformula: (C-I)/(C+I),\nreference: Doe et al., 1984\n)\n"
machine_readable_output == expected_machine_readable_output
end
20 changes: 19 additions & 1 deletion test/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,25 @@ using SpectralIndices
# Test offline
@test SpectralIndices._get_indices() isa Dict
# Test online
@test SpectralIndices._get_indices(true) isa Dict
@testset "Download Indices Test" begin
test_dir = mktempdir()
try
# Define the file location within the test directory
test_fileloc = joinpath(test_dir, "spectral-indices-dict.json")

# Run the _get_indices function with online = true
indices = SpectralIndices._get_indices(true; fileloc=test_fileloc)

# Test if the file was downloaded and parsed successfully
@test isfile(test_fileloc)
@test !isempty(indices)
@test indices isa Dict

finally
# Clean up: Delete the test file and directory
rm(test_dir; recursive=true)
end
end

params = Dict("N" => 0.6, "R" => 0.3)
# Test correctness
Expand Down
Loading