Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rebase dev #96

Merged
merged 58 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
56813a7
Merge pull request #79 from roaldarbol/dev
roaldarbol Dec 7, 2024
57d6b36
Lots of new functions! Still rough
Dec 12, 2024
72cd715
Mostly docstrings and a few modifications.
Dec 12, 2024
db721a8
A few name changes
Dec 12, 2024
33f4790
Log y-axis on check_poses
Dec 12, 2024
b95b92a
Fix. Also silence ggplot
Dec 12, 2024
e39fc92
Add reference_keypoint to check_pose
Dec 12, 2024
622b533
Add doc and export to replace_na
Dec 12, 2024
143d137
Allow na_interpolation to return an unfiltered data frame with a warning
Dec 12, 2024
d817a39
Pathc to last commit
Dec 12, 2024
d692715
Another small patch
Dec 12, 2024
efcbe5b
Allow plotting of all NAs in check_na_timing
Dec 12, 2024
0118c58
Add min_obs parameter to smooth_movement
Dec 12, 2024
4d752ea
Hopefully improve speed of translate_coords_keypoint
Dec 13, 2024
9e47f16
lots of new functions and test data moved
Dec 14, 2024
d40e5e0
Just filename changes
Dec 14, 2024
906c0f1
Just docs and patches to ensure successful building
Dec 14, 2024
530e868
Add better read_trex docstring
Dec 14, 2024
eb9cb2b
Merge pull request #87 from roaldarbol/main
roaldarbol Dec 14, 2024
4b835ab
Expose and add documentation to set_individual and set_framerate
Dec 15, 2024
6a304fd
Add imports
Dec 15, 2024
91afc51
Expport set_ functions and update get_example_data
Dec 15, 2024
0177652
Add to NAMESPACE
Dec 15, 2024
c58f8ca
Fix time series plots when all values are NA
Dec 15, 2024
4904fdd
Tiny patch
Dec 15, 2024
be0c861
Add peak/trough detection
Dec 16, 2024
7111681
Great improvements the extrema detection functions. Also lots of test…
Dec 16, 2024
004aa31
Add movement classification
Dec 16, 2024
5a12cce
Export classification
Dec 16, 2024
2d5e44d
And add it to NAMESPACE
Dec 16, 2024
add149a
Merge branch 'everything_everywhere_all_at_once' of https://github.co…
Dec 16, 2024
6b71fbc
Fix bug in filter_by_speed
Dec 17, 2024
4f1e22e
Add NA tests
Dec 17, 2024
b547bfd
Bug fix for calculate_kinematics - added group_by keypoint and indivi…
Dec 17, 2024
679d30c
Fix set_framerate so it detects whether a frame rate has previously b…
Dec 17, 2024
a497269
Add bandwidth filters
Dec 19, 2024
d5b0ec6
Updates to the classification functions
Dec 19, 2024
414e90b
Changed method names in smooth_movement function
Dec 19, 2024
eb39902
Added NA testing for filter_by_speed
Dec 19, 2024
fe972ff
Add return_type parameter
Dec 19, 2024
1d35cfc
Add return_type parameter
Dec 19, 2024
4685c92
Merge branch 'everything_everywhere_all_at_once' of https://github.co…
Dec 19, 2024
d746502
Clean-up
Dec 19, 2024
ca26260
Patch
Dec 19, 2024
b05dca9
Another patch
Dec 19, 2024
6a1a08c
Patch again
Dec 19, 2024
65c696d
Improved bandwidth filters
Dec 20, 2024
18e7661
Adds Kalman filters
Dec 20, 2024
d9d1ca5
Add rotation of coordinates and egocentric transformation
Dec 21, 2024
ff8ebdc
Alignment of timeseries and classification w peak+trough
Dec 25, 2024
383aa8d
Improve detection of active periods
Dec 25, 2024
e0589f2
Adds replace_na functions and classify_low_periods
Dec 26, 2024
f8e440e
Filtering functions
Jan 3, 2025
d4b790d
Calculations
Jan 3, 2025
0492954
Filter NA functions
Jan 3, 2025
e64e9fa
The rest
Jan 3, 2025
0c2bba8
Update version
Jan 3, 2025
635e699
Merge pull request #94 from roaldarbol/everything_everywhere_all_at_once
roaldarbol Jan 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add rotation of coordinates and egocentric transformation
  • Loading branch information
