From d9eefa23e7351adf2389593b15e4b3134f89b1b0 Mon Sep 17 00:00:00 2001 From: Austin Hurst Date: Sun, 22 Sep 2019 14:01:40 -0400 Subject: [PATCH] Fixed bug parsing certain files --- R/eyelink_parser.R | 4 +-- tests/testthat/test_eyelink_parser.R | 47 ++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/R/eyelink_parser.R b/R/eyelink_parser.R index f9c8e5c..a5166b5 100755 --- a/R/eyelink_parser.R +++ b/R/eyelink_parser.R @@ -88,8 +88,8 @@ read.asc <- function(fname, samples = TRUE, events = TRUE, parse_all = FALSE) { for (i in 2:length(dividers)) { start <- dividers[i - 1] end <- dividers[i] - endline <- which(inp_first[start:end] == "END") + start - if (length(endline) > 0) { + endline <- which(inp_first[start:end] == "END") + start - 1 + if (length(endline) > 0 && endline < end) { block[endline[1]:end] <- block[endline[1]:end] + 0.5 } } diff --git a/tests/testthat/test_eyelink_parser.R b/tests/testthat/test_eyelink_parser.R index 39771ec..60b19aa 100644 --- a/tests/testthat/test_eyelink_parser.R +++ b/tests/testthat/test_eyelink_parser.R @@ -205,3 +205,50 @@ test_that("test_events", { expect_equal(all(is.na(c(sacc$ampl[1], sacc$href.ampl[1]))), TRUE) expect_equal(any(is.na(c(sacc$ampl[2], sacc$href.ampl[2]))), FALSE) }) + + +# Test block division edge cases +test_that("test_block_parsing", { + + testfile <- c( + "** VERSION: EYELINK II 1", + "** SOURCE: EYELINK II", + "** EYELINK II v2.31 Mar 13 2010", + "MSG\t2035323 GAZE_COORDS 0.00 0.00 1919.00 1199.00", + "MSG\t2035324 !MODE RECORD CR 250 2 1", + "START\t 2035326\t LEFT\t RIGHT\t SAMPLES\t EVENTS", + "PRESCALER\t 1", + "VPRESCALER\t 1", + "PUPIL\t AREA", + "EVENTS\tGAZE\tLEFT\tRIGHT\tRATE\t 250.00\tTRACKING\tCR\tFILTER\t2", + "SAMPLES\tGAZE\tLEFT\tRIGHT\tRATE\t 250.00\tTRACKING\tCR\tFILTER\t2", + "2035326\t 955.4\t 602.7\t 1022.0\t 959.0\t 602.2\t 1193.0\t.....", + "2035330\t 955.5\t 602.1\t 1022.0\t 958.7\t 601.6\t 1192.0\t.....", + "2035334\t 956.8\t 602.4\t 1022.0\t 958.9\t 602.4\t 1191.0\t.....", + "MSG\t2035338 TRIAL_ID 1" + ) + + # Test handling of no END line + a <- read.asc(testfile) + expect_equal(nrow(a$raw), 3) + expect_equal("TRIAL_ID 1" %in% a$msg$text, TRUE) + + # Test handling of END line as last line + testfile <- c(testfile, "END\t598729\t SAMPLES\t EVENTS\t RES\t 41.91\t 38.82") + a <- read.asc(testfile) + expect_equal(nrow(a$raw), 3) + expect_equal("TRIAL_ID 1" %in% a$msg$text, TRUE) + + # Test handling of content after last END line + testfile <- c(testfile, "MSG\t601379 trialResult 3") + a <- read.asc(testfile) + expect_equal(nrow(a$raw), 3) + expect_equal("TRIAL_ID 1" %in% a$msg$text, TRUE) + + # Test handling of content after last END line when parse_all is TRUE + a <- read.asc(testfile, parse_all = TRUE) + expect_equal(nrow(a$raw), 3) + expect_equal("TRIAL_ID 1" %in% a$msg$text, TRUE) + expect_equal("trialResult 3" %in% a$msg$text, TRUE) + expect_equal(subset(a$msg, block == 1.5)$text[1], "trialResult 3") +})