Skip to content

Commit

Permalink
new format
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinuzziFrancesco committed Jan 12, 2025
1 parent 13460e0 commit 25e7978
Show file tree
Hide file tree
Showing 24 changed files with 508 additions and 397 deletions.
4 changes: 2 additions & 2 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
style = "blue"
style = "sciml"
format_markdown = false
whitespace_in_kwargs = false
always_use_return = true
margin = 92
indent = 4
format_docstrings = true
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
[julia-img]: https://img.shields.io/badge/julia-v1.10+-blue.svg
[julia-url]: https://julialang.org/

[style-img]: https://img.shields.io/badge/code%20style-blue-4495d1.svg
[style-url]: https://github.com/invenia/BlueStyle
[style-img]: https://img.shields.io/static/v1?label=code%20style&message=SciML&color=9558b2&labelColor=389826
[style-url]: https://github.com/SciML/SciMLStyle

[aqua-img]: https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg
[aqua-url]: https://github.com/JuliaTesting/Aqua.jl
Expand Down
6 changes: 3 additions & 3 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ makedocs(;
format=DocumenterVitepress.MarkdownVitepress(;
repo="github.com/awesome-spectral-indices/SpectralIndices.jl", # this must be the full URL!
devbranch="main",
devurl="dev",
devurl="dev"
),
draft=false,
source="src",
build="build",
pages=pages,
pages=pages
)

deploydocs(;
repo="github.com/awesome-spectral-indices/SpectralIndices.jl",
target="build", # this is where Vitepress stores its output
branch="gh-pages",
devbranch="main",
push_preview=true,
push_preview=true
)
6 changes: 3 additions & 3 deletions docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ pages = [
"Tutorials" => [
"Basics" => "tutorials/basic_types.md",
"DataFrames.jl" => "tutorials/dataframes.md",
"YAXArrays.jl" => "tutorials/yaxarrays.md",
"YAXArrays.jl" => "tutorials/yaxarrays.md"
],
"API Documentation" => [
"Axioms" => "api/axioms.md",
"Compute" => "api/compute.md",
"Datasets" => "api/datasets.md",
],
"Datasets" => "api/datasets.md"
]
]
37 changes: 18 additions & 19 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,
AbstractSpectralIndex,
compute_index,
create_indices,
linear,
poly,
RBF,
load_dataset,
load_json

function create_params(kw_args::Pair{Symbol,DataFrame}...)
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()

for pair in kw_args
Expand All @@ -25,20 +25,19 @@ 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 = [
compute_index(index, Dict(zip(names(params), row)); indices=indices) for
row in eachrow(params)
]
results = [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
return DataFrame(Symbol(index.short_name) => results)
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 @@ -68,7 +67,7 @@ function RBF(params::DataFrame)
return result_df
end

function load_dataset(dataset::String, ::Type{T}) where {T<:DataFrame}
function load_dataset(dataset::String, ::Type{T}) where {T <: DataFrame}
datasets = Dict("spectral" => "spectral.json")

if dataset in keys(datasets)
Expand All @@ -88,7 +87,7 @@ function load_dataset(dataset::String, ::Type{T}) where {T<:DataFrame}
df = DataFrame(; index=all_indices)

for col_name in keys(ds)
df[!, col_name] = Vector{Union{Missing,Any}}(missing, length(all_indices))
df[!, col_name] = Vector{Union{Missing, Any}}(missing, length(all_indices))
end

for (col_name, col_data) in ds
Expand Down
40 changes: 20 additions & 20 deletions ext/SpectralIndicesYAXArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ 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
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
if !(band in params.Variables)
throw(
ArgumentError(
"'$band' is missing in the parameters for $index computation!"
),
"'$band' is missing in the parameters for $index computation!"
),
)
end
end
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,7 +52,7 @@ 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)
Expand All @@ -62,7 +62,7 @@ function compute_index(
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 All @@ -75,8 +75,8 @@ function compute_index(
end

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

Expand All @@ -89,7 +89,7 @@ function poly(params::YAXArray)
params[Variable=At("a")],
params[Variable=At("b")],
params[Variable=At("c")],
params[Variable=At("p")],
params[Variable=At("p")]
)
end

Expand All @@ -99,7 +99,7 @@ function RBF(params::YAXArray)
)
end

function load_dataset(dataset::String, ::Type{T}) where {T<:YAXArray}
function load_dataset(dataset::String, ::Type{T}) where {T <: YAXArray}
datasets = Dict("sentinel" => "S2_10m.json")

if dataset in keys(datasets)
Expand Down
15 changes: 8 additions & 7 deletions src/SpectralIndices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ 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))
@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
Expand Down
8 changes: 4 additions & 4 deletions src/bands.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct Band{S<:String,F<:Number,P<:Dict{String,PlatformBand}}
struct Band{S <: String, F <: Number, P <: Dict{String, PlatformBand}}
short_name::S
long_name::S
common_name::S
Expand Down Expand Up @@ -54,14 +54,14 @@ julia> bands["B"].long_name
```
"""
function Band(band::Dict{String,Any})
function Band(band::Dict{String, Any})
short_name = band["short_name"]
long_name = band["long_name"]
common_name = band["common_name"]
min_wavelength = band["min_wavelength"]
max_wavelength = band["max_wavelength"]

platforms = Dict{String,PlatformBand}()
platforms = Dict{String, PlatformBand}()

for (platform, platform_info) in band["platforms"]
if isa(platform_info, PlatformBand)
Expand All @@ -88,7 +88,7 @@ end

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

for (key, value) in bands_dict
bands_class[key] = Band(value)
Expand Down
Loading

0 comments on commit 25e7978

Please sign in to comment.