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

Adding public API, fixed naming #78

Merged
merged 5 commits into from
Dec 19, 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
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name = "SpectralIndices"
uuid = "df0093a1-273d-40bc-819a-796ec3476907"
authors = ["MartinuzziFrancesco <[email protected]>"]
version = "0.2.11"
version = "0.2.12"

[deps]
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
Expand All @@ -18,6 +19,7 @@ SpectralIndicesDataFramesExt = "DataFrames"
SpectralIndicesYAXArraysExt = ["YAXArrays", "DimensionalData"]

[compat]
Compat = "4.16.0"
DataFrames = "1"
Dates = "1"
DimensionalData = "0.26, 0.27, 0.28"
Expand Down
10 changes: 10 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ DocumenterVitepress = "4710194d-e776-4893-9690-8d956a29c365"
PartialFunctions = "570af359-4316-4cb7-8c74-252c00c2016b"
SpectralIndices = "df0093a1-273d-40bc-819a-796ec3476907"
YAXArrays = "c21b50f5-aa40-41ea-b809-c0f5e47bfa5c"

[compat]
CairoMakie = "0.12"
DataFrames = "1"
DimensionalData = "0.28"
Documenter = "1.8"
DocumenterVitepress = "0.1"
PartialFunctions = "1.2"
SpectralIndices = "0.2"
YAXArrays = "0.5"
14 changes: 7 additions & 7 deletions ext/SpectralIndicesDataFramesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module SpectralIndicesDataFramesExt
using SpectralIndices
using DataFrames
import SpectralIndices:
_create_params,
create_params,
AbstractSpectralIndex,
compute_index,
_create_indices,
create_indices,
linear,
poly,
RBF,
load_dataset,
_load_json
load_json

function _create_params(kw_args::Pair{Symbol,DataFrame}...)
function create_params(kw_args::Pair{Symbol,DataFrame}...)
combined_df = DataFrame()

for pair in kw_args
Expand All @@ -25,7 +25,7 @@ function _create_params(kw_args::Pair{Symbol,DataFrame}...)
end

