Skip to content

Commit

Permalink
Merge pull request #61 from awesome-spectral-indices/auto-juliaformat…
Browse files Browse the repository at this point in the history
…ter-pr

Automatic JuliaFormatter.jl run
  • Loading branch information
MartinuzziFrancesco authored Apr 4, 2024
2 parents 57b0a81 + 79ed937 commit 5206384
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
17 changes: 12 additions & 5 deletions ext/SpectralIndicesDataFramesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ module SpectralIndicesDataFramesExt

using SpectralIndices
using DataFrames
import SpectralIndices: _create_params, AbstractSpectralIndex, compute_index,
_create_indices, linear, poly, RBF, load_dataset, _load_json
import SpectralIndices:
_create_params,
AbstractSpectralIndex,
compute_index,
_create_indices,
linear,
poly,
RBF,
load_dataset,
_load_json

function _create_params(kw_args::Pair{Symbol,DataFrame}...)
combined_df = DataFrame()
Expand All @@ -21,9 +29,8 @@ function compute_index(
)
# Convert DataFrame to a dictionary for each row and compute the index
results = [
compute_index(
index, Dict(zip(names(params), row)); indices=indices
) for row in eachrow(params)
compute_index(index, Dict(zip(names(params), row)); indices=indices) for
row in eachrow(params)
]

# Return the results as a DataFrame with the column named after the index
Expand Down
20 changes: 14 additions & 6 deletions ext/SpectralIndicesYAXArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@ module SpectralIndicesYAXArraysExt
using SpectralIndices
using YAXArrays
using DimensionalData
import SpectralIndices: _check_params, _create_params, _order_params,
AbstractSpectralIndex, compute_index, _create_indices,
linear, poly, RBF, load_dataset, _load_json
import SpectralIndices:
_check_params,
_create_params,
_order_params,
AbstractSpectralIndex,
compute_index,
_create_indices,
linear,
poly,
RBF,
load_dataset,
_load_json

function _check_params(index::AbstractSpectralIndex, params::YAXArray)
for band in index.bands
Expand Down Expand Up @@ -42,9 +51,8 @@ end

## TODO: simplify even further
# this is same function contente as dispatch on Dict
function compute_index(index::AbstractSpectralIndex,
params::YAXArray;
indices=_create_indices()
function compute_index(
index::AbstractSpectralIndex, params::YAXArray; indices=_create_indices()
)
_check_params(index, params)
params = _order_params(index, params)
Expand Down
40 changes: 23 additions & 17 deletions src/compute_index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ julia> compute_index(
```
"""
function compute_index(
index::AbstractSpectralIndex, params=nothing, online::Bool=false; indices=indices, kwargs...
index::AbstractSpectralIndex,
params=nothing,
online::Bool=false;
indices=indices,
kwargs...,
)

if isnothing(params)
params = _create_params(kwargs...)
end
Expand All @@ -73,26 +76,27 @@ function compute_index(
return results
end

function compute_index(index::Vector{<:AbstractSpectralIndex},
function compute_index(
index::Vector{<:AbstractSpectralIndex},
params=nothing,
online::Bool=false;
indices=_create_indices(online),
kwargs...
kwargs...,
)

if isnothing(params)
params = _create_params(kwargs...)
end

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

function compute_index(index::Vector{String},
function compute_index(
index::Vector{String},
params=nothing,
online::Bool=false;
indices = _create_indices(online),
kwargs...)

indices=_create_indices(online),
kwargs...,
)
names = keys(indices)
for idx in index
@assert idx in names "$index is not a valid Spectral Index!"
Expand All @@ -113,7 +117,9 @@ end

# TODO: return results in a matrix columnswise
#multi_result = compute_index(["NDVI", "SAVI"], N = fill(0.643, 5), R = fill(0.175, 5), L = fill(0.5, 5))
function compute_index(index::Vector{<:AbstractSpectralIndex}, params::Dict; indices=indices)
function compute_index(
index::Vector{<:AbstractSpectralIndex}, params::Dict; indices=indices
)
results = []
for (nidx, idx) in enumerate(index)
result = compute_index(idx, params; indices=indices)
Expand All @@ -124,17 +130,15 @@ function compute_index(index::Vector{<:AbstractSpectralIndex}, params::Dict; ind
end

#_compute_index(idx::AbstractSpectralIndex, prms::Number...) = idx(prms...)
function _compute_index(::Type{T},
idx::AbstractSpectralIndex,
prms::Number...
function _compute_index(
::Type{T}, idx::AbstractSpectralIndex, prms::Number...
) where {T<:Number}
return idx(T, prms...)
end

#_compute_index(idx::AbstractSpectralIndex, prms::AbstractArray...) = idx.(prms...)
function _compute_index(::Type{T},
idx::AbstractSpectralIndex,
prms::AbstractArray...
function _compute_index(
::Type{T}, idx::AbstractSpectralIndex, prms::AbstractArray...
) where {T<:Number}
return idx.(T, prms...)
end
Expand All @@ -148,7 +152,9 @@ function compute_index(index::AbstractSpectralIndex, params::NamedTuple; indices
return result_nt
end

function compute_index(index::Vector{<:AbstractSpectralIndex}, params::NamedTuple; indices=indices)
function compute_index(
index::Vector{<:AbstractSpectralIndex}, params::NamedTuple; indices=indices
)
results_dict = Dict{Symbol,Any}()
for idx in index
result_nt = compute_index(idx, params; indices=indices)
Expand Down
1 change: 0 additions & 1 deletion test/compute_index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ end
GC.gc()
end


msi = custom_key_combinations(indices, 2, 200)

@testset "Built-in types compute_index $T multiple indices tests: $idxs" for idxs in msi,
Expand Down

0 comments on commit 5206384

Please sign in to comment.