From ee4884f36f20593026d77c4a9e83e82a30c986c4 Mon Sep 17 00:00:00 2001 From: buddekai Date: Wed, 30 Oct 2024 11:18:50 +0100 Subject: [PATCH] new version 0.2.0 adding sinusoidal stimulation --- DESCRIPTION | 30 ++-- R/convertMeasurementListToTibble.R | 1 - R/getMeasurements.R | 25 +++- R/getWaveforms.R | 48 +++++-- R/plotWaveforms.R | 195 +++++++++++++++++--------- man/convertMeasurementListToTibble.Rd | 4 +- man/getMeasurements.Rd | 2 +- man/getWaveforms.Rd | 11 +- man/plotMeasurements.Rd | 2 +- man/plotWaveforms.Rd | 30 +++- 10 files changed, 247 insertions(+), 101 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index b39b808..4dc1369 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,14 +1,24 @@ -Package: oscilliscopeR Type: Package -Title: Import and work with data from oscilloscope -Version: 0.1.4 -Author: c(person("Kai", "Budde", email = "kai.budde@uni-rostock.de", - role = c("aut", "cre")) -Maintainer: Kai Budde -Description: This package reads recorded data from measurements - of an oscilloscope (as yaml files) and plots them. -License: LICENSE +Package: oscilloscopeR +Title: Import and work with data from oscilloscope(s) +Version: 0.2.0 +Authors@R: + person("Kai", "Budde-Sagert", , "kai.budde-sagert@uni-rostock.de", role = c("aut", "cre"), + comment = c(ORCID = "0000-0001-6836-9865")) +Maintainer: Kai Budde-Sagert +Description: This package reads recorded data from measurements of an + oscilloscope (as yaml files), calculates some values, and plots them. +License: GPL-3 + file LICENSE +URL: https://github.com/SFB-ELAINE/oscilloscopeR +BugReports: https://github.com/SFB-ELAINE/oscilloscopeR/issues +Depends: + R (>= 4.0.0) +Imports: + lubridate (>= 1.9.0), + scattermore (>= 0.8), + tidyverse (>= 1.3.2), + yaml (>= 2.3.6) +ByteCompile: TRUE Encoding: UTF-8 LazyData: true -Imports: tidyverse, yaml, scattermore RoxygenNote: 7.2.3 diff --git a/R/convertMeasurementListToTibble.R b/R/convertMeasurementListToTibble.R index 5f1be70..52da5d7 100644 --- a/R/convertMeasurementListToTibble.R +++ b/R/convertMeasurementListToTibble.R @@ -6,7 +6,6 @@ #' @author Kai Budde-Sagert #' @export convertMeasurementListToTibble #' @param df_data_list A list (list of data) -#' @param variabel_name A character () #' @return A tibble with the data # Created: 2022/04/19 diff --git a/R/getMeasurements.R b/R/getMeasurements.R index dcd8034..4b166d8 100644 --- a/R/getMeasurements.R +++ b/R/getMeasurements.R @@ -10,7 +10,7 @@ #' @return A tibble with the data # Created: 2022/04/19 -# Last changed: 2022/04/19 +# Last changed: 2024/04/25 getMeasurements <- function(input_file = NULL, input_directory = NULL) { @@ -68,9 +68,9 @@ getMeasurements <- function(input_file = NULL, input_directory = NULL) { if(zip_container){ yaml_files <- unzip(zipfile = file.path(input_directory, input_file), list = TRUE) yaml_files <- yaml_files$Name - yaml_files <- yaml_files[grepl(pattern = "measurement\\.yml", x = yaml_files, ignore.case = TRUE)] + yaml_files <- yaml_files[grepl(pattern = "measurement.+yml", x = yaml_files, ignore.case = TRUE)] }else if(yml_directory){ - yaml_files <- list.files(path = input_directory, pattern = "measurement\\.yml") + yaml_files <- list.files(path = input_directory, pattern = "measurement.+yml") }else{ yaml_files <- input_file } @@ -89,9 +89,23 @@ getMeasurements <- function(input_file = NULL, input_directory = NULL) { df_data_dummy <- convertMeasurementListToTibble(df_data_list) # Get date and time of measurement - df_data_dummy$date_time <- gsub(pattern = "-measurement\\.yml", + df_data_dummy$date_time <- gsub(pattern = "-measurement.+yml", replacement = "", x = basename(yaml_files[i])) + + # Get further information depending on file name + if(length(grep(pattern = ".+-measurement(.+)\\.yml", x = basename(yaml_files[i]), ignore.case = TRUE)) > 0){ + df_data_dummy$file_name_extension <- gsub(pattern = ".+-measurement(.+)\\.yml", + replacement = "\\1", + x = basename(yaml_files[i])) + }else{ + df_data_dummy$file_name_extension <- NA + } + + # Delete possible character of Channel column + df_data_dummy$Channel <- as.numeric(gsub(pattern = "[^0-9.-]", replacement = "", x = df_data_dummy$Channel)) + + # Add ID (file index) df_data_dummy$ID <- i if(i == 1){ @@ -107,5 +121,8 @@ getMeasurements <- function(input_file = NULL, input_directory = NULL) { # Convert date and time df_data$date_time <- as.POSIXct(strptime(df_data$date_time, "%Y%m%d-%H%M%S")) + # Make channel number as factor + df_data$Channel <- as.factor(df_data$Channel) + return(df_data) } diff --git a/R/getWaveforms.R b/R/getWaveforms.R index b382c00..9d7f772 100644 --- a/R/getWaveforms.R +++ b/R/getWaveforms.R @@ -7,12 +7,16 @@ #' @export getWaveforms #' @param input_file A character (path to yaml file to be converted) #' @param input_directory A character (path to directory with yaml files) +#' @param reduce_resolution_of_input_data A Boolean (showing if fewer point +#' are to be imported) #' @return A tibble with the data # Created: 2022/04/21 -# Last changed: 2022/04/21 +# Last changed: 2024/04/17 -getWaveforms <- function(input_file = NULL, input_directory = NULL) { +getWaveforms <- function(input_file = NULL, + input_directory = NULL, + reduce_resolution_of_input_data = TRUE) { # Check for null input if(is.null(input_file) & is.null(input_directory)){ @@ -32,7 +36,7 @@ getWaveforms <- function(input_file = NULL, input_directory = NULL) { print("Please call function with a correct input file ('*.yml' or '*.zip').") return() }else{ - if(grepl(pattern = "wave\\.yml", x = input_file, ignore.case = TRUE)){ + if(grepl(pattern = "wave.+yml", x = input_file, ignore.case = TRUE)){ # A yml file is given yml_file <- TRUE yml_directory <- FALSE @@ -68,9 +72,9 @@ getWaveforms <- function(input_file = NULL, input_directory = NULL) { if(zip_container){ yaml_files <- unzip(zipfile = file.path(input_directory, input_file), list = TRUE) yaml_files <- yaml_files$Name - yaml_files <- yaml_files[grepl(pattern = "wave\\.yml", x = yaml_files, ignore.case = TRUE)] + yaml_files <- yaml_files[grepl(pattern = "wave.+yml", x = yaml_files, ignore.case = TRUE)] }else if(yml_directory){ - yaml_files <- list.files(path = input_directory, pattern = "wave\\.yml") + yaml_files <- list.files(path = input_directory, pattern = "wave.+yml") }else{ yaml_files <- input_file } @@ -97,13 +101,41 @@ getWaveforms <- function(input_file = NULL, input_directory = NULL) { # Convert list to tibble df_data_dummy <- dplyr::as_tibble(df_data_list) + # Reduce dimension of input if required + if(reduce_resolution_of_input_data){ + max_number_of_points <- 2000 + if(nrow(df_data_dummy) > max_number_of_points){ + # time_step <- min(diff(df_data_dummy$time)) + # time_interval <- max(df_data_dummy$time) - min(df_data_dummy$time) + + df_data_dummy <- df_data_dummy[seq(1, nrow(df_data_dummy), floor(nrow(df_data_dummy)/max_number_of_points)), ] + } + } + + # Get date and time of measurement - df_data_dummy$date_time <- gsub(pattern = "-wave\\.yml", + df_data_dummy$date_time <- gsub(pattern = "-wave.+yml", replacement = "", x = basename(yaml_files[i])) + # Get further information depending on file name + if(length(grep(pattern = ".+-wave(.+)\\.yml", x = basename(yaml_files[i]), ignore.case = TRUE)) > 0){ + df_data_dummy$file_name_extension <- gsub(pattern = ".+-wave(.+)\\.yml", + replacement = "\\1", + x = basename(yaml_files[i])) + }else{ + df_data_dummy$file_name_extension <- NA + } + + # Add ID (file index) df_data_dummy$ID <- i + # Put all voltages into one column and the respective channel into another one + df_data_dummy <- tidyr::gather(data = df_data_dummy, key = "Channel", value = "U", -c("time", "date_time", "file_name_extension", "ID")) + + # Delete possible character of Channel column + df_data_dummy$Channel <- as.numeric(gsub(pattern = "[^0-9.-]", replacement = "", x = df_data_dummy$Channel)) + if(i == 1 || first_non_zero_data){ df_data <- df_data_dummy first_non_zero_data <- FALSE @@ -124,8 +156,8 @@ getWaveforms <- function(input_file = NULL, input_directory = NULL) { # Convert date and time df_data$date_time <- as.POSIXct(strptime(df_data$date_time, "%Y%m%d-%H%M%S")) - # Put all voltages into one column and the respective channel into another one - df_data <- tidyr::gather(data = df_data, key = "Channel", value = "U", -c("time", "date_time", "ID")) + # Make channel number as factor + df_data$Channel <- as.factor(df_data$Channel) return(df_data) } diff --git a/R/plotWaveforms.R b/R/plotWaveforms.R index 524d87b..9bd027f 100644 --- a/R/plotWaveforms.R +++ b/R/plotWaveforms.R @@ -9,8 +9,9 @@ #' @param output_dir A character (path to output directory) #' @param show_time_in_us A boolean (indicates whether time is to be shown #' in us) -#' @param channel_stimulation_pulse A character (name of the channel with -#' the stimulation pulse) +#' @param waveform_of_function_generator A character (waveform of the signal: either sinusoidal or rectangular) +#' @param channel_stimulation A character (name of the channel with +#' the stimulation) #' @param channel_function_generator A character (name of the channel with #' the function generator pulse (for GATE TTL)) #' @param channel_resistor A character (name of the channel that recorded the @@ -32,13 +33,14 @@ #' @return 0 # Created: 2022/04/21 -# Last changed: 2023/08/01 +# Last changed: 2024/10/30 plotWaveforms <- function(input_data = NULL, output_dir = NULL, show_time_in_us = FALSE, - channel_function_generator = "CHAN1", - channel_stimulation_pulse = "CHAN2", + waveform_of_function_generator = "rectangular", # rectangular | sinusoidal + channel_function_generator = 1, + channel_stimulation = 2, channel_resistor = NA, plot_waveforms = "all", voltage_limits_of_plot = NULL, @@ -96,15 +98,15 @@ plotWaveforms <- function(input_data = NULL, } # Min and Max for plotting (avoid outliers) - global_max_value <- as.numeric(quantile(input_data$U[input_data$Channel == channel_stimulation_pulse], 0.99, na.rm = TRUE)) - global_min_value <- as.numeric(quantile(input_data$U[input_data$Channel == channel_stimulation_pulse], 0.01, na.rm = TRUE)) + global_max_value <- as.numeric(quantile(input_data$U[input_data$Channel == channel_stimulation], 0.99, na.rm = TRUE)) + global_min_value <- as.numeric(quantile(input_data$U[input_data$Channel == channel_stimulation], 0.01, na.rm = TRUE)) if(is.null(voltage_limits_of_plot)){ voltage_limits_of_plot <- factor_for_min_max_scaling * max(abs(global_max_value), abs(global_min_value)) } - # Detect frequency of generator pulses ################################### + # Detect frequency ####################################################### # Delete data without signal from frequency generator df_dummy <- input_data %>% @@ -120,32 +122,69 @@ plotWaveforms <- function(input_data = NULL, dplyr::filter(Channel == channel_function_generator, date_time == filter_date) - # rising_edges <- which(df_dummy$U < mean(df_dummy$U))[ - # (which(df_dummy$U < mean(df_dummy$U)) + 1) %in% - # which(df_dummy$U > mean(df_dummy$U))] + if(waveform_of_function_generator == "rectangular"){ - rising_edges <- which(df_dummy$U < max(df_dummy$U, na.rm = TRUE)/2)[ - ( which(df_dummy$U < max(df_dummy$U, na.rm = TRUE)/2) + 1 ) %in% - which(df_dummy$U > max(df_dummy$U, na.rm = TRUE)/2) ] + # rising_edges <- which(df_dummy$U < mean(df_dummy$U))[ + # (which(df_dummy$U < mean(df_dummy$U)) + 1) %in% + # which(df_dummy$U > mean(df_dummy$U))] - start_point_function_generator_pulse <- df_dummy$time[rising_edges[1]] - end_point_function_generator_pulse <- df_dummy$time[rising_edges[length(rising_edges)]] + rising_edges <- which(df_dummy$U < max(df_dummy$U, na.rm = TRUE)/2)[ + ( which(df_dummy$U < max(df_dummy$U, na.rm = TRUE)/2) + 1 ) %in% + which(df_dummy$U > max(df_dummy$U, na.rm = TRUE)/2) ] - # Calculate frequency of - if(start_point_function_generator_pulse == end_point_function_generator_pulse){ - print("The frequency cannot be determined because you do not have recorded the full period of the function generator.") - }else{ - periods <- length(rising_edges)-1 - frequency_of_function_generator <- periods/(end_point_function_generator_pulse-start_point_function_generator_pulse) + start_point_function_generator_pulse <- df_dummy$time[rising_edges[1]] + end_point_function_generator_pulse <- df_dummy$time[rising_edges[length(rising_edges)]] + + # Calculate frequency of + if(start_point_function_generator_pulse == end_point_function_generator_pulse){ + print("The frequency cannot be determined because you do not have recorded the full period of the function generator.") + }else{ + periods <- length(rising_edges)-1 + frequency_of_function_generator <- periods/(end_point_function_generator_pulse-start_point_function_generator_pulse) + + print(paste("The frequency of the function generator was: ", + frequency_of_function_generator, " Hz.", sep="")) + } + + }else if(waveform_of_function_generator == "sinusoidal"){ + + generator_amplitude <- (max(df_dummy$U)-min(df_dummy$U))/2 + + # Find approximate frequency + first_max <- which(df_dummy$U > 0.95*max(df_dummy$U))[1] + first_min <- which(df_dummy$U < 0.95*min(df_dummy$U)) + first_min <- first_min[first_min > first_max][1] + + approximate_frequency <- pi/(df_dummy$time[first_min] - df_dummy$time[first_max]) + + fit <- nls(y ~ A * sin(B*x + C) + D, + start = list(A = generator_amplitude/2, B = approximate_frequency, C = 0, D = max(df_dummy$U)/2), + data = data.frame(x = df_dummy$time, y = df_dummy$U)) + + + # plot(df_dummy$time, df_dummy$U) + # co <- coef(fit) + # fit <- function(x, a, b, c, d) {a*sin(b*x+c)+d} + # curve(fit(x, a=co["A"], b=co["B"], c=co["C"], d=co["D"]), add=TRUE ,lwd=2, col="steelblue") + + # Model: A * sin(B*x + C) + D + generator_amplitude <- coef(fit)["A"] + generator_frequency <- coef(fit)["B"]/2/pi + generator_phase <- coef(fit)["C"] + generator_offset <- coef(fit)["D"] + + frequency_of_function_generator <- generator_frequency print(paste("The frequency of the function generator was: ", - frequency_of_function_generator, " Hz.", sep="")) - } + generator_frequency, " Hz.", sep="")) + }else{ + print("Please call the function with a correct waveform (either sinusoidal or rectangular).") + } # Filter data for stimulation pulses ##################################### df_dummy <- input_data %>% - dplyr::filter(Channel == channel_stimulation_pulse) %>% + dplyr::filter(Channel == channel_stimulation) %>% dplyr::group_by(ID) %>% dplyr::summarise(PeakToPeak=max(U)-min(U)) @@ -177,20 +216,20 @@ plotWaveforms <- function(input_data = NULL, input_data_filtered <- input_data %>% dplyr::filter(ID == i) - # Get max and min (smoothed) of all stimulation pulses ################# - lower_bound <- (max(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation_pulse]) - - mean(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation_pulse]))/2 - upper_bound <- (min(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation_pulse]) - - mean(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation_pulse]))/2 + # Get max and min (smoothed) of stimulation ########################## + lower_bound <- (max(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation]) + + mean(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation]))/2 + upper_bound <- (min(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation]) + + mean(input_data_filtered$U[input_data_filtered$Channel == channel_stimulation]))/2 max_value <- as.numeric(quantile(input_data_filtered$U[ - input_data_filtered$Channel == channel_stimulation_pulse & input_data_filtered$U > lower_bound], + input_data_filtered$Channel == channel_stimulation & input_data_filtered$U > lower_bound], 0.99, na.rm = TRUE)) min_value <- as.numeric(quantile(input_data_filtered$U[ - input_data_filtered$Channel == channel_stimulation_pulse & input_data_filtered$U < upper_bound], + input_data_filtered$Channel == channel_stimulation & input_data_filtered$U < upper_bound], 0.01, na.rm = TRUE)) - p2p_value <- abs(max_value) + abs(min_value) + p2p_value <- max_value - min_value if(is.na(p2p_value)){ p2p_value <- 0 } @@ -202,9 +241,9 @@ plotWaveforms <- function(input_data = NULL, # Get max and min (smoothed) of resistor voltages #################### if(!is.na(channel_resistor) && !is.na(shunt_resistance)){ - lower_bound <- (max(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]) - + lower_bound <- (max(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]) + mean(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]))/2 - upper_bound <- (min(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]) - + upper_bound <- (min(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]) + mean(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]))/2 max_value_resistor <- as.numeric(quantile(input_data_filtered$U[ @@ -214,7 +253,7 @@ plotWaveforms <- function(input_data = NULL, input_data_filtered$Channel == channel_resistor & input_data_filtered$U < upper_bound], 0.01, na.rm = TRUE)) - p2p_value_resistor <- abs(max_value_resistor) + abs(min_value_resistor) + p2p_value_resistor <- max_value_resistor - min_value_resistor if(is.na(p2p_value_resistor)){ p2p_value_resistor <- 0 } @@ -222,26 +261,34 @@ plotWaveforms <- function(input_data = NULL, p2p_value_resistor <- paste("V_p2p=", p2p_value_resistor, "V", sep="") max_resistor_value_text <- paste("I(+)=", format(round(max_value_resistor / shunt_resistance * 1000), nsmall = 1), "mA", sep="") - min_resistor_value_text <- paste("I(-)=", format(round(abs(min_value_resistor) / shunt_resistance * 1000, digits = 1), nsmall = 1), "mA", sep="") + min_resistor_value_text <- paste("I(-)=", format(round(min_value_resistor / shunt_resistance * 1000, digits = 1), nsmall = 1), "mA", sep="") } - # Calculate mean value of stimulation pulses ########################### + # Calculate mean value of stimulation ################################ - if(start_point_function_generator_pulse == end_point_function_generator_pulse){ - df_dummy <- input_data_filtered %>% - dplyr::filter(Channel == channel_stimulation_pulse) + if(waveform_of_function_generator == "retangular"){ + if(start_point_function_generator_pulse == end_point_function_generator_pulse){ + df_dummy <- input_data_filtered %>% + dplyr::filter(Channel == channel_stimulation) - mean_value <- mean(df_dummy$U) + mean_value <- mean(df_dummy$U) + }else{ + df_dummy <- input_data_filtered %>% + dplyr::filter(Channel == channel_stimulation) %>% + dplyr::filter(time >= start_point_function_generator_pulse & + time < end_point_function_generator_pulse) + + mean_value <- mean(df_dummy$U) + } }else{ df_dummy <- input_data_filtered %>% - dplyr::filter(Channel == channel_stimulation_pulse) %>% - dplyr::filter(time >= start_point_function_generator_pulse & - time < end_point_function_generator_pulse) + dplyr::filter(Channel == channel_stimulation) mean_value <- mean(df_dummy$U) } + mean_value_value_text <- paste("V_mean=", format(round(mean_value, digits = 1), nsmall = 1), "V", sep="") @@ -253,14 +300,15 @@ plotWaveforms <- function(input_data = NULL, plot_waveform <- ggplot2::ggplot(data = input_data_filtered, aes(x = time, y = U, color=Channel)) + geom_line() + - 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) + + geom_hline(yintercept=max_value, linetype="dashed", color = "darkgray", linewidth=1) + + geom_hline(yintercept=min_value, linetype="dashed", color = "darkgray", linewidth=1) + + geom_hline(yintercept=mean_value, linetype="dotdash", color = "darkgray", linewidth=1) + annotate("text", x=plot_annotation_x, y=(voltage_limits_of_plot-1), label=p2p_value, color = "darkgray") + annotate("text", x=plot_annotation_x_minmax, y=(max_value-1), label=max_value_text, color = "darkgray") + annotate("text", x=plot_annotation_x_minmax, y=(min_value+1), label=min_value_text, color = "darkgray") + annotate("text", x=plot_annotation_x, y=-0.5, label=mean_value_value_text, color = "darkgray") + coord_cartesian(ylim = c(-voltage_limits_of_plot, voltage_limits_of_plot)) + + scale_y_continuous(breaks = scales::breaks_pretty()) + # 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=" "), @@ -300,19 +348,19 @@ plotWaveforms <- function(input_data = NULL, # Plot all waveforms in one image ######################################## # Get max and min (smoothed) of all stimulation pulses ################# - lower_bound <- (max(input_data$U[input_data$Channel == channel_stimulation_pulse]) - - mean(input_data$U[input_data$Channel == channel_stimulation_pulse]))/2 - upper_bound <- (min(input_data$U[input_data$Channel == channel_stimulation_pulse]) - - mean(input_data$U[input_data$Channel == channel_stimulation_pulse]))/2 + lower_bound <- (max(input_data$U[input_data$Channel == channel_stimulation]) + + mean(input_data$U[input_data$Channel == channel_stimulation]))/2 + upper_bound <- (min(input_data$U[input_data$Channel == channel_stimulation]) + + mean(input_data$U[input_data$Channel == channel_stimulation]))/2 max_value <- as.numeric(quantile(input_data$U[ - input_data$Channel == channel_stimulation_pulse & input_data$U > lower_bound], + input_data$Channel == channel_stimulation & input_data$U > lower_bound], 0.99, na.rm = TRUE)) min_value <- as.numeric(quantile(input_data$U[ - input_data$Channel == channel_stimulation_pulse & input_data$U < upper_bound], + input_data$Channel == channel_stimulation & input_data$U < upper_bound], 0.01, na.rm = TRUE)) - p2p_value <- abs(max_value) + abs(min_value) + p2p_value <- max_value - min_value if(is.na(p2p_value)){ p2p_value <- 0 @@ -325,20 +373,28 @@ plotWaveforms <- function(input_data = NULL, # Calculate mean - if(start_point_function_generator_pulse == end_point_function_generator_pulse){ - df_dummy <- input_data %>% - dplyr::filter(Channel == channel_stimulation_pulse) - - mean_value <- mean(df_dummy$U) + if(waveform_of_function_generator == "retangular"){ + if(start_point_function_generator_pulse == end_point_function_generator_pulse){ + df_dummy <- input_data %>% + dplyr::filter(Channel == channel_stimulation) + + mean_value <- mean(df_dummy$U) + }else{ + df_dummy <- input_data %>% + dplyr::filter(Channel == channel_stimulation) %>% + dplyr::filter(time >= start_point_function_generator_pulse & + time < end_point_function_generator_pulse) + + mean_value <- mean(df_dummy$U) + } }else{ df_dummy <- input_data %>% - dplyr::filter(Channel == channel_stimulation_pulse) %>% - dplyr::filter(time >= start_point_function_generator_pulse & - time < end_point_function_generator_pulse) + dplyr::filter(Channel == channel_stimulation) mean_value <- mean(df_dummy$U) } + mean_value_value_text <- paste("V_mean=", format(round(mean_value, digits = 1), nsmall = 1), "V", sep="") @@ -353,18 +409,20 @@ plotWaveforms <- function(input_data = NULL, input_data$time_of_experiment_in_minutes <- as.double(difftime( input_data$date_time, start_time, units = "mins")) + max_time_in_min <- ceiling(max(input_data$time_of_experiment_in_minutes)) + plot_waveform <- ggplot2::ggplot(data = input_data) + scattermore::geom_scattermore(aes(x = time, y = U, color=time_of_experiment_in_minutes), - data = . %>% filter(Channel == channel_stimulation_pulse), + data = . %>% filter(Channel == channel_stimulation), alpha = 1, pointsize = 0.6)+ - scale_color_viridis_c(name = "Time of\nexperiment\nin min.", direction = -1, limits = c(0, 120), option = "C") + + scale_color_viridis_c(name = "Time of\nexperiment\nin min.", direction = -1, limits = c(0, max_time_in_min), option = "C") + geom_line(aes(x = time, y = U, linetype = "channel_function_generator"), color = "#00BFC4", data = . %>% filter(Channel == channel_function_generator) %>% group_by(time) %>% mutate(U=mean(U)), linewidth=0.6) + - geom_line(aes(x = time, y = U, linetype = "channel_stimulation_pulse"), color = "#F8766D", - data = . %>% filter(Channel == channel_stimulation_pulse) %>% group_by(time) %>% mutate(U=mean(U)), + geom_line(aes(x = time, y = U, linetype = "channel_stimulation"), color = "#F8766D", + data = . %>% filter(Channel == channel_stimulation) %>% group_by(time) %>% mutate(U=mean(U)), linewidth=0.6) + - scale_linetype_manual(name = "Channel", values=c("channel_function_generator" = "solid", "channel_stimulation_pulse" = "dashed"), labels = c("FunGen", "ES (mean)")) + + scale_linetype_manual(name = "Channel", values=c("channel_function_generator" = "solid", "channel_stimulation" = "dashed"), labels = c("FunGen", "ES (mean)")) + guides(linetype = guide_legend(override.aes = list(color=c("#00BFC4", "#F8766D"), linetype=c(1,2)), order = 1 )) + geom_hline(yintercept=max_value, linetype="dashed", color = "darkgray") + geom_hline(yintercept=min_value, linetype="dashed", color = "darkgray") + @@ -374,6 +432,7 @@ plotWaveforms <- function(input_data = NULL, annotate("text", x=plot_annotation_x_minmax, y=(min_value-1), label=min_value_text, color = "darkgray") + annotate("text", x=plot_annotation_x, y=-0.5, label=mean_value_value_text, color = "darkgray") + coord_cartesian(ylim = c(-voltage_limits_of_plot, voltage_limits_of_plot)) + + scale_y_continuous(breaks = scales::breaks_pretty()) + labs(title=plot_title, x = xaxis_lab, y = "U/V") + theme_bw() + diff --git a/man/convertMeasurementListToTibble.Rd b/man/convertMeasurementListToTibble.Rd index 987669d..0dca207 100644 --- a/man/convertMeasurementListToTibble.Rd +++ b/man/convertMeasurementListToTibble.Rd @@ -9,8 +9,6 @@ convertMeasurementListToTibble(df_data_list) } \arguments{ \item{df_data_list}{A list (list of data)} - -\item{variabel_name}{A character ()} } \value{ A tibble with the data @@ -23,5 +21,5 @@ This function takes a list with oscilloscope readings saved as yaml and coverts it to a tibble } \author{ -Kai Budde +Kai Budde-Sagert } diff --git a/man/getMeasurements.Rd b/man/getMeasurements.Rd index f7b560c..4598ce0 100644 --- a/man/getMeasurements.Rd +++ b/man/getMeasurements.Rd @@ -23,5 +23,5 @@ This function reads yaml files (from a directory) or a zip file and imports the data into a tibble } \author{ -Kai Budde +Kai Budde-Sagert } diff --git a/man/getWaveforms.Rd b/man/getWaveforms.Rd index f257395..1aa9b75 100644 --- a/man/getWaveforms.Rd +++ b/man/getWaveforms.Rd @@ -5,12 +5,19 @@ \alias{getwaveforms} \title{getWaveforms} \usage{ -getWaveforms(input_file = NULL, input_directory = NULL) +getWaveforms( + input_file = NULL, + input_directory = NULL, + reduce_resolution_of_input_data = TRUE +) } \arguments{ \item{input_file}{A character (path to yaml file to be converted)} \item{input_directory}{A character (path to directory with yaml files)} + +\item{reduce_resolution_of_input_data}{A Boolean (showing if fewer point +are to be imported)} } \value{ A tibble with the data @@ -23,5 +30,5 @@ This function reads yaml files (from a directory) or a zip file and imports the waveform data into a tibble } \author{ -Kai Budde +Kai Budde-Sagert } diff --git a/man/plotMeasurements.Rd b/man/plotMeasurements.Rd index afd2dda..2941842 100644 --- a/man/plotMeasurements.Rd +++ b/man/plotMeasurements.Rd @@ -41,5 +41,5 @@ This functions takes recorded oscilloscope data (Vavg and Vpp) (obtained through getMeasurements.R) and plots it } \author{ -Kai Budde +Kai Budde-Sagert } diff --git a/man/plotWaveforms.Rd b/man/plotWaveforms.Rd index a92dbf2..af601b9 100644 --- a/man/plotWaveforms.Rd +++ b/man/plotWaveforms.Rd @@ -9,13 +9,20 @@ plotWaveforms( input_data = NULL, output_dir = NULL, show_time_in_us = FALSE, + waveform_of_function_generator = "rectangular", channel_function_generator = "CHAN1", channel_stimulation_pulse = "CHAN2", + channel_resistor = NA, plot_waveforms = "all", voltage_limits_of_plot = NULL, filter_stim_off = TRUE, + shunt_resistance = NA, + plot_current_calculation = FALSE, + y_position_current_text = NA, epsilon_for_filtering = 1.5, - plot_title = NULL + plot_title = NULL, + print_width = 19.2, + print_height = 10.8 ) } \arguments{ @@ -26,23 +33,40 @@ plotWaveforms( \item{show_time_in_us}{A boolean (indicates whether time is to be shown in us)} +\item{waveform_of_function_generator}{A character (waveform of the signal: either sinusoidal or rectangular)} + \item{channel_function_generator}{A character (name of the channel with the function generator pulse (for GATE TTL))} \item{channel_stimulation_pulse}{A character (name of the channel with the stimulation pulse)} +\item{channel_resistor}{A character (name of the channel that recorded the +voltage drop across a shunt resistor)} + \item{plot_waveforms}{A character (shows if all, none, or one waveform shall be plotted)} \item{voltage_limits_of_plot}{A number (value of min (negativ of the value) and max values to be shown on y axis)} -\item{filter_stim_off}{A boolean (indicates whether measurements while +\item{filter_stim_off}{A Boolean (indicates whether measurements while stim==off are to be filtered)} +\item{shunt_resistance}{A number (Resistance in ohm)} + +\item{plot_current_calculation}{A Boolean (plot min and max of measurements +using the ohmic resistance of the shunt resistor)} + +\item{y_position_current_text}{A number (y position for displaying current +measurement text).} + \item{epsilon_for_filtering}{A number (min. distance for mean from 0 needed for filtering out the signal generator)} \item{plot_title}{A character (title of the plot)} + +\item{print_width}{A number (width in cm of the saved plot)} + +\item{print_height}{A number (height in cm of the saved plot)} } \value{ 0 @@ -55,5 +79,5 @@ This functions takes recorded oscilloscope data (waveforms) (obtained through getWaveforms.R) and plots it } \author{ -Kai Budde +Kai Budde-Sagert }