## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ## ----setup-------------------------------------------------------------------- library(fishmechr) library(dplyr) library(tidyr) library(ggplot2) library(patchwork) ## ----------------------------------------------------------------------------- head(lampreydata) ## ----------------------------------------------------------------------------- lampreydata |> filter(frame %in% c(10, 20)) |> mutate(frame = factor(frame)) |> ggplot(aes(x = mxmm, mymm, color = frame, group = frame)) + geom_path() + coord_fixed() ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> group_by(frame) |> mutate(arclen0 = arclength(mxmm, mymm)) ## ----------------------------------------------------------------------------- lampreydata |> ungroup() |> filter(point %in% c(5, 10, 18)) |> ggplot(aes(x = t, y = arclen0, color = point)) + geom_point() ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> interpolate_points_df(arclen0, mxmm, mymm, spar = 0.2, tailmethod = 'extrapolate', .out = c(arclen='arclen', xs='mxmm_s', ys='mymm_s')) ## ----------------------------------------------------------------------------- lampreydata |> filter(frame %in% c(10, 20)) |> mutate(frame = factor(frame)) |> ggplot(aes(x = mxmm, mymm, color = frame, group = frame)) + geom_point(shape = 10) + geom_path() + geom_point(aes(x = mxmm_s, y = mymm_s), shape = 5) + coord_fixed() ## ----------------------------------------------------------------------------- fishwidth ## ----------------------------------------------------------------------------- fishwidth |> ggplot(aes(x = s, y = ammowidth)) + geom_path() + labs(x = 'Arc length (BL)', y = 'Width (BL)') ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> group_by(frame) |> mutate(width = interpolate_width(fishwidth$s, fishwidth$ammowidth, arclen)) ## ----------------------------------------------------------------------------- lampreydata |> dplyr::filter(frame == 10) ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> ungroup() |> get_midline_center_df(arclen, mxmm_s,mymm_s, width=width) lampreydata |> filter(frame %in% c(10, 20, 30, 40, 50)) |> ggplot(aes(x = mxmm_s, y = mymm_s, color = frame)) + geom_path(aes(group = frame)) + geom_point(data = ~filter(.x, point == 1), aes(x = xcom, y = ycom)) + coord_fixed() ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> group_by(frame) |> mutate(curve_ang = curvature(arclen0, mxmm_s, mymm_s), curve_xy = curvature(arclen0, mxmm_s, mymm_s, method="xy")) ## ----------------------------------------------------------------------------- lampreydata |> filter(frame %in% c(10, 20)) |> mutate(frame = factor(frame)) |> ggplot(aes(x = arclen0, color = frame, group = frame)) + geom_path(aes(y = curve_ang), linetype='solid') + geom_path(aes(y = curve_xy), linetype='dashed') ## ----------------------------------------------------------------------------- lampreydata |> filter(point %in% c(10, 18)) |> mutate(point = factor(point)) |> ggplot(aes(x = t, y = curve_ang, color = point)) + geom_path() ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> ungroup() |> mutate(mxmm_ctr = mxmm_s - xcom, mymm_ctr = mymm_s - ycom) |> get_primary_swimming_axis_df(t, mxmm_ctr, mymm_ctr, .frame=frame) ## ----------------------------------------------------------------------------- lampreydata |> filter(frame %in% c(10, 20, 30)) |> ggplot(aes(x = mxmm, y = mymm, color = frame)) + geom_path(aes(group = frame)) + geom_segment(data = ~ filter(.x, point == 20), aes(x = xcom, y = ycom, xend = xcom - 50*swimaxis_x, yend = ycom - 50*swimaxis_y)) + geom_point(data = ~ filter(.x, point == 20), aes(x = xcom, y = ycom), color = 'red') + facet_grid(frame ~ .) + coord_fixed() ## ----------------------------------------------------------------------------- lampreydata |> filter(point %in% c(10, 18)) |> mutate(point = factor(point)) |> ggplot(aes(x = t, y = exc, color = point)) + geom_path() ## ----------------------------------------------------------------------------- sampfreq <- lampreydata |> filter(frame %in% c(1, 2)) |> group_by(frame) |> summarize(t = first(t)) |> ungroup() |> summarize(sampfreq = 1 / (t[2] - t[1])) |> pull(sampfreq) sampfreq ## ----------------------------------------------------------------------------- filt <- build_filter(0.5, 15, sampfreq = sampfreq) ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> arrange(point, t) |> group_by(point) |> mutate(exc_s = apply_filter(filt, exc), curve_ang_s = apply_filter(filt, curve_ang)) ## ----------------------------------------------------------------------------- lampreydata |> filter(point %in% c(10, 18)) |> mutate(point = factor(point)) |> ggplot(aes(x = t, color = point)) + geom_path(aes(y = exc)) + geom_path(aes(y = exc_s), linetype = 'dashed') ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> arrange(frame, desc(point)) |> group_by(point) |> mutate(ph_c = hilbert_phase(curve_ang_s), ph_e = hilbert_phase(exc_s), ph_p = peak_phase(exc_s)) ## ----------------------------------------------------------------------------- lampreydata |> ungroup() |> filter(point %in% c(3, 8, 15, 18)) |> mutate(point = factor(point)) |> pivot_longer(cols = c(ph_c, ph_e, ph_p), names_to = "method", values_to = "phase") |> ggplot(aes(x = t, y = phase, color = point, linetype = method)) + geom_path() + facet_wrap(~point) ## ----------------------------------------------------------------------------- examplet <- 1.2 colors <- c("#1f78b4", "black") p1 <- lampreydata |> ungroup() |> filter(point %in% c(13, 15)) |> mutate(point = factor(point)) |> ggplot(aes(x = t, y = exc, color = point, linetype = point)) + geom_path(aes(group = point)) + geom_vline(xintercept = examplet) + scale_color_manual(values = colors) + theme_minimal() + xlim(c(1,1.5)) + labs(x = "Time (s)", y = "Excursion (mm)") + theme(legend.position="none", panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) p2 <- lampreydata |> ungroup() |> filter(point %in% c(13, 15)) |> mutate(point = factor(point), ph_e = (ph_e %% (2*pi)) / (2*pi)) |> ggplot(aes(x = t, y = ph_e, color = point, linetype = point)) + geom_path(aes(group = point)) + geom_vline(xintercept = examplet) + scale_color_manual(values = colors) + xlim(c(1,1.5)) + ylim(0,1) + theme_minimal() + labs(x = "Time (s)", y = "Phase") + theme(legend.position="none", panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) p3 <- lampreydata |> ungroup() |> mutate(point = factor(point)) |> filter(near(t, examplet, tol = 0.001)) |> ggplot(aes(y = exc, x = exc_x)) + geom_path() + geom_point(data = ~ filter(.x, point %in% c(13, 15)), aes(color = point, shape = point)) + scale_color_manual(values = colors) + scale_shape_manual(values = c(19, 5)) + coord_fixed() + theme_minimal() + theme(legend.position="none") p4 <- lampreydata |> ungroup() |> mutate(point = factor(point)) |> filter(near(t, examplet, tol = 0.001)) |> mutate(ph_eu = (gsignal::unwrap(ph_e) / (2*pi)) %% 1) |> ggplot(aes(y = ph_eu, x = arclen)) + geom_path() + #color = "#1b9e77") + geom_point(data = ~ filter(.x, point %in% c(13, 15)), aes(color = point, shape = point)) + scale_color_manual(values = colors) + scale_shape_manual(values = c(19, 5)) + ylim(0,1) + theme_minimal() + labs(x = "Arc length (mm)", y = "Phase") + theme(legend.position="none", panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) # scale_y_continuous(transform = "reverse") p1 + p3 + p2 + p4 + plot_layout(nrow = 2, heights = c(1,2)) ## ----------------------------------------------------------------------------- lampreydata |> group_by(point) |> mutate(freq_c = get_frequency(t, ph_c, method='deriv'), freq_e = get_frequency(t, ph_e, method='deriv'), freq_p = get_frequency(t, ph_p, method='deriv')) |> pivot_longer(cols = c(freq_c, freq_e, freq_p), names_to = "method", values_to = "freq") |> filter(point %in% c(3, 15, 18)) |> mutate(point = factor(point)) |> ggplot(aes(x = t, y = freq, color = point, shape = method)) + scale_shape_manual(values = c(1, 17, 22)) + geom_point() + facet_wrap(~point) ## ----------------------------------------------------------------------------- lampreydata |> filter(frame %in% c(60, 80)) |> group_by(frame) |> mutate(phbody = ph_p) |> ggplot(aes(x = arclen, y = phbody, color = frame)) + geom_path(aes(group = frame)) ## ----------------------------------------------------------------------------- lampreydata |> filter(frame %in% c(50, 60)) |> group_by(frame) |> mutate(phbody = gsignal::unwrap(ph_e)) |> ggplot(aes(x = arclen, y = phbody, color = frame)) + geom_path(aes(group = frame)) ## ----------------------------------------------------------------------------- w <- lampreydata |> arrange(frame, arclen) |> filter(frame %in% c(40)) |> group_by(frame) |> mutate(ph2 = gsignal::unwrap(ph_e), wavelen_deriv = get_wavelength(arclen, ph2, method="deriv", ignore_arclen_vals = \(s) s < 30), wavelen_slope = get_wavelength(arclen, ph2, method="slope", ignore_arclen_vals = \(s) s < 30), wavelen_cycle = get_wavelength(arclen, ph2, method="cycle", ignore_arclen_vals = \(s) s < 30, sort_arclen = FALSE), wavelen_halfcycle = get_wavelength(arclen, ph2, method="halfcycle", ignore_arclen_vals = \(s) s < 30, sort_arclen = TRUE)) w |> pivot_longer(cols = contains("wavelen"), names_to = "method", values_to = "wavelen") |> ggplot(aes(x = arclen, y = wavelen, color = method, shape = method)) + geom_point() ## ----------------------------------------------------------------------------- lampreydata |> arrange(frame, arclen) |> group_by(frame) |> mutate(phbody_e = gsignal::unwrap(ph_e), phbody_c = gsignal::unwrap(ph_c), wavelen_exc = get_wavelength(arclen, phbody_e, method="halfcycle", ignore_arclen_vals = \(s) s < 30), wavelen_curve = get_wavelength(arclen, phbody_c, method="halfcycle", ignore_arclen_vals = \(s) s < 30)) |> pivot_longer(cols = contains("wavelen"), names_to = "method", values_to = "wavelen") |> ggplot(aes(x = t)) + geom_point(aes(y = wavelen, color = arclen, shape=method)) + scale_shape_manual(values = c(15, 4)) ## ----------------------------------------------------------------------------- lampreydata |> arrange(frame, arclen) |> group_by(frame) |> mutate(phbody_e = gsignal::unwrap(ph_e), phbody_c = gsignal::unwrap(ph_c), wavelen_exc = get_wavelength(arclen, phbody_e, method="halfcycle", ignore_arclen_vals = \(s) s < 30), wavelen_curve = get_wavelength(arclen, phbody_c, method="halfcycle", ignore_arclen_vals = \(s) s < 30)) |> pivot_longer(cols = contains("wavelen"), names_to = "method", values_to = "wavelen") |> ggplot(aes(x = arclen)) + geom_point(aes(y = wavelen, color = arclen, shape=method)) + scale_shape_manual(values = c(15, 4)) + facet_wrap(~method) ## ----------------------------------------------------------------------------- lampreydata <- lampreydata |> get_body_cycle_numbers_df(ph_p, 20) |> arrange(t, point) lampreydata ## ----------------------------------------------------------------------------- lampreydata |> group_by(point, cycle) |> summarize(amp = (max(exc) - min(exc)) / 2, arclen = mean(arclen)) |> ggplot(aes(x = arclen, y = amp, color = cycle)) + geom_path(aes(group = cycle))