Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
173 lines (115 sloc) 5.71 KB
title author date output vignette
Introduction to fnirsr package
Eryk Walczak
`r Sys.Date()`
%\VignetteIndexEntry{Vignette Title} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8}

This vignette will show how to load raw csv files produced by Hitachi ETG-4000 fNIRS. The rationale for writing this package was to allow conducting fNIRS data analysis in R.

Loading a file (ETG-4000)

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:


file_path <- system.file("extdata", "Hitachi_ETG4000_24Ch_Total.csv", package = "fnirsr")
header <- load_ETG4000_header(file_path)


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)


Plotting a signal (ETG-4000)

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.


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)


Once GrandAverage column is created, the plot for averaged channels can be created:

plot_ETG4000(df, type = "average")

Cleaning a signal (ETG-4000)

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))


Detrending a signal (ETG-4000)

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

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)

Loading a file (.nirs)

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:


The most interesting elements are t (time) and d (data):

# matrix dimensions

# have a look at the data

Plotting a signal (.nirs)

Signals can be visualised in faceted time series plots. Red lines symbolise the events (triggers).