From ab7ca3f9f8f2d942b6c4e4b2aafefdf0d51ec321 Mon Sep 17 00:00:00 2001 From: hejtmy Date: Sat, 2 Nov 2019 12:45:39 +0100 Subject: [PATCH] closes #22. Addressed #37 --- DESCRIPTION | 2 +- NAMESPACE | 2 +- R/navr-angle-calculations.R | 34 ++++++++++++++++-------- _pkgdown.yml | 10 ++----- man/angle_diff.Rd | 16 +++++++++++ man/angle_from_positions.Rd | 4 +++ man/angle_to_360.Rd | 5 ++-- man/calculate_angle_differences.Rd | 4 +-- tests/testthat/test-angle_calculations.R | 18 +++++++++++++ tests/testthat/test-getting.R | 2 +- 10 files changed, 71 insertions(+), 26 deletions(-) create mode 100644 man/angle_diff.Rd create mode 100644 tests/testthat/test-angle_calculations.R diff --git a/DESCRIPTION b/DESCRIPTION index b1682ab..d2da136 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -14,7 +14,7 @@ Roxygen: list(markdown = TRUE) Depends: ggplot2 Suggests: - testthat, + testthat (>= 2.1.0), knitr, rmarkdown, covr diff --git a/NAMESPACE b/NAMESPACE index 7e1f56f..fc54f61 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -31,12 +31,12 @@ export(add_speeds) export(add_time_columns) export(add_time_diffs) export(add_times_since_start) +export(angle_diff) export(angle_from_positions) export(angle_to_180) export(angle_to_360) export(angle_to_radian) export(animate_path) -export(calculate_angle_differences) export(calculate_distances) export(calculate_speeds) export(euclid_distance) diff --git a/R/navr-angle-calculations.R b/R/navr-angle-calculations.R index 6c0dba0..5d5cda6 100644 --- a/R/navr-angle-calculations.R +++ b/R/navr-angle-calculations.R @@ -1,20 +1,32 @@ -#' Adds new colum angle_diff_axis where it calculates angle difference between rows +#' Creates a series of angle_diffs conversted to degrees of angukar difference where it calculates angle difference between rows #' #' @param rotations vector of angles in 360 -#' -#' @export -#' -#' @example calculate_angle_differences <- function(rotations){ angle_diffs <- round(c(0, diff(rotations)), 4) angle_diffs <- angle_to_180(angle_diffs) return(angle_diffs) } -# converts positive and negative angles to 0-360 -# asumes it is not below -360 -# 390 is converted to 30, -40 to 320 etc -#' Title +#' Calculates angular difference between passed angle matrices +#' +#' @param angle1 vector of angles (in degrees) +#' @param angle2 +#' +#' @return +#' @export +#' +#' @examples +angle_diff <- function(angle1, angle2) { + if(length(angle1)!=length(angle2)){ + warning("lengths of the matrices don't match") + return(NULL) + } + return(navr::angle_to_180(angle2-angle1)) +} + +#' converts positive and negative angles to 0-360 +#' @description asumes it is not below -360 +#' 390 is converted to 30, -40 to 320 etc #' #' @param angle #' @@ -69,8 +81,8 @@ angle_to_radian <- function(angle){ #' Calculates angle from two 2d positions #' -#' @param pos_from -#' @param pos_to +#' @param pos_from numeric(2) vector of original position +#' @param pos_to numeric(2) vector of position towards the target #' @param zero_vec defines which axis should correspond to 0 degrees. defaults to c(0,1) (Y axis) #' #' @return diff --git a/_pkgdown.yml b/_pkgdown.yml index 5e171cc..80ee3d3 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -8,13 +8,7 @@ reference: - title: Preprocessing desc: Function to preprocess navr object. Adding time and angle columns, smoothing speeds etc. contents: - - '`add_angle_differences`' - - '`add_distances`' - - '`add_speeds`' - - '`add_times_since_start`' - - '`add_time_columns.navr`' - - '`add_time_columns`' - - '`add_time_diffs`' + - '`prepare_navr`' - '`add_area_boundaries`' - '`pick_unreal_speeds`' - '`remove_unreal_speeds.navr`' @@ -77,10 +71,10 @@ reference: - '`vector_from_angle`' - '`smooth_vector`' - '`angle_from_positions`' + - '`angle_diff`' - '`angle_to_180`' - '`angle_to_360`' - '`angle_to_radian`' - - '`calculate_angle_differences`' - '`calculate_distances`' - '`calculate_speeds`' - '`euclid_distance`' diff --git a/man/angle_diff.Rd b/man/angle_diff.Rd new file mode 100644 index 0000000..607eef5 --- /dev/null +++ b/man/angle_diff.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/navr-angle-calculations.R +\name{angle_diff} +\alias{angle_diff} +\title{Calculates angular difference between passed angle matrices} +\usage{ +angle_diff(angle1, angle2) +} +\arguments{ +\item{angle1}{vector of angles (in degrees)} + +\item{angle2}{} +} +\description{ +Calculates angular difference between passed angle matrices +} diff --git a/man/angle_from_positions.Rd b/man/angle_from_positions.Rd index 76ae474..cce8282 100644 --- a/man/angle_from_positions.Rd +++ b/man/angle_from_positions.Rd @@ -7,6 +7,10 @@ angle_from_positions(pos_from, pos_to, zero_vec = c(0, 1)) } \arguments{ +\item{pos_from}{numeric(2) vector of original position} + +\item{pos_to}{numeric(2) vector of position towards the target} + \item{zero_vec}{defines which axis should correspond to 0 degrees. defaults to c(0,1) (Y axis)} } \description{ diff --git a/man/angle_to_360.Rd b/man/angle_to_360.Rd index e33dc7d..d91afd5 100644 --- a/man/angle_to_360.Rd +++ b/man/angle_to_360.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/navr-angle-calculations.R \name{angle_to_360} \alias{angle_to_360} -\title{Title} +\title{converts positive and negative angles to 0-360} \usage{ angle_to_360(angle) } @@ -10,7 +10,8 @@ angle_to_360(angle) \item{angle}{} } \description{ -Title +asumes it is not below -360 +390 is converted to 30, -40 to 320 etc } \examples{ diff --git a/man/calculate_angle_differences.Rd b/man/calculate_angle_differences.Rd index b1362a9..b3df755 100644 --- a/man/calculate_angle_differences.Rd +++ b/man/calculate_angle_differences.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/navr-angle-calculations.R \name{calculate_angle_differences} \alias{calculate_angle_differences} -\title{Adds new colum angle_diff_axis where it calculates angle difference between rows} +\title{Creates a series of angle_diffs conversted to degrees of angukar difference where it calculates angle difference between rows} \usage{ calculate_angle_differences(rotations) } @@ -10,5 +10,5 @@ calculate_angle_differences(rotations) \item{rotations}{vector of angles in 360} } \description{ -Adds new colum angle_diff_axis where it calculates angle difference between rows +Creates a series of angle_diffs conversted to degrees of angukar difference where it calculates angle difference between rows } diff --git a/tests/testthat/test-angle_calculations.R b/tests/testthat/test-angle_calculations.R new file mode 100644 index 0000000..9878973 --- /dev/null +++ b/tests/testthat/test-angle_calculations.R @@ -0,0 +1,18 @@ +test_that("angle to 180 works", { + expect_equal(angle_to_180(359), -1) + expect_equal(angle_to_180(180), -180) + expect_equal(angle_to_180(179), 179) + expect_equal(angle_to_180(90), 90) + expect_equal(angle_to_180(0), 0) + expect_equal(angle_to_180(360), 0) + expect_equal(angle_to_180(-90), -90) + expect_equal(angle_to_180(-270), 90) +}) + +test_that("angle diff works", { + expect_warning(angle_diff(1:5, 330:335)) + expect_silent(res <- angle_diff(1:5, 330:334)) + expect_true(all(res == -31)) + expect_equal(angle_diff(270, 0), 90) + expect_equal(angle_diff(0, 270), -90) +}) diff --git a/tests/testthat/test-getting.R b/tests/testthat/test-getting.R index ab3470e..ecc115f 100644 --- a/tests/testthat/test-getting.R +++ b/tests/testthat/test-getting.R @@ -1,5 +1,6 @@ context("Getting and subsetting") obj <- navr_object + test_that("Data can filtered with time", { obj_filtered <- filter_times(obj, c(58227, 58242)) expect_equal(nrow(obj_filtered$data), 412) @@ -12,7 +13,6 @@ test_that("Data can filtered with time", { expect_equal(nrow(obj_filtered$data), 5091) }) - test_that("Can select time_diff", { expect_error(get_time_diffs(obj)) obj_prepped <- add_time_columns(obj)