---
title: fnirsr
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, echo = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "README-"
)
```

[![Travis-CI Build Status](https://travis-ci.org/erzk/fnirsr.svg?branch=master)](https://travis-ci.org/erzk/fnirsr)
[![Coverage Status](https://img.shields.io/codecov/c/github/erzk/fnirsr/master.svg)](https://codecov.io/github/erzk/fnirsr?branch=master)
[![Package-License](http://img.shields.io/badge/license-GPL--3-brightgreen.svg?style=flat)](http://www.gnu.org/licenses/gpl-3.0.html)
[![DOI](https://zenodo.org/badge/80361844.svg)](https://zenodo.org/badge/latestdoi/80361844)
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/fnirsr)](https://cran.r-project.org/package=fnirsr)

**An R Package for analysing functional near-infrared spectroscopy (fNIRS) data**

## Installation

* Install *devtools* if you don't have it yet: `install.packages("devtools")`
* Use `devtools::install_github("erzk/fnirsr")` to install the package.

Sample data files come from [NIRS-SPM](https://www.nitrc.org/projects/nirs_spm/) and [HOMER2](http://homer-fnirs.org/).

## Usage

## 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](http://bispl.weebly.com/nirs-spm.html#/).

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:

```{r, message=FALSE, warning=FALSE}
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.

```{r}
df <- load_ETG4000_data(file_path, header)

str(df)
```

## 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.

```{r facets, fig.width=7, fig.height=8}
plot_ETG4000(df)
```

Another option is plotting all channels overlapping each other:

```{r overlap, fig.width=7, fig.height=7}
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).

```{r separate, fig.width=7, fig.height=6}
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:

```{r}
df <- grand_average_ETG4000(df)

names(df)
```

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

```{r grand_average, fig.width=7, fig.height=6}
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:

```{r}
df <- remove_channels_ETG4000(df, channel = c(15, 20))

names(df)
```

## 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.

```{r facets_detrended, fig.width=7, fig.height=8}
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:

```{r separate_18}
plot_ETG4000(df, "separate", 18) # zoom on one channel to notice detrending
```

Here is the same channel but without the linear trend:

```{r separate_18_detrended}
plot_ETG4000(fnirs_detrended, "separate", 18)
```

It is also possible to detrend the signal of only one channel:

```{r separate_24}
# plot of the original channel before detrending
plot_ETG4000(df, "separate", 24)
```

Here is that channel after detrending. Other channels are not changed.

```{r separate_24_detrended}
# 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](http://homer-fnirs.org/). 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:

```{r}
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:

```{r}
str(nirs_file)
```

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

```{r}
# matrix dimensions
dim(nirs_file$d)

# have a look at the data
head(nirs_file$d)
```

## Plotting a signal (.nirs)

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

```{r facets_nirs, fig.width=7, fig.height=8}
plot_nirs(nirs_file)
```

*Next releases will include filtering, splitting, and transforming the continuous recordings.*
