Skip to content

Commit

Permalink
Merge pull request #13 from awesome-spectral-indices/fm/ck
Browse files Browse the repository at this point in the history
Add `compute_kernel`
  • Loading branch information
MartinuzziFrancesco authored Dec 19, 2023
2 parents b206b8a + ec97ead commit 61c8635
Show file tree
Hide file tree
Showing 11 changed files with 450 additions and 4,812 deletions.
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.3"
version = "0.1.4"

[deps]
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SpectralIndices.jl is a Julia package for working with spectral indices commonly
## Features

- Compute a wide range of spectral indices.
- Support for various data types, including but not limited to
- Support for various data types, including but not limited to (see this [issue](https://github.com/awesome-spectral-indices/SpectralIndices.jl/issues/8) for an updated list and WIP)
- [x] Arrays
- [x] DataFrames
- [x] YAXArrays
Expand Down
9 changes: 6 additions & 3 deletions src/SpectralIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ using JSON
using DataFrames
using YAXArrays
using DimensionalData
#using Symbolics

abstract type AbstractSpectralIndex end
abstract type AbstractPlatformBand end

include("utils.jl")
include("axioms.jl")
include("compute.jl")
include("compute_index.jl")
include("compute_kernel.jl")
#include("datasets.jl")

indices = _create_indices()

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

for (name, instance) in indices
@eval begin
Expand Down
61 changes: 14 additions & 47 deletions src/compute.jl β†’ src/compute_index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,7 @@ julia> compute_index(
```
"""
function compute_index(
index::String,
params=nothing,
online::Bool=false;
kwargs...
)

function compute_index(index::String, params=nothing, online::Bool=false; kwargs...)
indices = _create_indices(online)
names = keys(indices)
@assert index in names "$index is not a valid Spectral Index!"
Expand All @@ -68,29 +62,22 @@ function compute_index(
params = _create_params(kwargs...)
end

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

function compute_index(
index::String,
params::Dict
)
indices = _create_indices()
function compute_index(index::String, params::Dict; indices=_create_indices())
_check_params(indices[index], params)
params = _order_params(indices[index], params)
result = _compute_index(indices[index], params...)

return result
end

function compute_index(
index::String,
params::DataFrame
)
function compute_index(index::String, params::DataFrame; indices=_create_indices())
# Convert DataFrame to a dictionary for each row and compute the index
results = [
compute_index(index, Dict(zip(names(params), row))) for
compute_index(index, Dict(zip(names(params), row)); indices=indices) for
row in eachrow(params)
]

Expand All @@ -100,21 +87,14 @@ end

## TODO: simplify even further
# this is same function contente as dispatch on Dict
function compute_index(index::String, params::YAXArray)
indices = _create_indices()
function compute_index(index::String, params::YAXArray; indices=_create_indices())
_check_params(indices[index], params)
params = _order_params(indices[index], params)
result = _compute_index(indices[index], params...)
return result
end

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

function compute_index(index::Vector{String}, params=nothing, online::Bool=false; kwargs...)
indices = _create_indices(online)
names = keys(indices)
for idx in index
Expand All @@ -125,19 +105,14 @@ function compute_index(
params = _create_params(kwargs...)
end

results = compute_index(index, params)
results = compute_index(index, params; indices=indices)

return results
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{String},
params::Dict
)

indices = _create_indices()
function compute_index(index::Vector{String}, params::Dict; indices=_create_indices())
results = []

for (nidx, idx) in enumerate(index)
Expand All @@ -149,30 +124,22 @@ function compute_index(
return results
end

function compute_index(
index::Vector{String},
params::DataFrame
)
function compute_index(index::Vector{String}, params::DataFrame; indices=_create_indices())
# Similar conversion and computation for a vector of indices
result_dfs = DataFrame()
for idx in index
result_df = compute_index(idx, params)
result_df = compute_index(idx, params; indices=indices)
result_dfs[!, Symbol(idx)] = result_df[!, 1]
end

# Return the combined DataFrame with columns named after each index
return result_dfs
end


function compute_index(
index::Vector{String},
params::YAXArray
)

function compute_index(index::Vector{String}, params::YAXArray; indices=_create_indices())
results = []
for (nidx, idx) in enumerate(index)
res_tmp = compute_index(idx, params)
res_tmp = compute_index(idx, params; indices=indices)
push!(results, res_tmp)
end
result = concatenatecubes(results, Dim{:Variables}(index))
Expand All @@ -182,4 +149,4 @@ end

_compute_index(idx::AbstractSpectralIndex, prms::Number...) = idx(prms...)
_compute_index(idx::AbstractSpectralIndex, prms::AbstractArray...) = idx.(prms...)
_compute_index(idx::AbstractSpectralIndex, prms::YAXArray...) = idx.(prms...)
_compute_index(idx::AbstractSpectralIndex, prms::YAXArray...) = idx.(prms...)
Loading

2 comments on commit 61c8635

@MartinuzziFrancesco
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

Release notes:

SpectralIndices v0.1.4 Released πŸŽ‰

Hello SpectralIndices Users!

We're excited to announce the release of SpectralIndices v0.1.4! This update introduces a significant enhancement that expands your capabilities for spectral analysis:

🌟 New Feature: compute_kernel Function 🌟

  • Enhanced Flexibility: The new compute_kernel function is here! This addition broadens your analytical toolkit, allowing you to compute various types of kernels such as linear, poly, and RBF.
  • Versatile Input Handling: compute_kernel supports multiple input types, including numbers, arrays, data frames, and even YAXArrays. This makes it incredibly flexible for different data processing needs.
  • Streamlined Workflow: Integrating kernel computations into your spectral analysis is now seamless. With compute_kernel, handling complex kernel operations is as simple as calling a single function.

How to Use:

Use the compute_kernel function by specifying the kernel type and providing the necessary parameters. Here's a quick look at how it works:

# Compute a linear kernel
result = compute_kernel(linear, Dict("a" => 2, "b" => 3))

# Compute a polynomial kernel using a DataFrame
df = DataFrame(a = [1, 2], b = [3, 4], c = [1, 1], p = [2, 2])
result = compute_kernel(poly, df)

Keep the feedback coming, and let's continue to make SpectralIndices the go-to solution for spectral analysis in Julia!

Happy Analyzing! πŸš€

β€” The SpectralIndices Team

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/97412

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.4 -m "<description of version>" 61c863588075a74362f03d52b51b7f2ae8b4f69b
git push origin v0.1.4

Please sign in to comment.