umap_tile: Perform UMAP dimension reduction on raster data

Description Usage Arguments Examples

View source: R/umap_tile.R

Description

umap_tile takes a multi-band raster stack and reduces it to three components via the UMAP algorithm as implemented in UWOT package. Tuning parameters may be passed to umap() via ...

Usage

1
umap_tile(tile, out_dir, scl = TRUE, ...)

Arguments

tile

File path to a multi-band raster stack.

out_dir

Directory into which rasterized UMAP data are stored.

scl

Whether to z-score input data.

...

Additional arguments to pass to umap().

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
library(paint2train)

image_dir <- tempfile()
image_url <- 'https://storage.googleapis.com/mpgranch_data/sample_4band.tif'
download.file(url = image_url, destfile = image_dir)
tdir <- tempdir()
setwd(tdir) 
preproc_dir <- 'preproc_tiles'
umap_dir <- 'umap_tiles'
dir.create(preproc_dir)
dir.create(umap_dir)

#some test coordinates
xcoords <- c(727495,
             727919)

ycoords <- c(5175339,
             5175408)

coord_mat <- cbind(xcoords, ycoords)

ls <- 30 #how big should the tiles be, this is the side length (in units of data, meters here)
buff <- 5  #buffer in native units of CRS
cores <- ifelse(.Platform$OS.type == 'unix', #how many cores to use for preprocessing
                   parallel::detectCores() - 1,
                   1) 
umap_cores <- parallel::detectCores() - 1                  
   
                   
tile_at_coords(coords = coord_mat,
 len_side = ls,
 buffer = buff,
 out_dir = preproc_dir,
 img = image_dir,
 ncores = cores)

preproc_pipeline <- function(t, fs, b){
 ndvi_msavi(tile = t, r_band = 1, nir_band = 4)
 sobel(t, axes = 3, fill_na = TRUE)
 mean_var(t, axes = 3, f_width = fs, fill_na = TRUE)
 remove_buffer(tile = t, b = b)
}

targ_tiles <- list.files(preproc_dir, full.names = TRUE)

mclapply(FUN = preproc_pipeline, 
 X = targ_tiles, 
 mc.cores = cores, 
 fs = c(0.5, 1),
 b = buff)
 
lapply(FUN = umap_tile,
 X = targ_tiles,
 out_dir = umap_dir,
 n_threads = umap_cores, #args passed to umap
 n_sgd_threads = umap_cores, #args passed to umap
)

par(mfrow = c(2,2))
for(i in seq_along(xcoords)) {
 plotRGB(stack(list.files(preproc_dir, full.names = T)[i])[[1:3]])
}
for (i in seq_along(xcoords)) {
 umap_st <- stack(list.files(umap_dir, full.names = T)[i])[[1:3]]
 values(umap_st) <- rescale(getValues(umap_st))
 plotRGB(umap_st, scale = 1)
}
par(mfrow = c(1, 1))

mosscoder/paint2train documentation built on Jan. 21, 2022, 11 a.m.