Skip to content

Commit

Permalink
new SOM functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Sep 16, 2024
1 parent a6aa810 commit d4c33ca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
51 changes: 51 additions & 0 deletions R/api_som.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,54 @@

return(kohonen_obj)
}

#' @title Adjacency matrix
#' @name .som_adjacency
#' @keywords internal
#' @noRd
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#'
#' @description This function calculates the adjacency matrix for the SOM
#'
#' @param som_map kohonen_map
#' @return adjacency matrix with the distances btw neurons.
#'
.som_adjacency <- function(som_map) {
koh <- som_map$som_properties
adjacency <- as.matrix(proxy::dist(koh$codes$NDVI, method = "dtw"))
}

#' @title Transform SOM map into sf object.
#' @name .som_to_sf
#' @keywords internal
#' @noRd
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#'
#' @description This function transforms a SOM map into an sf object
#'
#' @param som_map kohonen_map
#' @return sf object with same geometry and attributes as SOM map
#'
.som_to_sf <- function(som_map) {
koh <- som_map$som_properties

grid_idx <- 0

neuron_ids <- koh$grid$pts
neuron_pols <- purrr::map(1:nrow(neuron_ids), function(id) {
x <- neuron_ids[id,"x"]
y <- neuron_ids[id,"y"]
pol <- rbind(c((x - 1), (y - 1)),
c(x, (y - 1)),
c(x, y),
c((x - 1), y),
c((x - 1), (y - 1)))
pol = sf::st_polygon(list(pol))
return(pol)
})
neuron_attr <- as.data.frame(koh$codes)
neuron_attr$geometry <- sf::st_sfc(neuron_pols)

sf_neurons <- sf::st_sf(neuron_attr, geometry = neuron_attr$geometry)
return(sf_neurons)
}
2 changes: 1 addition & 1 deletion inst/extdata/sources/config_source_mpc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ sources:
bands :
NDVI : &mpc_modis_ndvi
missing_value : -2000000000
minimum_value : -1000000000
minimum_value : -152000000000
maximum_value : 1000000000
scale_factor : 0.00000001
offset_value : 0
Expand Down

0 comments on commit d4c33ca

Please sign in to comment.