Mikkel Roald-Arbøl committed Dec 21, 2024
commit d9d1ca5d474f187d710125519d9312697d210528
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,14 @@ export(read_trackball)
export(read_treadmill)
export(read_trex)
export(replace_missing)
export(rotate_coords)
export(set_framerate)
export(set_individual)
export(set_start_datetime)
export(set_uuid)
export(smooth_by_savgol)
export(smooth_movement)
export(transform_to_egocentric)
export(translate_coords)
export(validate_animalta)
export(validate_deeplabcut_csv)
Expand Down
167 changes: 160 additions & 7 deletions R/coordinates_transform.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
#'
#' Translates coordinates in Cartesian space. Takes either a single point
#' (`to_x` and `to_y`), a vector with the same length as the time dimension or a
#' keypoint (`to_keypoint`), which can be used to tranform the data into an
#' keypoint (`to_keypoint`), which can be used to transform the data into an
#' egocentric reference frame.
#'
#'
#' @param data movement data frame
#' @param to_x x coordinates; either a single value or a time-length vector.
#' @param to_y y coordinates; either a single value or a time-length vector.
#' @param to_z z coordinates (only if 3D); either a single value or a time-length vector.
#' @param to_keypoint all other coordinates becomes relative to this keypoint.
#' @param data movement data frame with columns: time, individual, keypoint, x, y
#' @param to_x x coordinates; either a single value or a time-length vector
#' @param to_y y coordinates; either a single value or a time-length vector
#' @param to_z z coordinates (only if 3D); either a single value or a time-length vector
#' @param to_keypoint all other coordinates becomes relative to this keypoint
#'
#' @return movement data frame with translated coordinates
#' @export
Expand Down Expand Up @@ -88,6 +87,160 @@ translate_coords_vector <- function(data, to_x, to_y, to_z=NULL){
return(data)
}

#' Rotate coordinates in Cartesian space
#'
#' @description
#' `r lifecycle::badge('experimental')`
#'
#' Rotates coordinates in Cartesian space based on two alignment points.
#' The rotation aligns these points either with the 0-degree axis (parallel)
#' or makes them perpendicular to it. This is particularly useful for
#' creating egocentric reference frames or standardizing orientation across
#' multiple frames or individuals.
#'
#' @param data movement data frame with columns: time, individual, keypoint, x, y
#' @param alignment_points character vector of length 2 specifying the keypoint names
#' to use for alignment
#' @param align_perpendicular logical; if TRUE, alignment_points will be rotated to be
#' perpendicular to the 0-degree axis. If FALSE (default), alignment_points
#' will be rotated to align with the 0-degree axis
#'
#' @details
#' The function processes each individual separately and maintains their independence.
#' For each time point, it:
#' 1. Calculates the vector between the alignment points
#' 2. Determines the current angle of this vector
#' 3. Rotates all points to achieve the desired alignment
#'
#' @return movement data frame with rotated coordinates
#' @export
rotate_coords <- function(
data,
alignment_points, # Two keypoint names to use for alignment
align_perpendicular = FALSE # If TRUE, alignment_points will be made perpendicular to 0°
) {
# Input validation
if (length(alignment_points) != 2) {
stop("alignment_points must contain exactly 2 keypoint names")
}
if (!all(alignment_points %in% unique(data$keypoint))) {
stop("Some specified keypoints not found in data")
}

# Process each individual separately
individuals <- unique(data$individual)
out_data <- data.frame()

for (ind in individuals) {
ind_data <- data |> filter(individual == ind)

# Get all coordinates of alignment points for this individual
p1 <- ind_data |>
filter(keypoint == alignment_points[1]) |>
select(time, x, y) |>
rename(x1 = x, y1 = y)

p2 <- ind_data |>
filter(keypoint == alignment_points[2]) |>
select(time, x, y) |>
rename(x2 = x, y2 = y)

# Calculate rotation angles for each time point
angles <- p1 |>
left_join(p2, by = "time") |>
mutate(
# Calculate vector between alignment points
vec_x = x2 - x1,
vec_y = y2 - y1,
# Calculate current angle and needed rotation
current_angle = atan2(vec_y, vec_x),
target_angle = if_else(isTRUE(align_perpendicular), pi/2, 0),
rotation_angle = target_angle - current_angle
) |>
select(time, rotation_angle)

# Apply rotation to all points for this individual
ind_rotated <- ind_data |>
left_join(angles, by = "time") |>
mutate(
x_new = x * cos(rotation_angle) - y * sin(rotation_angle),
y_new = x * sin(rotation_angle) + y * cos(rotation_angle)
) |>
select(-rotation_angle, -x, -y) |>
rename(x = x_new, y = y_new)

out_data <- bind_rows(out_data, ind_rotated)
}

return(out_data |> arrange(time, individual, keypoint))
}

