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 1, 2023
1 parent 6ed29a4 commit 42d89d2
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
Binary file not shown.
62 changes: 62 additions & 0 deletions tests/testthat/test_readAxivity.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,68 @@ 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 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
AX3c = readAxivity(filename = cwafile, desiredtz = "Europe/Berlin", start = 1, end = 20)
expect_equal(AX3c$header$device, "Axivity")
expect_equal(nrow(AX3c$data), 2306)
expect_equal(ncol(AX3c$data), 7)
expect_equal(floor(sum(abs(AX3c$data[,2:4]))), 3244)

expect_false(is.null(AX3c$QClog))

# 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_equal(nrow(AX3c$QClog),3)

expect_false(AX3c$QClog$checksum_pass[1])
expect_false(AX3c$QClog$imputed[1])
expect_equal(AX3c$QClog$blockID_current[1], 13) # this is a made up number, equal to the last valid block number + 1
expect_equal(AX3c$QClog$blockID_next[1], 13) # keeping the same block id, since we couldn't read the block # from the block

expect_false(AX3c$QClog$checksum_pass[2])
expect_false(AX3c$QClog$imputed[2])
expect_equal(AX3c$QClog$blockID_current[2], 13) # this is still the last valid block number + 1, 12+1=13
expect_equal(AX3c$QClog$blockID_next[2], 13)

expect_true(AX3c$QClog$checksum_pass[3])
expect_true(AX3c$QClog$imputed[3])
expect_equal(AX3c$QClog$blockID_current[3], 12) # we finally found the first valid block after #12, so we imputed block #12, up to 15
expect_equal(AX3c$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 - AX3c$data[nrow11+1,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3c$data[nrow11+2,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3c$data[nrow11+100,2:4] ) < .0001))
expect_true(all(abs(imputedValues - AX3c$data[nrow11+240,2:4] ) < .0001))
expect_equal(sum(abs(imputedValues)),
sum(abs(AX3c$data[(nrow11+1) : (nrow11+240), 2:4])) / 240,
tolerance = .0001, scale = 1)
})


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 Down

0 comments on commit 42d89d2

Please sign in to comment.