Skip to content

Commit

Permalink
Merge pull request #54 from awesome-spectral-indices/fm/types
Browse files Browse the repository at this point in the history
Fixing type handling
  • Loading branch information
MartinuzziFrancesco authored Mar 29, 2024
2 parents 123e7cd + 75b069f commit 18859be
Show file tree
Hide file tree
Showing 14 changed files with 1,072 additions and 752 deletions.
22 changes: 11 additions & 11 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ makedocs(;
doctest=true,
linkcheck=true,
warnonly=[:missing_docs],
format=DocumenterVitepress.MarkdownVitepress(
repo = "github.com/awesome-spectral-indices/SpectralIndices.jl", # this must be the full URL!
devbranch = "main",
devurl = "dev";
format=DocumenterVitepress.MarkdownVitepress(;
repo="github.com/awesome-spectral-indices/SpectralIndices.jl", # this must be the full URL!
devbranch="main",
devurl="dev",
),
draft = false,
source = "src",
build = "build",
draft=false,
source="src",
build="build",
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
target="build", # this is where Vitepress stores its output
branch="gh-pages",
devbranch="main",
push_preview=true,
)
4 changes: 2 additions & 2 deletions docs/src/tutorials/basic_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,11 @@ The package can compute indices at custom precision

```@example basic
T = Float32
savi = compute_index(T, "SAVI"; N=T(nir), R=T(red), L=T(0.5))
savi = compute_index("SAVI"; N=T(nir), R=T(red), L=T(0.5))
```
```@example basic
T = Float16
savi = compute_index(T, "SAVI"; N=T(nir), R=T(red), L=T(0.5))
savi = compute_index("SAVI"; N=T(nir), R=T(red), L=T(0.5))
```

## Computing Multiple Indices
Expand Down
24 changes: 12 additions & 12 deletions docs/src/tutorials/yaxarrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ using YAXArrays, DimensionalData
using SpectralIndices
```

```@ansi yaxarrays
```@example yaxarrays
yaxa = load_dataset("sentinel", YAXArray)
```

As it is possible to observe we have a `YAXArray` object with three dimensions: `bands`, `x` and `y`. Each band is one of the 10 m spectral bands of a Sentinel-2 image.

The data is stored as `Int64`, so let us convert it to `Float` and rescale it:

```@ansi yaxarrays
```@example yaxarrays
yaxa = yaxa./10000
```

Expand All @@ -32,7 +32,7 @@ nothing # hide

now, let's compute the index

```@ansi yaxarrays
```@example yaxarrays
ndvi_compute = compute_index("NDVI"; N=b8, R=b4)
```

Expand All @@ -46,7 +46,7 @@ ndvi_p = NDVI.compute $Float64
```
now, we can compute the index

```@ansi yaxarrays
```@example yaxarrays
ndvi_map = map(ndvi_p, b8, b4)
```
Let's check that we have the same output:
Expand All @@ -70,7 +70,7 @@ out_dims = OutDims("x") # dito
nothing # hide
```

```@ansi yaxarrays
```@example yaxarrays
ndvi_cube = mapCube(ndvi_out, (b8, b4), indims=(in_dims, in_dims),
outdims=OutDims("x", outtype=Float64))
```
Expand All @@ -81,7 +81,7 @@ and we check again the data output matches
ndvi_compute.data == ndvi_cube.data
```

## compute index by named dims
## Computing index by named dims

As usual we can also just feed a properly constructed `YAXArray` to the `compute_index` function. Let's built the array:

Expand All @@ -93,7 +93,7 @@ new_bands_dim = Dim{:Variables}(["R", "N"])
nr_data = cat(yaxa[:, :, index_R], yaxa[:, :, index_N], dims=3)
nothing # hide
```
```@ansi yaxarrays
```@example yaxarrays
new_yaxa = YAXArray((yaxa.x, yaxa.y, new_bands_dim), nr_data)
```

Expand All @@ -102,7 +102,7 @@ new_yaxa = YAXArray((yaxa.x, yaxa.y, new_bands_dim), nr_data)

Now that we have our `YAXArray` with the correctly names `Dim`s we can use it direcly into `compute_index`:

```@ansi yaxarrays
```@example yaxarrays
ndvi = compute_index(
"NDVI", new_yaxa
)
Expand All @@ -124,7 +124,7 @@ kNDVI.reference

Onto the calculations:

```@ansi yaxarrays
```@example yaxarrays
knn = YAXArray((yaxa.x, yaxa.y), fill(1.0, 300, 300));
knr = compute_kernel(
RBF;
Expand All @@ -134,7 +134,7 @@ knr = compute_kernel(
)
```

As always, you can decide to build an `YAXArray` and feed that to the `compute_kernel` function if you prefer:
As always, you can decide to build a `YAXArray` and feed that to the `compute_kernel` function if you prefer:

```@example yaxarrays
a = Float64.(yaxa[bands = At("B08")])
Expand All @@ -146,13 +146,13 @@ params = concatenatecubes([a, b, sigma], kernel_dims)
nothing # hide
```

```@ansi yaxarrays
```@example yaxarrays
knr = compute_kernel(RBF, params)
```

We can finally compute the kNDVI:

```@ansi yaxarrays
```@example yaxarrays
kndvi = compute_index("kNDVI"; kNN = knn, kNR=knr)
```

Expand Down
57 changes: 19 additions & 38 deletions ext/SpectralIndicesDataFramesExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ function SpectralIndices._create_params(kw_args::Pair{Symbol,DataFrame}...)
end

function SpectralIndices.compute_index(
::Type{T}, index::String, params::DataFrame; indices=SpectralIndices._create_indices()
) where {T<:Number}
index::String, params::DataFrame; indices=SpectralIndices._create_indices()
)
# Convert DataFrame to a dictionary for each row and compute the index
results = [
SpectralIndices.compute_index(
T, index, Dict(zip(names(params), row)); indices=indices
index, Dict(zip(names(params), row)); indices=indices
) for row in eachrow(params)
]

