Skip to content

Commit

Permalink
reset rf
Browse files Browse the repository at this point in the history
reset rf
  • Loading branch information
PondiB authored Nov 26, 2023
2 parents 2997cb5 + d36a395 commit 2eda560
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 70 deletions.
55 changes: 37 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,48 @@
FROM r-base:4.3.0
# Use a stable Ubuntu version
FROM rocker/rstudio:4.3

# Install software dependencies
RUN apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y software-properties-common cmake g++ git supervisor wget
# Install essential tools
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Etc/UTC
RUN apt-get install -y libnetcdf-dev libcurl4-openssl-dev libcpprest-dev doxygen graphviz libsqlite3-dev libboost-all-dev
RUN apt-get update && apt-get install -y libproj-dev libgdal-dev

# Install devtools package
RUN R -e "install.packages('devtools')"

# Install gdalcubes package
RUN R -e "install.packages('gdalcubes')"

# install other necessary packages
RUN apt-get update && apt-get install -y \
software-properties-common \
cmake \
g++ \
git \
supervisor \
wget

# Install dependencies
RUN apt-get install -y \
libnetcdf-dev \
libcurl4-openssl-dev \
libcpprest-dev \
doxygen \
graphviz \
libsqlite3-dev \
libboost-all-dev

# Install devtools in R
RUN Rscript -e "install.packages(c('devtools'))"

# Install additional R packages
RUN apt-get install -y libsodium-dev libudunits2-dev
RUN Rscript -e "install.packages(c('plumber', 'useful', 'ids', 'R6', 'sf', 'rstac','bfast'))"
RUN Rscript -e "install.packages(c('plumber', 'useful', 'ids', 'R6', 'sf', 'rstac','bfast', 'stars', 'geojsonsf'))"
RUN Rscript -e "install.packages('sits')"
RUN Rscript -e "install.packages('gdalcubes')"

# create directories
RUN mkdir -p /opt/dockerfiles/ && mkdir -p /var/openeo/workspace/ && mkdir -p /var/openeo/workspace/data/
# Create directories
RUN mkdir -p /opt/dockerfiles/ \
&& mkdir -p /var/openeo/workspace/ \
&& mkdir -p /var/openeo/workspace/data/

# install packages from local directory
# Install local packages
COPY ./ /opt/dockerfiles/
RUN Rscript -e "remotes::install_local('/opt/dockerfiles',dependencies=TRUE)"
RUN Rscript -e "remotes::install_local('/opt/dockerfiles', dependencies=TRUE)"

# cmd or entrypoint for startup
# Set the startup command
CMD ["R", "-q", "--no-save", "-f /opt/dockerfiles/Dockerfiles/start.R"]

# Expose port
EXPOSE 8000
10 changes: 4 additions & 6 deletions Dockerfiles/start.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Start service
library(openeosits)

aws.host <-Sys.getenv("AWSHOST")
aws.host <- Sys.getenv("AWSHOST")

if (aws.host == ""){
aws.host = NULL
}else{
message("AWS host port id is:", aws.host)

}


config = SessionConfig(api.port = 8000, host = "0.0.0.0", aws.ipv4 = aws.host)
config$workspace.path = "/var/openeo/workspace"
config <- SessionConfig(api.port = 8000, host = "0.0.0.0", aws.ipv4 = aws.host)
config$workspace.path <- "/var/openeo/workspace"
createSessionInstance(config)
Session$startSession()
Session$startSession()
24 changes: 16 additions & 8 deletions R/processes.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ ml_regularize_data_cube <- Process$new(
CORES <- parallel::detectCores()

#' regularize irregular data cubes using sits
cube <- sits::sits_regularize(cube = data,
output_dir = tempdir(),
period = period,
res = resolution,
multicores = CORES)
cube <- sits::sits_regularize(
cube = data,
output_dir = tempdir(),
period = period,
res = resolution,
multicores = CORES
)
message("data cube regularization done.")
return(cube)
}
Expand Down Expand Up @@ -277,9 +279,15 @@ ml_fit_class_random_forest <- Process$new(
),
returns = eo_datacube,
operation = function(predictors, target, max_variables = "sqrt", num_trees = 100, seed = NULL, job) {
# TO DO
model <- sits::sits_rfor()
return(model)
# TO DO , samples need to be a union of predictors and targets
# samples <- predictors, target
samples <- predictors
# model
model <- sits::sits_train(
samples = samples,
ml_method = sits_rfor(num_trees = num_trees)
)
return(model)
}
)

Expand Down
111 changes: 80 additions & 31 deletions playground/playground.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,123 @@
library(sits)
library(parallel)
library(sitsdata)
library(stars)

# detect the number of CPU cores on the current host
CORES <- parallel::detectCores()