function compute_index(
index::AbstractSpectralIndex, params::DataFrame; indices=_create_indices()
index::AbstractSpectralIndex, params::DataFrame; indices = create_indices()
)
# Convert DataFrame to a dictionary for each row and compute the index
results = [
Expand All @@ -38,7 +38,7 @@ function compute_index(
end

function compute_index(
index::Vector{<:AbstractSpectralIndex}, params::DataFrame; indices=_create_indices()
index::Vector{<:AbstractSpectralIndex}, params::DataFrame; indices = create_indices()
)
# Similar conversion and computation for a vector of indices
result_dfs = DataFrame()
Expand Down Expand Up @@ -77,7 +77,7 @@ function load_dataset(dataset::String, ::Type{T}) where {T<:DataFrame}
error("Dataset name not valid. Datasets available for DataFrames: spectral")
end

ds = _load_json(datasets[dataset])
ds = load_json(datasets[dataset])
all_indices = Set{Int}()
for val in values(ds)
for idx in keys(val)
Expand Down
26 changes: 13 additions & 13 deletions ext/SpectralIndicesYAXArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ using SpectralIndices
using YAXArrays
using DimensionalData
import SpectralIndices:
_check_params,
_create_params,
_order_params,
check_params,
create_params,
order_params,
AbstractSpectralIndex,
compute_index,
_create_indices,
create_indices,
linear,
poly,
RBF,
load_dataset,
_load_json
load_json

function _check_params(index::AbstractSpectralIndex, params::YAXArray)
function check_params(index::AbstractSpectralIndex, params::YAXArray)
for band in index.bands
if !(band in params.Variables)
throw(
Expand All @@ -28,7 +28,7 @@ function _check_params(index::AbstractSpectralIndex, params::YAXArray)
end
end

function _order_params(index::AbstractSpectralIndex, params::YAXArray)
function order_params(index::AbstractSpectralIndex, params::YAXArray)
new_params = []
for (bidx, band) in enumerate(index.bands)
push!(new_params, params[Variable=At(band)])
Expand All @@ -37,7 +37,7 @@ function _order_params(index::AbstractSpectralIndex, params::YAXArray)
return new_params
end

function _create_params(kw_args::Pair{Symbol,<:YAXArray}...)
function create_params(kw_args::Pair{Symbol,<:YAXArray}...)
params_yaxa = []
names_yaxa = []
for (key, value) in kw_args
Expand All @@ -52,17 +52,17 @@ end
## TODO: simplify even further
# this is same function contente as dispatch on Dict
function compute_index(
index::AbstractSpectralIndex, params::YAXArray; indices=_create_indices()
index::AbstractSpectralIndex, params::YAXArray; indices = create_indices()
)
_check_params(index, params)
params = _order_params(index, params)
check_params(index, params)
params = order_params(index, params)
T = eltype(first(params))
result = _compute_index(T, index, params...)
return result
end

function compute_index(
index::Vector{<:AbstractSpectralIndex}, params::YAXArray; indices=_create_indices()
index::Vector{<:AbstractSpectralIndex}, params::YAXArray; indices = create_indices()
)
results = []
for (nidx, idx) in enumerate(index)
Expand Down Expand Up @@ -108,7 +108,7 @@ function load_dataset(dataset::String, ::Type{T}) where {T<:YAXArray}
error("Dataset name not valid. Datasets available for YAXArrays: sentinel")
end

ds = _load_json(datasets[dataset])
ds = load_json(datasets[dataset])
matrices = [hcat(ds[i]...) for i in 1:length(ds)]
data_3d = cat(matrices...; dims=3)
x_dim = Dim{:x}(1:300)
Expand Down
20 changes: 15 additions & 5 deletions src/SpectralIndices.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module SpectralIndices

using Compat: @compat #for @compat public
using Dates
using Downloads
using JSON
Expand All @@ -19,10 +20,19 @@ include("compute_index.jl")
include("compute_kernel.jl")
include("datasets.jl")

indices = _create_indices()
bands = _create_bands()
constants = _create_constants()
indices = create_indices()
bands = create_bands()
constants = create_constants()

@compat(public, (create_indices,
create_bands,
create_constants,
load_json,
check_params,
create_params,
order_params))

export get_indices, create_indexfun
export get_datasets, load_dataset
export SpectralIndex, indices, compute
export PlatformBand, Band
Expand All @@ -32,15 +42,15 @@ export compute_kernel, linear, poly, RBF
export bands
export constants

function _export_index(si::SpectralIndex)
function export_index(si::SpectralIndex)
@eval begin
export $(Symbol(si.short_name))
const $(Symbol(si.short_name)) = $si
end
end

for (name, instance) in indices
_export_index(indices[name])
export_index(indices[name])
end

end #module
4 changes: 2 additions & 2 deletions src/bands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ function Base.show(io::IO, ::MIME"text/plain", b::Band)
return println(io, "$(b.short_name): $(b.long_name)")
end

function _create_bands()
bands_dict = _load_json("bands.json")
function create_bands()
bands_dict = load_json("bands.json")
bands_class = Dict{String,Band}()

for (key, value) in bands_dict
Expand Down
44 changes: 22 additions & 22 deletions src/compute_index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ julia> compute_index(
"""
function compute_index(
index::AbstractSpectralIndex,
params=nothing,
online::Bool=false;
indices=indices,
params = nothing,
online::Bool = false;
indices = indices,
kwargs...,
)
if isnothing(params)
params = _create_params(kwargs...)
params = create_params(kwargs...)
end

return compute_index(index, params; indices=indices)
end

function compute_index(
index::String, params=nothing, online::Bool=false; indices=indices, kwargs...
index::String, params = nothing, online::Bool = false; indices=indices, kwargs...
)
names = keys(indices)
@assert index in names "$index is not a valid Spectral Index!"
Expand All @@ -84,17 +84,17 @@ function compute_index(
kwargs...,
)
if isnothing(params)
params = _create_params(kwargs...)
params = create_params(kwargs...)
end

return compute_index(index, params; indices=indices)
end

function compute_index(
index::Vector{String},
params=nothing,
online::Bool=false;
indices=_create_indices(online),
params = nothing,
online::Bool = false;
indices = create_indices(online),
kwargs...,
)
names = keys(indices)
Expand All @@ -106,9 +106,9 @@ function compute_index(
return results
end

function compute_index(index::AbstractSpectralIndex, params::Dict; indices=indices)
_check_params(index, params)
params = _order_params(index, params)
function compute_index(index::AbstractSpectralIndex, params::Dict; indices = indices)
check_params(index, params)
params = order_params(index, params)
T = eltype(first(values(params)))
result = _compute_index(T, index, params...)

Expand Down Expand Up @@ -144,8 +144,8 @@ function _compute_index(
end

function compute_index(index::AbstractSpectralIndex, params::NamedTuple; indices=indices)
_check_params(index, params)
params = _order_params(index, params)
check_params(index, params)
params = order_params(index, params)
T = eltype(first(values(params)))
result = _compute_index(T, index, params...)
result_nt = (; Dict(Symbol(index.short_name) => result)...)
Expand All @@ -167,7 +167,7 @@ function compute_index(
end

"""
_check_params(index::String, params::Dict, indices::Dict)
check_params(index::String, params::Dict, indices::Dict)

Check if the parameters dictionary contains all required bands for spectral index computation.

Expand All @@ -190,10 +190,10 @@ parameters = Dict("N" => 0.6, "R" => 0.3, "G" => 0.7)
indices = _get_indices()

# Check if parameters contain required bands
_check_params(index_name, parameters, indices)
check_params(index_name, parameters, indices)
```
"""
function _check_params(index::AbstractSpectralIndex, params::Dict)
function check_params(index::AbstractSpectralIndex, params::Dict)
for band in index.bands
if !(band in keys(params))
throw(
Expand All @@ -205,7 +205,7 @@ function _check_params(index::AbstractSpectralIndex, params::Dict)
end
end

function _order_params(index::AbstractSpectralIndex, params)
function order_params(index::AbstractSpectralIndex, params)
T = eltype(params)
new_params = T[]
for (bidx, band) in enumerate(index.bands)
Expand All @@ -215,7 +215,7 @@ function _order_params(index::AbstractSpectralIndex, params)
return new_params
end

function _order_params(index::AbstractSpectralIndex, params::Dict)
function order_params(index::AbstractSpectralIndex, params::Dict)
T = eltype(values(params))
new_params = T[]
for (bidx, band) in enumerate(index.bands)
Expand All @@ -225,12 +225,12 @@ function _order_params(index::AbstractSpectralIndex, params::Dict)
return new_params
end

function _create_params(kw_args...)
function create_params(kw_args...)
params = Dict(String(k) => v for (k, v) in kw_args)
return params
end

function _check_params(index::AbstractSpectralIndex, params::NamedTuple)
function check_params(index::AbstractSpectralIndex, params::NamedTuple)
for band in index.bands
if !(Symbol(band) in keys(params))
throw(
Expand All @@ -242,7 +242,7 @@ function _check_params(index::AbstractSpectralIndex, params::NamedTuple)
end
end

function _order_params(index::AbstractSpectralIndex, params::NamedTuple)
function order_params(index::AbstractSpectralIndex, params::NamedTuple)
T = eltype(values(params))
new_params = T[]
for (bidx, band) in enumerate(index.bands)
Expand Down
2 changes: 1 addition & 1 deletion src/compute_kernel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ result = compute_kernel(linear; params=Dict("a" => 1, "b" => 2))
"""
function compute_kernel(kernel, params=nothing; kwargs...)
if isnothing(params)
params = _create_params(kwargs...)
params = create_params(kwargs...)
end

results = kernel(params)
Expand Down
4 changes: 2 additions & 2 deletions src/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ function Base.show(io::IO, ::MIME"text/plain", c::Constant)
return println(io, "* Current value: $(c.value)")
end

function _create_constants()
constants = _load_json("constants.json")
function create_constants()
constants = load_json("constants.json")
constants_class = Dict{String,Constant}()

for (key, value) in constants
Expand Down
4 changes: 2 additions & 2 deletions src/datasets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ get_datasets(; datasets=["custom_dataset.json"], data_loc="path/to/custom/direct
This is particularly useful for setting up local environments with necessary data files for further processing or analysis.
"""
function get_datasets(;
datasets=["S2_10m.json", "spectral.json"],
data_loc=joinpath(dirname(@__FILE__), "..", "data"),
datasets = ["S2_10m.json", "spectral.json"],
data_loc = joinpath(dirname(@__FILE__), "..", "data"),
)
for ds in datasets
file_dest = joinpath(data_loc, ds)
Expand Down
4 changes: 2 additions & 2 deletions src/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function _spectral_indices(
return indices
end

function _create_indices(online::Bool=false)
indices_dict = _get_indices(online)
function create_indices(online::Bool=false)
indices_dict = get_indices(online)
return _spectral_indices(indices_dict)
end
Loading
Loading