For importing and working with fNIRS data from COBI Studio in R.
You can install the development version of cobifnirs from GitHub
# install.packages("devtools")
devtools::install_github("christopherjwilson/cobifnirs")
library(cobifnirs)
# Import a single .nir file
nirData <- import_nir("path/to/your/data.nir")
# Import multiple .nir files
# path should be the folder where the file is, ending in /
myPath <- "C:.../fnirs_data/"
#list all of the nirs files in a directory
nirsFiles <- list.files(pattern = ".nir$", recursive = TRUE, path = myPath)
# import all of the nirs files
nirsData <- lapply(nirsFiles, import_nirs, folder = myPath)
# base the number of participants on the number of files in the directory
# this is useful for later analysis
nparticipants <- length(nirsFiles)
# add a participant id as the name of each dataframe in the data list
names(nirsData) <- c(paste("p",pids, sep = ""))
# add the participant id column to each of the dataframes in the data list,
# so we can identify each dataset if they are combined later
nirsData <- mapply(`[<-`, nirsData, 'npid', value = pids, SIMPLIFY = FALSE)Important: the markers file must be in the same folder as the .nir file and have the same name.
See the function documentation for more details on how to use this function (re: duplicate markers).
# add markers to a single dataset
nirData <- add_markers(nirData)
# add markers to multiple datasets
nirsData <- lapply(nirsData, add_markers)
# create a summary report of signal quality for a single dataset
nirData <- signal_quality(nirData)
# create a summary report of signal quality for multiple datasets and write to a file
signal_summary <- lapply(nirsData, signalSummary, min=1000, max = 4000)
signal_summary <- bind_rows(signal_summary, .id = "PID")
problem_channels <- signal_summary %>% filter(min_max_signal_status != "ok")
write.csv(problem_channels, "problem_channels.csv")
# remove selected channels from a single dataset
nirData <- remove_channels(nirData, channels = c(1, 2, 3))
# remove selected channels from multiple datasets
## List of problem channels for removal
problem_channels_r <- read_csv(paste0(myPath, "/problem_channels.csv"))
problem_channels_r$remove <- if_else(is.na(problem_channels_r$remove), 0, 1)
problem_channels_r$remove <- as.logical(problem_channels_r$remove)
problem_channels_r <- problem_channels_r %>% filter(problem_channels_r$remove == TRUE)
## remove the channels (after visual inspection too!)
nirsData <- lapply(nirsData, removeChannels, problem_channels_r)