-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mikkel Roald-Arbøl
committed
Sep 5, 2024
1 parent
028c10b
commit e3d473b
Showing
35 changed files
with
221,003 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,4 @@ | |
^\.github$ | ||
^CITATION\.cff$ | ||
^README\.Rmd$ | ||
^vignettes/articles$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ | |
.Ruserdata | ||
docs | ||
inst/doc | ||
*.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(augment_trackball) | ||
export(mode) | ||
export(read_trackball_data) | ||
export(compute_kinematics) | ||
export(read_trackball) | ||
export(smooth_track) | ||
import(dplyr) | ||
importFrom(cli,cli_abort) | ||
importFrom(rlang,":=") | ||
importFrom(rlang,.data) | ||
importFrom(stats,median) | ||
importFrom(tidyr,replace_na) | ||
importFrom(vroom,vroom) | ||
importFrom(zoo,rollmean) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#' Compute kinematics | ||
#' | ||
#' @description | ||
#' Compute kinematics. | ||
#' | ||
#' @param data Data frame | ||
#' | ||
#' @return An data frame data frame with kinematics computed | ||
#' @export | ||
#' @import dplyr | ||
#' @importFrom rlang := | ||
#' @importFrom rlang .data | ||
#' | ||
compute_kinematics <- function( | ||
data | ||
) { | ||
# We first temporarily back-compute from our xy coordinates to the distances (dx, dy) covered between each observation (which is what we got from the sensors initially) | ||
data <- data |> | ||
dplyr::mutate(dx = .data$x - lag(.data$x), | ||
dy = .data$y - lag(.data$y), | ||
dt = .data$time - lag(.data$time)) | ||
|
||
# Find the sampling rate | ||
sampling_rate <- round(1 / median(data$dt, na.rm = TRUE)) | ||
|
||
# Compute kinematics | ||
data <- data |> | ||
dplyr::mutate(distance = if_else(.data$dx^2 > 0 & .data$dy^2 > 0, sqrt(.data$dx^2 + .data$dy^2), 0), | ||
v_translation = .data$distance * sampling_rate, | ||
direction = if_else(.data$dx^2 > 0 & .data$dy^2 > 0, atan2(.data$dx, .data$dy), 0), | ||
direction = if_else(.data$dy == 0 | .data$dy == 0, NA, direction), | ||
rotation = direction - lag(direction), | ||
v_rotation = .data$rotation * sampling_rate, | ||
# We change the directions to stay within 2pi only here, otherwise rotation becomes harder to alculate | ||
direction = if_else(.data$direction < 0, .data$direction + 2*pi, .data$direction), # Keep direction between 0 and 2*pi | ||
direction = zoo::na.locf(.data$direction, na.rm = FALSE), | ||
) | ||
|
||
# Remove leftover columns | ||
data <- data |> | ||
select(-c("dx", "dy", "dt")) | ||
|
||
return(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#' Compute summary statistics | ||
#' | ||
#' @description | ||
#' Compute summary statistics for tracks | ||
#' | ||
#' @param data A kinematics data frame | ||
#' @param threshold_velocity Choose which observations to use based on the velocity. A number, "auto" or "none". Can take a number (e.g. estimated from histograms) or "auto". "auto" fits a density function to the velocities and tries to identify a local minimum between the first and second local maxima, and uses that as the threshold. "none" keeps all observations. | ||
#' | ||
#' @return An data frame data frame with kinematics computed | ||
#' @export | ||
#' @import dplyr | ||
#' @importFrom rlang := | ||
#' @importFrom rlang .data | ||
#' | ||
compute_statistics <- function( | ||
data, | ||
threshold_velocity | ||
) { | ||
# Make some tests to ensure that `compute_kinematics` has been run first | ||
|
||
# Make sure to remove observations with almost no movement (figure out a robust method for this) | ||
|
||
# Compute translational and rotational separately (maybe?) and gather at the end | ||
data <- data |> | ||
dplyr::summarise() | ||
|
||
# dplyr::mutate(distance = sqrt(.data$dx^2 + .data$dy^2), | ||
# v_translation = .data$distance * sampling_rate, | ||
# direction = atan2(.data$dx, .data$dy), | ||
# direction = if_else(.data$dy == 0 | .data$dy == 0, NA, direction), | ||
# direction = if_else(.data$direction < 0, .data$direction + 2*pi, .data$direction), # Keep direction between 0 and 2*pi | ||
# direction = zoo::na.locf(.data$direction, na.rm = FALSE), | ||
# rotation = direction - lag(direction), | ||
# # rotation = dplyr::if_else(abs(.data$dx) > 1 & abs(.data$dy) > 1, | ||
# # abs(dplyr::lag(.data$direction) - .data$direction), | ||
# # 0), | ||
# # rotation = dplyr::if_else(.data$rotation > pi, 2*pi - .data$rotation, .data$rotation), | ||
# v_rotation = .data$rotation * sampling_rate, | ||
# ) | ||
|
||
return(data) | ||
} |
Oops, something went wrong.