Skip to content

Commit

Permalink
Merge pull request #88 from carlesmila/master
Browse files Browse the repository at this point in the history
Error in tests + nndm plotting improvement
  • Loading branch information
HannaMeyer authored Mar 12, 2024
2 parents 283b997 + 157601e commit cb1416e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
12 changes: 10 additions & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,16 @@ plot.nndm <- function(x, ...){
val=sum(x$Gj<=x$phi)/length(x$Gj)))
Gj_df$Function <- "3_Gj(r)"

# Merge data for plotting
Gplot <- rbind(Gij_df, Gjstar_df, Gj_df)
# Merge data for plotting, get maxdist relevant for plotting
if(any(Gj_df$val==1)&any(Gjstar_df$val==1)&any(Gij_df$val==1)){
Gplot <- rbind(Gij_df, Gjstar_df, Gj_df)
maxdist <- max(Gplot$r[Gplot$val!=1]) + 1e-9
Gplot <- Gplot[Gplot$r <= maxdist,]
Gplot <- rbind(Gplot, data.frame(r=maxdist, val=1,
Function = c("1_Gij(r)", "2_Gjstar(r)", "3_Gj(r)")))
}else{
Gplot <- rbind(Gij_df, Gjstar_df, Gj_df)
}

# Plot
ggplot2::ggplot(Gplot) +
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-knndm.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
test_that("kNNDM works with geographical coordinates and prediction points", {

sf::sf_use_s2(TRUE)
aoi <- sf::st_as_sfc("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", crs="epsg:4326")
tpoints <- sf::st_as_sfc("MULTIPOINT ((1 1), (1 2), (2 2), (2 3), (1 4), (5 4))", crs="epsg:4326") |>
sf::st_cast("POINT")
Expand Down Expand Up @@ -72,6 +73,7 @@ test_that("kNNDM works with modeldomain and projected coordinates", {

test_that("kNNDM works with modeldomain and geographical coordinates", {

sf::sf_use_s2(TRUE)
aoi <- sf::st_as_sfc("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", crs="epsg:4326")
tpoints <- sf::st_as_sfc("MULTIPOINT ((1 1), (1 2), (2 2), (2 3), (1 4), (5 4))", crs="epsg:4326") |>
sf::st_cast("POINT")
Expand Down Expand Up @@ -108,6 +110,7 @@ test_that("kNNDM works with modeldomain and no crs", {

test_that("kNNDM works when no clustering is present", {

sf::sf_use_s2(TRUE)
aoi <- sf::st_as_sfc("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", crs="epsg:25832")

set.seed(1)
Expand Down Expand Up @@ -166,6 +169,7 @@ test_that("kNNDM works with many points and different configurations", {

test_that("kNNDM recognizes erroneous input", {

sf::sf_use_s2(TRUE)
aoi <- sf::st_as_sfc("POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))", crs="epsg:25832")
tpoints <- sf::st_as_sfc("MULTIPOINT ((1 1), (1 2), (2 2), (2 3), (1 4), (5 4))", crs="epsg:25832") |>
sf::st_cast("POINT")
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-nndm.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ test_that("NNDM detects wrong data and geometry types", {

test_that("NNDM detects different CRS in inputs", {

sf::sf_use_s2(TRUE)
set.seed(1234)
poly <- sf::st_polygon(list(matrix(c(0,0,0,50,50,50,50,0,0,0), ncol=2,
byrow=TRUE)))
Expand Down Expand Up @@ -91,6 +92,7 @@ test_that("NNDM yields the expected results for all data types", {

test_that("NNDM yields the expected results for all CRS", {

sf::sf_use_s2(TRUE)
set.seed(1234)
poly <- sf::st_polygon(list(matrix(c(0,0,0,50,50,50,50,0,0,0), ncol=2,
byrow=TRUE)))
Expand Down

0 comments on commit cb1416e

Please sign in to comment.