Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/e-sensing/sits into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Jan 10, 2025
2 parents a5edcb0 + 7cbc5e8 commit 9a45718
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 138 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ export(sits_tae)
export(sits_tempcnn)
export(sits_tiles_to_roi)
export(sits_timeline)
export(sits_timeseries_to_csv)
export(sits_to_csv)
export(sits_to_xlsx)
export(sits_train)
Expand Down
9 changes: 7 additions & 2 deletions R/api_classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,8 @@
prediction <- .classify_ts_gpu(
pred = pred,
ml_model = ml_model,
gpu_memory = gpu_memory)
gpu_memory = gpu_memory
)
else
prediction <- .classify_ts_cpu(
pred = pred,
Expand Down Expand Up @@ -651,6 +652,8 @@
values <- .pred_features(pred_part)
# Classify
values <- ml_model(values)
# normalize and calibrate values
values <- .ml_normalize(ml_model, values)
# Return classification
values <- tibble::as_tibble(values)
values
Expand Down Expand Up @@ -691,8 +694,10 @@
values <- .pred_features(pred_part)
# Classify
values <- ml_model(values)
# normalize and calibrate values
values <- .ml_normalize(ml_model, values)
# Return classification
values <- tibble::tibble(data.frame(values))
values <- tibble::as_tibble(values)
# Clean GPU memory
.ml_gpu_clean(ml_model)
return(values)
Expand Down
19 changes: 19 additions & 0 deletions R/api_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,22 @@
)
return(samples)
}
#' @title Get samples metadata as CSV
#' @name .csv_metadata_from_samples
#' @author Gilberto Camara
#' @keywords internal
#' @noRd
#' @param data A sits tibble.
#' @return A tibble with metadata
#'
.csv_metadata_from_samples <- function(data) {
# select the parts of the tibble to be saved
csv_columns <- .conf("df_sample_columns")
csv <- dplyr::select(data, dplyr::all_of(csv_columns))
# create a column with the id
n_rows_csv <- nrow(csv)
id <- tibble::tibble(id = 1:n_rows_csv)
# join the two tibbles
csv <- dplyr::bind_cols(id, csv)
return(csv)
}
2 changes: 2 additions & 0 deletions R/api_ml_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@
#' @export
#'
.ml_normalize.torch_model <- function(ml_model, values){
column_names <- colnames(values)
values[is.na(values)] <- 0
values <- softmax(values)
colnames(values) <- column_names
return(values)
}
#' @export
Expand Down
3 changes: 2 additions & 1 deletion R/api_tile.R
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,8 @@ NULL
x = r_obj,
y = segments,
fun = NULL,
include_cols = "pol_id"
include_cols = "pol_id",
progress = FALSE
)
values <- dplyr::bind_rows(values)
values <- dplyr::select(values, -"coverage_fraction")
Expand Down
10 changes: 7 additions & 3 deletions R/sits_classify.R
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,18 @@ sits_classify.raster_cube <- function(data,
msg = .conf("messages", ".check_gpu_memory")
)
# Calculate available memory from GPU
memsize <- floor(gpu_memory - .torch_mem_info())
.check_int_parameter(memsize, min = 1,
gpu_available_memory <- floor(gpu_memory - .torch_mem_info())
.check_int_parameter(gpu_available_memory, min = 1,
msg = .conf("messages", ".check_gpu_memory_size")
)
proc_bloat <- .conf("processing_bloat_gpu")
}
# avoid memory race in Apple MPS
if (.torch_mps_enabled(ml_model)) {
.check_int_parameter(gpu_memory, min = 1, max = 16384,
msg = .conf("messages", ".check_gpu_memory")
)

warning(.conf("messages", "sits_classify_mps"),
call. = FALSE
)
Expand Down Expand Up @@ -440,7 +444,7 @@ sits_classify.segs_cube <- function(data,
proc_bloat <- .conf("processing_bloat_gpu")
}
# avoid memory race in Apple MPS
if(.torch_mps_enabled(ml_model)){
if (.torch_mps_enabled(ml_model)) {
memsize <- 1
gpu_memory <- 1
}
Expand Down
50 changes: 42 additions & 8 deletions R/sits_csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,8 @@ sits_to_csv.sits <- function(data, file = NULL) {
extensions = "csv",
file_exists = FALSE
)
# select the parts of the tibble to be saved
csv_columns <- .conf("df_sample_columns")
csv <- dplyr::select(data, dplyr::all_of(csv_columns))
# create a column with the id
n_rows_csv <- nrow(csv)
id <- tibble::tibble(id = 1:n_rows_csv)
# join the two tibbles
csv <- dplyr::bind_cols(id, csv)
# get metadata
csv <- .csv_metadata_from_samples(data)
# write the CSV file
if (.has(file))
utils::write.csv(csv, file, row.names = FALSE, quote = FALSE)
Expand All @@ -68,3 +62,43 @@ sits_to_csv.tbl_df <- function(data, file) {
sits_to_csv.default <- function(data, file) {
stop(.conf("messages", "sits_to_csv_default"))
}

#' @title Export a a full sits tibble to the CSV format
#'
#' @name sits_timeseries_to_csv
#'
#' @author Gilberto Camara, \email{gilberto.camara@@inpe.br}
#'
#' @description Converts metadata and data from a sits tibble to a CSV file.
#' The CSV file will not contain the actual time
#' series. Its columns will be the same as those of a
#' CSV file used to retrieve data from
#' ground information ("latitude", "longitude", "start_date",
#' "end_date", "cube", "label"), plus the all the time series for
#' each data
#' @param data Time series (tibble of class "sits").
#' @param file Full path of the exported CSV file
#' (valid file name with extension ".csv").
#' @return Return data.frame with CSV columns (optional)
#'
#' @examples
#' csv_file <- paste0(tempdir(), "/cerrado_2classes_ts.csv")
#' sits_timeseries_to_csv(cerrado_2classes, file = csv_file)
#' @export
#'
sits_timeseries_to_csv <- function(data, file = NULL) {
# check the samples are valid
data <- .check_samples(data)
csv_1 <- .csv_metadata_from_samples(data)
csv_2 <- .predictors(data)[-2:0]
csv_combined <- dplyr::bind_cols(csv_1, csv_2)

# write the CSV file
if (.has(file))
utils::write.csv(csv_combined,
file,
row.names = FALSE,
quote = FALSE)

return(csv_combined)
}
2 changes: 2 additions & 0 deletions R/sits_plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,8 @@ plot.vector_cube <- function(x, ...,
sf_seg = sf_seg,
seg_color = seg_color,
line_width = line_width,
first_quantile = first_quantile,
last_quantile = last_quantile,
scale = scale,
max_cog_size = max_cog_size,
tmap_params = tmap_params
Expand Down
Loading

0 comments on commit 9a45718

Please sign in to comment.