forked from max-vdL/Optomotorics-Bachelor-Thesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy_plot - txt.R
407 lines (357 loc) · 12.1 KB
/
easy_plot - txt.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
library(dygraphs)
library(ggplot2)
library(reshape2)
library(plotly)
# Lists for saving of data from multiple test flys
evenPeriods_allFlys <- list()
oddPeriods_allFlys <- 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)]
rawdata <<- cbind(time, flydata, arenadata, perioddata)
arenadata_smoothing(rawdata)
}
# 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)
} # unused at the moment
# 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[1:(nrow(rawdata)/2), 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()
max_row = nrow(rawdata)
while (row <= max_row) {
periodname <- paste("Period", periodcount, sep = "")
i = 1
temp <- list()
while ((rawdata[row, c(4)] == periodcount)) {
temp[[i]] <- rawdata[row, c(2)]
i = i +1
row = row +1
if (is.na(rawdata[row, c(4)])) {break}
# break if subscript is out of bounds
}
periodlist_[[periodname]] <- temp
periodcount = periodcount +1
}
periodlist <<- periodlist_
# figure the length of the smallest list in "periodlist" out
# to avoid stepping over list boundaries
temp2 <- list()
for (i in 1:length(periodlist)) {
temp2[[i]] <- length(periodlist[[i]])
}
max_perioddata <<- min(unlist(temp2))
}
######## data merging
# 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
}
}
######## plotting of merged data
# 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)
names(data) <- c("FlyPosition")
plotdata <- cbind(data, freq)
plotname <- paste("Plot of merged even periods from", name)
print(
ggplot(plotdata, aes(x=freq, y=FlyPosition)) +
geom_line(aes(y=FlyPosition)) +
ggtitle(plotname) +
geom_smooth()
)
# save the merged data in the flylist for future mergign of different flys
evenPeriods_allFlys[[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)
names(data) <- c("FlyPosition")
plotdata = cbind(data, freq)
plotname <- paste("Plot of merged odd periods from", name)
print(
ggplot(plotdata, aes(x=freq, y=FlyPosition)) +
geom_line(aes(y=FlyPosition)) +
ggtitle(plotname) +
geom_smooth()
)
# save the merged data in the flylist for future mergign of different flys
oddPeriods_allFlys[[name]] <<- as.list(mean)
}
######## plotting of individual periods
# plottiong Function for all even periods seperated in one Graph
even_period_plot <- function() {
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")
plotname <- paste("Even period plot", name)
print(
ggplot(plotdata_new, aes(x=time, y=fly)) +
geom_line(aes(color = period)) +
geom_smooth() +
ggtitle(plotname)
)
}
# plottiong Function for all odd periods seperated in one Graph
odd_period_plot <- function() {
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")
plotname <- paste("Odd period plot", name)
print(
ggplot(plotdata_new, aes(x=time, y=fly)) +
geom_line(aes(color = period)) +
geom_smooth() +
ggtitle(plotname)
)
}
######## histogram plotting
# histogram plotting functions for the frequency of
# flydatapoints in even/odd periods
histogram_even <- function() {
period_sorting(rawdata)
even_singleflyasone <- c(
periodlist[[2]], periodlist[[4]], periodlist[[6]],
periodlist[[8]], periodlist[[10]])
x = unlist(even_singleflyasone, use.names = FALSE)
histname <- paste("Histogram of all even periods from fly ", name)
hist(
x, breaks = "Scott", xlim = range(0, 3), ylim = range(0, 3200),
main = histname)
}
histogram_odd <- function() {
period_sorting(rawdata)
odd_singleflyasone <- c(
periodlist[[1]], periodlist[[3]], periodlist[[5]],
periodlist[[7]], periodlist[[9]])
x = unlist(odd_singleflyasone, use.names = FALSE)
histname <- paste("Histogram of all odd periods from fly ", name)
hist(
x, breaks = "Scott", xlim = range(0, 3), ylim = range(0, 3200),
main = histname)
}
######## flytrace plotting
# simple plotting function with dygraphs for the whole rawdata
single_fly_plot <- function() {
graphname <- paste("Flytraces from", name)
dygraph(rawdata, main = graphname) %>%
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(evenPeriods_allFlys)) {
templist[[i]] <- length(evenPeriods_allFlys[[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(evenPeriods_allFlys), by=2)) {
point = point + evenPeriods_allFlys[[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(oddPeriods_allFlys)) {
templist[[i]] <- length(oddPeriods_allFlys[[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(oddPeriods_allFlys), by=2)) {
point = point + oddPeriods_allFlys[[i]][[x]]
count = count +1
}
average = point/count
merged_odd_flys[[a]] <<- average
a = a +1
}
}
######## plotting of merged data from all flys
# 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 (no arenaturn)")
)
}
# 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 (no arenaturn)")
)
}
######## histogram plotting of merged data from all flys
# histogram plotting function for alls fly data combined
# (even/odd_flylist required)
histogram_odd_all <- function() {
odd_flylistasone <- c(oddPeriods_allFlys[[1]], oddPeriods_allFlys[[2]], oddPeriods_allFlys[[3]])
x = unlist(odd_flylistasone, use.names = FALSE)
hist(
x, breaks = "Scott", xlim = range(0, 3),
main = "Histogram of merged odd periods from D1, D2, D4 flys (no arenaturn)")
}
histogram_even_all <- function() {
even_flylistasone <- c(evenPeriods_allFlys[[1]], evenPeriods_allFlys[[2]], evenPeriods_allFlys[[3]])
x = unlist(even_flylistasone, use.names = FALSE)
hist(
x, breaks = "Scott", xlim = range(0, 3),
main = "Histogram of merged even periods from D1, D2, D4 flys (no arenaturn)")
}