From 1dc00af7593d09829c4f7c38b5e4b2b061ebe73c Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Tue, 8 Mar 2011 18:05:49 +0100 Subject: [PATCH] Updated all functions to use the new ProvideSampleId() function. --- CHI.R | 49 +++++++++++++++++++++++++++++++------------------ Renishaw.R | 19 ++----------------- xrdtf.R | 0 3 files changed, 33 insertions(+), 35 deletions(-) mode change 100755 => 100644 xrdtf.R diff --git a/CHI.R b/CHI.R index 5e13ae6..8994f2e 100644 --- a/CHI.R +++ b/CHI.R @@ -3,6 +3,7 @@ # Taha Ahmed, Jan 2011 - Feb 2011 # CONTENTS +source("/home/taha/chepec/chetex/common/R/common.R") # >>>> ocp2df # >>>> chronocm2df # >>>> chronoamp2df @@ -26,6 +27,8 @@ ocp2df <- function(datafilename) { chifile <- readLines(datafile, n = -1) #read all lines of input file close(datafile) # + sampleid <- ProvideSampleId(datafilename) + # rgxp.number <- "^\\-?\\d\\.\\d+[e,]" # regexp that matches a decimal number at the beginning of the line. # Matches numbers with or without a negative sign (hyphen), @@ -53,11 +56,11 @@ ocp2df <- function(datafilename) { for (s in 1:length(starts)) { zz <- textConnection(chifile[starts[s]:ends[s]], "r") ff <- rbind(ff, - data.frame(matrix(scan(zz, what = numeric(), sep = ","), + data.frame(sampleid, matrix(scan(zz, what = numeric(), sep = ","), ncol = 2, byrow = T))) close(zz) } - names(ff) <- c("time", "potential") + names(ff) <- c("sampleid", "time", "potential") # ### Collect attributes of this 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 close(datafile) # + sampleid <- ProvideSampleId(datafilename) + # rgxp.number <- "^\\-?\\d\\.\\d+[e,]" # regexp that matches a decimal number at the beginning of the line. # Matches numbers with or without a negative sign (hyphen), @@ -118,12 +123,12 @@ chronocm2df <- function(datafilename) { for (s in 1:length(starts)) { zz <- textConnection(chifile[starts[s]:ends[s]], "r") ff <- rbind(ff, - data.frame(step = factor(s), + data.frame(sampleid, step = factor(s), matrix(scan(zz, what = numeric(), sep = ","), ncol = 2, byrow = T))) close(zz) } - names(ff) <- c("step", "time", "charge") + names(ff) <- c("sampleid", "step", "time", "charge") # ### Collect attributes of this 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 close(datafile) # + sampleid <- ProvideSampleId(datafilename) + # rgxp.number <- "^\\-?\\d\\.\\d+[e,]" # regexp that matches a decimal number at the beginning of the line. # Matches numbers with or without a negative sign (hyphen), @@ -195,12 +202,12 @@ chronoamp2df <- function(datafilename, wearea = 1) { for (s in 1:length(starts)) { zz <- textConnection(chifile[starts[s]:ends[s]], "r") ff <- rbind(ff, - data.frame(step = factor(s), + data.frame(sampleid, step = factor(s), matrix(scan(zz, what = numeric(), sep = ","), ncol = 2, byrow = T))) close(zz) } - names(ff) <- c("step", "time", "current") + names(ff) <- c("sampleid", "step", "time", "current") # Calculate current density currentdensity <- ff$current / wearea 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 close(datafile) # + sampleid <- ProvideSampleId(datafilename) + # rgxp.number <- "^\\-?\\d\\.\\d+[e,]" # regexp that matches a decimal number at the beginning of the line. # Matches numbers with or without a negative sign (hyphen), @@ -279,11 +288,11 @@ amperometry2df <- function(datafilename, wearea = 1) { for (s in 1:length(starts)) { zz <- textConnection(chifile[starts[s]:ends[s]], "r") ff <- rbind(ff, - data.frame(matrix(scan(zz, what = numeric(), sep = ","), + data.frame(sampleid, matrix(scan(zz, what = numeric(), sep = ","), ncol = 2, byrow = T))) close(zz) } - names(ff) <- c("time", "current") + names(ff) <- c("sampleid", "time", "current") # Calculate current density currentdensity <- ff$current / wearea ff <- cbind(ff, currentdensity = currentdensity) @@ -318,16 +327,18 @@ amperometry2df <- function(datafilename, wearea = 1) { ################################################## #################### cv2df ####################### ################################################## -cv2df <- function(cvfilename, wearea = 1) { +cv2df <- function(datafilename, wearea = 1) { # Function description: # CH Instruments potentiostat records all data using standard SI units, # so all potential values are in volts, currents are in amperes, # 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 close(cvfile) # + sampleid <- ProvideSampleId(datafilename) + # rgxp.number <- "^\\-?\\d\\.\\d+," # regexp that matches a decimal number at the beginning of the line. # Matches numbers with or without a negative sign (hyphen), @@ -355,15 +366,15 @@ cv2df <- function(cvfilename, wearea = 1) { for (s in 1:length(starts)) { zz <- textConnection(chifile[starts[s]:ends[s]], "r") 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 = ","), ncol = 3, byrow = T))) close(zz) } - names(ff) <- c("segment", "cycle", "potential", "current", "charge") + names(ff) <- c("sampleid", "segment", "cycle", "potential", "current", "charge") # Calculate current density 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 # These attributes are specific for each kind of experiment, @@ -403,16 +414,18 @@ cv2df <- function(cvfilename, wearea = 1) { ################################################## ################### lsv2df ####################### ################################################## -lsv2df <- function(lsvfilename, wearea = 1) { +lsv2df <- function(datafilename, wearea = 1) { # Function description: # CH Instruments potentiostat records all data using standard SI units, # so all potential values are in volts, currents are in amperes, # 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 close(lsvfile) # + sampleid <- ProvideSampleId(datafilename) + # rgxp.number <- "^\\-?\\d\\.\\d+," # regexp that matches a decimal number at the beginning of the line. # Matches numbers with or without a negative sign (hyphen), @@ -440,15 +453,15 @@ lsv2df <- function(lsvfilename, wearea = 1) { for (s in 1:length(starts)) { zz <- textConnection(chifile[starts[s]:ends[s]], "r") ff <- rbind(ff, - data.frame(segment = factor(s), + data.frame(sampleid, segment = factor(s), matrix(scan(zz, what = numeric(), sep = ","), ncol = 3, byrow = T))) close(zz) } - names(ff) <- c("segment", "potential", "current", "charge") + names(ff) <- c("sampleid", "segment", "potential", "current", "charge") # Calculate current density 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 # These attributes are specific for each kind of experiment, diff --git a/Renishaw.R b/Renishaw.R index f096ec3..dbde806 100644 --- a/Renishaw.R +++ b/Renishaw.R @@ -3,6 +3,7 @@ # Taha Ahmed, Feb 2011 # CONTENTS +source("/home/taha/chepec/chetex/common/R/common.R") # >>>> Raman2df @@ -20,23 +21,7 @@ Raman2df <- function(datafilename) { close(datafile) # ##### - # A nice algorithm that extracts the filename from the datafilename argument - # 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)) + sampleid <- ProvideSampleId(datafilename) # ff <- data.frame(NULL) zz <- textConnection(chifile, "r") diff --git a/xrdtf.R b/xrdtf.R old mode 100755 new mode 100644