-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_shift_bootstrap_functions.R
86 lines (66 loc) · 4.39 KB
/
circular_shift_bootstrap_functions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
setwd('C:/cmatlab-Roman/DivPrograms/Individual/RomanDoronin/Scripts/Analysis')
library(ggplot2)
library(plotly)
source("analyze_perches.R")
source("upload_s7s8.R")
source("ggplot-extensions.R")
path = "C:/cmatlab-Roman/DivPrograms/Individual/RomanDoronin/Data/s7s8/"
experiment = "s7s8"
#---- functions for the circular shifts plots BEGIN ----
MakeCircularShifts = function(bird_df){
# Creates dataframe with randomly circularly shifted points within each perch window
# For each perch it generates random amount of time shift and then we shift all elements within this perch on this amount
#
# Args:
# bird_df: processed dataframe (note that you need columns t1 and t2 for each element (function Preprocessing does this) which says what are the timestamps of corresponding perch)
#
# Returns:
# bird_df_shifted: identical by structure dataframe, but with modified timestamps
perch_onsets = unique(bird_df$t1) # Creates a vector of onsets of the perches where there is at least one element from bird_df
indices = match(perch_onsets, bird_df$t1)
perch_offsets = bird_df$t2[indices]
perch_durations = (perch_offsets - perch_onsets) * 3600 # Creates vector of durations of perches in order to generate random number from distribution, in sec - because we need to feed it to DFCircularShifts
sample_shifts = sapply(perch_durations, FUN = function(x) runif(1, 0, x))
map_df = data.frame(x = perch_onsets, y = sample_shifts)
shift_times = map_df$y[match(bird_df$t1, map_df$x)] # this is vector of time shifts for every element! (for each element within same perch window there is a same shift)
bird_df_shifted = DFCircularShifts(bird_df, shift_times)
return(bird_df_shifted)
}
DFCircularShifts = function(bird_df_mapped, shift_time) {
# Makes circular shifts of the timestamps of the elements within their own communication window.
# Note: here as
#
# Args:
# bird_df_mapped: processed dataframe. It is normal df, with added columns "window", "t1", "t2"
# "window": shows whether this element inside or outside perch window (need it in case if we have outsiders due to flatclust bug)
# "t1" - timestamp of the beginning of the communication window where this element was pronounced
# "t2" - timestamp of the end of the communication window where this element was pronounced
# this df can be generated by
# shift_t: time of shift in seconds - vector! It says to what amount each element should shift! these times can be different for different elements!
#
# Returns:
# input df, but with circularly shifted values
#bird_df_mapped_filtered = filter(bird_df_mapped, window == "inside") # if we will manage to fix the buf with outsiders, we won't
# need this line because all points will be inside window by definition
time_h_shifted = mapply(function(t, t1, t2, shift_t) t1 + ((t + shift_t / 3600 - t1) %% (t2 - t1)), # NOTE!!! Here I provide shift_t as a vector!!!
bird_df_mapped$time_h, bird_df_mapped$t1, bird_df_mapped$t2, shift_time) # You should feed good shift-t vector (how should we shift each element and
bird_df_mapped$time_h = time_h_shifted # for elements within same perch window these should be the same!!)
return(bird_df_mapped)
}
# Not needed really
# PlotCircularShifts = function(juvenile_df, adult_df, window_length=5, ...){
# # Code for plotting peristimulus time histogram with circular shifts on the background
# # Note: very buggy, better to rewrite (the main crap - is how to pass arguments to internal functions because different
# # functions require different arguments)
# # also in seq(-5, 5, 0.5) there are magic numbers, need to rewrite with variables
# p = CreateCumulativePlot(juvenile_df, adult_df, dt=0.15, ...)
# for (i in seq(-5, 5, 0.5)){
# shifted_adult = DFCircularShifts(adult_df, i)
# temp_stack_df = DFForStackPlot(juvenile_df, shifted_adult, ...)
# temp_cumulative_df = DFForCumulativePlot(temp_stack_df, window_length=window_length, dt=0.15)
# p = p + geom_step(data=temp_cumulative_df[temp_cumulative_df$bird == "adult", ], aes(t, y), alpha=0.4, color="indianred2")
# }
# p
# }
#
# #---- functions for the circular shifts plots END ----