Skip to content

Commit

Permalink
add possibility to plot current through measurement at shunt resistor
Browse files Browse the repository at this point in the history
  • Loading branch information
buddekai committed Feb 15, 2024
1 parent 162b59e commit 95c2e49
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 23 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: oscilliscopeR
Type: Package
Title: Import and work with data from oscilloscope
Version: 0.1.3
Version: 0.1.4
Author: c(person("Kai", "Budde", email = "[email protected]",
role = c("aut", "cre"))
Maintainer: Kai Budde <[email protected]>
Expand Down
2 changes: 1 addition & 1 deletion R/convertMeasurementListToTibble.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @details This function takes a list with oscilloscope readings saved
#' as yaml and coverts it to a tibble
#' @aliases convertmeasurementlisttotibble
#' @author Kai Budde
#' @author Kai Budde-Sagert
#' @export convertMeasurementListToTibble
#' @param df_data_list A list (list of data)
#' @param variabel_name A character ()
Expand Down
2 changes: 1 addition & 1 deletion R/getMeasurements.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @details This function reads yaml files (from a directory) or a zip file
#' and imports the data into a tibble
#' @aliases getmeasurements
#' @author Kai Budde
#' @author Kai Budde-Sagert
#' @export getMeasurements
#' @param input_file A character (path to yaml file to be converted)
#' @param input_directory A character (path to directory with yaml files)
Expand Down
2 changes: 1 addition & 1 deletion R/getWaveforms.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @details This function reads yaml files (from a directory) or a zip file
#' and imports the waveform data into a tibble
#' @aliases getwaveforms
#' @author Kai Budde
#' @author Kai Budde-Sagert
#' @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)
Expand Down
2 changes: 1 addition & 1 deletion R/plotMeasurements.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @details This functions takes recorded oscilloscope data (Vavg and Vpp)
#' (obtained through getMeasurements.R) and plots it
#' @aliases plotmeasurements
#' @author Kai Budde
#' @author Kai Budde-Sagert
#' @export plotMeasurements
#' @param input_data A tibble (measurement data)
#' @param output_dir A character (path to output directory)
Expand Down
81 changes: 68 additions & 13 deletions R/plotWaveforms.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' @details This functions takes recorded oscilloscope data (waveforms)
#' (obtained through getWaveforms.R) and plots it
#' @aliases plotwaveforms
#' @author Kai Budde
#' @author Kai Budde-Sagert
#' @export plotWaveforms
#' @param input_data A tibble (measurement data)
#' @param output_dir A character (path to output directory)
Expand All @@ -13,13 +13,22 @@
#' the stimulation pulse)
#' @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
#' voltage drop across a shunt resistor)
#' @param voltage_limits_of_plot A number (value of min (negativ of the value)
#' and max values to be shown on y axis)
#' @param plot_waveforms A character (shows if all, none, or one waveform shall be plotted)
#' @param filter_stim_off A boolean (indicates whether measurements while
#' @param filter_stim_off A Boolean (indicates whether measurements while
#' stim==off are to be filtered)
#' @param shunt_resistance A number (Resistance in ohm)
#' @param plot_current_calculation A Boolean (plot min and max of measurements
#' using the ohmic resistance of the shunt resistor)
#' @param y_position_current_text A number (y position for displaying current
#' measurement text).
#' @param epsilon_for_filtering A number (min. distance for mean from 0 needed for filtering out the signal generator)
#' @param plot_title A character (title of the plot)
#' @param print_width A number (width in cm of the saved plot)
#' @param print_height A number (height in cm of the saved plot)
#' @return 0

# Created: 2022/04/21
Expand All @@ -30,11 +39,17 @@ plotWaveforms <- function(input_data = NULL,
show_time_in_us = FALSE,
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) {

# Some function parameters
factor_for_min_max_scaling <- 1.1
Expand Down Expand Up @@ -185,6 +200,32 @@ plotWaveforms <- function(input_data = NULL,
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="")

# 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]) -
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]) -
mean(input_data_filtered$U[input_data_filtered$Channel == channel_resistor]))/2

