Skip to content

Commit 749ce45

Browse files
Merge pull request #1590 from M3nin0/feat/huggingface-source
add support to hugging face datasets as source in sits
2 parents bbdb70d + cde28c9 commit 749ce45

19 files changed

Lines changed: 1874 additions & 16 deletions

‎DESCRIPTION‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Collate:
199199
'api_source_cdse.R'
200200
'api_source_deafrica.R'
201201
'api_source_deaustralia.R'
202+
'api_source_hf.R'
202203
'api_source_hls.R'
203204
'api_source_local.R'
204205
'api_source_mpc.R'

‎NAMESPACE‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ S3method(.source_collection_access_test,"cdse_cube_sentinel-1-grd")
164164
S3method(.source_collection_access_test,"mpc_cube_sentinel-1-grd")
165165
S3method(.source_collection_access_test,alphaearth_cube)
166166
S3method(.source_collection_access_test,cdse_cube)
167+
S3method(.source_collection_access_test,hf_cube)
167168
S3method(.source_collection_access_test,hls_cube)
168169
S3method(.source_collection_access_test,mpc_cube)
169170
S3method(.source_collection_access_test,ogh_cube)
170171
S3method(.source_collection_access_test,stac_cube)
171172
S3method(.source_collection_access_test,usgs_cube)
172173
S3method(.source_cube,alphaearth_cube)
174+
S3method(.source_cube,hf_cube)
173175
S3method(.source_cube,stac_cube)
174176
S3method(.source_filter_tiles,"bdc_cube_landsat-1y")
175177
S3method(.source_filter_tiles,"bdc_cube_landsat-2m")
@@ -242,6 +244,7 @@ S3method(.source_items_tile,bdc_cube)
242244
S3method(.source_items_tile,cdse_cube)
243245
S3method(.source_items_tile,deafrica_cube)
244246
S3method(.source_items_tile,deaustralia_cube)
247+
S3method(.source_items_tile,hf_cube)
245248
S3method(.source_items_tile,hls_cube)
246249
S3method(.source_items_tile,mpc_cube_hlsl30)
247250
S3method(.source_items_tile,mpc_cube_hlss30)

‎NEWS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ remove deprecated functions. The highlights below are grouped by theme.
6262

6363
### New data sources and collections
6464
* Add AlphaEarth (AEF) as a data source
65+
* Add support for data cubes shared on HuggingFace (`source = "HF:<user>"`)
6566
* Add support for the Amazonia-1 and GLAD image collections in the Brazil Data
6667
Cube (BDC)
6768
* Add support for the `SENTINEL-1-GRD` collection in CDSE

‎R/api_band.R‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,12 @@
162162
cube_source <- .cube_source(cube)
163163
collection <- .cube_collection(cube)
164164
# find which are possible color composites for the cube
165-
comp_source <- .conf("composites", "sources", cube_source)
166-
composites <- comp_source[["collections"]][[collection]]
165+
# (not all sources have color composites)
166+
composites <- NULL
167+
if (.conf_exists("composites", "sources", cube_source)) {
168+
comp_source <- .conf("composites", "sources", cube_source)
169+
composites <- comp_source[["collections"]][[collection]]
170+
}
167171
# for each color composite (in order)
168172
# see if bands are available
169173
for (i in seq_along(composites)) {

‎R/api_conf.R‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,38 @@
113113
}
114114
return(invisible(sits_env[["config"]]))
115115
}
116+
#' @title Add a source to the configuration of the current session
117+
#' @name .conf_add_source
118+
#' @keywords internal
119+
#' @noRd
120+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
121+
#' @description Define a given source configuration on-the-fly and remains
122+
#' available until the session ends.
123+
#' @param source Data source.
124+
#' @param source_conf Configuration of the data source.
125+
#' @return Called for side effects.
126+
.conf_add_source <- function(source, source_conf) {
127+
# define source configuration
128+
source_conf <- stats::setNames(list(source_conf), source)
129+
# set source configuration
130+
.conf_set_options(sources = source_conf)
131+
# prepare object to save in the current session tracker
132+
sources_session <- .default(sits_env[["sources_session"]], list())
133+
# save extra defined sources to keep track of them until the
134+
# session ends
135+
sits_env[["sources_session"]] <- utils::modifyList(sources_session, source_conf)
136+
# return!
137+
invisible(source)
138+
}
139+
#' @title Return the sources defined during the current session
140+
#' @name .conf_sources_session
141+
#' @keywords internal
142+
#' @noRd
143+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
144+
#' @return Configuration of the sources defined during the session.
145+
.conf_sources_session <- function() {
146+
sits_env[["sources_session"]]
147+
}
116148
#' @title Return the default configuration file
117149
#' @name .conf_file
118150
#' @keywords internal

