-
Notifications
You must be signed in to change notification settings - Fork 15
Add SpectralIndex transform #300
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -242,6 +242,12 @@ SDS | |
| ProjectionPursuit | ||
| ``` | ||
|
|
||
| ## SpectralIndex | ||
|
|
||
| ```@docs | ||
| SpectralIndex | ||
| ``` | ||
|
|
||
| ## KMedoids | ||
|
|
||
| ```@docs | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| # ------------------------------------------------------------------ | ||
| # Licensed under the MIT License. See LICENSE in the project root. | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| """ | ||
| SpectralIndex(name, [bands...]) | ||
|
|
||
| Compute the spectral index of given `name` from SpectralIndices.jl. | ||
| Optionally, specify the column names corresponding to spectral `bands` | ||
| as keyword arguments. | ||
|
|
||
| # Examples | ||
|
|
||
| ```julia | ||
| # vegetation index | ||
| SpectralIndex("NDVI") | ||
|
|
||
| # water index | ||
| SpectralIndex("NDWI") | ||
|
|
||
| # specify "R" (red) and "N" (near infra red) columns | ||
| SpectralIndex("NDVI", R="col1", N="col4") | ||
| ``` | ||
|
|
||
| ### Notes | ||
|
|
||
| The list of supported indices can be obtained with `spectralindices()` | ||
| and the list of spectral bands for a given index can be obtained with | ||
| `spectralbands(name)`. | ||
| """ | ||
| struct SpectralIndex{B} <: StatelessFeatureTransform | ||
| name::String | ||
| bands::B | ||
|
|
||
| function SpectralIndex{B}(name, bands) where {B} | ||
| sname = string(name) | ||
| @assert sname ∈ keys(indices) "$sname not found in SpectralIndices.jl" | ||
| sbands = if isempty(bands) | ||
| nothing | ||
| else | ||
| skeys = string.(keys(bands)) | ||
| vkeys = Tuple(indices[sname].bands) | ||
| @assert skeys ⊆ vkeys "bands $skeys are not valid for spectral index $sname, please choose from $vkeys" | ||
| svals = string.(values(values(bands))) | ||
juliohm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| (; zip(Symbol.(skeys), svals)...) | ||
| end | ||
| new{typeof(sbands)}(sname, sbands) | ||
| end | ||
| end | ||
|
|
||
| SpectralIndex(name; bands...) = SpectralIndex{typeof(bands)}(name, bands) | ||
|
|
||
| function applyfeat(transform::SpectralIndex, feat, prep) | ||
| # retrieve spectral index | ||
| iname = transform.name | ||
| index = indices[iname] | ||
|
|
||
| # extract band names from feature table | ||
| cols = Tables.columns(feat) | ||
| names = Tables.columnnames(cols) | ||
| bnames = Symbol.(index.bands) | ||
| tbands = transform.bands | ||
| snames = map(bnames) do b | ||
| if !isnothing(tbands) && b ∈ keys(tbands) | ||
| Symbol(tbands[b]) | ||
| else | ||
| b | ||
| end | ||
| end | ||
|
|
||
| # throw helpful error message in case of invalid names | ||
| if !(snames ⊆ names) | ||
| notfound = setdiff(snames, names) | ||
| required = ["$(b.short_name): $(b.long_name)" for b in spectralbands(iname)] | ||
| pprint(names) = "\"" * join(string.(names), "\", \"", "\" and \"") * "\"" | ||
| throw(ArgumentError("""columns $(pprint(notfound)) not found in table. | ||
|
|
||
| Please specify valid columns names for the spectral bands as keyword arguments. | ||
|
|
||
| Required bands for $iname: | ||
|
|
||
| $(join(required, "\n ")) | ||
|
|
||
| Available column names: $(pprint(names)) | ||
| """)) | ||
| end | ||
|
|
||
| # compute index for all locations | ||
| icols = [b => Tables.getcolumn(cols, n) for (b, n) in zip(bnames, snames)] | ||
| ivals = compute(index; icols...) | ||
|
|
||
| # new table with index feature | ||
| newfeat = (; Symbol(iname) => ivals) |> Tables.materializer(feat) | ||
|
|
||
| newfeat, nothing | ||
| end | ||
|
|
||
| """ | ||
| spectralindices() | ||
|
|
||
| List of spectral indices supported by SpectralIndices.jl. | ||
| """ | ||
| spectralindices() = indices | ||
|
|
||
| """ | ||
| spectralbands(name) | ||
|
|
||
| List of spectral bands for spectral index of given `name`. | ||
| """ | ||
| spectralbands(name) = [bands[b] for b in indices[name].bands] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| @testset "SpectralIndex" begin | ||
| @test !isrevertible(SpectralIndex("NDVI")) | ||
|
|
||
| # standard names | ||
| t = Table(R=[1,2,3], N=[4,5,6]) | ||
| T = SpectralIndex("NDVI") | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
| T = SpectralIndex("NDVI", R="R") | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
| T = SpectralIndex("NDVI", N="N") | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
| T = SpectralIndex("NDVI", R="R", N="N") | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
|
|
||
| # some non-standard names | ||
| t = Table(R=[1,2,3], NIR=[4,5,6]) | ||
| T = SpectralIndex("NDVI", N="NIR") | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
| T = SpectralIndex("NDVI") | ||
| @test_throws ArgumentError T(t) | ||
| T = SpectralIndex("NDVI", R="RED") | ||
| @test_throws ArgumentError T(t) | ||
|
|
||
| # all non-standard names | ||
| t = Table(RED=[1,2,3], NIR=[4,5,6]) | ||
| T = SpectralIndex("NDVI", R="RED", N="NIR") | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
| T = SpectralIndex("NDVI", R="RED") | ||
| @test_throws ArgumentError T(t) | ||
| T = SpectralIndex("NDVI", N="NIR") | ||
| @test_throws ArgumentError T(t) | ||
|
|
||
| # bands as symbols | ||
| t = Table(RED=[1,2,3], NIR=[4,5,6]) | ||
| T = SpectralIndex("NDVI", R=:RED, N=:NIR) | ||
| @test T(t).NDVI ≈ [0.6, 0.42857142857142855, 0.3333333333333333] | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.