Major update in CHI.R, function amperometry2df() improved, fixed.

There was an error in amperometry2df(), whereby the timediff calculated
was way too low. Was possibly related to the It2charge() function
used to calculate those values. That function (in common.R) is no longer
used by amperometry2df().
Also updated amperometry2df() so it now includes the data attributes
in the return dataframe itself, as columns (compare cv2df()).

common.R: do not use the It2charge() function in the future.
Do such calculations inline instead.
master
Taha Ahmed 13 years ago
parent fd62a103c8
commit bd2b1731fc

101
CHI.R

@ -250,10 +250,38 @@ chronoamp2df <- function(datafilename, wearea = 1) {
############### amperometry2df ################### ############### amperometry2df ###################
################################################## ##################################################
amperometry2df <- function(datafilename, wearea = 1) { amperometry2df <- function(datafilename, wearea = 1) {
# Function description: for recorded amperometric i-T curves ## Description:
# CH Instruments potentiostat records all data using standard SI units, ## Reads current-time data (from CHI 760 potentiostat)
# so all potential values are in volts, currents are in amperes, ## and returns a dataframe with the data,
# charges in Coulombs, time in seconds, etc. ## the data attributes (experimental conditions),
## and some calculated parameters (charge, didt, etc.)
## Usage:
## amperometry2df(datafilename, wearea)
## Arguments:
## datafilename: text string with full path to experimental file
## wearea: (optional) area of working electrode (in square centimeter)
## Value:
## Dataframe with the following columns (and no extra attributes):
## $ sampleid : chr
## $ time : num
## $ current : num
## $ currentdensity : num
## $ timediff : num
## $ dIdt : num
## $ didt : num
## $ charge : num
## $ chargedensity : num
## $ InitE : num
## $ SampleInterval : num
## $ RunTime : num
## $ QuietTime : num
## $ Sensitivity : num
## Note:
## The CH Instruments 760 potentiostat records all data
## using standard SI units, therefore this function
## assumes all potential values to be in volts,
## currents to be in amperes, charges in Coulombs,
## time in seconds, and so on.
# #
datafile <- file(datafilename, "r") datafile <- file(datafilename, "r")
chifile <- readLines(datafile, n = -1) #read all lines of input file chifile <- readLines(datafile, n = -1) #read all lines of input file
@ -288,39 +316,54 @@ amperometry2df <- function(datafilename, wearea = 1) {
for (s in 1:length(starts)) { for (s in 1:length(starts)) {
zz <- textConnection(chifile[starts[s]:ends[s]], "r") zz <- textConnection(chifile[starts[s]:ends[s]], "r")
ff <- rbind(ff, ff <- rbind(ff,
data.frame(sampleid, matrix(scan(zz, what = numeric(), sep = ","), data.frame(stringsAsFactors = FALSE,
ncol = 2, byrow = T))) sampleid, matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("sampleid", "time", "current") names(ff) <- c("sampleid", "time", "current")
# Calculate current density # Calculate current density
currentdensity <- ff$current / wearea currentdensity <- ff$current / wearea
ff <- cbind(ff, currentdensity = currentdensity) ff <- cbind(ff, currentdensity = currentdensity)
# Calculate charge densities and differentials # Calculate time and current diffs
charge.df <- It2charge(ff$currentdensity, ff$time) timediff <- c(ff$time[1], diff(ff$time))
ff <- cbind(ff, charge.df) currentdiff <- c(ff$current[1], diff(ff$current))
currentdensitydiff <- c(ff$currentdensity[1], diff(ff$currentdensity))
# Calculate differential of current and current density
dIdt <- currentdiff / timediff
didt <- currentdensitydiff / timediff
# Calculate charge and charge density
charge <- cumsum(ff$current)
chargedensity <- cumsum(ff$currentdensity)
# Update ff dataframe
ff <- cbind(ff,
timediff = timediff,
dIdt = dIdt,
didt = didt,
charge = charge,
chargedensity = chargedensity)
# #
### Collect attributes of this experiment ### Collect attributes of this experiment
# These attributes are specific for each kind of experiment, # InitE (volt)
# be careful when adapting to other electrochemical data position.InitE <- regexpr("^Init\\sE\\s\\(V\\)", chifile)
rgxp.attr <- c("^Init\\sE\\s\\(V\\)", InitE <- as.numeric(strsplit(chifile[which(position.InitE == 1)], "\\s=\\s")[[1]][2])
"^Sample\\sInterval\\s\\(s\\)", ff$InitE <- InitE
"^Run\\sTime\\s\\(sec\\)", # SampleInterval (volt)
"^Quiet\\sTime\\s\\(sec\\)", position.SampleInterval <- regexpr("^Sample\\sInterval\\s\\(s\\)", chifile)
"^Sensitivity\\s\\(A/V\\)") SampleInterval <- as.numeric(strsplit(chifile[which(position.SampleInterval == 1)], "\\s=\\s")[[1]][2])
names.attr <- c("InitE", ff$SampleInterval <- SampleInterval
"SamplingInterval", # Run time (seconds)
"RunTime", position.RunTime <- regexpr("^Run\\sTime\\s\\(sec\\)", chifile)
"QuietTime", RunTime <- as.numeric(strsplit(chifile[which(position.RunTime == 1)], "\\s=\\s")[[1]][2])
"Sensitivity") ff$RunTime <- RunTime
for (n in 1:length(rgxp.attr)) { # Quiet time (seconds)
attrow.idx <- regexpr(rgxp.attr[n], chifile) position.QuietTime <- regexpr("^Quiet\\sTime\\s\\(sec\\)", chifile)
attrow.len <- attr(attrow.idx, "match.length") QuietTime <- as.numeric(strsplit(chifile[which(position.QuietTime == 1)], "\\s=\\s")[[1]][2])
attr(attrow.idx, "match.length") <- NULL ff$QuietTime <- QuietTime
# attrow.idx should now contain only one matching row # Sensitivity (ampere per volt)
attr(ff, names.attr[n]) <- strsplit(chifile[which(attrow.idx == 1)], position.Sensitivity <- regexpr("^Sensitivity\\s\\(A/V\\)", chifile)
"\\s=\\s")[[1]][2] Sensitivity <- as.numeric(strsplit(chifile[which(position.Sensitivity == 1)], "\\s=\\s")[[1]][2])
} ff$Sensitivity <- Sensitivity
# #
return(ff) return(ff)
} }

@ -70,6 +70,7 @@ int2padstr <- function (ii, pchr, w) {
################### It2charge #################### ################### It2charge ####################
################################################## ##################################################
It2charge <- function (time, current) { It2charge <- function (time, current) {
## **** STOP USING THIS FUNCTION *** CAUSED WEIRD, UNREPRODUCIBLE ERRORS /110304
## Description: ## Description:
## Calculates cumulative charge, differentials, etc. from ## Calculates cumulative charge, differentials, etc. from
## amperometric data (current and time). ## amperometric data (current and time).
@ -96,6 +97,7 @@ It2charge <- function (time, current) {
# Return value # Return value
ff <- data.frame(timediff = timediff, ff <- data.frame(timediff = timediff,
dIdt = dIdt, charge = charge, dIdt = dIdt, charge = charge,
# perhaps it is more correct to calculate cumsum of the absolute charge?
sumcharge = cumsum(charge)) sumcharge = cumsum(charge))
return(ff) return(ff)
} }

Loading…
Cancel
Save