‎R/api_download.R‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@
3838
.download_asset.derived_cube <- function(asset, roi, res, n_tries,
3939
output_dir) {
4040
# Extract version
41-
fields <- .file_sans_ext(.file_base(.tile_path(asset)))
42-
fields <- strsplit(fields, split = "_", fixed = TRUE)
43-
fields <- fields[[1L]]
44-
version <- fields[[length(fields)]]
41+
version <- .file_derived_version(.tile_path(asset))
4542
# Define filename
4643
output_file <- .file_derived_name(
4744
tile = asset,
@@ -64,10 +61,7 @@
6461
.download_asset.class_cube <- function(asset, roi, res, n_tries,
6562
output_dir) {
6663
# Extract version
67-
fields <- .file_sans_ext(.file_base(.tile_path(asset)))
68-
fields <- strsplit(fields, split = "_", fixed = TRUE)
69-
fields <- fields[[1L]]
70-
version <- fields[[length(fields)]]
64+
version <- .file_derived_version(.tile_path(asset))
7165
# Define filename
7266
output_file <- .file_derived_name(
7367
tile = asset,

‎R/api_file.R‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,31 @@
185185
)
186186
}
187187

188+
#' @title Get the version of a derived file
189+
#' @noRd
190+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
191+
#' @param file File name
192+
#' @return Version name
193+
.file_derived_version <- function(file) {
194+
# get parse info of files produced by sits
195+
parse_info <- .conf("results_parse_info_def")
196+
# split the file name
197+
fields <- strsplit(.file_sans_ext(file), split = "_", fixed = TRUE)[[1L]]
198+
# verification - file name fields and parse info fields match
199+
is_field_match <- length(fields) == length(parse_info)
200+
# verification - file band is in the list of sits results bands
201+
is_results_band <- fields[[which(parse_info == "band")]] %in% .conf("sits_results_bands")
202+
# verify if the file name follows the sits convention
203+
is_sits_file <- is_field_match && is_results_band
204+
# if the file name does not follow the sits convention, return
205+
# the default version
206+
if (!is_sits_file) {
207+
return(.conf("results_version_def"))
208+
}
209+
# otherwise, return the version name
210+
fields[[which(parse_info == "version")]]
211+
}
212+
188213
#' @title Build a file path for a mosaic of derived cubes
189214
#' @author Rolf Simoes, \email{rolfsimoes@@gmail.com}
190215
#' @author Felipe Carvalho, \email{felipe.carvalho@@inpe.br}

‎R/api_parallel.R‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,32 @@
5555
)
5656
}
5757

58+
#' @title Export the sources defined in the session to cluster workers
59+
#' @name .parallel_export_sources
60+
#' @keywords internal
61+
#' @noRd
62+
#' @author Felipe Carlos, \email{efelipecarlos@@gmail.com}
63+
#' @description Workers build their own configuration when sits is loaded, and
64+
#' are not aware of sources defined while sits is running (e.g., data cubes
65+
#' shared on HuggingFace). This function includes those sources in each worker
66+
#' of an open cluster.
67+
#' @return No value, called for side effect.
68+
.parallel_export_sources <- function() {
69+
# get the sources defined during the session
70+
sources <- .conf_sources_session()
71+
# verify if there are workers and sources
72+
if (!.parallel_is_open() || .has_not(sources)) {
73+
return(invisible(NULL))
74+
}
75+
# if so, export sources to cluster workers
76+
parallel::clusterCall(
77+
cl = sits_env[["cluster"]],
78+
fun = .conf_set_options,
79+
sources = sources
80+
)
81+
# return!
82+
invisible(NULL)
83+
}
5884
#' @title Start a new sits cluster for parallel processing
5985
#' @name .parallel_start
6086
#' @keywords internal
@@ -78,6 +104,7 @@
78104
# export variables to cluster workers only; with workers <= 1 and
79105
# no cluster, processing is sequential in the main process and
80106
# there is nothing to export
107+
.parallel_export_sources()
81108
if (.parallel_is_open() && .has(export_vars)) {
82109
parallel::clusterExport(
83110
cl = sits_env[["cluster"]],
@@ -127,6 +154,9 @@
127154
flag = log,
128155
output_dir = output_dir
129156
)
157+
# workers build their own configuration when sits is loaded: sources
158+
# defined during the session must also be included in them
159+
.parallel_export_sources()
130160
# export export_list
131161
if (.has(export_vars)) {
132162
parallel::clusterExport(

0 commit comments

Comments
 (0)