-
Notifications
You must be signed in to change notification settings - Fork 1
/
easy_plot.R
370 lines (324 loc) · 11.5 KB
/
easy_plot.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
library(dygraphs)
library(ggplot2)
library(reshape2)
library(plotly)
# Lists for saving of data from multiple test flys
even_flylist <- list()
odd_flylist <- list()
global_data <- function() {
rawdatafile <<- file.choose()
name <<- basename(rawdatafile)
readdata <<- read.csv(rawdatafile, header = FALSE, sep = " ")
flydata <<- readdata[c(1)]
arenadata <<- readdata[c(2)]
time <- readdata[c(3)]
perioddata <<- readdata[c(4)]
rawdata0 <<- readdata[c(3, 1, 2 ,4)]
rawdata <<- cbind(time, flydata, arenadata, perioddata)
}
# accumulating of all even/odd datapoints in lists (no time reference) and calculating averages
even_odd_sorting <- function(perioddata, flydata) {
fly_even <- list()
fly_odd <- list()
i <- 1
a <- 1
b <- 1
while (TRUE) {
if (perioddata[i, c(1)] %% 2 == 0) {
fly_even[[a]] <- flydata[i, c(1)]
a = a +1
i = i +1
}
else {
fly_odd[[b]] <- flydata[i, c(1)]
b = b +1
i = i +1
}
}
average_even_period = Reduce("+",fly_even)/length(fly_even)
average_odd_period = Reduce("+",fly_odd)/length(fly_odd)
print(average_even_period)
print(average_odd_period)
general_average = (average_odd_period + average_even_period) / 2
print(general_average)
average_difference = average_odd_period - average_even_period
print(average_difference)
}
# removes the voltage jumps in the arena data and gives proper column names
arenadata_smoothing <- function(rawdata) {
names(rawdata) <- c("time", "fly", "arena", "period")
arena_min <- min(rawdata[7400:7500, c(3)])
for (i in 1:nrow(rawdata)) {
if (rawdata[i, c(3)] > (arena_min + 3.5)) {
rawdata[i, c(3)] <- rawdata[i-1, c(3)]
}
}
rawdata <<- rawdata
}
# sorting of the rawdata by period into one list: periodlist
period_sorting <- function(rawdata){
periodcount = 0
row = 1
periodlist <- list()
rawdata_max = nrow(rawdata)
while (row <= rawdata_max) {
periodname <- paste("Period", periodcount, sep = "")
d = 1
temp <- list()
while ((rawdata[row, c(4)] == periodcount)) {
temp[[d]] <- rawdata[row, c(2)]
d = d +1
row = row +1
if (is.na(rawdata[row, c(4)])) {break}
}
periodlist[[periodname]] <- temp
periodcount = periodcount +1
}
periodlist <<- periodlist
# figure the length of the smallest list in "periodlist" out to avoid stepping over list boundaries
templist <- list()
for (i in 1:length(periodlist)) {
templist[[i]] <- length(periodlist[[i]])
}
max_perioddata <<- min(unlist(templist))
}
# merging of all even Periods into one List: merged_even_periods
data_merging_even_period <- function(rawdata) {
period_sorting(rawdata)
i = 1
x = 1
a = 1
merged_even_periods <<- list()
for (x in 1:max_perioddata) {
point = 0
count = 0
for (i in seq(from=1, to=length(periodlist), by=2)) {
point = point + periodlist[[i]][[x]]
count = count +1
}
average = point/count
merged_even_periods[[a]] <<- average
a = a +1
}
}
# merging of all odd Periods into one List: merged_odd_periods
data_merging_odd_period <- function(rawdata) {
period_sorting(rawdata)
i = 1
x = 1
a = 1
merged_odd_periods <<- list()
for (x in 1:max_perioddata) {
point = 0
count = 0
for (i in seq(from=2, to=length(periodlist), by=2)) {
point = point + periodlist[[i]][[x]]
count = count +1
}
average = point/count
merged_odd_periods[[a]] <<- average
a = a +1
}
}
# exercise plotting Function for all even periods combined, write merged data to list
even_merged_plot <- function() {
data_merging_even_period(rawdata)
mean <- do.call("rbind", merged_even_periods)
freq <- c(1:length(merged_even_periods))
data <- as.data.frame(mean)
plotdata <- cbind(data, freq)
print(
ggplot(plotdata, aes(x=freq, y=V1)) +
geom_line(aes(y=V1)) +
ggtitle("Powerspectra") +
geom_smooth()
)
# save the merged data in the flylist for future mergign of different flys
even_flylist[[name]] <<- as.list(mean)
}
# exercise plotting Function for all odd periods combined, write merged data to list
odd_merged_plot <- function() {
data_merging_odd_period(rawdata)
mean <- do.call("rbind", merged_odd_periods)
freq <- c(1:length(merged_odd_periods))
data <- as.data.frame(mean)
plotdata = cbind(data, freq)
print(
ggplot(plotdata, aes(x=freq, y=V1)) +
geom_line(aes(y=V1)) +
ggtitle("Powerspectra") +
geom_smooth()
)
# save the merged data in the flylist for future mergign of different flys
odd_flylist[[name]] <<- as.list(mean)
}
# plottiong Function for all even periods seperated in one Graph
even_period_plot <- function() {
arenadata_smoothing(rawdata)
period_sorting(rawdata)
n <- as.list(c(1:max_perioddata))
time <- do.call("rbind", n)
period0 <- as.list(periodlist[[1]][1:max_perioddata])
period2 <- as.list(periodlist[[3]][1:max_perioddata])
period4 <- as.list(periodlist[[5]][1:max_perioddata])
period6 <- as.list(periodlist[[7]][1:max_perioddata])
period8 <- as.list(periodlist[[9]][1:max_perioddata])
plotdata <- as.data.frame(time)
plotdata["period0"] <- unlist(period0)
plotdata["period2"] <- unlist(period2)
plotdata["period4"] <- unlist(period4)
plotdata["period6"] <- unlist(period6)
plotdata["period8"] <- unlist(period8)
plotdata_new <- melt(plotdata, id = c("V1"))
names(plotdata_new) <- c("time", "period", "fly")
print(
ggplot(plotdata_new, aes(x=time, y=fly)) +
geom_line(aes(color = period)) +
geom_smooth() +
ggtitle("Even period plot") #+
#geom_smooth()
)
}
# plottiong Function for all odd periods seperated in one Graph
odd_period_plot <- function() {
arenadata_smoothing(rawdata)
period_sorting(rawdata)
n <- as.list(c(1:max_perioddata))
time <- do.call("rbind", n)
period0 <- as.list(periodlist[[2]][1:max_perioddata])
period2 <- as.list(periodlist[[4]][1:max_perioddata])
period4 <- as.list(periodlist[[6]][1:max_perioddata])
period6 <- as.list(periodlist[[8]][1:max_perioddata])
period8 <- as.list(periodlist[[10]][1:max_perioddata])
plotdata <- as.data.frame(time)
plotdata["period1"] <- unlist(period0)
plotdata["period3"] <- unlist(period2)
plotdata["period5"] <- unlist(period4)
plotdata["period7"] <- unlist(period6)
plotdata["period9"] <- unlist(period8)
plotdata_new <- melt(plotdata, id = c("V1"))
names(plotdata_new) <- c("time", "period", "fly")
print(
ggplot(plotdata_new, aes(x=time, y=fly)) +
geom_line(aes(color = period)) +
geom_smooth() +
ggtitle("Odd period plot") #+
#geom_smooth()
)
}
# even_period_plot2 <- function(rawdata) {
# rawdata <- arenadata_smoothing(rawdata)
# print(
# ggplot(rawdata, aes(x=time, y=fly, colour=period)) +
# geom_point() +
# ggtitle("Powerspectra") #+
# #geom_smooth()
# )
# }
# simple plotting function with dygraphs for the whole rawdata
single_fly_plot <- function() {
arenadata_smoothing(rawdata)
dygraph(rawdata, main = "test graph") %>%
dyRangeSelector() %>%
dyRoller(showRoller = TRUE, rollPeriod = 0) %>%
dySeries("fly", label = "Fly", color = "darkred") %>%
dySeries("arena", label = "Arena") %>%
dySeries("period", label = "Period") %>%
dyAxis("y", label = "Voltage (V)") %>%
dyAxis("x", label = "Time (millisec.)")
}
################ FINAL EVALUATION OF ALL FLYS ####################
# merging of the merged even periods from all test flys into one List: merged_even_flys
data_merging_even_flys <- function(rawdata) {
i = 1
x = 1
a = 1
merged_even_flys <<- list()
# figure the length of the smallest list in "even_flylist" out to avoid stepping over list boundaries
templist <- list()
for (i in 1:length(even_flylist)) {
templist[[i]] <- length(even_flylist[[i]])
}
max_even_flydata <<- min(unlist(templist))
for (x in 1:max_even_flydata) {
point = 0
count = 0
for (i in seq(from=1, to=length(even_flylist), by=2)) {
point = point + even_flylist[[i]][[x]]
count = count +1
}
average = point/count
merged_even_flys[[a]] <<- average
a = a +1
}
}
# merging of the merged odd periods from all test flys into one List: merged_odd_flys
data_merging_odd_flys <- function(rawdata) {
i = 1
x = 1
a = 1
merged_odd_flys <<- list()
# figure the length of the smallest list in "odd_flylist" out to avoid stepping over list boundaries
templist <- list()
for (i in 1:length(odd_flylist)) {
templist[[i]] <- length(odd_flylist[[i]])
}
max_odd_flydata <<- min(unlist(templist))
for (x in 1:max_odd_flydata) {
point = 0
count = 0
for (i in seq(from=1, to=length(odd_flylist), by=2)) {
point = point + odd_flylist[[i]][[x]]
count = count +1
}
average = point/count
merged_odd_flys[[a]] <<- average
a = a +1
}
}
# exercise plotting Function for the even periods of all flys combined
even_merged_plot_all_flys <- function() {
data_merging_even_flys(rawdata)
mean <- do.call("rbind", merged_even_flys)
freq <- c(1:length(merged_even_flys))
data <- as.data.frame(mean)
plotdata <- cbind(data, freq)
names(plotdata) <- c("voltage", "time")
print(
ggplot(plotdata, aes(x=time, y=voltage)) +
geom_line(aes(y=voltage)) +
geom_smooth() +
ggtitle("Average all even flys")
)
}
# exercise plotting Function for the odd periods of all flys combined
odd_merged_plot_all_flys <- function() {
data_merging_odd_flys(rawdata)
mean <- do.call("rbind", merged_odd_flys)
freq <- c(1:length(merged_odd_flys))
data <- as.data.frame(mean)
plotdata <- cbind(data, freq)
names(plotdata) <- c("voltage", "time")
print(
ggplot(plotdata, aes(x=time, y=voltage)) +
geom_line(aes(y=voltage)) +
geom_smooth() +
ggtitle("Average all odd flys")
)
}
x = unlist(odd_flylistasone, use.names = FALSE)
hist(x, breaks = "Scott")
qplot(x, binwdth = 5)
p = plot_ly(x, type = "histogram")
odd_flylistasone <- c(odd_flylist[[1]], odd_flylist[[2]], odd_flylist[[3]], odd_flylist[[4]], odd_flylist[[5]], odd_flylist[[6]], odd_flylist[[7]], odd_flylist[[8]], odd_flylist[[9]], odd_flylist[[10]], odd_flylist[[11]])
x = unlist(odd_flylistasone, use.names = FALSE)
hist(x, breaks = "Scott", xlim = range(3, 4.5), main = "Histogram of merged odd periods from 11 flys")
odd_singleflyasone <- c(periodlist[[1]], periodlist[[3]], periodlist[[5]], periodlist[[7]], periodlist[[9]])
x = unlist(odd_singleflyasone, use.names = FALSE)
hist(x, breaks = "Scott", xlim = range(3, 4.5), main = "Histogram of all odd periods from fly E11")
even_flylistasone <- c(even_flylist[[1]], even_flylist[[2]], even_flylist[[3]], even_flylist[[4]], even_flylist[[5]], even_flylist[[6]], even_flylist[[7]], even_flylist[[8]], even_flylist[[9]], even_flylist[[10]], even_flylist[[11]])
x = unlist(even_flylistasone, use.names = FALSE)
hist(x, breaks = "Scott", xlim = range(3, 4.5), main = "Histogram of merged even periods from 3 flys")
even_singleflyasone <- c(periodlist[[2]], periodlist[[4]], periodlist[[6]], periodlist[[8]], periodlist[[10]])
x = unlist(even_singleflyasone, use.names = FALSE)
hist(x, breaks = "Scott", xlim = range(3, 4.5), main = "Histogram of all even periods from fly E11")