Reading EDF header

614 Views Asked by At

I have 100+ EEG EDF files. I want to extract the start time and date along with the recording duration into a data frame. Is there any easy way to extract this data? Preferabely in R or Matlab.

I have succesfully extracted the data using:

library(edfReader)
CHdr <-  readEdfHeader("E:/data/EDF/Rtest/EEG1 (2).edf")
summary(CHdr)

format (CHdr$startTime, format="%Y-%m-%d %H:%M:%S",  usetz = FALSE)
CHdr$recordedPeriod

But doing this for the 100+ EDF files, might get a bit tiresome...

2

There are 2 best solutions below

1
On BEST ANSWER

You can use lapply with readEdfHeader to get all the headers in one code line.

First, a working example with the package data sets.

old_dir <- getwd()
libDir <- system.file("extdata", package = 'edfReader')
setwd(libDir)

Get the .edf filenames and read their headers.

fls <- list.files(pattern = '\\.edf')
edf_headers <- lapply(fls, readEdfHeader)

Next, extract the relevant information and rbind it to create a data.frame.

res <- lapply(edf_headers, function(x){
  startTime <- x[['startTime']]
  startDate <- substr(x[['recordingId']], 11, 21)
  recordDuration <- x[['recordDuration']]
  data.frame(startTime, startDate, recordDuration)
})
res <- do.call(rbind, res)
res
#            startTime   startDate recordDuration
#1 2000-01-01 14:15:16 01-JAN-2000            0.1
#2 2009-12-10 12:44:02 10-DEC-2009            1.0
#3 2009-12-10 12:44:02 10-DEC-2009            1.0

Reset the working directory.

setwd(old_dir)
2
On

It is difficult to answer without a reproducible example, but i would suggest looping over your files though there may be a more elegant way to functionalize all this into fewer lines with an apply. Provided they are all in the same directory this should be possible with the following:

library(edfReader)    
files <- list.files("E:/data/EDF/Rtest/", pattern = "*.edf")
outputs <- list()

for (i in 1:length(files)) {

i_file <- paste0("E:/data/EDF/Rtest/",files[i])
i_CHdr <-  readEdfHeader(i_file)

#whatever else you are doing

outputs[[i]] <- output_from_your_code
rm(i_file, i_CHdr) #plus whatever objects your code has

}

#work with outputs list as necessary; dplyr::bind_rows is often helpful