---
title: "SVS QA"
output: html_document
params:
  data: ""
  label: ""
---

```{r setup, include=FALSE}
library(spant)
library(ggplot2)
knitr::opts_chunk$set(echo = FALSE)
theme_set(theme_bw())
```

## `r params$label`

### Mean spectra
```{r, fig.width=10}
par(mfcol = c(1, 2))
plot(zf(params$data$mean_corr), xlim = c(4, 0.2), restore_def_par = FALSE,
     main = "Freq & phase corrected")
plot(zf(params$data$mean_uncorr), xlim = c(4, 0.2), main = "Uncorrected")
```

### Quality metrics
Median single shot SNR : `r round(params$data$summary_diags['ss_median_spec_snr'], 1)`

Linewidth of mean spectrum (post FP correction) : `r round(params$data$summary_diags['mean_corr_spec_lw'], 3)` ppm

SNR of mean spectrum (post FP correction) : `r round(params$data$summary_diags['mean_corr_spec_snr'], 1)`

Linewidth of mean spectrum (no correction) : `r round(params$data$summary_diags['mean_uncorr_spec_lw'], 3)` ppm

SNR of mean spectrum (no correction) : `r round(params$data$summary_diags['mean_uncorr_spec_snr'], 1)`

Frequency shift range : `r round(params$data$summary_diags['shift_hz_range'], 1)` Hz

Dynamic lipid fluctuation range : `r round(100 * params$data$summary_diags['dlfr'], 1)`% tCr intensity

```{r, results = 'asis', fig.width=10}
if (!is.null(params$data$dyn_wref_table)) {
  cat("### Dynamic water linewidths\n")
  diag_table <- params$data$dyn_wref_table
  p <- ggplot(diag_table, aes(x = time_sec, y = water_fwhm)) + geom_line() +
    xlab("Time (s)") + theme(legend.position = "none") + 
    scale_y_continuous("Water linewidth (ppm)",
                       sec.axis = sec_axis(~ . * params$data$mean_corr$ft / 1e6,
                                           name = "Water linewidth (Hz)")
  )
  print(p)
}
```

```{r, results = 'asis', fig.width=10}
if (!is.null(params$data$dyn_wref_table)) {
  cat("### Dynamic water areas\n")
  diag_table <- params$data$dyn_wref_table
  p <- ggplot(diag_table, aes(x = time_sec, y = water_area)) + geom_line() +
    xlab("Time (s)") + ylab("Water area (a.u.)") +
    theme(legend.position = "none")
  print(p)
}
```

```{r, results = 'asis', fig.width=10}
if (!is.null(params$data$dyn_wref_table)) {
  cat("### Dynamic water peak height\n")
  diag_table <- params$data$dyn_wref_table
  p <- ggplot(diag_table, aes(x = time_sec, y = water_height)) + geom_line() +
    xlab("Time (s)") + ylab("Water peak height (a.u.)") +
    theme(legend.position = "none")
  print(p)
}
```

### Dynamic frequency shifts (metabolite)
```{r, fig.width=10}
diag_table <- params$data$diag_table
    ggplot(diag_table, aes(x = time_sec, y = shifts_hz)) + geom_line() + 
      xlab("Time (s)") + ylab("Shift (Hz)")
```

### Dynamic linewidths (tNAA)
```{r, fig.width=10}
diag_table <- params$data$diag_table
p <- ggplot(diag_table, aes(x = time_sec, y = lw_ppm)) + geom_line() +
  xlab("Time (s)") +
  theme(legend.position = "none") + scale_y_continuous("tNAA linewidth (ppm)",
                       sec.axis = sec_axis(~ . * params$data$mean_corr$ft / 1e6,
                                           name = "tNAA linewidth (Hz)"))
if (!anyNA(diag_table$lw_ppm)) {
  p <- p + geom_line(aes(x = time_sec, y = lw_ppm_smo), linewidth = 1.5,
                     colour = "red")
}
print(p)
```

### Spectrograms
```{r, fig.width=10}
corr_spec   <- crop_spec(params$data$corrected)
uncorr_spec <- crop_spec(params$data$uncorrected)
par(mfcol = c(1, 2))
image(corr_spec, xlim = c(4, 0.2), restore_def_par = FALSE,
      main = "Freq & phase corrected", plot_dim = "time_sec", vline = NULL,
      vline_col = "red", vline_lty = 1)
image(uncorr_spec, xlim = c(4, 0.2), main = "Uncorrected",
      vline = NULL, plot_dim = "time_sec", vline_col = "red", vline_lty = 1)
```

### Mean subtracted spectrograms
```{r, fig.width=10}
par(mfcol = c(1, 2))
image(params$data$mrs_mean_sub, xlim = c(4, 0.2), restore_def_par = FALSE,
      main = "Smoothed", plot_dim = "time_sec")
image(params$data$mrs_mean_sub_bc, xlim = c(4, 0.2),
      main = "Smoothed and baseline corrected", plot_dim = "time_sec")
```