Skip to content

Commit

Permalink
non normalized bands
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Feb 5, 2024
2 parents 4823f44 + 8ae5fa3 commit 1691f16
Show file tree
Hide file tree
Showing 15 changed files with 1,043 additions and 15 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Collate:
'api_raster_sub_image.R'
'api_raster_terra.R'
'api_reclassify.R'
'api_reduce.R'
'api_regularize.R'
'api_roi.R'
'api_s2tile.R'
Expand Down Expand Up @@ -223,6 +224,7 @@ Collate:
'sits_plot.R'
'sits_predictors.R'
'sits_reclassify.R'
'sits_reduce.R'
'sits_regularize.R'
'sits_resnet.R'
'sits_sample_functions.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ S3method(sits_model_export,sits_model)
S3method(sits_reclassify,class_cube)
S3method(sits_reclassify,default)
S3method(sits_reclassify,tbl_df)
S3method(sits_reduce,raster_cube)
S3method(sits_reduce,sits)
S3method(sits_regularize,"mpc_cube_sentinel-1-grd")
S3method(sits_regularize,default)
S3method(sits_regularize,derived_cube)
Expand Down Expand Up @@ -436,6 +438,7 @@ export(sits_pred_references)
export(sits_pred_sample)
export(sits_predictors)
export(sits_reclassify)
export(sits_reduce)
export(sits_reduce_imbalance)
export(sits_regularize)
export(sits_resnet)
Expand Down
68 changes: 68 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,74 @@ C_normalize_data_0 <- function(data, min, max) {
.Call(`_sits_C_normalize_data_0`, data, min, max)
}

C_temp_max <- function(mtx) {
.Call(`_sits_C_temp_max`, mtx)
}

C_temp_min <- function(mtx) {
.Call(`_sits_C_temp_min`, mtx)
}

C_temp_mean <- function(mtx) {
.Call(`_sits_C_temp_mean`, mtx)
}

C_temp_median <- function(mtx) {
.Call(`_sits_C_temp_median`, mtx)
}

C_temp_sum <- function(mtx) {
.Call(`_sits_C_temp_sum`, mtx)
}

C_temp_std <- function(mtx) {
.Call(`_sits_C_temp_std`, mtx)
}

C_temp_skew <- function(mtx) {
.Call(`_sits_C_temp_skew`, mtx)
}

C_temp_kurt <- function(mtx) {
.Call(`_sits_C_temp_kurt`, mtx)
}

C_temp_amplitude <- function(mtx) {
.Call(`_sits_C_temp_amplitude`, mtx)
}

C_temp_fslope <- function(mtx) {
.Call(`_sits_C_temp_fslope`, mtx)
}

C_temp_abs_sum <- function(mtx) {
.Call(`_sits_C_temp_abs_sum`, mtx)
}

C_temp_amd <- function(mtx) {
.Call(`_sits_C_temp_amd`, mtx)
}

C_temp_mse <- function(mtx) {
.Call(`_sits_C_temp_mse`, mtx)
}

C_temp_fqr <- function(mtx) {
.Call(`_sits_C_temp_fqr`, mtx)
}

C_temp_sqr <- function(mtx) {
.Call(`_sits_C_temp_sqr`, mtx)
}

C_temp_tqr <- function(mtx) {
.Call(`_sits_C_temp_tqr`, mtx)
}

C_temp_iqr <- function(mtx) {
.Call(`_sits_C_temp_iqr`, mtx)
}

