Skip to content

Commit

Permalink
v0.2 first big push
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikkel Roald-Arbøl committed Sep 5, 2024
1 parent 028c10b commit e3d473b
Show file tree
Hide file tree
Showing 35 changed files with 221,003 additions and 274 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@
^\.github$
^CITATION\.cff$
^README\.Rmd$
^vignettes/articles$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.Ruserdata
docs
inst/doc
*.DS_Store
11 changes: 8 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ LazyData: true
Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0)
testthat (>= 3.0.0),
here,
readxl,
stringr,
tinyplot,
tinytable,
ggplot2,
patchwork
Config/testthat/edition: 3
Imports:
cli,
dplyr,
rlang,
tidyr,
tinyplot,
tinytable,
vroom,
zoo
RoxygenNote: 7.3.2
Expand Down
7 changes: 3 additions & 4 deletions NAMESPACE
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)
55 changes: 0 additions & 55 deletions R/augment_trackball.R

This file was deleted.

44 changes: 44 additions & 0 deletions R/compute_kinematics.R
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)
}
42 changes: 42 additions & 0 deletions R/compute_statistics.R
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)
}
18 changes: 0 additions & 18 deletions R/mode.R

This file was deleted.

Loading

0 comments on commit e3d473b

Please sign in to comment.