knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
An R Package for analysing functional near-infrared spectroscopy (fNIRS) data
install.packages("devtools")
devtools::install_github("erzk/fnirsr")
to install the package.Sample data files come from NIRS-SPM and HOMER2.
In its current development stage this package can only read raw csv files produced by Hitachi ETG-4000. Other systems produce file with a different structure and so far I did not need to use them. Eventually, I might expand this package to work with other file types.
File Hitachi_ETG4000_24Ch_Total.csv , which is used in this vignette and is attached to this package, comes from NIRS-SPM.
Top level information about the recording is held in a header. It has an irregular form so it is a bit tricky to parse. This package version reads the section of the csv file before the data section and returns a vector with header info:
library(fnirsr) file_path <- system.file("extdata", "Hitachi_ETG4000_24Ch_Total.csv", package = "fnirsr") header <- load_ETG4000_header(file_path) head(header)
Loading the signal from csv files can be accomplished using the basic load_ETG4000_data()
function. It reads the data section of a csv file, changes the Time
column to reflect time period from the beginning of the recording (instead of actual hour), and returns a data frame. Header of the ETG-4000 file needs to be provided as it includes the information about the sampling period.
df <- load_ETG4000_data(file_path, header) str(df)
Once the csv file is loaded and a data frame is created, you can start plotting the signal.
Plotting function plot_ETG4000()
comes with four arguments:
facets
overlap
channel
average
The default choice is facets
which will show all channels in separate facets. This should enable spotting outliers.
plot_ETG4000(df)
Another option is plotting all channels overlapping each other:
plot_ETG4000(df, type = "overlap")
Alternatively, if you want to plot a single channel of interest then use the separate
argument and a channel number. This option uses time column as an x-axis (as opposed to the previous plots using samples).
plot_ETG4000(df, type = "separate", channel = 1)
In order to create a plot showing averaged signal, it is necessary to first create a column with the averaged signal:
df <- grand_average_ETG4000(df) names(df)
Once GrandAverage
column is created, the plot for averaged channels can be created:
plot_ETG4000(df, type = "average")
If a channel (or several channels) is corrupt and cannot be cleaned then the simplest way to obtain clean grand average is to remove the noisy channel.
The faceted plots above show that channel 15 and 20 look noisy. To remove these channels from the signal data frame use the following:
df <- remove_channels_ETG4000(df, channel = c(15, 20)) names(df)
fNIRS signal is likely to show a linear trend which can be removed.
Grand Average in the plot above is showing a linear downward trend. The linear trend can be removed from all channels (recommended) or from a single channel.
fnirs_detrended <- detrend_ETG4000_data(df) # detrend all channels plot_ETG4000(fnirs_detrended)
I suggest detrending the signal before creating a Grand Average. This way the grand_average_ETG4000()
function will create a Grand Average column with detrended signal.
The effect of detrending is easier to observe when zooming on a particular channel. Compare the plots underneath to see how removing the linear trend is changing the signal:
plot_ETG4000(df, "separate", 18) # zoom on one channel to notice detrending
Here is the same channel but without the linear trend:
plot_ETG4000(fnirs_detrended, "separate", 18)
It is also possible to detrend the signal of only one channel:
# plot of the original channel before detrending plot_ETG4000(df, "separate", 24)
Here is that channel after detrending. Other channels are not changed.
# detrend only one channel - 24 fnirs_det_24 <- detrend_ETG4000_data(df, "single", 24) # plot of the same channel after detrending plot_ETG4000(fnirs_det_24, "separate", 24)
While working with fNIRS data you might come across other file formats. One of the most popular formats is .nirs which is used by HOMER2. This package's main goal is to help in analysing ETG-4000 data but I happened to write simple .nirs functions.
To load .nirs data use the following code:
file_path_nirs <- system.file("extdata", "Simple_Probe.nirs", package = "fnirsr") nirs_file <- load_nirs_data(file_path_nirs)
This will load a list with data and additional information. You can explore it in the following way:
str(nirs_file)
The most interesting elements are t
(time) and d
(data):
# matrix dimensions dim(nirs_file$d) # have a look at the data head(nirs_file$d)
Signals can be visualised in faceted time series plots. Red lines symbolise the events (triggers).
plot_nirs(nirs_file)
Next releases will include filtering, splitting, and transforming the continuous recordings.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.