sample_points_inclusion <- function(polymatrix, n_sam_pol) {
.Call(`_sits_sample_points_inclusion`, polymatrix, n_sam_pol)
}
Expand Down
8 changes: 3 additions & 5 deletions R/api_apply.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
# Returned block files for each fraction
block_files
})
# Merge blocks into a new class_cube tile
# Merge blocks into a new eo_cube tile
band_tile <- .tile_eo_merge_blocks(
files = out_file,
bands = out_band,
Expand Down Expand Up @@ -219,16 +219,14 @@
#' @noRd
#'
#' @param cube Data cube.
#' @param bands Input bands in a cube or samples.
#' @param expr Band combination expression.
#' @return List of input bands required to run the expression
#'
.apply_input_bands <- function(cube, expr) {
.apply_input_bands <- function(cube, bands, expr) {
# Get all required bands in expression
expr_bands <- toupper(.apply_get_all_names(expr[[1]]))

# Get all input bands in cube data
bands <- .cube_bands(cube)

# Select bands that are in input expression
bands <- bands[bands %in% expr_bands]

Expand Down
10 changes: 7 additions & 3 deletions R/api_classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
values <- .classify_data_read(
tile = tile,
block = block,
bands = .ml_bands(ml_model),
ml_model = ml_model,
filter_fn = filter_fn
)
Expand Down Expand Up @@ -304,17 +305,18 @@
#'
#' @param tile Input tile to read data.
#' @param block Bounding box in (col, row, ncols, nrows).
#' @param bands Bands to extract time series
#' @param ml_model Model trained by \code{\link[sits]{sits_train}}.
#' @param filter_fn Smoothing filter function to be applied to the data.
#' @return A matrix with values for classification.
.classify_data_read <- function(tile, block, ml_model, filter_fn) {
.classify_data_read <- function(tile, block, bands, ml_model, filter_fn) {
# For cubes that have a time limit to expire (MPC cubes only)
tile <- .cube_token_generator(tile)
# Read and preprocess values of cloud
# Get cloud values (NULL if not exists)
cloud_mask <- .tile_cloud_read_block(tile = tile, block = block)
# Read and preprocess values of each band
values <- purrr::map(.ml_bands(ml_model), function(band) {
values <- purrr::map(bands, function(band) {
# Get band values (stops if band not found)
values <- .tile_read_block(tile = tile, band = band, block = block)
# Log
Expand Down Expand Up @@ -365,7 +367,9 @@
# Compose final values
values <- as.matrix(values)
# Set values features name
colnames(values) <- .ml_features_name(ml_model)
if (.has(ml_model)) {
colnames(values) <- .ml_features_name(ml_model)
}
# Return values
values
}
Expand Down
206 changes: 206 additions & 0 deletions R/api_reduce.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
.reduce_tile <- function(tile,
block,
expr,
out_band,
in_bands,
output_dir,
progress) {

# Output file
out_file <- .file_eo_name(
tile = tile, band = out_band,
date = .tile_start_date(tile),
output_dir = output_dir
)
# Resume feature
if (.raster_is_valid(out_file, output_dir = output_dir)) {
# recovery message
.check_recovery(out_file)

# Create tile based on template
tile <- .tile_eo_from_files(
files = out_file, fid = .fi_fid(.fi(tile)),
bands = out_band, date = .tile_start_date(tile),
base_tile = tile, update_bbox = FALSE
)
return(tile)
}
# Remove remaining incomplete fractions files
unlink(out_file)
# Create chunks as jobs
chunks <- .tile_chunks_create(
tile = tile, overlap = 0, block = block
)
# Filter tile bands
if (.band_cloud() %in% .tile_bands(tile)) {
in_bands <- c(in_bands, .band_cloud())
}
tile <- .tile_filter_bands(tile, in_bands)
# Process jobs in parallel
block_files <- .jobs_map_parallel_chr(chunks, function(chunk) {
# Get job block
block <- .block(chunk)
# Block file name
block_file <- .file_block_name(
pattern = .file_pattern(out_file),
block = block,
output_dir = output_dir
)
# Resume processing in case of failure
if (.raster_is_valid(block_file)) {
return(block_file)
}
# Read and preprocess values
values <- .classify_data_read(
tile = tile,
block = block,
bands = in_bands,
ml_model = NULL,
filter_fn = NULL
)
# Convert to named list
values <- list(values)
names(values) <- in_bands
# Evaluate expression here
# Band and kernel evaluation
values <- eval(
expr = expr[[out_band]],
envir = values,
enclos = .temp_functions()
)
# Prepare fractions to be saved
band_conf <- .tile_band_conf(tile = tile, band = out_band)
offset <- .offset(band_conf)
if (.has(offset) && offset != 0) {
values <- values - offset
}
scale <- .scale(band_conf)
if (.has(scale) && scale != 1) {
values <- values / scale
}
# Job crop block
crop_block <- .block(.chunks_no_overlap(chunk))
# Prepare and save results as raster
.raster_write_block(
files = block_file, block = block, bbox = .bbox(chunk),
values = values, data_type = .data_type(band_conf),
missing_value = .miss_value(band_conf),
crop_block = crop_block
)
# Free memory
gc()
# Returned block files for each fraction
block_file

}, progress = progress)
# Merge blocks into a new eo_cube tile
band_tile <- .tile_eo_merge_blocks(
files = out_file,
bands = out_band,
base_tile = tile,
block_files = block_files,
multicores = 1,
update_bbox = FALSE
)
# Return a reduced tile
band_tile
}

.reduce_samples <- function(data, expr, in_band, out_band) {
col <- "time_series"
min_date <- min(.samples_timeline(data))
# Pre-condition
.check_chr_within(
col,
within = names(data),
msg = "invalid column name"
)
# Select data do unpack
x <- data[col]
# Prepare to unpack
x[["#.."]] <- as.factor(seq_len(nrow(data)))
# Unpack
x <- tidyr::unnest(x, cols = dplyr::all_of(col))
# Apply the temporal operation
x <- by(x, x[, "#.."], function(y) {
y <- list(t(y[[in_band]]))
names(y) <- in_band
eval(
expr = expr[[out_band]],
envir = y,
enclos = .temp_functions()
)
})
# Unlist results
x <- unlist(x)
# Create a data frame
x <- tibble::tibble(x)
colnames(x) <- out_band
x[["#.."]] <- as.factor(seq_len(nrow(x)))
# Add index column
x <- tibble::add_column(x, Index = min_date, .before = out_band)
# pack
x <- tidyr::nest(x, `..unnest_col` = -"#..")
# remove garbage
x[["#.."]] <- NULL
names(x) <- col
# prepare result
data[[col]] <- x[[col]]
return(data)
}

#' @title Temporal functions for reduce operations
#' @name .temp_functions
#' @noRd
#' @return operations on reduce function
.temp_functions <- function() {
result_env <- list2env(list(
t_max = function(m) {
C_temp_max(mtx = as.matrix(m))
},
t_min = function(m) {
C_temp_min(mtx = as.matrix(m))
},
t_mean = function(m) {
C_temp_mean(mtx = as.matrix(m))
},
t_median = function(m) {
C_temp_median(mtx = as.matrix(m))
},
t_sum = function(m) {
C_temp_sum(mtx = as.matrix(m))
},
t_std = function(m) {
C_temp_std(mtx = as.matrix(m))
},
t_skewness = function(m) {
C_temp_skew(mtx = as.matrix(m))
},
t_kurtosis = function(m) {
C_temp_kurt(mtx = as.matrix(m))
},
t_amplitude = function(m) {
C_temp_amplitude(mtx = as.matrix(m))
},
t_fslope = function(m) {
C_temp_fslope(mtx = as.matrix(m))
},
t_mse = function(m) {
C_temp_mse(mtx = as.matrix(m))
},
t_fqr = function(m) {
C_temp_fqr(mtx = as.matrix(m))
},
t_sqr = function(m) {
C_temp_sqr(mtx = as.matrix(m))
},
t_tqr = function(m) {
C_temp_tqr(mtx = as.matrix(m))
},
t_iqr = function(m) {
C_temp_iqr(mtx = as.matrix(m))
}
), parent = parent.env(environment()), hash = TRUE)

return(result_env)
}
9 changes: 9 additions & 0 deletions R/api_samples.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@
# Bands of the first sample governs whole samples data
setdiff(names(.samples_ts(samples)), "Index")
}

#' @title Get timeline of time series samples
#' @noRd
#' @param samples Data.frame with samples
#' @return Timeline of the first sample
.samples_timeline <- function(samples) {
as.Date(samples$time_series[[1]]$Index)
}

#' @title Select bands of time series samples
#' @noRd
#' @param samples Data.frame with samples
Expand Down
Loading

0 comments on commit 1691f16

Please sign in to comment.