Skip to content

Commit

Permalink
work on plotting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
buddekai committed Aug 8, 2023
1 parent 946d0c5 commit 0fd64d2
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 44 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Directories
output/

# History files
.Rhistory
.Rapp.history
Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ License: LICENSE
Encoding: UTF-8
LazyData: true
Imports: tidyverse, yaml, scattermore
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
31 changes: 21 additions & 10 deletions R/getWaveforms.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ getWaveforms <- function(input_file = NULL, input_directory = NULL) {
return(NULL)
}

first_non_zero_data <- FALSE

for(i in 1:length(yaml_files)){
# Read data
if(zip_container){
Expand All @@ -90,20 +92,29 @@ getWaveforms <- function(input_file = NULL, input_directory = NULL) {
df_data_list <- yaml::read_yaml(file = file.path(input_directory, yaml_files[i]))
}

# Convert list to tibble
df_data_dummy <- dplyr::as_tibble(df_data_list)
if(length(df_data_list[[1]]) > 0){

# Convert list to tibble
df_data_dummy <- dplyr::as_tibble(df_data_list)

# Get date and time of measurement
df_data_dummy$date_time <- gsub(pattern = "-wave\\.yml",
replacement = "",
x = basename(yaml_files[i]))

# Get date and time of measurement
df_data_dummy$date_time <- gsub(pattern = "-wave\\.yml",
replacement = "",
x = basename(yaml_files[i]))
df_data_dummy$ID <- i

df_data_dummy$ID <- i
if(i == 1 || first_non_zero_data){
df_data <- df_data_dummy
first_non_zero_data <- FALSE
}else{
df_data <- dplyr::bind_rows(df_data, df_data_dummy)
}

if(i == 1){
df_data <- df_data_dummy
}else{
df_data <- dplyr::bind_rows(df_data, df_data_dummy)
if(i == 1){
first_non_zero_data <- TRUE
}
}

}
Expand Down
25 changes: 18 additions & 7 deletions R/plotMeasurements.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
# Created: 2022/04/20
# Last changed: 2023/02/21

plotMeasurements <- function(input_data = NULL, output_dir = NULL,
vavg_min = NULL, vavg_max = NULL,
vpp_min = NULL, vpp_max = NULL,
plotMeasurements <- function(input_data = NULL,
output_dir = NULL,
vavg_min = NULL,
vavg_max = NULL,
vpp_min = NULL,
vpp_max = NULL,
plot_title = NULL) {

# Some function parameters
Expand Down Expand Up @@ -56,18 +59,26 @@ plotMeasurements <- function(input_data = NULL, output_dir = NULL,

# Determine Vavg minimum and maximum if not given
if(is.null(vavg_min)){
vavg_min <- as.numeric(quantile(df_data$VAVG, 0.01, na.rm = TRUE))*factor_for_min_max_scaling
vavg_min <- round(
as.numeric(quantile(df_data$VAVG, 0.01, na.rm = TRUE)) *
factor_for_min_max_scaling, digits = 0)
}
if(is.null(vavg_max)){
vavg_max <- as.numeric(quantile(df_data$VAVG, 0.99, na.rm = TRUE))*factor_for_min_max_scaling
vavg_max <- round(
as.numeric(quantile(df_data$VAVG, 0.99, na.rm = TRUE)) *
factor_for_min_max_scaling, digits = 0)
}

# Determine Vpp minimum and maximum if not given
if(is.null(vpp_min)){
vpp_min <- as.numeric(quantile(df_data$VPP, 0.01, na.rm = TRUE))*factor_for_min_max_scaling
vpp_min <- round(
as.numeric(quantile(df_data$VPP, 0.01, na.rm = TRUE)) *
factor_for_min_max_scaling, digits = 0)
}
if(is.null(vpp_max)){
vpp_max <- as.numeric(quantile(df_data$VPP, 0.99, na.rm = TRUE))*factor_for_min_max_scaling
vpp_max <- round(
as.numeric(quantile(df_data$VPP, 0.99, na.rm = TRUE)) *
factor_for_min_max_scaling, digits = 0)
}

# # Set time breaks
Expand Down
44 changes: 32 additions & 12 deletions R/plotWaveforms.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#' @return 0

# Created: 2022/04/21
# Last changed: 2023/02/21
# Last changed: 2023/08/01

plotWaveforms <- function(input_data = NULL,
output_dir = NULL,
Expand Down Expand Up @@ -57,7 +57,7 @@ plotWaveforms <- function(input_data = NULL,
# Set plot title
if(is.null(plot_title)){
plot_title <- ""
plot_title_without_date <- ""
# plot_title_without_date <- ""

# Get date of experiment
date_of_experiment <- unique(as.Date(input_data$date_time))[1]
Expand Down Expand Up @@ -156,7 +156,7 @@ plotWaveforms <- function(input_data = NULL,
waveforms <- NA
}

if(!is.na(waveforms)){
if(length(waveforms) > 1 || !is.na(waveforms)){
for(i in waveforms){

input_data_filtered <- input_data %>%
Expand All @@ -179,9 +179,12 @@ plotWaveforms <- function(input_data = NULL,
if(is.na(p2p_value)){
p2p_value <- 0
}
p2p_value <- format(round(p2p_value, digits = 2), nsmall = 2)
p2p_value <- format(round(p2p_value, digits = 1), nsmall = 1)
p2p_value <- paste("V_p2p=", p2p_value, "V", sep="")

max_value_text <- paste("V_max=", format(round(max_value, digits = 1), nsmall = 1), "V", sep="")
min_value_text <- paste("V_min=", format(round(min_value, digits = 1), nsmall = 1), "V", sep="")

# Calculate mean value of stimulation pulses ###########################

if(start_point_function_generator_pulse == end_point_function_generator_pulse){
Expand All @@ -198,9 +201,12 @@ plotWaveforms <- function(input_data = NULL,
mean_value <- mean(df_dummy$U)
}

mean_value_value_text <- paste("V_mean=", format(round(mean_value, digits = 1), nsmall = 1), "V", sep="")


# Plot complete data from YAML file ####################################
plot_annotation_x <- 0.05*max(input_data_filtered$time)
plot_annotation_x <- 0.5*max(input_data_filtered$time)
plot_annotation_x_minmax <- 0.1*max(input_data_filtered$time)
xaxis_lab <- ifelse(test = show_time_in_us,yes = "time/\U00B5s", no = "time/s")

plot_waveform <- ggplot2::ggplot(data = input_data_filtered,
Expand All @@ -209,9 +215,14 @@ plotWaveforms <- function(input_data = NULL,
geom_hline(yintercept=max_value, linetype="dashed", color = "darkgray", size=1) +
geom_hline(yintercept=min_value, linetype="dashed", color = "darkgray", size=1) +
geom_hline(yintercept=mean_value, linetype="dotdash", color = "darkgray", size=1) +
annotate("text", x=plot_annotation_x, y=(voltage_limits_of_plot-2), label=p2p_value) +
annotate("text", x=plot_annotation_x, y=(voltage_limits_of_plot-1), label=p2p_value) +
annotate("text", x=plot_annotation_x_minmax, y=(max_value-1), label=max_value_text) +
annotate("text", x=plot_annotation_x_minmax, y=(min_value+1), label=min_value_text) +
annotate("text", x=plot_annotation_x, y=-0.5, label=mean_value_value_text) +
coord_cartesian(ylim = c(-voltage_limits_of_plot, voltage_limits_of_plot)) +
labs(title=paste(plot_title_without_date, input_data_filtered$date_time[1], sep=" "),
# labs(title=paste(plot_title_without_date, input_data_filtered$date_time[1], sep=" "),
# x = xaxis_lab, y = "U/V") +
labs(title=paste(plot_title, format(as.POSIXct(input_data_filtered$date_time[1]), format = "%H:%M:%S"), sep=" "),
x = xaxis_lab, y = "U/V") +
theme_bw() +
theme(axis.text.x = element_text(angle=90, vjust = 0.5),
Expand Down Expand Up @@ -251,9 +262,13 @@ plotWaveforms <- function(input_data = NULL,
if(is.na(p2p_value)){
p2p_value <- 0
}
p2p_value <- format(round(p2p_value, digits = 2), nsmall = 2)
p2p_value <- format(round(p2p_value, digits = 1), nsmall = 1)
p2p_value <- paste("V_p2p=", p2p_value, "V", sep="")

max_value_text <- paste("V_max=", format(round(max_value, digits = 1), nsmall = 1), "V", sep="")
min_value_text <- paste("V_min=", format(round(min_value, digits = 1), nsmall = 1), "V", sep="")


# Calculate mean
if(start_point_function_generator_pulse == end_point_function_generator_pulse){
df_dummy <- input_data %>%
Expand All @@ -269,12 +284,14 @@ plotWaveforms <- function(input_data = NULL,
mean_value <- mean(df_dummy$U)
}

mean_value_value_text <- paste("V_mean=", format(round(mean_value, digits = 1), nsmall = 1), "V", sep="")




# Plot complete data from YAML file ####################################
plot_annotation_x <- 0.05*max(input_data$time)
# Plot complete data from YAML file ######################################
plot_annotation_x <- 0.5*max(input_data$time)
plot_annotation_x_minmax <- 0.1*max(input_data$time)

plot_waveform <- ggplot2::ggplot(data = input_data,
aes(x = time, y = U, color=Channel)) +
Expand All @@ -283,9 +300,12 @@ plotWaveforms <- function(input_data = NULL,
geom_hline(yintercept=max_value, linetype="dashed", color = "darkgray") +
geom_hline(yintercept=min_value, linetype="dashed", color = "darkgray") +
geom_hline(yintercept=mean_value, linetype="dotdash", color = "darkgray", size=1) +
annotate("text", x=plot_annotation_x, y=(voltage_limits_of_plot-2), label=p2p_value) +
annotate("text", x=plot_annotation_x, y=(voltage_limits_of_plot-1), label=p2p_value) +
annotate("text", x=plot_annotation_x_minmax, y=(max_value-1), label=max_value_text) +
annotate("text", x=plot_annotation_x_minmax, y=(min_value+1), label=min_value_text) +
annotate("text", x=plot_annotation_x, y=-0.5, label=mean_value_value_text) +
coord_cartesian(ylim = c(-voltage_limits_of_plot, voltage_limits_of_plot)) +
labs(title=paste(plot_title, sep=" "),
labs(title=plot_title,
x = xaxis_lab, y = "U/V") +
theme_bw() +
theme(axis.text.x = element_text(angle=90, vjust = 0.5),
Expand Down
102 changes: 90 additions & 12 deletions inst/testscriptDevelop.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Testscript for using the R package oscilloscopeR for development ++++++++++++++
# Author: Kai Budde
# Created: 2022/04/19
# Last changed: 2022/04/21
# Last changed: 2023/08/01

# Delete everything in the environment
rm(list = ls())
Expand All @@ -11,18 +11,18 @@ graphics.off()
# Load packages ############################################################

# Set groundhog day for reproducibility (see https://groundhogr.com)
groundhog.day <- "2022-03-01"
groundhog.day <- "2023-01-01"

if(!any(grepl(pattern = "groundhog", x = installed.packages(), ignore.case = TRUE))){
install.packages("groundhog")
}

# Load packages
# Load packages
library(groundhog)
pkgs <- c("tidyverse", "yaml", "ggplot2", "devtools", "scattermore")
pkgs <- c("tidyverse", "yaml", "scattermore", "devtools")
groundhog.library(pkgs, groundhog.day)


# Package development ######################################################

# Check package
Expand All @@ -36,18 +36,48 @@ devtools::load_all()

# Please adapt the following parameters ####################################
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# YAML File
input_file <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/IonOptixMeasurements/1_GateOut/20211203-110117-measurement.yml"
input_file <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/IonOptixMeasurements/1_GateOut/20211203-110117-wave.yml"

# ZIP File (containing yaml files)
input_file <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/CellBioShortStimI.zip"

# Directory (containing yaml files)
input_directory <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/01_hotMedium_hotElectrodes"
input_zip_file <- file.path("inst", "testdata","recorded_stimulation.zip")

# # YAML File
# # input_file <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/IonOptixMeasurements/1_GateOut/20211203-110117-measurement.yml"
# # input_file <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/IonOptixMeasurements/1_GateOut/20211203-110117-wave.yml"
#
# # ZIP File (containing yaml files)
# input_file <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/CellBioShortStimI.zip"
#
# # Directory (containing yaml files)
# input_directory <- "C:/Users/Kai/Documents/git/gitHub/OscilloscopeRecordings/data/01_hotMedium_hotElectrodes"

# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

# Measurement data ---------------------------------------------------------
# Zip file
df_data <- getMeasurements(input_file = input_zip_file)
# Plot measurement data
plotMeasurements(input_data = df_data)


# Waveform data ------------------------------------------------------------
df_data <- getWaveforms(input_file = input_zip_file)

# Plot wave form data
plotWaveforms(input_data = df_data,
plot_waveforms = "one")














### Test small functions ---------------------------------------------------

# Get Vavg (average voltage) and Vpp (peak-to-peak voltage) and plot the data
Expand All @@ -60,3 +90,51 @@ df_data <- getWaveforms(input_file = input_file)
df_data <- getWaveforms(input_directory = input_directory)
plotWaveforms(input_data = df_data, channel_function_generator = "CHAN2",
channel_stimulation_pulse = "CHAN1")


### Plotting IonOptix Measurements ---------------------------------------------------

# Version of R package oscilloscopeR: 0.1.0
input_directory <- "C:/Users/Kai/Documents/git/gitHub/SI_IonOptix_Measurements/IonOptixMeasurements/1_GateOut"
df_data <- getMeasurements(input_directory = input_directory)
plotMeasurements(input_data = df_data)

df_data <- getWaveforms(input_directory = input_directory)
plotWaveforms(input_data = df_data, epsilon = 0.01, channel_stimulation_pulse = "CHAN1",
channel_function_generator = "CHAN2")


input_directory <- "C:/Users/Kai/Documents/git/gitHub/SI_IonOptix_Measurements/IonOptixMeasurements/2_GateAndAux"
df_data <- getMeasurements(input_directory = input_directory)
plotMeasurements(input_data = df_data)

df_data <- getWaveforms(input_directory = input_directory)
plotWaveforms(input_data = df_data, epsilon = 0.01, channel_stimulation_pulse = "CHAN1",
channel_function_generator = "CHAN2")


input_directory <- "C:/Users/Kai/Documents/git/gitHub/SI_IonOptix_Measurements/IonOptixMeasurements/3_Reihenmessung"
df_data <- getMeasurements(input_directory = input_directory)
plotMeasurements(input_data = df_data)

df_data <- getWaveforms(input_directory = input_directory)
plotWaveforms(input_data = df_data, epsilon = 0.01, channel_stimulation_pulse = "CHAN1",
channel_function_generator = "CHAN2")


input_directory <- "C:/Users/Kai/Documents/git/gitHub/SI_IonOptix_Measurements/IonOptixMeasurements/4_Breakoutbox_trocken/a1c1"
df_data <- getMeasurements(input_directory = input_directory)
plotMeasurements(input_data = df_data)

df_data <- getWaveforms(input_directory = input_directory)
plotWaveforms(input_data = df_data, epsilon = 0.01, channel_stimulation_pulse = "CHAN3",
channel_function_generator = "CHAN2")

plot_waveform <- ggplot2::ggplot(data = df_data,
aes(x = time, y = U, color=Channel)) +
geom_line() +
theme_bw() +
theme(axis.text.x = element_text(angle=90, vjust = 0.5),
plot.title = element_text(hjust = 0.5))


10 changes: 8 additions & 2 deletions man/plotWaveforms.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0fd64d2

Please sign in to comment.