From 5b8d33ad8b31d89f4b4c50542f3ed9e713c9532f Mon Sep 17 00:00:00 2001 From: l-k- Date: Wed, 21 Feb 2024 18:02:33 -0500 Subject: [PATCH 1/7] fix handling of ad hoc csv file header --- R/g.inspectfile.R | 29 +++----- R/read.myacc.csv.R | 2 +- tests/testthat/test_greadaccfile.R | 101 ++++++++++++++++++++++++++- tests/testthat/test_read.myacc.csv.R | 2 + 4 files changed, 112 insertions(+), 22 deletions(-) diff --git a/R/g.inspectfile.R b/R/g.inspectfile.R index b3b504834..182c2d0ce 100644 --- a/R/g.inspectfile.R +++ b/R/g.inspectfile.R @@ -208,16 +208,15 @@ g.inspectfile = function(datafile, desiredtz = "", params_rawdata = c(), rmc.scalefactor.acc = params_rawdata[["rmc.scalefactor.acc"]], desiredtz = desiredtz, configtz = configtz) - if (Pusercsvformat$header == "no header" || is.null(Pusercsvformat$header$sample_rate)) { - + if (class(Pusercsvformat$header) == "character" && Pusercsvformat$header == "no header") { sf = params_rawdata[["rmc.sf"]] - if (is.null(sf)) { - stop("\nFile header doesn't specify sample rate. Please provide rmc.sf value to process ", datafile) - } else if (sf == 0) { - stop("\nFile header doesn't specify sample rate. Please provide a non-zero rmc.sf value to process ", datafile) - } } else { - sf = Pusercsvformat$header$sample_rate + sf = as.numeric(Pusercsvformat$header["sample_rate",1]) + } + if (is.null(sf) || is.na(sf)) { + stop("\nFile header doesn't specify sample rate. Please provide rmc.sf value to process ", datafile) + } else if (sf == 0) { + stop("\nFile header doesn't specify sample rate. Please provide a non-zero rmc.sf value to process ", datafile) } } @@ -264,15 +263,7 @@ g.inspectfile = function(datafile, desiredtz = "", params_rawdata = c(), H = PP$header } else if (dformat == FORMAT$AD_HOC_CSV) { # csv data in a user-specified format - - H = header = Pusercsvformat$header - if (Pusercsvformat$header != "no header") { - H = data.frame(name = row.names(header), value = header, stringsAsFactors = TRUE) - } - sf = params_rawdata[["rmc.sf"]] - if (sf == 0) { - stop("\nPlease provide a non-zero rmc.sf value to process ", datafile) - } + header = Pusercsvformat$header } else if (dformat == FORMAT$GT3X) { # gt3x info = try(expr = {read.gt3x::parse_gt3x_info(datafile, tz = desiredtz)},silent = TRUE) if (inherits(info, "try-error") == TRUE || is.null(info)) { @@ -300,7 +291,7 @@ g.inspectfile = function(datafile, desiredtz = "", params_rawdata = c(), stop(paste0("\nSample frequency not recognised in ", datafile), call. = FALSE) } - if (is.null(sf) == FALSE) { + if (dformat != FORMAT$AD_HOC_CSV && is.null(sf) == FALSE) { H = as.matrix(H) if (ncol(H) == 3 && dformat == FORMAT$CSV && mon == MONITOR$ACTIGRAPH) { if (length(which(is.na(H[,2]) == FALSE)) == 0) { @@ -333,7 +324,7 @@ g.inspectfile = function(datafile, desiredtz = "", params_rawdata = c(), if ((mon == MONITOR$GENEACTIV && dformat == FORMAT$BIN) || (mon == MONITOR$MOVISENS && length(H) > 0)) { varname = rownames(as.matrix(H)) H = data.frame(varname = varname,varvalue = as.character(H), stringsAsFactors = TRUE) - } else { + } else if (dformat != FORMAT$AD_HOC_CSV) { if (length(H) > 1 && class(H)[1] == "matrix") H = data.frame(varname = H[,1],varvalue = H[,2], stringsAsFactors = TRUE) } } diff --git a/R/read.myacc.csv.R b/R/read.myacc.csv.R index 41a907b72..4076bc071 100644 --- a/R/read.myacc.csv.R +++ b/R/read.myacc.csv.R @@ -142,7 +142,7 @@ read.myacc.csv = function(rmc.file=c(), rmc.nrow=Inf, rmc.skip=c(), rmc.dec=".", # first see if maybe sf *is* in the header, just not under the rmc.headername.sf name sf = as.numeric(header[which(row.names(header) == "sample_rate"),1]) # if sf isn't in the header under the default name either, then use the default value - if (is.na(sf)) { + if (is.na(sf) && !is.null(rmc.sf)) { sf = rmc.sf header = rbind(header, sf) # add it also to the header row.names(header)[nrow(header)] = "sample_rate" diff --git a/tests/testthat/test_greadaccfile.R b/tests/testthat/test_greadaccfile.R index cfc752cca..093ef7de0 100644 --- a/tests/testthat/test_greadaccfile.R +++ b/tests/testthat/test_greadaccfile.R @@ -1,6 +1,6 @@ library(GGIR) context("g.readaccfile") -test_that("g.readaccfile and g.inspectfile can read movisens, gt3x, cwa, Axivity csv, and actigraph csv files correctly", { +test_that("g.readaccfile and g.inspectfile can read movisens, gt3x, cwa, Axivity csv, actigraph csv, and ad-hoc csv files correctly", { skip_on_cran() desiredtz = "Pacific/Auckland" @@ -218,11 +218,25 @@ test_that("g.readaccfile and g.inspectfile can read movisens, gt3x, cwa, Axivity timestamps = as.POSIXlt(x, origin="1970-1-1", tz = configtz) mydata = data.frame(Xcol = rnorm(N), timecol = timestamps, Ycol = rnorm(N), Zcol = rnorm(N), tempcol = rnorm(N) + 20) - testfile = "testcsv1.csv" + testfile = "testcsv.csv" on.exit({if (file.exists(testfile)) file.remove(testfile)}, add = TRUE) write.csv(mydata, file = testfile, row.names = FALSE) + # check that for files with no header, g.inspectfile() errors out if sampling rate is not specified as rmc.sf, or if rmc.sf == 0 + expect_error(g.inspectfile(testfile, + rmc.dec=".", rmc.unit.time="POSIX", + rmc.firstrow.acc = 1, rmc.firstrow.header=c(), + rmc.col.acc = c(1,3,4), rmc.col.temp = 5, rmc.col.time=2, + rmc.unit.acc = "g", rmc.unit.temp = "C", rmc.origin = "1970-01-01"), + regexp = "File header doesn't specify sample rate. Please provide rmc.sf value to process") + expect_error(g.inspectfile(testfile, + rmc.dec=".", rmc.sf=0, rmc.unit.time="POSIX", + rmc.firstrow.acc = 1, rmc.firstrow.header=c(), + rmc.col.acc = c(1,3,4), rmc.col.temp = 5, rmc.col.time=2, + rmc.unit.acc = "g", rmc.unit.temp = "C", rmc.origin = "1970-01-01"), + regexp = "File header doesn't specify sample rate. Please provide a non-zero rmc.sf value to process") + AHcsv = g.inspectfile(testfile, rmc.dec=".", rmc.sf=30, rmc.unit.time="POSIX", rmc.firstrow.acc = 1, rmc.firstrow.header=c(), @@ -289,6 +303,89 @@ test_that("g.readaccfile and g.inspectfile can read movisens, gt3x, cwa, Axivity expect_equal(nrow(csv_read4$P$data), 3000) expect_equal(sum(csv_read3$P$data[c("x","y","z")]), sum(csv_read4$P$data[c("x","y","z")]), tolerance = .01, scale = 1) + # Create test file: 2-column header, with time, + # but sample rate not specified in the header + + N = 6000 + sf = 30 + x = Sys.time()+((0:(N-1))/sf) + timestamps = as.POSIXlt(x, origin="1970-1-1", tz = configtz) + mydata = data.frame(Xcol = rnorm(N), timecol = timestamps, Ycol = rnorm(N), Zcol = rnorm(N)) + S1 = as.matrix(mydata) + + hd_NR = 10 + hd = matrix("", hd_NR + 1, ncol(S1)) + hd[1, 1:2] = c("ID","12345") + hd[2, 1:2] = c("serial_number","30") + hd[3, 1:2] = c("bit","8") + hd[4, 1:2] = c("dynamic_range","6") + + S1 = rbind(hd, S1) + S1[hd_NR + 1,] = colnames(S1) + colnames(S1) = NULL + + testfile = "testcsv.csv" + on.exit({if (file.exists(testfile)) file.remove(testfile)}, add = TRUE) + write.table(S1, file = testfile, col.names = FALSE, row.names = FALSE) + + # check that for a file whose header doesn't specify sampling rate, + # g.inspectfile() errors out if sampling rate is not specified as rmc.sf, or if rmc.sf==0 + expect_error(g.inspectfile(testfile, + rmc.dec=".", rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header = 1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01"), + regexp = "File header doesn't specify sample rate. Please provide rmc.sf value to process") + expect_error(g.inspectfile(testfile, + rmc.dec=".", rmc.sf = 0, rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header = 1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01"), + regexp = "File header doesn't specify sample rate. Please provide a non-zero rmc.sf value to process") + + # check that for a file whose header doesn't specify sampling rate, + # g.inspectfile() returns sf == rmc.sf if the latter was specified + I = g.inspectfile(testfile, + rmc.dec=".", rmc.sf = 80, rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header = 1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01") + expect_equal(I$sf, 80) + + # Create test file: 2-column header, with temperature, with time, + # and sample rate correctly specified in the header + hd_NR = 10 + hd = matrix("", hd_NR + 1, ncol(S1)) + hd[1, 1:2] = c("ID","12345") + hd[2, 1:2] = c("sample_freq","30") + hd[3, 1:2] = c("serial_number","30") + hd[4, 1:2] = c("bit","8") + hd[5, 1:2] = c("dynamic_range","6") + S1 = as.matrix(mydata) + S1 = rbind(hd, S1) + S1[hd_NR + 1,] = colnames(S1) + colnames(S1) = NULL + testfile = "testcsv.csv" + write.table(S1, file = testfile, col.names = FALSE, row.names = FALSE) + + # check that g.inspectfile() returns sf value that was specified in the header, even if rmc.sf was also specified + I = g.inspectfile(testfile, + rmc.dec=".", rmc.sf = 80, rmc.headername.sf = "sample_freq", + rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header=1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01") + expect_equal(I$sf, 30) + + # check that g.inspectfile() correctly reads the sf value from the header + I = g.inspectfile(testfile, + rmc.dec=".", rmc.headername.sf = "sample_freq", + rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header=1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01") + expect_equal(I$sf, 30) + # test decimal separator recognition extraction decn = g.dotorcomma(Ax3CwaFile,dformat = FORMAT$CWA, mon = MONITOR$AXIVITY, desiredtz = desiredtz) expect_equal(decn,".") diff --git a/tests/testthat/test_read.myacc.csv.R b/tests/testthat/test_read.myacc.csv.R index 567d6501a..e4e49a072 100644 --- a/tests/testthat/test_read.myacc.csv.R +++ b/tests/testthat/test_read.myacc.csv.R @@ -273,6 +273,8 @@ test_that("read.myacc.csv can handle header and bit-value acceleration", { expect_that(ncol(D1$data), equals(5)) expect_that(nrow(D1$header), equals(5)) expect_that(ncol(D1$header), equals(1)) + expect_equal(as.numeric(D1$header["sample_rate",1]), 30) + # Test 2 - 2 column header, bit-valued acceleration D2 = read.myacc.csv(rmc.file = testfile[2], rmc.nrow = 20, rmc.dec = ".", rmc.firstrow.acc = 11, rmc.firstrow.header = 1, From bd53a687fbe4d5f6831388bcd70a3986df325cd4 Mon Sep 17 00:00:00 2001 From: l-k- Date: Wed, 21 Feb 2024 18:46:36 -0500 Subject: [PATCH 2/7] any extra input vars are now parsed by extract_params --- R/g.getmeta.R | 10 ---------- R/g.inspectfile.R | 11 ----------- 2 files changed, 21 deletions(-) diff --git a/R/g.getmeta.R b/R/g.getmeta.R index 68408d02f..d2c449fb0 100644 --- a/R/g.getmeta.R +++ b/R/g.getmeta.R @@ -25,16 +25,6 @@ g.getmeta = function(datafile, params_metrics = c(), params_rawdata = c(), params_general = params$params_general params_cleaning = params$params_cleaning } - #get input variables - if (length(input) > 0) { - for (i in 1:length(names(input))) { - txt = paste0(names(input)[i], "=", input[i]) - if (is(unlist(input[i]), "character")) { - txt = paste0(names(input)[i], "='", unlist(input[i]), "'") - } - eval(parse(text = txt)) - } - } metrics2do = data.frame(do.bfen = params_metrics[["do.bfen"]], do.enmo = params_metrics[["do.enmo"]], diff --git a/R/g.inspectfile.R b/R/g.inspectfile.R index 182c2d0ce..4221c9e19 100644 --- a/R/g.inspectfile.R +++ b/R/g.inspectfile.R @@ -13,17 +13,6 @@ g.inspectfile = function(datafile, desiredtz = "", params_rawdata = c(), rm(params) } - #get input variables (relevant when read.myacc.csv is used - if (length(input) > 0) { - for (i in 1:length(names(input))) { - txt = paste0(names(input)[i], "=", input[i]) - if (is(unlist(input[i]), "character")) { - txt = paste0(names(input)[i], "='", unlist(input[i]), "'") - } - eval(parse(text = txt)) - } - } - # note that if the file is an RData file then this function will not be called # the output of this function for the original datafile is stored inside the RData file in the form of object I getbrand = function(filename = c(), datafile = c()) { From c990c8f835bf253b6009bc1ace71ee0de4c80c14 Mon Sep 17 00:00:00 2001 From: l-k- Date: Wed, 21 Feb 2024 19:33:24 -0500 Subject: [PATCH 3/7] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 445822522..99c05737b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # CHANGES IN GGIR VERSION 3.0-6 +- Part 1: Fix handling of ad hoc csv file header in g.inspectfile() #1057 - Part 1: Improve g.calibrate to better handle scenario when no non-movement periods are found in the entire recording #1032 From e249d2cbd68cb30f9d5f75791e828f5120066a5b Mon Sep 17 00:00:00 2001 From: l-k- Date: Thu, 22 Feb 2024 00:15:28 -0500 Subject: [PATCH 4/7] rmc.bitrate can be string if bitrate is specified in header --- R/check_params.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/check_params.R b/R/check_params.R index 54288e244..77eb88d18 100644 --- a/R/check_params.R +++ b/R/check_params.R @@ -56,7 +56,7 @@ check_params = function(params_sleep = c(), params_metrics = c(), numeric_params = c("chunksize", "spherecrit", "minloadcrit", "minimumFileSizeMB", "dynrange", "rmc.col.acc", "interpolationType", "rmc.firstrow.acc", "rmc.firstrow.header", "rmc.header.length", - "rmc.col.temp", "rmc.col.time", "rmc.bitrate", "rmc.dynamic_range", + "rmc.col.temp", "rmc.col.time", "rmc.dynamic_range", "rmc.sf", "rmc.col.wear", "rmc.noise", "frequency_tol", "rmc.scalefactor.acc") boolean_params = c("printsummary", "do.cal", "rmc.unsignedbit", "rmc.check4timegaps", "rmc.doresample", "imputeTimegaps") From a749f419dc853ada1b8ff741e3cc2b55d46c4a4d Mon Sep 17 00:00:00 2001 From: l-k- Date: Thu, 22 Feb 2024 00:19:23 -0500 Subject: [PATCH 5/7] rmc.dynamic_range can be string if dynamic_range is specified in header --- R/check_params.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/check_params.R b/R/check_params.R index 77eb88d18..85e2f2f0a 100644 --- a/R/check_params.R +++ b/R/check_params.R @@ -56,7 +56,7 @@ check_params = function(params_sleep = c(), params_metrics = c(), numeric_params = c("chunksize", "spherecrit", "minloadcrit", "minimumFileSizeMB", "dynrange", "rmc.col.acc", "interpolationType", "rmc.firstrow.acc", "rmc.firstrow.header", "rmc.header.length", - "rmc.col.temp", "rmc.col.time", "rmc.dynamic_range", + "rmc.col.temp", "rmc.col.time", "rmc.sf", "rmc.col.wear", "rmc.noise", "frequency_tol", "rmc.scalefactor.acc") boolean_params = c("printsummary", "do.cal", "rmc.unsignedbit", "rmc.check4timegaps", "rmc.doresample", "imputeTimegaps") From 602a342bffbb4d85111e5febabccaea4e28e8230 Mon Sep 17 00:00:00 2001 From: l-k- Date: Thu, 22 Feb 2024 00:39:48 -0500 Subject: [PATCH 6/7] remove empty lines for both header types 1-column header with empty lines wasn't being parsed correctly. --- R/read.myacc.csv.R | 5 +- tests/testthat/test_greadaccfile.R | 144 +++++++++++++++++++---------- 2 files changed, 99 insertions(+), 50 deletions(-) diff --git a/R/read.myacc.csv.R b/R/read.myacc.csv.R index 4076bc071..5793397c9 100644 --- a/R/read.myacc.csv.R +++ b/R/read.myacc.csv.R @@ -92,6 +92,9 @@ read.myacc.csv = function(rmc.file=c(), rmc.nrow=Inf, rmc.skip=c(), rmc.dec=".", dec = rmc.dec, showProgress = FALSE, header = FALSE, blank.lines.skip = TRUE, data.table=FALSE, stringsAsFactors=FALSE) + validrows = which(is.na(header_tmp[,1]) == FALSE & header_tmp[,1] != "") + header_tmp = header_tmp[validrows,1:2] + options(warn = 0) if (length(rmc.header.structure) != 0) { # header is stored in 1 column, with strings that need to be split if (length(header_tmp) == 1) { # one header item @@ -119,8 +122,6 @@ read.myacc.csv = function(rmc.file=c(), rmc.nrow=Inf, rmc.skip=c(), rmc.dec=".", header = header_tmp2 } else { # column 1 is header name, column 2 is header value colnames(header_tmp) = NULL - validrows = which(is.na(header_tmp[,1]) == FALSE & header_tmp[,1] != "") - header_tmp = header_tmp[validrows,1:2] header_tmp2 = as.data.frame(header_tmp[,2], stringsAsFactors = FALSE) row.names(header_tmp2) = header_tmp[,1] colnames(header_tmp2) = NULL diff --git a/tests/testthat/test_greadaccfile.R b/tests/testthat/test_greadaccfile.R index 093ef7de0..71b831cdb 100644 --- a/tests/testthat/test_greadaccfile.R +++ b/tests/testthat/test_greadaccfile.R @@ -324,68 +324,116 @@ test_that("g.readaccfile and g.inspectfile can read movisens, gt3x, cwa, Axivity S1[hd_NR + 1,] = colnames(S1) colnames(S1) = NULL - testfile = "testcsv.csv" - on.exit({if (file.exists(testfile)) file.remove(testfile)}, add = TRUE) - write.table(S1, file = testfile, col.names = FALSE, row.names = FALSE) + testfile_two_col = "testcsv2col.csv" + on.exit({if (file.exists(testfile_two_col)) file.remove(testfile_two_col)}, add = TRUE) + write.table(S1, file = testfile_two_col, col.names = FALSE, row.names = FALSE) - # check that for a file whose header doesn't specify sampling rate, - # g.inspectfile() errors out if sampling rate is not specified as rmc.sf, or if rmc.sf==0 - expect_error(g.inspectfile(testfile, - rmc.dec=".", rmc.unit.time="POSIX", - rmc.firstrow.acc = 11, rmc.firstrow.header = 1, - rmc.col.acc = c(1,3,4), rmc.col.time=2, - rmc.unit.acc = "g", rmc.origin = "1970-01-01"), - regexp = "File header doesn't specify sample rate. Please provide rmc.sf value to process") - expect_error(g.inspectfile(testfile, - rmc.dec=".", rmc.sf = 0, rmc.unit.time="POSIX", - rmc.firstrow.acc = 11, rmc.firstrow.header = 1, - rmc.col.acc = c(1,3,4), rmc.col.time=2, - rmc.unit.acc = "g", rmc.origin = "1970-01-01"), - regexp = "File header doesn't specify sample rate. Please provide a non-zero rmc.sf value to process") + # Create test file: 1-column header, with time, + # but sample rate not specified in the header + S1 = as.matrix(mydata) + hd = matrix("", hd_NR + 1, ncol(S1)) + hd[1, 1:2] = c("ID: 12345", "") + hd[2, 1:2] = c("serial_number: 4321", "") + hd[3, 1:2] = c("bit: 8", "") + hd[4, 1:2] = c("dynamic_range: 6", "") - # check that for a file whose header doesn't specify sampling rate, - # g.inspectfile() returns sf == rmc.sf if the latter was specified - I = g.inspectfile(testfile, - rmc.dec=".", rmc.sf = 80, rmc.unit.time="POSIX", - rmc.firstrow.acc = 11, rmc.firstrow.header = 1, - rmc.col.acc = c(1,3,4), rmc.col.time=2, - rmc.unit.acc = "g", rmc.origin = "1970-01-01") - expect_equal(I$sf, 80) + S1 = as.matrix(mydata) + S1 = rbind(hd, S1) + S1[hd_NR + 1,] = colnames(S1) + colnames(S1) = NULL + + testfile_one_col = "testcsv1col.csv" + on.exit({if (file.exists(testfile_one_col)) file.remove(testfile_one_col)}, add = TRUE) + write.table(S1, file = testfile_one_col, col.names = FALSE, row.names = FALSE) + + for (testfile in c(testfile_one_col, testfile_two_col)) { + # check that for a file whose header doesn't specify sampling rate, + # g.inspectfile() errors out if sampling rate is not specified as rmc.sf, or if rmc.sf==0 + expect_error(g.inspectfile(testfile, + rmc.dec=".", rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header = 1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01"), + regexp = "File header doesn't specify sample rate. Please provide rmc.sf value to process") + expect_error(g.inspectfile(testfile, + rmc.dec=".", rmc.sf = 0, rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header = 1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01"), + regexp = "File header doesn't specify sample rate. Please provide a non-zero rmc.sf value to process") + + # check that for a file whose header doesn't specify sampling rate, + # g.inspectfile() returns sf == rmc.sf if the latter was specified + I = g.inspectfile(testfile, + rmc.dec=".", rmc.sf = 80, rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header = 1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01") + expect_equal(I$sf, 80) + } # Create test file: 2-column header, with temperature, with time, # and sample rate correctly specified in the header + S1 = as.matrix(mydata) hd_NR = 10 hd = matrix("", hd_NR + 1, ncol(S1)) hd[1, 1:2] = c("ID","12345") - hd[2, 1:2] = c("sample_freq","30") - hd[3, 1:2] = c("serial_number","30") + hd[2, 1:2] = c("sample_freq","40") + hd[3, 1:2] = c("serial_number","9876") hd[4, 1:2] = c("bit","8") hd[5, 1:2] = c("dynamic_range","6") S1 = as.matrix(mydata) S1 = rbind(hd, S1) S1[hd_NR + 1,] = colnames(S1) colnames(S1) = NULL - testfile = "testcsv.csv" - write.table(S1, file = testfile, col.names = FALSE, row.names = FALSE) - - # check that g.inspectfile() returns sf value that was specified in the header, even if rmc.sf was also specified - I = g.inspectfile(testfile, - rmc.dec=".", rmc.sf = 80, rmc.headername.sf = "sample_freq", - rmc.unit.time="POSIX", - rmc.firstrow.acc = 11, rmc.firstrow.header=1, - rmc.col.acc = c(1,3,4), rmc.col.time=2, - rmc.unit.acc = "g", rmc.origin = "1970-01-01") - expect_equal(I$sf, 30) - - # check that g.inspectfile() correctly reads the sf value from the header - I = g.inspectfile(testfile, - rmc.dec=".", rmc.headername.sf = "sample_freq", - rmc.unit.time="POSIX", - rmc.firstrow.acc = 11, rmc.firstrow.header=1, - rmc.col.acc = c(1,3,4), rmc.col.time=2, - rmc.unit.acc = "g", rmc.origin = "1970-01-01") - expect_equal(I$sf, 30) - + + testfile_two_col = "testcsv2col.csv" + on.exit({if (file.exists(testfile_two_col)) file.remove(testfile_two_col)}, add = TRUE) + write.table(S1, file = testfile_two_col, col.names = FALSE, row.names = FALSE) + + # Create test file: 1-column header, with time, + # and sample rate not specified in the header + S1 = as.matrix(mydata) + hd = matrix("", hd_NR + 1, ncol(S1)) + hd[1, 1:2] = c("ID: 12345", "") + hd[2, 1:2] = c("sample_freq: 40", "") + hd[3, 1:2] = c("serial_number: 4321", "") + hd[4, 1:2] = c("bit: 8", "") + hd[5, 1:2] = c("dynamic_range: 6", "") + S1 = rbind(hd, S1) + S1[hd_NR + 1,] = colnames(S1) + colnames(S1) = NULL + + testfile_one_col = "testcsv1col.csv" + on.exit({if (file.exists(testfile_one_col)) file.remove(testfile_one_col)}, add = TRUE) + write.table(S1, file = testfile_one_col, col.names = FALSE, row.names = FALSE) + + for (csvData in list(list(testfile_one_col, ": "), + list(testfile_two_col, c()))) { + # check that g.inspectfile() returns sf value that was specified in the header, even if rmc.sf was also specified + I = g.inspectfile(csvData[[1]], + rmc.dec=".", rmc.sf = 80, rmc.headername.sf = "sample_freq", + rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header=1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01", + rmc.headername.sn = "serial_number", + rmc.headername.recordingid = "ID", + rmc.bitrate = "bit", rmc.dynamic_range = "dynamic_range", + rmc.header.structure = csvData[[2]]) + + expect_equal(I$sf, 40) + + # check that g.inspectfile() correctly reads the sf value from the header + I = g.inspectfile(csvData[[1]], + rmc.dec=".", rmc.headername.sf = "sample_freq", + rmc.unit.time="POSIX", + rmc.firstrow.acc = 11, rmc.firstrow.header=1, + rmc.col.acc = c(1,3,4), rmc.col.time=2, + rmc.unit.acc = "g", rmc.origin = "1970-01-01", + rmc.header.structure = csvData[[2]]) + expect_equal(I$sf, 40) + } # test decimal separator recognition extraction decn = g.dotorcomma(Ax3CwaFile,dformat = FORMAT$CWA, mon = MONITOR$AXIVITY, desiredtz = desiredtz) expect_equal(decn,".") From bd129f9525344ee3af6a0a2b5e981391059d2df3 Mon Sep 17 00:00:00 2001 From: l-k- Date: Thu, 22 Feb 2024 01:08:46 -0500 Subject: [PATCH 7/7] make sure we are reading sf from header, not rmc.sf --- tests/testthat/test_read.myacc.csv.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test_read.myacc.csv.R b/tests/testthat/test_read.myacc.csv.R index e4e49a072..b7f3dcc89 100644 --- a/tests/testthat/test_read.myacc.csv.R +++ b/tests/testthat/test_read.myacc.csv.R @@ -265,7 +265,6 @@ test_that("read.myacc.csv can handle header and bit-value acceleration", { rmc.format.time = "%Y-%m-%d %H:%M:%OS", rmc.origin = "1970-01-01", desiredtz = "Europe/London", - rmc.sf = sf, rmc.headername.sf = "sample_frequency", rmc.headername.sn = "serial_number", rmc.headername.recordingid = "ID")