Skip to content

Commit

Permalink
updated Base.show and testing for axioms
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinuzziFrancesco committed Jan 24, 2024
1 parent d46cc83 commit 6ba4919
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 50 deletions.
6 changes: 4 additions & 2 deletions src/SpectralIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ if !isdefined(Base, :get_extension)
end

indices = _create_indices()
bands = _create_bands()
constants = _create_constants()

export SpectralIndex, indices, compute
export PlatformBand, Band, bands
export Constant, constants
export PlatformBand, Band
export Constant
export compute_index
export compute_kernel, linear, poly, RBF

Expand Down
46 changes: 18 additions & 28 deletions src/axioms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -227,25 +227,20 @@ function PlatformBand(platform_band::Dict)
return PlatformBand(platform, band, name, wavelength, bandwidth)
end

# Machine-readable output
function Base.show(io::IO, pb::PlatformBand)
println(io, "PlatformBand(Platform: $(pb.platform), Band: $(pb.name))")
println(io, "* Band: $(pb.band)")
println(io, "* Center Wavelength (nm): $(pb.wavelength)")
return println(io, "* Bandwidth (nm): $(pb.bandwidth)")
end

Base.show(io::IO, mime::MIME{Symbol("text/plain")}, pb::PlatformBand) = Base.show(io, pb)

function Base.show(io::IO, mime::MIME{Symbol("text/html")}, pb::PlatformBand)
println(io, "<div style=\"background-color:#F9F9F9; padding:10px;\">")
println(
io,
"<strong>Platform:</strong> $(pb.platform), <strong>Band:</strong> $(pb.name)<br>",
)
println(io, "<strong>Band:</strong> $(pb.band)<br>")
println(io, "<strong>Center Wavelength (nm):</strong> $(pb.wavelength)<br>")
println(io, "<strong>Bandwidth (nm):</strong> $(pb.bandwidth)<br>")
return println(io, "</div>")
# Human-readable output
function Base.show(io::IO, ::MIME"text/plain", pb::PlatformBand)
println(io, "Platform: $(pb.platform), Band: $(pb.name)")
println(io, "* Band: $(pb.band)")
println(io, "* Center Wavelength (nm): $(pb.wavelength)")
return println(io, "* Bandwidth (nm): $(pb.bandwidth)")
end

struct Band{S<:String,F<:Number,P<:Dict{String,PlatformBand}}
Expand Down Expand Up @@ -314,27 +309,26 @@ function Band(band::Dict{String,Any})
platforms = Dict{String,PlatformBand}()

for (platform, platform_info) in band["platforms"]
platforms[platform] = PlatformBand(platform_info)
if isa(platform_info, PlatformBand)
platforms[platform] = platform_info
else
platforms[platform] = PlatformBand(platform_info)
end
end

return Band(
short_name, long_name, common_name, min_wavelength, max_wavelength, platforms
)
end

Base.show(io::IO, b::Band) = begin
println(io, "Band($(b.short_name): $(b.long_name))")
# Machine-readable output
function Base.show(io::IO, b::Band)
return println(io, "Band($(b.short_name): $(b.long_name))")
end

Base.show(io::IO, mime::MIME{Symbol("text/plain")}, b::Band) = Base.show(io, b)

function Base.show(io::IO, mime::MIME{Symbol("text/html")}, b::Band)
println(io, "<div style=\"background-color:#F9F9F9; padding:10px;\">")
println(io, "<strong>Band:</strong> $(b.short_name): $(b.long_name)<br>")
println(io, "<strong>Common Name:</strong> $(b.common_name)<br>")
println(io, "<strong>Min Wavelength (nm):</strong> $(b.min_wavelength)<br>")
println(io, "<strong>Max Wavelength (nm):</strong> $(b.max_wavelength)<br>")
return println(io, "</div>")
# Human-readable output
function Base.show(io::IO, ::MIME"text/plain", b::Band)
return println(io, "$(b.short_name): $(b.long_name)")
end

function _create_bands()
Expand All @@ -348,8 +342,6 @@ function _create_bands()
return bands_class
end

bands = _create_bands()

struct Constant{S<:String,D,V}
description::S
long_name::S
Expand Down Expand Up @@ -383,5 +375,3 @@ function _create_constants()

return constants_class
end

constants = _create_constants()
110 changes: 90 additions & 20 deletions test/axioms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,96 @@ 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
@testset "SpectralIndex Show Methods Tests" begin
# 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
end

@testset "PlatformBand Show Methods Tests" begin
# Create a sample PlatformBand instance
sample_band = PlatformBand("Sentinel-2A", "B2", "Blue", 492.4, 66.0)

# Test Human-readable Output
@test begin
io_buffer = IOBuffer()
show(io_buffer, MIME("text/plain"), sample_band)
output = String(take!(io_buffer))
expected_output = """
Platform: Sentinel-2A, Band: Blue
* Band: B2
* Center Wavelength (nm): 492.4
* Bandwidth (nm): 66.0
"""
output == expected_output
end

# Test Machine-readable Output
@test begin
io_buffer = IOBuffer()
show(io_buffer, sample_band)
output = String(take!(io_buffer))
expected_output = """
PlatformBand(Platform: Sentinel-2A, Band: Blue)
* Band: B2
* Center Wavelength (nm): 492.4
* Bandwidth (nm): 66.0
"""
output == expected_output
end
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
@testset "Band Show Methods Tests" begin
# Create a dummy PlatformBand instance with minimal data
dummy_platform_band = PlatformBand("DummyPlatform", "DummyBand", "DummyName", 0.0, 0.0)

# Create a sample Band instance
sample_band_dict = Dict(
"short_name" => "B",
"long_name" => "Blue",
"common_name" => "Blue",
"min_wavelength" => 450.0,
"max_wavelength" => 495.0,
"platforms" =>
Dict("sentinel2a" => dummy_platform_band, "sentinel2b" => dummy_platform_band),
)
sample_band = Band(sample_band_dict)

# Test Machine-readable Output
@test begin
io_buffer = IOBuffer()
show(io_buffer, sample_band)
output = String(take!(io_buffer))
expected_output = "Band(B: Blue)\n"
output == expected_output
end

# Test Human-readable Output
@test begin
io_buffer = IOBuffer()
show(io_buffer, MIME("text/plain"), sample_band)
output = String(take!(io_buffer))
expected_output = "B: Blue\n"
output == expected_output
end
end

0 comments on commit 6ba4919

Please sign in to comment.