Skip to content

Commit f50bb3d

Browse files
committed
implemented less conservative strategy by which all packets in file are read, then GGIR will do usual data quality checks
1 parent b3be00c commit f50bb3d

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

R/readParmayMatrix.R

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,14 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
189189
n_declared = total_packets
190190
n_observed_file = length(packet_starti)
191191

192-
# validate packet headers (i.e., nr. of appearances of MDTCPACK in files)
193-
# in case that empty packets have been added at the end, only use valid packets
194-
n_use = min(total_packets, length(packet_starti))
192+
# Use all packets available in the file (even if empty packets have been added at the end)
193+
n_use = n_observed_file
195194
packet_starti = packet_starti[seq_len(n_use)]
196195
packet_endi = packet_endi[seq_len(n_use)]
197196

198197
# Apply chunking AFTER truncation
199198
i_from = max(1, start)
200-
i_to = if (is.null(end)) n_use else min(end, n_use)
199+
i_to = n_use
201200
idx = seq.int(i_from, i_to)
202201
n_rows = length(idx)
203202

@@ -257,7 +256,8 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
257256
gap_with_previous_block_secs = numeric(n_rows),
258257
start_time_adjustment_secs = numeric(n_rows),
259258
declared_packets = rep(n_declared, n_rows),
260-
observed_packets = rep(n_observed_file, n_rows)
259+
observed_packets = rep(n_observed_file, n_rows),
260+
acc_recorded = TRUE
261261
)
262262

263263
# log corrupt packets
@@ -299,7 +299,6 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
299299
seek(con, packet_starti[i] + 31, "start") # Move to correct position
300300
readBin(con, what = "integer", size = 4, endian = "little")
301301
}, numeric(1))
302-
303302
# Process timestamps and calculate sampling frequency
304303
packets_dur_s = packets_t1 - packets_t0
305304
sf_acc_observed = acc_count / packets_dur_s
@@ -312,6 +311,20 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
312311
QClog[, "frequency_set"] = sf
313312
QClog[, "frequency_observed"] = sf_acc_observed
314313

314+
# At the moment the function requires that accelerometer data has been
315+
# recorded to work, as accelerometer timestamps are used as reference time
316+
if (sum(acc_count) == 0) {
317+
QClog[, "acc_recording"] = FALSE
318+
return(list(
319+
QClog = QClog,
320+
data = matrix(nrow = 0, ncol = 3),
321+
header = list(sf = sf,
322+
acc_dynrange = acc_dynrange,
323+
starttime = starttime_posix),
324+
lastchunk = TRUE
325+
))
326+
}
327+
315328
# build data structure lists
316329
# acc
317330
acc_starts = packet_starti + 36; acc_stops = acc_starts + 6*acc_count - 1
@@ -408,7 +421,11 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
408421
} else {
409422
# if the first packet is corrupted, then impute by first non-corrupted temperature observed
410423
first_valid_temp = which(!1:length(temp_count) %in% corrupt_packets)[1]
411-
prev_temp = temp_readings[[first_valid_temp]][1,]
424+
if (is.na(first_valid_temp)) {
425+
prev_temp = NA
426+
} else {
427+
prev_temp = temp_readings[[first_valid_temp]][1,]
428+
}
412429
}
413430
# next index
414431
next_index = ifelse(any(!is.na(x[(i + 1):length(x)])),
@@ -422,15 +439,17 @@ readParmayMatrix = function(filename, output = c("all", "sf", "dynrange")[1],
422439
} else {
423440
next_temp = NA
424441
}
425-
426442
if (!is.na(prev_temp[1]) & !is.na(next_temp[1])) {
427443
mean_temp = (prev_temp + next_temp) / 2
428-
} else if (is.na(prev_temp[1])) {
444+
} else if (is.na(prev_temp[1]) & !is.na(next_temp[1])) {
429445
mean_temp = next_temp
430-
} else if (is.na(next_temp[1])) {
446+
} else if (!is.na(prev_temp[1]) & is.na(next_temp[1])) {
431447
# if the last packet is corrupted, then impute by last temperature observed
432448
mean_temp = prev_temp
433-
}
449+
} else {
450+
# safe conduct, if both are NA, then leave it without imputation
451+
mean_temp = temp_readings[[i]]
452+
}
434453

435454
temp_readings[[i]] = matrix(mean_temp, nrow = nrow(temp_readings[[i]]),
436455
ncol = 2, byrow = T)

tests/testthat/test_readParmayMatrix.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ test_that("Identification of corrupt files and foreseen errors", {
7272
expect_equal(BIN$data$acc_x[1], 0)
7373
expect_equal(BIN$data$acc_y[1], 0)
7474
expect_equal(BIN$data$acc_z[1], 1)
75-
expect_true(is.na(BIN$data$bodySurface_temp[1]))
76-
expect_true(is.na(BIN$data$ambient_temp[1]))
75+
expect_false(is.na(BIN$data$bodySurface_temp[1]))
76+
expect_false(is.na(BIN$data$ambient_temp[1]))
7777
# also, identification of full corrupted file
7878
binfile = system.file("testfiles/mtx_corrupted_full.bin", package = "GGIRread")[1]
7979
expect_error(readParmayMatrix(filename = binfile, desiredtz = "Europe/Berlin",

0 commit comments

Comments
 (0)