Unverified Commit 5db7042e authored by Maximilian von der Linde's avatar Maximilian von der Linde Committed by GitHub
Browse files

Update plotdata.R

plotdata.R has been modified so that platform data can be processed and hopefully still works for Torquemeter.
Nevertheless there are many functions which have the wrong parameter or simply have been excluded for platform data (Performance indices!!)
parent bbdf89f2
......@@ -41,6 +41,21 @@ dytraces <- function(rawdata)
return(traces)
}
dytraces_platform <- function(rawdata)
{
traces <- dygraph(rawdata, main = paste("Time Traces", flyname)) %>%
dySeries("a_pos", label = "position", color = "darkred") %>%
dySeries("fly", axis = 'y2', color = "blue") %>%
dyAxis("x", drawGrid = FALSE) %>%
dyAxis("y", label = "Position [arb_units]") %>%
dyOptions(gridLineColor = "lightgrey") %>%
dyAxis("y2", label = "Torque [arb_units]", independentTicks = TRUE, drawGrid = FALSE) %>%
dyOptions(includeZero = TRUE) %>%
dyRangeSelector()
return(traces)
}
############ Function to plot several plots into a single plot #########
multiplot <- function(..., plotlist = NULL, file, cols = 1, layout = NULL)
......
......@@ -17,8 +17,8 @@ grouped.data <- list()
############# read file list and plot graphs for each file #############################
xml_list <- choose.files()
if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group have the identical experimental design
{
if(MultiFlyDataVerification(xml_list)==TRUE) { # make sure all flies in a group have the identical experimental design
print("identical")
for (l in 1:length(xml_list))
{
xml_name=xml_list[l]
......@@ -40,6 +40,7 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
#create plot lists
poshistos <- list()
trqhistos <- list()
flyhistos <- list()
##### analyze data from each period separately #######
......@@ -51,11 +52,16 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
#only look at period data
temp <- rawdata[rawdata$period == i, ]
if (sequence$type[i] == "OptomotoL" | sequence$type[i] == "OptomotoR") {
keeps = c("a_pos","fly")
period.data[[i]] <- temp[keeps] #list only position and torque data by period
} else {
keeps = c("a_pos","torque")
period.data[[i]] <- temp[keeps] #list only position and torque data by period
}
if(sequence$type[i]!="optomotor")
{
if(sequence$type[i]!="optomotor") {
## plot the torque and position time traces
filename = paste(flyname,"timetraces",i,".png", sep="_")
png(file = filename, width = 1920) # direct the following output to the image
......@@ -64,6 +70,13 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
}
#plot period histograms
#platform
flyhistos[[i]] <- ggplot(data=temp, aes_string(temp$fly)) +
geom_histogram(binwidth = 3, fill = sequence$histocolor[i]) +
labs(x="fly [arb units]", y="frequency") +
xlim(-600,600) +
ggtitle(paste(flyname, "Period", i))
#torque
trqhistos[[i]] <- ggplot(data=temp, aes_string(temp$torque)) +
geom_histogram(binwidth = 3, fill = sequence$histocolor[i]) +
......@@ -72,8 +85,7 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
ggtitle(paste(flyname, "Period", i))
#position
if(sequence$type[i]!="optomotor")
{
if(sequence$type[i]!="optomotor") {
poshistos[[i]] <- ggplot(data=temp, aes_string(temp$a_pos)) +
geom_histogram(binwidth=10, fill = sequence$histocolor[i]) +
labs(x="position [arb units]", y="frequency") +
......@@ -86,8 +98,7 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
if(!exists("PIprofile")){PIprofile <- data.frame(matrix(ncol = NofPeriods))}
#for position
if(sequence$type[i]!="optomotor")
{
if(sequence$type[i]!="optomotor" & sequence$type[i]!="OptomotoR" & sequence$type[i]!="OptomotoL") {
t1 = sum(abs(temp$a_pos) >= 512 & abs(temp$a_pos) <= 1538)
t2 = nrow(temp)-t1
sequence$lambda[i] = (t1-t2)/(t1+t2)
......@@ -103,6 +114,14 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
## plot position and torque histograms ##
#platform
flyhistos[[NofPeriods+1]] <- ggplot(data=rawdata, aes_string(rawdata$fly)) +
geom_histogram(binwidth=3) +
labs(x="fly [arb units]", y="frequency") +
xlim(-600,600) +
ggtitle(paste(flyname, "total"))
#torque
trqhistos[[NofPeriods+1]] <- ggplot(data=rawdata, aes_string(rawdata$torque)) +
geom_histogram(binwidth=3) +
......@@ -118,19 +137,27 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
ggtitle(paste(flyname, "total"))
##write histograms to file
if (sequence$type[i] == "OptomotoL" | sequence$type[i] == "OptomotoR") {
#fly
filename = paste(flyname,"fly_histos.png", sep="_")
png(file = filename, width = 1000) # direct the following output to the image
multiplot(plotlist = flyhistos, cols=2)
dev.off()
} else {
#torque
filename = paste(flyname,"torque_histos.png", sep="_")
png(file = filename, width = 1000) # direct the following output to the image
multiplot(plotlist = trqhistos, cols=2)
dev.off()
}
#position
filename = paste(flyname,"position_histos.png", sep="_")
png(file = filename, width = 1000) # direct the following output to the image
multiplot(plotlist = poshistos, cols=2)
dev.off()
##plot PI bargraph
##plot PI bargraph if !platform
if (sequence$type[i] != "OptomotoL" & sequence$type[i] != "OptomotoR") {
filename = paste(flyname,"performance.png", sep="_")
png(file = filename, width = 1000) # direct the following output to the image
barplot(sequence$lambda, main = paste("Performance Indices", flyname),
......@@ -139,14 +166,18 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
space=0,
col = sequence$color)
graphics.off()
##move PIs to multi-experiment data.frame
if(l>1){PIprofile <- rbind2(PIprofile, as.vector(t(sequence$lambda)))}
}
##dyplot traces
if (sequence$type[i] == "OptomotoL" | sequence$type[i] == "OptomotoR") {
traces <- dytraces_platform(rawdata)
} else {
traces <- dytraces(rawdata)
}
print(traces)
##move PIs to multi-experiment data.frame
if(l>1){PIprofile <- rbind2(PIprofile, as.vector(t(sequence$lambda)))}
##add period data to grouped data
grouped.data[[l]] <- period.data
......@@ -154,14 +185,14 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
########### plot graphs for all experiments #####################
##pool all data except "optomotor" by period
##pool all data except "optomotor" and "platform" by period
pooled.data<-list()
for(i in 1:NofPeriods)
{
period.data<-data.frame()
if(sequence$type[i]!="optomotor")
if(sequence$type[i]!="optomotor" & sequence$type[i] != "OptomotoL" & sequence$type[i] != "OptomotoR")
{
for (l in 1:length(xml_list))
{
......@@ -171,22 +202,30 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
pooled.data[[i]] <- period.data
}
## plot pooled position and torque histograms by period ##
## plot pooled position and torque or platform histograms by period ##
for(i in 1:NofPeriods)
{
temp<-pooled.data[[i]]
if (sequence$type[i] == "OptomotoL" | sequence$type[i] == "OptomotoR") {
#platform
flyhistos[[i]] <- ggplot(data=temp, aes_string(temp$fly)) +
geom_histogram(binwidth = 3, fill = sequence$histocolor[i]) +
labs(x="fly [arb units]", y="frequency") +
xlim(-600,600) +
ggtitle(paste("Period", i))
} else {
#torque
trqhistos[[i]] <- ggplot(data=temp, aes_string(temp$torque)) +
geom_histogram(binwidth = 3, fill = sequence$histocolor[i]) +
labs(x="torque [arb units]", y="frequency") +
xlim(-600,600) +
ggtitle(paste("Period", i))
}
#position
if(sequence$type[i]!="optomotor")
{
if(sequence$type[i]!="optomotor") {
poshistos[[i]] <- ggplot(data=temp, aes_string(temp$a_pos)) +
geom_histogram(binwidth=10, fill = sequence$histocolor[i]) +
labs(x="position [arb units]", y="frequency") +
......@@ -195,12 +234,13 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
}
}
## pool all torque and position data into single data.frame
## pool all torque or platform and position data into single data.frame
all.data <- do.call(rbind, pooled.data)
## plot pooled histograms for all flies over all periods
## plot and write pooled histograms for all flies over all periods if !platform
## also refrain from using any PI calcualtions for "platform" (at least for now)
if (sequence$type[i] != "OptomotoL" & sequence$type[i] != "OptomotoR") {
#torque
trqhistos[[NofPeriods+1]] <- ggplot(data=all.data, aes_string(all.data$torque)) +
geom_histogram(binwidth=3) +
......@@ -215,8 +255,6 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
xlim(-2047,2048) +
ggtitle("Pooled Position Histogram")
###write pooled histograms to file###
#torque
filename = paste(flyname,"torque_histos.png", sep="_")
......@@ -228,6 +266,7 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
multiplot(plotlist = poshistos, cols=2)
dev.off()
###make colnames in PIprofile for plotting###
colnames(PIprofile) <- sprintf("PI%d", 1:NofPeriods)
......@@ -269,4 +308,5 @@ if(MultiFlyDataVerification(xml_list)==TRUE) # make sure all flies in a group ha
ggtitle(paste("PI Profile, N=",length(xml_list))) +
ylim(-1,1)+theme_light(base_size = 18) + theme(panel.grid.major.x = element_blank(),panel.grid.minor.x = element_blank(), panel.border = element_rect(size = 0.5, linetype = "solid", colour = "black", fill=NA)) +
theme(axis.text.y = element_text(size=18))+ ylab("PI [rel. units]") + theme(aspect.ratio=4/NofPeriods)
}
} else {print("You have selected files with differing metadata")}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment