Skip to content

Commit

Permalink
adding test of processing .cwa blocks with failed checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
l-k- committed Dec 4, 2023
1 parent 6ed29a4 commit 841150c
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
Binary file not shown.
102 changes: 101 additions & 1 deletion tests/testthat/test_readAxivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,107 @@ test_that("readAxivity reads data from AX3 file correctly", {

})

test_that("readAxivity handles failed checksums correctly", {
# read the first 11 blocks from the non-corrupt file
cwafile = system.file("testfiles/ax3_testfile.cwa", package = "GGIRread")[1]
AX3 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 12)
nrow11 = nrow(AX3$data)
sum11 = floor(sum(abs(AX3$data[,2:4])))

# From now on we'll be reading a file with corrupt blocks 13 and 14
cwafile = system.file("testfiles/ax3_testfile_corrupt_blocks_13_14.cwa", package = "GGIRread")[1]

# if we read the first 11 blocks, results should be the same as for the non-corrupt file,
# and there should be nothing in QClog
AX3c = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 12)
expect_equal(AX3c$header$device, "Axivity")
expect_equal(nrow(AX3c$data), nrow11)
expect_equal(floor(sum(abs(AX3c$data[,2:4]))), sum11)
expect_true(is.null(AX3c$QClog))

# make a note of the last data point. It'll get used to impute block 12 and the the two corrupt blocks that follow it.
imputedValues = AX3c$data[nrow11,2:4]
VectorG = sqrt(sum(imputedValues^2))
imputedValues = imputedValues / VectorG

# Now read a bigger chunk of the file, so that the corrupt blocks fall in the middle
AX3_1_20 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 20)
expect_equal(AX3_1_20$header$device, "Axivity")
expect_equal(nrow(AX3_1_20$data), 2306)
expect_equal(ncol(AX3_1_20$data), 7)
expect_equal(floor(sum(abs(AX3_1_20$data[,2:4]))), 3244)

# there should be 3 entries in QClog: one each for blocks 13 and 14 with failed checksums (these initially get skipped, not imputed),
# and one for block 12 that preceeds the corrupt blocks, because data needs to be imputed from block 12 (inclusive) to 15 (exclusive)
expect_false(is.null(AX3_1_20$QClog))
expect_equal(nrow(AX3_1_20$QClog),3)

expect_false(AX3_1_20$QClog$checksum_pass[1])
expect_false(AX3_1_20$QClog$imputed[1])
expect_equal(AX3_1_20$QClog$blockID_current[1], 13)
expect_equal(AX3_1_20$QClog$blockID_next[1], 14)

expect_false(AX3_1_20$QClog$checksum_pass[2])
expect_false(AX3_1_20$QClog$imputed[2])
expect_equal(AX3_1_20$QClog$blockID_current[2], 14)
expect_equal(AX3_1_20$QClog$blockID_next[2], 15)

expect_true(AX3_1_20$QClog$checksum_pass[3])
expect_true(AX3_1_20$QClog$imputed[3])
expect_equal(AX3_1_20$QClog$blockID_current[3], 12) # we finally found the first valid block after #12, so we imputed block #12, up to 15
expect_equal(AX3_1_20$QClog$blockID_next[3], 15) # the first valid block after 12 is 15

# check that data in blocks 12-14 got imputed with the correct values
expect_true(all(abs(imputedValues - AX3_1_20$data[nrow11+1,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3_1_20$data[nrow11+2,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3_1_20$data[nrow11+100,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3_1_20$data[nrow11+200,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3_1_20$data[nrow11+360,2:4] ) < .0001))
expect_equal(sum(abs(imputedValues)),
sum(abs(AX3_1_20$data[(nrow11+1) : (nrow11+360), 2:4])) / 360,
tolerance = .0001, scale = 1)

# make sure a request for only corrupt blocks returns an error but doesn't crash
expect_error(readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 13, end = 14),
"All requested blocks are corrupt")
expect_error(readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 14, end = 15),
"All requested blocks are corrupt")
expect_error(readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 13, end = 15),
"All requested blocks are corrupt")

# Now see what happens if the corrupt blocks fall in the very beginning of the requested interval.
# These blocks should be skipped and the file should be read from the first non-corrupt block.
# So reading blocks 13-20 or 14-20 should give the same output as reading blocks 15-20.

AX3_13_20 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 13, end = 20)
AX3_14_20 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 14, end = 20)
AX3_15_20 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 15, end = 20)

expect_equal(nrow(AX3_13_20$data), nrow(AX3_15_20$data))
expect_equal(floor(sum(abs(AX3_13_20$data[,2:4]))), floor(sum(abs(AX3_15_20$data[,2:4]))))

expect_equal(nrow(AX3_14_20$data), nrow(AX3_15_20$data))
expect_equal(floor(sum(abs(AX3_14_20$data[,2:4]))), floor(sum(abs(AX3_15_20$data[,2:4]))))

# Now see what happens if the corrupt blocks fall in the very end of the requested interval.
# These blocks should be skipped and the file should be read from the first non-corrupt block.
# So reading blocks 1-13 or 1-14 should give the same output as reading blocks 1-15.

AX3_1_13 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 13)
AX3_1_14 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 14)
AX3_1_15 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 15)

expect_equal(nrow(AX3_1_13$data), nrow(AX3_1_15$data))
expect_equal(floor(sum(abs(AX3_1_13$data[,2:4]))), floor(sum(abs(AX3_1_15$data[,2:4]))))

expect_equal(nrow(AX3_1_14$data), nrow(AX3_1_15$data))
expect_equal(floor(sum(abs(AX3_1_14$data[,2:4]))), floor(sum(abs(AX3_1_15$data[,2:4]))))

expect_equal(floor(sum(abs(AX3_1_13$data[,2:4]))) + floor(sum(abs(AX3_13_20$data[,2:4]))), floor(sum(abs(AX3_1_20$data[,2:4]))))
expect_equal(nrow(AX3_1_13$data) + nrow(AX3_13_20$data), nrow(AX3_1_20$data))
})


test_that("readAxivity reads data from AX6 file correctly", {
cwafile = system.file("testfiles/ax6_testfile.cwa", package = "GGIRread")[1]
AX6 = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 4)
Expand All @@ -49,7 +150,6 @@ test_that("readAxivity reads data from AX6 file correctly", {
expect_equal(AX6b$data$temp[3], 27.34375, tolerance = 0.0001)
expect_equal(floor(sum(abs(AX6b$data[,2:4]))), 960046)
expect_true(is.null(AX6b$QClog))

})


Expand Down

0 comments on commit 841150c

Please sign in to comment.