It allows for transforming spatial signatures (outputs of the lsp_signature() function) using user-provided functions. See examples for more details.

lsp_transform(x, fun, ...)

Arguments

x

Object of class lsp - usually the output of the lsp_signature() function.

fun

A user-provided function.

...

Additional arguments for fun.

Value

Object of class lsp. It has three columns: (1) id - an id of each window. For irregular windows, it is the values provided in the window argument, (2) na_prop - share (0-1) of NA cells for each window, (3) signature - a list-column containing with calculated signatures

Examples

library(stars)
landform = read_stars(system.file("raster/landforms.tif", package = "motif"))
result_coma500 = lsp_signature(landform, type = "coma", threshold = 0.5, window = 500)

#see how the first signature looks
result_coma500$signature[[1]]
#>         1   5   6    7     8     9    10    11     12   14  15  17
#> 1  214604  33 126   10   474    72    33     0      0    0   0 690
#> 5      33 296  18    0    51     0     0     0      0    0   0   1
#> 6     126  18 994    0    72     0     0     0      0    0   0   0
#> 7      10   0   0 6184   422   249    19     0      0   72   0   0
#> 8     474  51  72  422 25208   124   105     0      0  140   2  10
#> 9      72   0   0  249   124 58622  1175   776      6   26  45   6
#> 10     33   0   0   19   105  1175 24174    14    452   13  40   0
#> 11      0   0   0    0     0   776    14 33368   1368    0   0   0
#> 12      0   0   0    0     0     6   452  1368 609562    0   0  39
#> 14      0   0   0   72   140    26    13     0      0 1366   3   0
#> 15      0   0   0    0     2    45    40     0      0    3 970   0
#> 17    690   1   0    0    10     6     0     0     39    0   0 542

my_function = function(mat){
    mat_c = colSums(mat)
    freqs = mat_c / sum(mat)
    # entropy
    -sum(freqs * log2(freqs), na.rm = TRUE)
}

result_coma500_2 = lsp_transform(result_coma500, my_function)

#see how the first signature looks after transformation
result_coma500_2$signature[[1]]
#> [1] 1.712131

# \donttest{
# larger data example
library(stars)
landform = read_stars(system.file("raster/landform.tif", package = "motif"))
result_coma500 = lsp_signature(landform, type = "coma", threshold = 0.5, window = 500)

#see how the first signature looks
result_coma500$signature[[1]]
#>         1    2    3     4    5     6     7     8    9    10   11    12    13 14
#> 1  126136  244   91   577   10   641   640   472   19     0   22     0     0  0
#> 2     244 2292   60    30    0     3     0     0    0     0   26     0     0  0
#> 3      91   60 7182   418    0   196     0     0    0     0   68     0     0  0
#> 4     577   30  418 19010    0   483     2     1    0     0  173     0     0  0
#> 5      10    0    0     0 9760   810   271    27    0     0    0   261     1  0
#> 6     641    3  196   483  810 86384   536   552    0     0    5   911    61  0
#> 7     640    0    0     2  271   536 76198  2474  177    52    0    58   303  0
#> 8     472    0    0     1   27   552  2474 81192    0   164    0    60   926  0
#> 9      19    0    0     0    0     0   177     0 5728   309    0     0     0  0
#> 10      0    0    0     0    0     0    52   164  309 36566    0     0     0  0
#> 11     22   26   68   173    0     5     0     0    0     0 1256     0     0  0
#> 12      0    0    0     0  261   911    58    60    0     0    0 12310   237  0
#> 13      0    0    0     0    1    61   303   926    0     0    0   237 26508  0
#> 14      0    0    0     0    0     0     0     0    0     0    0     0     0  0
#> 15    904    0    0    38    0   136    10    41    0     0    0     0     0  0
#>     15
#> 1  904
#> 2    0
#> 3    0
#> 4   38
#> 5    0
#> 6  136
#> 7   10
#> 8   41
#> 9    0
#> 10   0
#> 11   0
#> 12   0
#> 13   0
#> 14   0
#> 15 894

my_function = function(mat){
    mat_c = colSums(mat)
    freqs = mat_c / sum(mat)
    # entropy
    -sum(freqs * log2(freqs), na.rm = TRUE)
}

result_coma500_2 = lsp_transform(result_coma500, my_function)

#see how the first signature looks after transformation
result_coma500_2$signature[[1]]
#> [1] 2.996814
# }