Skip to content

Commit f1de4b6

Browse files
build rfor model with base bands
1 parent 3705928 commit f1de4b6

6 files changed

+81
-3
lines changed

NAMESPACE

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ S3method(.opensearch_cdse_extract_tile,RTC)
9898
S3method(.opensearch_cdse_extract_tile,S2MSI2A)
9999
S3method(.opensearch_cdse_search,RTC)
100100
S3method(.opensearch_cdse_search,S2MSI2A)
101+
S3method(.predictors,sits)
102+
S3method(.predictors,sits_base)
101103
S3method(.raster_check_package,terra)
102104
S3method(.raster_col,terra)
103105
S3method(.raster_crop,terra)
@@ -132,6 +134,8 @@ S3method(.reg_s2tile_convert,grd_cube)
132134
S3method(.reg_s2tile_convert,rtc_cube)
133135
S3method(.samples_alloc_strata,class_cube)
134136
S3method(.samples_alloc_strata,class_vector_cube)
137+
S3method(.samples_bands,sits)
138+
S3method(.samples_bands,sits_base)
135139
S3method(.slice_dfr,numeric)
136140
S3method(.source_collection_access_test,"mpc_cube_sentinel-1-grd")
137141
S3method(.source_collection_access_test,cdse_cube)

R/api_band.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' @return Updated sits object
1616
#' @export
1717
.band_rename.sits <- function(x, bands) {
18-
data_bands <- .samples_bands(x)
18+
data_bands <- .samples_bands.sits(x)
1919
# pre-condition
2020
.check_chr(
2121
bands,

R/api_check.R

+6-2
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,13 @@
17631763
cols <- .pred_cols # From predictors API
17641764
.check_that(cols %in% colnames(pred))
17651765
.check_that(nrow(pred) > 0)
1766-
n_bands <- length(.samples_bands(samples))
1766+
n_bands <- length(.samples_bands.sits(samples))
17671767
n_times <- length(.samples_timeline(samples))
1768-
.check_that(ncol(pred) == 2 + n_bands * n_times)
1768+
if(inherits(samples, "sits_base"))
1769+
n_bands_base <- length(.samples_bands_base(samples))
1770+
else
1771+
n_bands_base <- 0
1772+
.check_that(ncol(pred) == 2 + n_bands * n_times + n_bands_base)
17691773
return(invisible(pred))
17701774
}
17711775
#' @title Does the data contain the cols of sample data and is not empty?

R/api_predictors.R

+36
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
#' @param ml_model ML model (optional)
1010
#' @return Data.frame with predictors
1111
.predictors <- function(samples, ml_model = NULL) {
12+
UseMethod(".predictors", samples)
13+
}
14+
#'
15+
#' @export
16+
.predictors.sits <- function(samples, ml_model = NULL) {
1217
# Prune samples time series
1318
samples <- .samples_prune(samples)
1419
# Get samples time series
@@ -61,6 +66,37 @@
6166
# Return predictors
6267
pred
6368
}
69+
#' @export
70+
.predictors.sits_base <- function(samples, ml_model = NULL) {
71+
# Get predictors for time series
72+
# Prune samples time series
73+
samples <- .samples_prune(samples)
74+
# Get samples time series
75+
pred <- .ts(samples)
76+
# By default get bands as the same of first sample
77+
bands <- .samples_bands.sits(samples)
78+
# Create predictors...
79+
pred <- pred[c(.pred_cols, bands)]
80+
# Add sequence 'index' column grouped by 'sample_id'
81+
pred <- pred |>
82+
dplyr::select("sample_id", "label", dplyr::all_of(bands)) |>
83+
dplyr::group_by(.data[["sample_id"]]) |>
84+
dplyr::mutate(index = seq_len(dplyr::n())) |>
85+
dplyr::ungroup()
86+
# Rearrange data to create predictors
87+
pred <- tidyr::pivot_wider(
88+
data = pred, names_from = "index", values_from = dplyr::all_of(bands),
89+
names_prefix = if (length(bands) == 1) bands else "",
90+
names_sep = ""
91+
)
92+
# get predictors for base data
93+
base <- dplyr::bind_rows(samples$base_data)
94+
base <- base[,-1]
95+
# join time series predictors with base data predictors
96+
pred <- dplyr::bind_cols(pred, base)
97+
# Return predictors
98+
pred
99+
}
64100

65101
#' @title Get predictors names with timeline
66102
#' @keywords internal

R/api_samples.R

+20
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,29 @@
9090
#' @param samples Data.frame with samples
9191
#' @return Bands for the first sample
9292
.samples_bands <- function(samples) {
93+
# Bands of the first sample governs whole samples data
94+
UseMethod(".samples_bands", samples)
95+
}
96+
#' @export
97+
.samples_bands.sits <- function(samples) {
9398
# Bands of the first sample governs whole samples data
9499
setdiff(names(.samples_ts(samples)), "Index")
95100
}
101+
#' @export
102+
.samples_bands.sits_base <- function(samples) {
103+
# Bands of the first sample governs whole samples data
104+
ts_bands <- .samples_bands.sits(samples)
105+
base_bands <- .samples_bands_base(samples)
106+
bands <- c(ts_bands, base_bands)
107+
}
108+
#' @title Get bands of base data for samples
109+
#' @noRd
110+
#' @param samples Data.frame with samples
111+
#' @return Bands for the first sample
112+
.samples_bands_base <- function(samples) {
113+
# Bands of the first sample governs whole samples data
114+
setdiff(names(samples$base_data[[1]]), "Index")
115+
}
96116

97117
#' @title Get timeline of time series samples
98118
#' @noRd

R/api_tile.R

+14
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,26 @@ NULL
532532
setdiff(bands, .band_cloud())
533533
}
534534
#' @export
535+
.tile_bands.base_raster_cube <- function(tile, add_cloud = TRUE) {
536+
bands <- .tile_bands.raster_cube(tile, add_cloud)
537+
base_bands <- .tile_bands_base(tile)
538+
all_bands <- c(bands, base_bands)
539+
}
540+
#' @export
535541
.tile_bands.default <- function(tile, add_cloud = TRUE) {
536542
tile <- tibble::as_tibble(tile)
537543
tile <- .cube_find_class(tile)
538544
bands <- .tile_bands(tile, add_cloud)
539545
return(bands)
540546
}
547+
#' @title Get bands of base data for tile
548+
#' @noRd
549+
#' @param samples Data.frame with samples
550+
#' @return Bands for the first sample
551+
.tile_bands_base <- function(tile) {
552+
# Bands of the first sample governs whole samples data
553+
names(tile$base_info[[1]])
554+
}
541555
#' @title Set bands in tile file_info.
542556
#' @rdname .tile_bands
543557
#' @keywords internal

0 commit comments

Comments
 (0)