Code
library(tidyverse)
library(patchwork)
library(here)
library(mocapr)
library(gridExtra)library(tidyverse)
library(patchwork)
library(here)
library(mocapr)
library(gridExtra)# Joint center data
df <- vroom::vroom(file = "C:/Users/sharsted/FreeMocap_Data/sesh_2022-09-05_15_35_17/DataArrays/body_3d_xyz.csv")
# Timestamp and frame number
df_time <- vroom::vroom(file = "C:/Users/sharsted/FreeMocap_Data/sesh_2022-09-05_15_35_17/unix_synced_timestamps.csv") %>%
select(-...1, frame = Frame) %>%
mutate(
across(starts_with("Cam"), ~na_if(., -1))) %>%
mutate(
time = rowMeans(select(., starts_with("Cam")), na.rm = TRUE),
time_sec = time - time[1]) %>%
select(-(Cam1:Cam4))
# Join jointcenter and time data
mocap_data <-
add_column(df_time, df) %>%
select(
# frame, time
frame,
time_sec,
# Shoulders
LSX = left_shoulder_x,
LSY = left_shoulder_y,
LSZ = left_shoulder_z,
RSX = right_shoulder_x,
RSY = right_shoulder_y,
RSZ = right_shoulder_z,
# Elbow
LEX = left_elbow_x,
LEY = left_elbow_y,
LEZ = left_elbow_z,
REX = right_elbow_x,
REY = right_elbow_y,
REZ = right_elbow_z,
# Wrist
LWX = left_wrist_x,
LWY = left_wrist_y,
LWZ = left_wrist_z,
RWX = right_wrist_x,
RWY = right_wrist_y,
RWZ = right_wrist_z,
# Hips
LHX = left_hip_x,
LHY = left_hip_y,
LHZ = left_hip_z,
RHX = right_hip_x,
RHY = right_hip_y,
RHZ = right_hip_z,
# Knees
LKX = left_knee_x,
LKY = left_knee_y,
LKZ = left_knee_z,
RKX = right_knee_x,
RKY = right_knee_y,
RKZ = right_knee_z,
# Ankles
LAX = left_ankle_x,
LAY = left_ankle_y,
LAZ = left_ankle_z,
RAX = right_ankle_x,
RAY = right_ankle_y,
RAZ = right_ankle_z,
# Toes
LTX = left_foot_index_x,
LTY = left_foot_index_y,
LTZ = left_foot_index_z,
RTX = right_foot_index_x,
RTY = right_foot_index_y,
RTZ = right_foot_index_z,
) The whole shot contained 361 frames, and it took a total off 11.9878414 seconds. This gives an fps of 30.1138453
Time between frames
# table of left
mocap_data %>%
mutate(time_jump = time_sec -lag(time_sec),
.after = time_sec) %>%
summarise(
mean = mean(time_jump, na.rm = TRUE),
median = median(time_jump, na.rm = TRUE),
max = max(time_jump, na.rm = TRUE),
min = min(time_jump, na.rm = TRUE)
) %>%
pivot_longer(cols = everything(),
names_to = " ",
values_to = "seconds") %>%
mutate(fps = 1/seconds) %>%
knitr::kable(caption = "Time between frames")| seconds | fps | |
|---|---|---|
| mean | 0.0332996 | 30.03043 |
| median | 0.0320214 | 31.22911 |
| max | 0.0460563 | 21.71257 |
| min | 0.0255146 | 39.19324 |
Time between frames (density plot)
# plot on right
mocap_data %>%
mutate(time_jump = time_sec -lag(time_sec),
.after = time_sec) %>%
ggplot(aes(x = time_jump))+
geom_density(fill = "grey") +
theme_minimal()+
labs(x = "Time in seconds between each frame")# plot
mocap_data %>%
mutate(time_jump = time_sec -lag(time_sec),
FPS = 1/time_jump,
.after = time_sec) %>%
ggplot(aes(x = FPS))+
geom_density(fill = "grey") +
theme_minimal()+
labs(
title = "Frames Per Second",
x = "FPS",
y = NULL)This creates an animation using the raw coordinates from the FreeMoCap calibration
mocap_data %>%
mutate(
across(ends_with("y"), ~ .x*-1),
across(ends_with("y"), ~ .x + min(mocap_data$LTY))) %>%
animate_global()This creates a front and side view relative to pelvis. Notice the pelvis is always centered.
mocap_data %>%
mutate(
across(ends_with("y"), ~ .x*-1),
across(ends_with("y"), ~ .x + min(mocap_data$LTY))) %>%
project_full_body_to_AP() %>%
animate_anatomical()This creates a front and side view relative to the direction of movement. This makes more sense with movements where the subject changes position on the floor (e.g. gait or jumps)
mov <- mocap_data %>%
mutate(
across(ends_with("y"), ~ .x*-1),
across(ends_with("y"), ~ .x + min(mocap_data$LTY))) %>%
project_full_body_to_MP(.method = "first_dist")
mov <- mov %>%
mutate(
across(ends_with("_MPR"), ~ .x -mean( (mov$LH_MPR+mov$RH_MPR)/2, na.rm = TRUE)),
across(ends_with("_MPF"), ~ .x -mean( (mov$LH_MPF+mov$RH_MPF)/2, na.rm = TRUE))
)
mov %>%
animate_movement(
line_colored_size = 2,
line_black_size = 1.5,
line_black_alpha = 0.5,
point_size = 2)mov_rep <- mov %>%
# Only show Relevent parts of the movement
# credit goes to https://www.r-bloggers.com/2020/06/detecting-streaks-in-r/
mutate(
low = (RHY+LHY)/2 < 550,
lagged = lag(low),
start = (low != lagged) & low == FALSE,
rep = cumsum(start),
.after = frame
) %>%
# This part is HORRIBLE code, work out a better way in the future
mutate(
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
rep = ifelse(lag((RHY+LHY)/2) < (RHY+LHY)/2, lead(rep), rep),
)
mov_rep %>%
ggplot(aes(x = frame, y = (RHY+LHY)/2, color = factor(rep)))+
geom_hline(yintercept = 550, linetype = 2, alpha = 0.5)+
geom_point()+
geom_line()+
labs(
y = "Height of Center of Mass",
color = "Repetition",
title = "Height of center of mass through 5xSit-to-Stand test"
)+
theme_minimal()time_elapsed <- mov_rep %>%
filter(rep != 0) %>%
group_by(rep) %>%
summarise(
time_elapsed = max(time_sec) - min(time_sec),
height_gained = ((max(RHY)+max(LHY))/2) - ((RHY[1]+LHY[1])/2)
)The Five repetitions took 8.7 seconds to complete. This some code to show if this is normal relative to age, sex etc..
This code takes the entire movement and:
- Splits the movement into repetitions (5)
- Removes the descending parts of each repetition
- Splits each repetition into five phases (0-20%, 20-40%, 40-60%, 60-80%, 80-100%)
- Plots a matrix of each repetition and phase
- The resulting shows the five repetitions, and you can see the overall body posture at each phase
STS <- mov_rep %>%
filter(rep != 0) %>%
group_by(rep) %>%
# Remove after highest point
mutate(
max_h = (RHY+LHY)/2 == max((RHY+LHY)/2 ),
frame_at_max = max(if_else(max_h == TRUE, frame, -1)),
.after = frame
) %>%
filter(frame < frame_at_max) %>%
# Remove before lowest point
mutate(
min_h = (RHY+LHY)/2 == min((RHY+LHY)/2 ),
frame_at_min = max(if_else(min_h == TRUE, frame, -1)),
.after = frame
) %>%
filter(frame > frame_at_min) %>%
# Clean up variables
select(-low, -lagged, -start,
-min_h, -frame_at_min,
-max_h, -frame_at_max) %>%
ungroup() %>%
# Split each rep into 3
group_by(rep) %>%
mutate(
rep_part = case_when(
(frame - min(frame)) / (max(frame)-min(frame)) < 0.2 ~ 1,
(frame - min(frame)) / (max(frame)-min(frame)) < 0.4 ~ 2,
(frame - min(frame)) / (max(frame)-min(frame)) < 0.6 ~ 3,
(frame - min(frame)) / (max(frame)-min(frame)) < 0.8 ~ 4,
(frame - min(frame)) / (max(frame)-min(frame)) >= 0.8 ~ 5,
TRUE ~ 0),
.after = rep)
a_df <- STS %>%
# Plot
animate_movement(
planes = "F",
row_facets = rep,
col_facets = rep_part,
line_colored = FALSE,
line_black_alpha = 0.1,
point = FALSE,
return_data = TRUE,
reduce_data = TRUE
)
min_lim <- min(a_df$U, a_df$value)
max_lim <- max(a_df$U, a_df$value)
a <- STS %>%
# Plot
animate_movement(
planes = "F",
row_facets = rep,
col_facets = rep_part,
line_colored = FALSE,
line_black_alpha = 0.1,
point = FALSE,
return_plot = TRUE) +
coord_equal(
xlim = c(min_lim, max_lim),
ylim = c(min_lim, max_lim)
)+
labs(
y = NULL
)+
theme(
axis.text = element_blank(),
axis.ticks = element_blank()
)
aThis code takes the entire movement and:
- Splits the movement into repetitions (5)
- Removes the descending parts of each repetition
- Splits each repetition into six points (those closest to 0, 0.2, 0.4, 0.6, 0.8, and 1 of the movement)
- Plots a matrix of each repetition and phase
- The resulting shows the five repetitions, and you can see the overall body posture at each point
mov_rep %>%
filter(rep != 0) %>%
group_by(rep) %>%
# Remove after highest point
mutate(
max_h = (RHY+LHY)/2 == max((RHY+LHY)/2 ),
frame_at_max = max(if_else(max_h == TRUE, frame, -1)),
.after = frame
) %>%
filter(frame < frame_at_max) %>%
# Remove before lowest point
mutate(
min_h = (RHY+LHY)/2 == min((RHY+LHY)/2 ),
frame_at_min = max(if_else(min_h == TRUE, frame, -1)),
.after = frame
) %>%
filter(frame > frame_at_min) %>%
# Clean up variables
select(-low, -lagged, -start,
-min_h, -frame_at_min,
-max_h, -frame_at_max) %>%
ungroup() %>%
# Only keep special frames
group_by(rep) %>%
mutate(
rep_part = (frame - min(frame)) / (max(frame) - min(frame)),
.after = rep) %>%
#
mutate(
min_0 = abs(rep_part-0 )== min(abs(rep_part-0)),
min_02 = abs(rep_part-0.2) == min(abs(rep_part-0.2)),
min_04 = abs(rep_part-0.4) == min(abs(rep_part-0.4)),
min_06 = abs(rep_part-0.6) == min(abs(rep_part-0.6)),
min_08 = abs(rep_part-0.8) == min(abs(rep_part-0.8)),
min_1 = abs(rep_part-1 )== min(abs(rep_part-1)),
.after = rep_part
) %>%
filter(min_0 == TRUE | min_02 == TRUE | min_04 == TRUE | min_06 == TRUE | min_08 == TRUE | min_1 == TRUE) %>%
mutate(
rep_part = row_number()
) %>%
ungroup() %>%
# Plot
animate_movement(
planes = "F",
row_facets = rep,
col_facets = rep_part,
return_plot = TRUE,
#point = FALSE,
line_colored = FALSE
) +
labs(
y = NULL,
title = "5 x STS test",
subtitle = paste("The 5 repetitions took", round(time_elapsed, 1), "seconds for the patient to complete")
)+
theme(
panel.grid.major = element_line(),
panel.grid.minor = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
)l1 <- time_elapsed[1,] %>%
as_vector() %>%
as.matrix(ncol = 1) %>%
round(2) %>%
tableGrob(theme = ttheme_minimal())
grob_list1 <- map(1:nrow(time_elapsed), ~ {
time_elapsed[.x,] %>%
as_vector() %>%
as.matrix(ncol = 1) %>%
round(2) %>%
tableGrob(theme = ttheme_minimal())
})
b <- grid.arrange(grobs = grob_list1, ncol = 1)STS_sum <- STS %>%
group_by(rep_part) %>%
summarise(
Torso_a = NA_real_,
Knee_f = NA_real_,
Ankle_dfl = NA_real_
)
l2 <- STS_sum %>%
as_vector() %>%
as.matrix(nrow = 1) %>%
round(2) %>%
tableGrob(theme = ttheme_minimal())
grob_list2 <- map(1:nrow(STS_sum), ~ {
STS_sum[.x,] %>%
as_vector() %>%
as.matrix(nrow = 1) %>%
round(2) %>%
tableGrob(theme = ttheme_minimal())
})
c <- grid.arrange(grobs = grob_list2, nrow = 1)layout <- "
aaab
aaab
aaab
ccc#"
a+b+c+
plot_layout(design = layout)+
plot_annotation(
title = "5 x Sit to Stand test",
subtitle = paste0("The test was completed in ", round(sum(time_elapsed$time_elapsed), 1), " seconds\n",
"Times for each repetition can be seen to the right\n",
"Average joint angles for the phases can be seen at the bottom (NOT COMPUTED YET - WIP")
)ggsave(
filename = here("test_combined_table.png"),
dpi = "retina",
scale = 1
)