Skip to content

Commit

Permalink
Add ad-hoc tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Robinlovelace committed Aug 19, 2024
1 parent 3187b50 commit cf2df93
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
^\.binder$
^\.vscode$
^CRAN-SUBMISSION$
^ad-hoc-tests$
10 changes: 8 additions & 2 deletions R/od-funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ od_to_sfc = function(x,

#' Create matrices representing origin-destination coordinates
#'
#' This function takes a wide range of input data types (spatial lines, points or text strings)
#' and returns a data frame of coordinates representing origin (ox, oy) and destination (dx, dy) points.
#' This function takes an 'od data frame' with the first
#' two columns matching IDs of spatial objects, and
#' matches them with objects representing origins and destinations
#' in wide range of input data types (spatial lines, points or text strings).
#' It returns a data frame of coordinates representing movement between all origin (ox, oy) and destination (dx, dy) points.
#'
#' See [points_to_od()] for a function that creates
#' an 'od data frame' from a set (or two sets) of points.
#' @param p Points representing origins and destinations
#' @param pd Points representing destinations, if different from origin points
#' @param sfnames Should output column names be compatible with the sf package?
Expand Down
1 change: 1 addition & 0 deletions ad-hoc-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/.quarto/
69 changes: 69 additions & 0 deletions ad-hoc-tests/test-max-dist-speedup.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
format: gfm
---

This document tests the new `max-dist` functionality in PR [#48](https://github.com/ITSLeeds/od/pull/48).

Let's start the test documented in the PR with the installed version of the package.

```{r}
remotes::install_cran("od")
library(sf)
```

# Test 1: 1000 points


```{r}
p = pct::get_centroids_ew()
p = p[1:1000,]
system.time(r1 <- od::points_to_od(p))
head(r1)
nrow(r1)
```

Now let's test the new `max-dist` functionality.


```{r}
if (!file.exists("DESCRIPTION")) {
setwd("..")
}
devtools::load_all()
system.time(r2 <- points_to_od(p))
head(r2)
nrow(r2)
```


```{r}
system.time(r3 <- points_to_od(p, max_dist = 1000))
head(r3)
nrow(r3)
```

The benchmark shows that the new `max-dist` functionality is faster than the original implementation for large datasets.

Let's compare the results.


```{r}
waldo::compare(head(r1), head(r2))
r2_sorted = r2 |>
dplyr::arrange(desc(O), desc(D))
r1_sorted = r1 |>
dplyr::arrange(desc(O), desc(D))
waldo::compare(head(r1_sorted), head(r2_sorted))
```

Let's plot the results for the max-dist = 1000 case.


```{r}
r3_sf = od::od_to_sf(r3, p)
plot(sf::st_geometry(p), col = "red")
plot(sf::st_geometry(r3_sf), add = TRUE)
```

# Test 2: od_coordinates
11 changes: 9 additions & 2 deletions man/od_coordinates.Rd

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

0 comments on commit cf2df93

Please sign in to comment.