Skip to content

Commit

Permalink
Improved code and added README
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikkel Roald-Arbøl committed Mar 17, 2024
1 parent f0ff1d1 commit 2cd48ea
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
^renv\.lock$
^.*\.Rproj$
^\.Rproj\.user$
^codecov\.yml$
^LICENSE\.md$
5 changes: 4 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Author: Who wrote it
Maintainer: The package maintainer <yourself@somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Suggests:
testthat (>= 3.0.0)
Config/testthat/edition: 3
2 changes: 2 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2024
COPYRIGHT HOLDER: trackballr authors
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# MIT License

Copyright (c) 2024 trackballr authors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
23 changes: 14 additions & 9 deletions R/augment_trackball.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,49 @@
#' @param data
#' @param x
#' @param y
#' @param time Which variable contains time. If no column includes time, use sampling_rate.
#' @param sampling_rate
#' @param rollmean_k
#' @param mouse_dcpm
#'
#' @return
#' @export
#' @import dplyr
#' @importFrom tidyr replace_na
#' @importFrom zoo rollmean
#'
#' @examples
augment_trackball <- function(
data,
x,
y,
x = y_Right,
y = y_Left,
time = NULL,
sampling_rate = 125,
rollmean_k = 30,
mouse_dcpm = 394
) {
data <- data %>%
data <- data |>
mutate(row = row_number(),
time = row / sampling_rate,
x = y_Right,
x = zoo::rollmean(x, k = rollmean_k, fill = NA),
y = y_Left,
y = zoo::rollmean(y, k = rollmean_k, fill = NA),
cum_x = 0,
cum_y = 0) %>%
select(!c(1:4)) %>%
cum_y = 0) |>
select(!c(1:4)) |>
mutate(distance = sqrt(x^2 + y^2) / mouse_dpcm,
v_translation = distance * sampling_rate,
direction = atan2(y,x),
rotation = if_else(abs(x) > 1 & abs(y) > 1,
abs(lag(direction) - direction),
0),
rotation = if_else(rotation > pi, 2*pi - rotation, rotation),
v_rotation = rotation * sampling_rate) %>%
v_rotation = rotation * sampling_rate) |>
mutate(
x = replace_na(x, 0),
y = replace_na(y, 0))
# filter(distance < 1000) # Filters away trials covering less than 1000 pixels
x = tidyr::replace_na(x, 0),
y = tidyr::replace_na(y, 0))

data$cum_x <- cumsum(data$x)
data$cum_y <- cumsum(data$y)
return(data)
Expand Down
5 changes: 3 additions & 2 deletions R/mode.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#' Title
#' Mode
#'
#' @param x A vector of numbers
#'
Expand All @@ -7,5 +7,6 @@
#'
#' @examples
mode <- function(x){
which.max(tabulate(x))
y <- tabulate(x)
which(y == max(y, na.rm = TRUE))
}
9 changes: 6 additions & 3 deletions R/read_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
#' @export
#'
#' @examples
read_trackball_from_rdata <- function(
data,
sensor_names = c("Left", "Right")
read_trackball_data <- function(
folder_path,
sensor_names = c("Left", "Right"),
format = "RDS"
){



}
67 changes: 67 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# trackballr

<!-- badges: start -->
[![Codecov test coverage](https://codecov.io/gh/roaldarbol/trackballr/branch/main/graph/badge.svg)](https://app.codecov.io/gh/roaldarbol/trackballr?branch=main)
<!-- badges: end -->

The goal of {trackballr} is to make analysis of trackball data easy-

## Installation

You can install the development version of trackballr from [GitHub](https://github.com/) with:

``` r
# install.packages("devtools")
devtools::install_github("roaldarbol/trackballr")
```
or from my -universe:
```
install.packages("trackballr", repos = c("https://roaldarbol.r-universe.dev", "https://cran.r-project.org"))
```

## Example

This is a basic example which shows you how to solve a common problem:

``` r
library(trackballr)

## Read all files into list
data_list <- read_trackball_data(folder_path, sensors = c("Right", "Left"))

# Augment all data in list
data_list <- lapply(data_list, beetle_augment)

# Bind list into single data frame
data_df <- bind_rows(sensor_data_temp)
```

Once the data has been pre-processed, it can then easily generate useful summaries - here are some examples.
```r
# Compute translational summary
sensor_summary <- sensor_data_df |>
na.omit() |>
group_by(id, date) |>
filter(abs(x) > 0 | abs(y) > 0) |> # Only keep rows containing movement
summarise(total_translation = sum(distance),
v_translation_mean = mean(v_translation),
v_translation_sd = sd(v_translation),
sinuosity = sqrt(last(cum_x)^2 + last(cum_y)^2)/sum(distance)
) |>
mutate(trial = if_else(date <= min(date), "first", "second")) |> # Each animal has been on the trackball on two days - here we assign which day/trial
slice(1:2) |>
filter(total_translation > 0) |> # Filter away trials with no translation
filter(n() == 2) # Keep only observations where both trials are present

# Compute rotational summary
# Here wa are also filtering out trials with a faulty sensor/no data for one sensor
rotation_summary <- sensor_data_df |>
na.omit() |>
group_by(id, date) |>
filter(v_rotation > 0) |>
summarise(total_rotation = sum(rotation, na.rm = TRUE),
v_rotation_mean = mean(v_rotation, na.rm = TRUE)) |>
mutate(trial = if_else(date <= min(date), "first", "second")) |>
slice(1:2) |>
filter(n() == 2)
```
14 changes: 14 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
comment: false

coverage:
status:
project:
default:
target: auto
threshold: 1%
informational: true
patch:
default:
target: auto
threshold: 1%
informational: true
12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(trackballr)

test_check("trackballr")
14 changes: 14 additions & 0 deletions tests/testthat/test-mode.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test_that("Mode works!", {

# Single outcome
my_single_vector <- c(1,2,3,3,4,5)
expect_equal(trackballr::mode(my_single_vector), 3)

# Multiple outcomes
my_multi_vector <- c(1,2,3,3,4,5,5)
expect_equal(trackballr::mode(my_multi_vector), 3)

# Removes NA
my_na_vector <- c(1,2,3,3,NA,5)
expect_equal(trackballr::mode(my_na_vector), 3)
})

0 comments on commit 2cd48ea

Please sign in to comment.