Updated all functions to use the new ProvideSampleId() function.

master
Taha Ahmed 14 years ago
parent 6469feb7be
commit 1dc00af759

49
CHI.R

@ -3,6 +3,7 @@
# Taha Ahmed, Jan 2011 - Feb 2011 # Taha Ahmed, Jan 2011 - Feb 2011
# CONTENTS # CONTENTS
source("/home/taha/chepec/chetex/common/R/common.R")
# >>>> ocp2df # >>>> ocp2df
# >>>> chronocm2df # >>>> chronocm2df
# >>>> chronoamp2df # >>>> chronoamp2df
@ -26,6 +27,8 @@ ocp2df <- function(datafilename) {
chifile <- readLines(datafile, n = -1) #read all lines of input file chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile) close(datafile)
# #
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]" rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line. # regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen), # Matches numbers with or without a negative sign (hyphen),
@ -53,11 +56,11 @@ ocp2df <- function(datafilename) {
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(matrix(scan(zz, what = numeric(), sep = ","), data.frame(sampleid, matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T))) ncol = 2, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("time", "potential") names(ff) <- c("sampleid", "time", "potential")
# #
### Collect attributes of this experiment ### Collect attributes of this experiment
# These attributes are specific for each kind of experiment, # These attributes are specific for each kind of experiment,
@ -91,6 +94,8 @@ chronocm2df <- function(datafilename) {
chifile <- readLines(datafile, n = -1) #read all lines of input file chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile) close(datafile)
# #
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]" rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line. # regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen), # Matches numbers with or without a negative sign (hyphen),
@ -118,12 +123,12 @@ chronocm2df <- function(datafilename) {
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(step = factor(s), data.frame(sampleid, step = factor(s),
matrix(scan(zz, what = numeric(), sep = ","), matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T))) ncol = 2, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("step", "time", "charge") names(ff) <- c("sampleid", "step", "time", "charge")
# #
### Collect attributes of this experiment ### Collect attributes of this experiment
# These attributes are specific for each kind of experiment, # These attributes are specific for each kind of experiment,
@ -168,6 +173,8 @@ chronoamp2df <- function(datafilename, wearea = 1) {
chifile <- readLines(datafile, n = -1) #read all lines of input file chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile) close(datafile)
# #
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]" rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line. # regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen), # Matches numbers with or without a negative sign (hyphen),
@ -195,12 +202,12 @@ chronoamp2df <- 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(step = factor(s), data.frame(sampleid, step = factor(s),
matrix(scan(zz, what = numeric(), sep = ","), matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T))) ncol = 2, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("step", "time", "current") names(ff) <- c("sampleid", "step", "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)
@ -252,6 +259,8 @@ amperometry2df <- function(datafilename, wearea = 1) {
chifile <- readLines(datafile, n = -1) #read all lines of input file chifile <- readLines(datafile, n = -1) #read all lines of input file
close(datafile) close(datafile)
# #
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+[e,]" rgxp.number <- "^\\-?\\d\\.\\d+[e,]"
# regexp that matches a decimal number at the beginning of the line. # regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen), # Matches numbers with or without a negative sign (hyphen),
@ -279,11 +288,11 @@ 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(matrix(scan(zz, what = numeric(), sep = ","), data.frame(sampleid, matrix(scan(zz, what = numeric(), sep = ","),
ncol = 2, byrow = T))) ncol = 2, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("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)
@ -318,16 +327,18 @@ amperometry2df <- function(datafilename, wearea = 1) {
################################################## ##################################################
#################### cv2df ####################### #################### cv2df #######################
################################################## ##################################################
cv2df <- function(cvfilename, wearea = 1) { cv2df <- function(datafilename, wearea = 1) {
# Function description: # Function description:
# CH Instruments potentiostat records all data using standard SI units, # CH Instruments potentiostat records all data using standard SI units,
# so all potential values are in volts, currents are in amperes, # so all potential values are in volts, currents are in amperes,
# charges in Coulombs, time in seconds, etc. # charges in Coulombs, time in seconds, etc.
# #
cvfile <- file(cvfilename, "r") cvfile <- file(datafilename, "r")
chifile <- readLines(cvfile, n = -1) #read all lines of input file chifile <- readLines(cvfile, n = -1) #read all lines of input file
close(cvfile) close(cvfile)
# #
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+," rgxp.number <- "^\\-?\\d\\.\\d+,"
# regexp that matches a decimal number at the beginning of the line. # regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen), # Matches numbers with or without a negative sign (hyphen),
@ -355,15 +366,15 @@ cv2df <- function(cvfilename, 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(segment = factor(s), cycle = factor(ceiling(s/2)), data.frame(sampleid, segment = factor(s), cycle = factor(ceiling(s/2)),
matrix(scan(zz, what = numeric(), sep = ","), matrix(scan(zz, what = numeric(), sep = ","),
ncol = 3, byrow = T))) ncol = 3, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("segment", "cycle", "potential", "current", "charge") names(ff) <- c("sampleid", "segment", "cycle", "potential", "current", "charge")
# Calculate current density # Calculate current density
currentdensity <- ff$current / wearea currentdensity <- ff$current / wearea
ff <- cbind(ff[, 1:4], currentdensity = currentdensity, ff[, 5]) ff <- cbind(ff[, 1:5], currentdensity = currentdensity, charge = ff[, 6])
# #
### Collect attributes of this experiment ### Collect attributes of this experiment
# These attributes are specific for each kind of experiment, # These attributes are specific for each kind of experiment,
@ -403,16 +414,18 @@ cv2df <- function(cvfilename, wearea = 1) {
################################################## ##################################################
################### lsv2df ####################### ################### lsv2df #######################
################################################## ##################################################
lsv2df <- function(lsvfilename, wearea = 1) { lsv2df <- function(datafilename, wearea = 1) {
# Function description: # Function description:
# CH Instruments potentiostat records all data using standard SI units, # CH Instruments potentiostat records all data using standard SI units,
# so all potential values are in volts, currents are in amperes, # so all potential values are in volts, currents are in amperes,
# charges in Coulombs, time in seconds, etc. # charges in Coulombs, time in seconds, etc.
# #
lsvfile <- file(lsvfilename, "r") lsvfile <- file(datafilename, "r")
chifile <- readLines(lsvfile, n = -1) #read all lines of input file chifile <- readLines(lsvfile, n = -1) #read all lines of input file
close(lsvfile) close(lsvfile)
# #
sampleid <- ProvideSampleId(datafilename)
#
rgxp.number <- "^\\-?\\d\\.\\d+," rgxp.number <- "^\\-?\\d\\.\\d+,"
# regexp that matches a decimal number at the beginning of the line. # regexp that matches a decimal number at the beginning of the line.
# Matches numbers with or without a negative sign (hyphen), # Matches numbers with or without a negative sign (hyphen),
@ -440,15 +453,15 @@ lsv2df <- function(lsvfilename, 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(segment = factor(s), data.frame(sampleid, segment = factor(s),
matrix(scan(zz, what = numeric(), sep = ","), matrix(scan(zz, what = numeric(), sep = ","),
ncol = 3, byrow = T))) ncol = 3, byrow = T)))
close(zz) close(zz)
} }
names(ff) <- c("segment", "potential", "current", "charge") names(ff) <- c("sampleid", "segment", "potential", "current", "charge")
# Calculate current density # Calculate current density
currentdensity <- ff$current / wearea currentdensity <- ff$current / wearea
ff <- cbind(ff[, 1:3], currentdensity = currentdensity, ff[, 4]) ff <- cbind(ff[, 1:4], currentdensity = currentdensity, charge = ff[, 5])
# #
### Collect attributes of this experiment ### Collect attributes of this experiment
# These attributes are specific for each kind of experiment, # These attributes are specific for each kind of experiment,

@ -3,6 +3,7 @@
# Taha Ahmed, Feb 2011 # Taha Ahmed, Feb 2011
# CONTENTS # CONTENTS
source("/home/taha/chepec/chetex/common/R/common.R")
# >>>> Raman2df # >>>> Raman2df
@ -20,23 +21,7 @@ Raman2df <- function(datafilename) {
close(datafile) close(datafile)
# #
##### #####
# A nice algorithm that extracts the filename from the datafilename argument sampleid <- ProvideSampleId(datafilename)
# and uses that as a sampleid in the returned dataframe
# THIS SHOULD PROBABLY BE CONVERTED INTO A STAND-ALONE FUNCTION
# Also make sure it works for vectors as well as single strings
#####
rgxp.sampleid <- "[^/]*(?=\\.\\w*)" ## THIS REQUIRES perl=TRUE
# Regular expression that extracts the filename out of a full path.
# Matches and extracts everything from the last forward slash (assuming Unix slashes)
# up until a dot folllowed by an arbitrary number of alphanumeric characters.
sampleidmtch <- regexpr(rgxp.sampleid, datafilename, perl=TRUE)
# Check that there was a match
if (sampleidmtch < 0) {
# -1 means no match
sampleid <- datafilename
# If match was unsuccessful we use the argument as passed to this function as sampleid
}
sampleid <- substr(datafilename, sampleidmtch, (sampleidmtch + attr(sampleidmtch, "match.length") - 1))
# #
ff <- data.frame(NULL) ff <- data.frame(NULL)
zz <- textConnection(chifile, "r") zz <- textConnection(chifile, "r")

Loading…
Cancel
Save