Expand All @@ -29,63 +29,36 @@ function SpectralIndices.compute_index(
end

function SpectralIndices.compute_index(
index::String, params::DataFrame; indices=SpectralIndices._create_indices()
index::Vector{String}, params::DataFrame; indices=SpectralIndices._create_indices()
)
return SpectralIndices.compute_index(Float64, index, params; indices=indices)
end

function SpectralIndices.compute_index(
::Type{T},
index::Vector{String},
params::DataFrame;
indices=SpectralIndices._create_indices(),
) where {T<:Number}
# Similar conversion and computation for a vector of indices
result_dfs = DataFrame()
for idx in index
result_df = SpectralIndices.compute_index(T, idx, params; indices=indices)
result_df = SpectralIndices.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 SpectralIndices.compute_index(
index::Vector{String}, params::DataFrame; indices=SpectralIndices._create_indices()
)
return SpectralIndices.compute_index(Float64, index, params; indices=indices)
end

function SpectralIndices.linear(::Type{T}, params::DataFrame) where {T<:Number}
result = linear(T, params[!, "a"], params[!, "b"])
function SpectralIndices.linear(params::DataFrame)
result = linear(params[!, "a"], params[!, "b"])
result_df = DataFrame(; linear=result)
return result_df
end

function SpectralIndices.linear(params::DataFrame)
return linear(Float64, params)
end

function SpectralIndices.poly(::Type{T}, params::DataFrame) where {T<:Number}
result = poly(T, params[!, "a"], params[!, "b"], params[!, "c"], params[!, "p"])
function SpectralIndices.poly(params::DataFrame)
result = poly(params[!, "a"], params[!, "b"], params[!, "c"], params[!, "p"])
result_df = DataFrame(; poly=result)
return result_df
end

function SpectralIndices.poly(params::DataFrame)
return poly(Float64, params)
end

function SpectralIndices.RBF(::Type{T}, params::DataFrame) where {T<:Number}
result = RBF(T, params[!, "a"], params[!, "b"], params[!, "sigma"])
function SpectralIndices.RBF(params::DataFrame)
result = RBF(params[!, "a"], params[!, "b"], params[!, "sigma"])
result_df = DataFrame(; RBF=result)
return result_df
end

function SpectralIndices.RBF(params::DataFrame)
return RBF(Float64, params)
end

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

Expand Down Expand Up @@ -116,6 +89,14 @@ function SpectralIndices.load_dataset(dataset::String, ::Type{T}) where {T<:Data
end
end

for col_name in names(df)
first_non_missing = findfirst(x -> !ismissing(x), df[!, col_name])
if !isnothing(first_non_missing)
target_type = typeof(df[first_non_missing, col_name])
df[!, col_name] = convert(Vector{target_type}, df[!, col_name])
end
end

select!(df, Not(:index))

return df
Expand Down
47 changes: 9 additions & 38 deletions ext/SpectralIndicesYAXArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,82 +40,53 @@ end
## TODO: simplify even further
# this is same function contente as dispatch on Dict
function SpectralIndices.compute_index(
::Type{T}, index::String, params::YAXArray; indices=SpectralIndices._create_indices()
) where {T<:Number}
index::String, params::YAXArray; indices=SpectralIndices._create_indices()
)
SpectralIndices._check_params(indices[index], params)
params = SpectralIndices._order_params(indices[index], params)
T = eltype(first(params))
result = SpectralIndices._compute_index(T, indices[index], params...)
return result
end

function SpectralIndices.compute_index(
index::String, params::YAXArray; indices=SpectralIndices._create_indices()
index::Vector{String}, params::YAXArray; indices=SpectralIndices._create_indices()
)
return SpectralIndices.compute_index(Float64, index, params; indices=indices)
end

function SpectralIndices.compute_index(
::Type{T},
index::Vector{String},
params::YAXArray;
indices=SpectralIndices._create_indices(),
) where {T<:Number}
results = []
for (nidx, idx) in enumerate(index)
res_tmp = compute_index(T, idx, params; indices=indices)
res_tmp = compute_index(idx, params; indices=indices)
push!(results, res_tmp)
end
result = concatenatecubes(results, Dim{:Variables}(index))

return result
end

function SpectralIndices.compute_index(
index::Vector{String}, params::YAXArray; indices=SpectralIndices._create_indices()
)
return SpectralIndices.compute_index(Float64, index, params; indices=indices)
end

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

function SpectralIndices.linear(::Type{T}, params::YAXArray) where {T<:Number}
return SpectralIndices.linear(T, params[Variable=At("a")], params[Variable=At("b")])
end

function SpectralIndices.linear(params::YAXArray)
return SpectralIndices.linear(
Float64, params[Variable=At("a")], params[Variable=At("b")]
)
return SpectralIndices.linear(params[Variable=At("a")], params[Variable=At("b")])
end

function SpectralIndices.poly(::Type{T}, params::YAXArray) where {T<:Number}
function SpectralIndices.poly(params::YAXArray)
return SpectralIndices.poly(
T,
params[Variable=At("a")],
params[Variable=At("b")],
params[Variable=At("c")],
params[Variable=At("p")],
)
end

function SpectralIndices.poly(params::YAXArray)
return SpectralIndices.poly(Float64, params)
end

function SpectralIndices.RBF(::Type{T}, params::YAXArray) where {T<:Number}
function SpectralIndices.RBF(params::YAXArray)
return SpectralIndices.RBF(
T, params[Variable=At("a")], params[Variable=At("b")], params[Variable=At("sigma")]
params[Variable=At("a")], params[Variable=At("b")], params[Variable=At("sigma")]
)
end

function SpectralIndices.RBF(params::YAXArray)
return SpectralIndices.RBF(Float64, params)
end

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

Expand Down
Loading

0 comments on commit 18859be

Please sign in to comment.