Skip to content

Commit 7d5d284

Browse files
Merge pull request #1475 from M3nin0/devsits
Refactoring segments classification
2 parents dffb0ef + 964d676 commit 7d5d284

7 files changed

Lines changed: 803 additions & 211 deletions

File tree

R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ C_sampling_stratified_select_cells <- function(vals, vwght, vcell, size, seed) {
237237
.Call(`_sits_C_sampling_stratified_select_cells`, vals, vwght, vcell, size, seed)
238238
}
239239

240+
C_terra_sampling_random_candidates <- function(bbox_xmin, bbox_xmax, bbox_ymin, bbox_ymax, areas, n_points, lonlat = FALSE, seed = 42L) {
241+
.Call(`_sits_C_terra_sampling_random_candidates`, bbox_xmin, bbox_xmax, bbox_ymin, bbox_ymax, areas, n_points, lonlat, seed)
242+
}
243+
244+
C_terra_sampling_filter_and_trim <- function(hit_mat, poly_ids, n_per_feature, seed = 777L) {
245+
.Call(`_sits_C_terra_sampling_filter_and_trim`, hit_mat, poly_ids, n_per_feature, seed)
246+
}
247+
240248
C_max_sampling <- function(x, nrows, ncols, window_size) {
241249
.Call(`_sits_C_max_sampling`, x, nrows, ncols, window_size)
242250
}

R/api_classify.R

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -245,51 +245,37 @@
245245
}
246246
}
247247

248-
#' @title Classify a chunk of raster data using multicores
249-
#' @name .classify_vector_tile
248+
#' @title Classify segments
249+
#' @name .classify_segments
250250
#' @keywords internal
251251
#' @noRd
252252
#' @author Rolf Simoes, \email{rolfsimoes@@gmail.com}
253253
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
254254
#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br}
255255
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
256256
#'
257-
#' @description Classifies a block of data using multicores. Breaks
258-
#' the data into blocks and divides them between the available cores.
259-
#' After all cores process their blocks,
260-
#' joins the result and then writes it.
257+
#' @description Classify segments based on a probability raster cube.
258+
#' The segments are split for each raster block and then combined the
259+
#' pixels probabilities using mean aggregation.
261260
#'
262261
#' @param tile Single tile of a data cube.
263-
#' @param bands Bands to extract time series
264-
#' @param base_bands Base bands to extract values
265-
#' @param ml_model Model trained by \code{\link[sits]{sits_train}}.
266262
#' @param block Optimized block to be read into memory.
267-
#' @param roi Region of interest.
268-
#' @param filter_fn Smoothing filter function to be applied to the data.
269-
#' @param impute_fn Imputation function to remove NA values.
270263
#' @param n_sam_pol Number of samples per polygon to be read
271264
#' for POLYGON or MULTIPOLYGON vector objects.
272265
#' @param multicores Number of cores for classification
273-
#' @param gpu_memory Memory available in GPU (default = NULL)
266+
#' @param memsize Memory available for classification in GB
274267
#' @param version Version of result.
275268
#' @param output_dir Output directory.
276269
#' @param progress Show progress bar?
277270
#' @return List of the classified raster layers.
278-
.classify_vector_tile <- function(tile,
279-
bands,
280-
base_bands,
281-
ml_model,
282-
block,
283-
roi,
284-
filter_fn,
285-
impute_fn,
286-
n_sam_pol,
287-
multicores,
288-
memsize,
289-
gpu_memory,
290-
version,
291-
output_dir,
292-
progress) {
271+
.classify_segments <- function(tile,
272+
block,
273+
n_sam_pol,
274+
multicores,
275+
memsize,
276+
version,
277+
output_dir,
278+
progress) {
293279
# Define output vector file name and extension
294280
out_file <- .file_derived_name(
295281
tile = tile,
@@ -307,7 +293,7 @@
307293
file = out_file,
308294
band = "probs",
309295
base_tile = tile,
310-
labels = .ml_labels(ml_model),
296+
labels = .tile_labels(tile),
311297
vector_class = "probs_vector_cube",
312298
update_bbox = FALSE
313299
)
@@ -319,14 +305,6 @@
319305
overlap = 0L,
320306
block = block
321307
)
322-
# By default, update_bbox is FALSE
323-
if (.has(roi)) {
324-
# Intersecting chunks with ROI
325-
chunks <- .chunks_filter_spatial(
326-
chunks = chunks,
327-
roi = roi
328-
)
329-
}
330308
# Filter segments that intersects with each chunk
331309
chunks <- .chunks_filter_segments(
332310
chunks = chunks,
@@ -354,28 +332,13 @@
354332
# Number of time series per segment is defined by n_sam_pol
355333
segments_ts <- .segments_poly_read(
356334
tile = tile,
357-
bands = bands,
358-
base_bands = base_bands,
359335
chunk = chunk,
360-
n_sam_pol = n_sam_pol,
361-
impute_fn = impute_fn
336+
n_sam_pol = n_sam_pol
362337
)
363338
# Deal with NO DATA cases (e.g., cloudy areas)
364339
if (nrow(segments_ts) == 0L) {
365340
return("")
366341
}
367-
# Classify times series
368-
# This is the same function called to classify
369-
# individual time series (with an extra polygon_id)
370-
segments_ts <- .classify_ts(
371-
samples = segments_ts,
372-
ml_model = ml_model,
373-
filter_fn = filter_fn,
374-
impute_fn = impute_fn,
375-
multicores = 1L,
376-
gpu_memory = gpu_memory,
377-
progress = progress
378-
)
379342
# Join probability values with segments
380343
segments_ts <- .segments_join_probs(
381344
data = segments_ts,
@@ -405,7 +368,7 @@
405368
file = out_file,
406369
band = "probs",
407370
base_tile = tile,
408-
labels = .ml_labels(ml_model),
371+
labels = .tile_labels(tile),
409372
vector_class = "probs_vector_cube",
410373
update_bbox = FALSE
411374
)

0 commit comments

Comments
 (0)