Skip to content

Commit

Permalink
add function to export time series to CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertocamara committed Dec 13, 2024
1 parent 814a7ff commit ced2f92
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 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
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)
}
8 changes: 6 additions & 2 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
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)
}
33 changes: 33 additions & 0 deletions man/sits_timeseries_to_csv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ced2f92

Please sign in to comment.