Skip to content

Commit

Permalink
add tests for ml processes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBrueggemann committed Feb 9, 2024
1 parent 6088723 commit ec33803
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 1 deletion.
5 changes: 4 additions & 1 deletion startLocal.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# build and install package locally (use for development)
remotes::install_local("./", dependencies = TRUE, force = TRUE)
remotes::install_local("./", dependencies = FALSE, force = TRUE)

# run test for processes of the package
test_res = devtools::test(stop_on_failure = TRUE)

# Start service
library(openeocubes)
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(openeocubes)

test_check("openeocubes")
Empty file added tests/testthat/file
Empty file.
23 changes: 23 additions & 0 deletions tests/testthat/test-apply_prediction.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("apply prediction works", {

# create Session-object for testing
config <- SessionConfig(api.port = 8000, host = "127.0.0.1")

# set workspace for testing
config$workspace.path = paste0(getwd(),"/", test_path("testData"))

# this silently returns "Session"
createSessionInstance(config)

expected_bands = c("predicted_class", "class_confidence")
expected_classes = c("apply_pixel_cube", "cube", "xptr")

datacube = test_path("testData", "train_model_test_cube.nc") |> gdalcubes::ncdf_cube()

# "myModel" must exist in the Session workspace path
prediction_datacube = spsUtil::quiet(apply_prediction_opp(datacube, model_id = "myModel"))


expect_equal(class(prediction_datacube), expected_classes)
expect_equal(names(prediction_datacube), expected_bands)
})
37 changes: 37 additions & 0 deletions tests/testthat/test-train_model.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
test_that("train model works", {

# create Session-object for testing
config <- SessionConfig(api.port = 8000, host = "127.0.0.1")

# set workspace for testing
config$workspace.path = base::paste0(base::getwd(),"/", test_path("testData"))

# this silently returns "Session"
createSessionInstance(config)

datacube = gdalcubes::ncdf_cube(test_path("testData", "train_model_test_cube.nc"))

training_data_path = test_path("testData", "classes.geojson")
training_data = sf::st_read(training_data_path, quiet = TRUE)

training_data_class_levels = base::unique(training_data$class) |>
base::as.numeric() |>
base::sort()

model = spsUtil:::quiet(train_model_opp(
data = datacube,
model_type = "RF",
labeled_polygons = training_data_path,
hyperparameters = base::list(mtry = 5, ntree = 50),
))

# get model class levels and parse Integer value
model_class_levels = base::levels(model$finalModel$y) |>
base::as.character() |>
stringr::str_extract_all("\\d+") |>
base::as.numeric() |>
base::sort()

expect_equal(class(model), c("train", "train.formula"))
expect_equal(model_class_levels, training_data_class_levels)
})
14 changes: 14 additions & 0 deletions tests/testthat/testData/classes.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"type": "FeatureCollection",
"name": "demo_training_data",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "class": "11" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.689557844550476, 51.949452468942482 ], [ 7.688892820575198, 51.949026810046234 ], [ 7.688995131956011, 51.947686746399938 ], [ 7.690990203881843, 51.948601147109812 ], [ 7.693778189008965, 51.948616912475792 ], [ 7.695517482482767, 51.949956948328605 ], [ 7.695645371708782, 51.950319539382036 ], [ 7.694443212984243, 51.951013183663179 ], [ 7.696003461541622, 51.951943280746399 ], [ 7.695798838779998, 51.952305855739226 ], [ 7.694340901603431, 51.952274327595376 ], [ 7.692499296748817, 51.950792479828458 ], [ 7.693113165033688, 51.950256480278995 ], [ 7.692192362606382, 51.949673179372205 ], [ 7.689660155931289, 51.949531294220662 ], [ 7.689557844550476, 51.949452468942482 ] ] ] } },
{ "type": "Feature", "properties": { "class": "22" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.670323304957849, 51.952810303026297 ], [ 7.670323304957849, 51.951517645492274 ], [ 7.67784319144752, 51.949279052842826 ], [ 7.679787107682945, 51.952904886260846 ], [ 7.670323304957849, 51.952810303026297 ] ] ] } },
{ "type": "Feature", "properties": { "class": "4" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.689212543640235, 51.95613636025216 ], [ 7.688266163367726, 51.954575823841481 ], [ 7.693023642575478, 51.952400440037813 ], [ 7.693611933015146, 51.95279453913448 ], [ 7.692205151528981, 51.954055632967936 ], [ 7.690619325126397, 51.954292084113142 ], [ 7.690082190377137, 51.954828035425976 ], [ 7.691156459875661, 51.956152122969122 ], [ 7.689212543640235, 51.95613636025216 ] ] ] } },
{ "type": "Feature", "properties": { "class": "33" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.697320720569576, 51.952416204068179 ], [ 7.695402382179351, 51.951092006198067 ], [ 7.697013786427139, 51.949752004262422 ], [ 7.697397454105184, 51.949893888715664 ], [ 7.697320720569576, 51.952416204068179 ] ] ] } },
{ "type": "Feature", "properties": { "class": "33" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.680976477484883, 51.949846593947818 ], [ 7.681002055330085, 51.948947983881332 ], [ 7.683099438636728, 51.948884922849665 ], [ 7.6837133069216, 51.949310583092625 ], [ 7.683687729076395, 51.950020007852835 ], [ 7.680976477484883, 51.949846593947818 ] ] ] } },
{ "type": "Feature", "properties": { "class": "22" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.689979878996324, 51.952778775237121 ], [ 7.689928723305917, 51.951785638530744 ], [ 7.691898217386547, 51.9512338864117 ], [ 7.692256307219387, 51.952195507138718 ], [ 7.689979878996324, 51.952778775237121 ] ] ] } },
{ "type": "Feature", "properties": { "class": "5" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7.697371876259982, 51.949468234009203 ], [ 7.696860319355923, 51.949231757426539 ], [ 7.695862783393008, 51.949736239295468 ], [ 7.695786049857396, 51.949625884371564 ], [ 7.696783585820314, 51.949089870878382 ], [ 7.697474187640793, 51.949310583092625 ], [ 7.697371876259982, 51.949468234009203 ] ] ] } }
]
}
Binary file added tests/testthat/testData/cube.nc
Binary file not shown.
Binary file added tests/testthat/testData/myModel.rds
Binary file not shown.
Binary file added tests/testthat/testData/train_model_test_cube.nc
Binary file not shown.

0 comments on commit ec33803

Please sign in to comment.