#' Transform coordinates to egocentric reference frame
#'
#' @description
#' `r lifecycle::badge('experimental')`
#'
#' Transforms Cartesian coordinates into an egocentric reference frame through
#' a two-step process: translation followed by rotation. First translates all
#' coordinates relative to a reference keypoint, then rotates the coordinate
#' system based on specified alignment points.
#'
#' @param data movement data frame with columns: time, individual, keypoint, x, y
#' @param to_keypoint character; keypoint to use as the new origin
#' @param alignment_points character vector of length 2 specifying the keypoint names
#' to use for alignment
#' @param align_perpendicular logical; if TRUE, alignment_points will be rotated to be
#' perpendicular to the 0-degree axis. If FALSE (default), alignment_points
#' will be rotated to align with the 0-degree axis
#'
#' @details
#' This function combines translation and rotation to create an egocentric reference
#' frame. It:
#' 1. Translates all coordinates relative to the specified keypoint (to_keypoint)
#' 2. Rotates the coordinate system based on the alignment points
#'
#' The translation makes the reference keypoint the new origin (0,0), while the
#' rotation standardizes the orientation. This is particularly useful for:
#' * Creating egocentric reference frames
#' * Standardizing pose data across frames or individuals
#' * Analyzing relative motion patterns
#'
#' @examples
#' \dontrun{
#' # Transform coordinates to make nose the origin and align body axis
#' transformed_data <- transform_to_egocentric(
#' data,
#' to_keypoint = "nose",
#' alignment_points = c("nose", "tail"),
#' align_perpendicular = FALSE
#' )
#'
#' # Transform to make nose origin and ears perpendicular to forward axis
#' transformed_data <- transform_to_egocentric(
#' data,
#' to_keypoint = "nose",
#' alignment_points = c("ear_left", "ear_right"),
#' align_perpendicular = TRUE
#' )
#' }
#'
#' @return movement data frame in egocentric reference frame
#' @export
transform_to_egocentric <- function(
data,
to_keypoint, # Reference point for translation
alignment_points, # Two keypoint names to use for alignment
align_perpendicular = FALSE # If TRUE, alignment_points will be made perpendicular to 0°
) {
# First translate
translated_data <- translate_coords(data, to_keypoint = to_keypoint)

# Then rotate
transformed_data <- rotate_coords(translated_data, alignment_points, align_perpendicular)

return(transformed_data)
}

#' Map from Cartesian to polar coordinates
#'
#' @param data movement data frame with Cartesian coordinates
Expand Down
39 changes: 39 additions & 0 deletions man/rotate_coords.Rd

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

72 changes: 72 additions & 0 deletions man/transform_to_egocentric.Rd

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

12 changes: 6 additions & 6 deletions man/translate_coords.Rd

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

4 changes: 2 additions & 2 deletions man/translate_coords_keypoint.Rd

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

8 changes: 4 additions & 4 deletions man/translate_coords_vector.Rd

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