# openeo load collection
load_collection <- function(id, spatial_extent, temporal_extent, bands = null, properties = null){
load_collection <- function(id, spatial_extent, temporal_extent, bands = null, properties = null) {
source_id <- NULL
if(id == "SENTINEL-2-L2A"){
source_id = "AWS"
}else{
if (id == "SENTINEL-2-L2A") {
source_id <- "AWS"
} else {
message("The image collection is defined is not suppported.")
}

# Temporal extent
t0 = temporal_extent[[1]]
t1 = temporal_extent[[2]]
t0 <- temporal_extent[[1]]
t1 <- temporal_extent[[2]]

# spatial extent of roi
xmin = as.numeric(spatial_extent$west)
ymin = as.numeric(spatial_extent$south)
xmax = as.numeric(spatial_extent$east)
ymax = as.numeric(spatial_extent$north)
xmin <- as.numeric(spatial_extent$west)
ymin <- as.numeric(spatial_extent$south)
xmax <- as.numeric(spatial_extent$east)
ymax <- as.numeric(spatial_extent$north)

# roi
roi_bounds = c(
roi_bounds <- c(
lon_min = xmin, lat_min = ymin,
lon_max = xmax, lat_max = ymax
)
#get irregular data cube from ARD image collection
cube <- sits::sits_cube(source = source_id,
collection = id,
roi = roi_bounds,
bands = bands,
start_date = t0,
end_date = t1)
# get irregular data cube from ARD image collection
cube <- sits::sits_cube(
source = source_id,
collection = id,
roi = roi_bounds,
bands = bands,
start_date = t0,
end_date = t1
)
return(cube)
}

# openeo regularize data cubes
ml_regularize_data_cube <- function(data, period, resolution){
cube <- sits::sits_regularize(cube = data,
output_dir = tempdir(),
period = period,
res = resolution,
multicores = CORES)
ml_regularize_data_cube <- function(data, period, resolution) {
cube <- sits::sits_regularize(
cube = data,
output_dir = tempdir(),
period = period,
res = resolution,
multicores = CORES
)
return(cube)
}

# openeo ml random forest class training
ml_fit_class_random_forest <- function(predictors, target, max_variables, num_trees, seed){
#TO DO
model <- sits::sits_rfor()
ml_fit_class_random_forest <- function(predictors, target, max_variables = NULL, num_trees = 100, seed = NULL) {
# TO DO , samples need to be a union of predictors and targets
# samples <- predictors, target
samples <- predictors
# model
model <- sits::sits_train(
samples = samples,
ml_method = sits_rfor(num_trees = 100)
)
return(model)
}

# openeo ml prediction
ml_predict <- function(data, model, dimensions){

ml_predict <- function(data, model, dimensions = "bands") {
# classify data cube to obtain a probability cube
cube_probability <- sits::sits_classify(
data = data,
ml_model = model,
output_dir = ".",
version = "rf-raster",
multicores = CORES,
memsize = 16
)
# smoothen a probability cube
cube_bayes <- sits::sits_smooth(
cube = cube_probability,
output_dir = ".",
version = "rf-raster",
multicores = CORES,
memsize = 16
)
return(cube_bayes)
}

# --------------------------------------------#
# -----------test-----------------------------#
spatial_extent = list(
spatial_extent <- list(
west = 24.97, south = -34.30,
east = 25.87, north = -32.63
)

temporal_extent = c("2019-09-01", "2019-10-01")
temporal_extent <- c("2019-09-01", "2019-10-01")

# load sentinel data
datacube <- load_collection("SENTINEL-2-L2A", spatial_extent = spatial_extent, bands = c("B02", "B8A", "B11", "CLOUD"), temporal_extent = temporal_extent)

datacube

# regularize the data cube
datacube_reg <- ml_regularize_data_cube(data = datacube, period = "P1M", resolution = 30)

datacube_reg

# ml fit rf model
## training data from sitsdata
ml_training_sample <- sitsdata::samples_deforestation_rondonia
ml_label <- ml_training_sample$label

# rf model
ml_model <- ml_fit_class_random_forest(predictors = ml_training_sample, target = ml_label)

plot(ml_model)

# predict
classified_cube <- ml_predict(data = datacube_reg, model = ml_model)
19 changes: 12 additions & 7 deletions playground/poc_sitsdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ out_to_json <- jsonlite::toJSON(out)
## convert to from json to sits tibble on sever side
data <- jsonlite::fromJSON(out_to_json)

orig <- data |> tidyr::nest(
orig <- data |> tidyr::nest(
.by = c(longitude, latitude, start_date, end_date, label, cube),
.key = "time_series")
.key = "time_series"
)


### function to convert 'Index' column to Date
convert_index_to_date <- function(data) {
Expand All @@ -24,10 +26,13 @@ convert_index_to_date <- function(data) {


orig <- orig |>
mutate(time_series = map(time_series, convert_index_to_date),
start_date = as.Date(start_date),
end_date = as.Date(end_date))
mutate(
time_series = map(time_series, convert_index_to_date),
start_date = as.Date(start_date),
end_date = as.Date(end_date)
)

orig <- structure(orig, class = c("sits", class(orig)))

orig = structure(orig, class = c("sits", class(orig)))

all.equal(orig, samples_prodes_4classes)
all.equal(orig, samples_prodes_4classes)

0 comments on commit 2eda560

Please sign in to comment.