max_value_resistor <- as.numeric(quantile(input_data_filtered$U[
input_data_filtered$Channel == channel_resistor & input_data_filtered$U > lower_bound],
0.99, na.rm = TRUE))
min_value_resistor <- as.numeric(quantile(input_data_filtered$U[
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)
if(is.na(p2p_value_resistor)){
p2p_value_resistor <- 0
}
p2p_value_resistor <- format(round(p2p_value_resistor, digits = 1), nsmall = 1)
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="")
}


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

if(start_point_function_generator_pulse == end_point_function_generator_pulse){
Expand Down Expand Up @@ -215,10 +256,10 @@ 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-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) +
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)) +
# labs(title=paste(plot_title_without_date, input_data_filtered$date_time[1], sep=" "),
# x = xaxis_lab, y = "U/V") +
Expand All @@ -228,14 +269,28 @@ plotWaveforms <- function(input_data = NULL,
theme(axis.text.x = element_text(angle=90, vjust = 0.5),
plot.title = element_text(hjust = 0.5))

if(plot_current_calculation){
plot_annotation_resistor_x <- 0.9*max(input_data_filtered$time)

if(is.na(y_position_current_text)){
y_position_current_text <- 1.1*max_value_resistor
}

plot_waveform <- plot_waveform +
geom_hline(yintercept=max_value_resistor, linetype="dotted", color = "darkgoldenrod", size=1) +
geom_hline(yintercept=min_value_resistor, linetype="dotted", color = "darkgoldenrod", size=1) +
annotate("text", x=plot_annotation_resistor_x, y=y_position_current_text, label=max_resistor_value_text, color = "darkgoldenrod") +
annotate("text", x=plot_annotation_resistor_x, y=-y_position_current_text, label=min_resistor_value_text, color = "darkgoldenrod")
}

# Date and time as writable string
date_time_measurement <- input_data_filtered$date_time[1]
date_time_measurement <- gsub(pattern = " ", replacement = "_", x = date_time_measurement)
date_time_measurement <- gsub(pattern = ":", replacement = "", x = date_time_measurement)

# Save files
file_path <- file.path(output_dir, paste0(date_time_measurement, "_", name_for_saving_without_date, ".png"))
ggsave(plot = plot_waveform, filename = file_path, device = "png", width = 19.2, height = 10.8, units = "cm")
ggsave(plot = plot_waveform, filename = file_path, device = "png", width = print_width, height = print_height, units = "cm")
}

rm(i)
Expand Down Expand Up @@ -314,10 +369,10 @@ 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-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) +
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)) +
labs(title=plot_title,
x = xaxis_lab, y = "U/V") +
Expand All @@ -327,7 +382,7 @@ plotWaveforms <- function(input_data = NULL,

# Save files
file_path <- file.path(output_dir, paste0(name_for_saving, "_all_in_one.png"))
ggsave(plot = plot_waveform, filename = file_path, device = "png", width = 19.2, height = 10.8, units = "cm")
ggsave(plot = plot_waveform, filename = file_path, device = "png", width = print_width, height = print_height, units = "cm")

return(invisible(NULL))
}
Expand Down
10 changes: 5 additions & 5 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
# Author: Kai Budde-Sagert
# Created: 2022/04/19
# Last changed: 2023/08/01
# Last changed: 2024/02/14

# Delete everything in the environment
rm(list = ls())
Expand All @@ -20,7 +20,7 @@ if(!any(grepl(pattern = "groundhog", x = installed.packages(), ignore.case = TRU
# Load packages
# Load packages
library(groundhog)
pkgs <- c("tidyverse", "yaml", "scattermore", "devtools")
pkgs <- c("tidyverse", "yaml", "scattermore", "devtools", "lubridate")
groundhog.library(pkgs, groundhog.day)

# Package development ######################################################
Expand All @@ -38,7 +38,7 @@ devtools::load_all()
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# ZIP File (containing yaml files)
input_zip_file <- file.path("inst", "testdata","recorded_stimulation.zip")

input_zip_file <- file.path("inst", "testdata","S16_20230323_ShortStimCellBio_MediumMediated.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"
Expand All @@ -63,7 +63,7 @@ df_data <- getWaveforms(input_file = input_zip_file)

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



Expand Down

0 comments on commit 95c2e49

Please sign in to comment.