---
title: "spant SVS analysis results"
output: html_document
params:
  fit_res: ""
  argg: ""
  w_ref_available: ""
  w_ref: ""
  res_tab_unscaled: ""
  res_tab_ratio: ""    
  res_tab_legacy: ""
  res_tab_molal: ""
  dyn_data_uncorr: ""
  dyn_data_corr: ""
  summary_tab: ""
---

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

# {.tabset}

## Fit plots {.tabset}

```{r, results = 'asis', fig.width=7, fig.height=6}
for (n in 1:Ndyns(params$fit_res$data)) {
  cat("\n### ", n, "\n", sep = "")
  plot(params$fit_res, dyn = n)
  cat("\n")
}
```

```{r, results = 'asis', fig.width=7, fig.height=7}
if (!is.null(dyn_data_uncorr)) {
  cat("## Dynamic plots {.tabset}\n")
  if (!is.null(dyn_data_corr)) {
    cat("### Spectrogram with dynamic correction\n")
    image(dyn_data_corr, xlim = c(4, 0.5))
  }
  cat("\n\n### Spectrogram without dynamic correction\n")
  image(dyn_data_uncorr, xlim = c(4, 0.5))
}
```

## Spectral plots {.tabset}

### Mean processed cropped
```{r, fig.width=7, fig.height=6}
phase_offset <- params$fit_res$res_tab$phase
shift_offset <- params$fit_res$res_tab$shift
proc_spec <- phase(mean_dyns(params$fit_res$data), mean(phase_offset))
proc_spec <- shift(proc_spec, mean(shift_offset), units = "ppm")
proc_spec <- zf(proc_spec)
plot(proc_spec, xlim = c(4, 0.5))
```

### Mean processed full
```{r, fig.width=7, fig.height=6}
plot(proc_spec)
```

```{r, results = 'asis', fig.width=7, fig.height=6}
if (params$w_ref_available) {
  cat("### Water reference resonance\n")
  # w_ref_proc <- shift(w_ref, shift_offset, units = "ppm")
  w_ref_proc <- auto_phase(w_ref, xlim = c(5.3, 4))
  w_ref_proc <- zf(w_ref_proc)
  plot(w_ref_proc, xlim = c(5.3, 4))
}
```

## Diagnostics table
```{r}
name  <- c("Mean spectral SNR")
value <- round_dp(mean(params$fit_res$res_tab$SNR), 2)

if (!is.null(params$fit_res$res_tab$tNAA_lw)) {
  name  <- c(name, "Mean tNAA linewidth (ppm)")
  value <- c(value, round_dp(mean(params$fit_res$res_tab$tNAA_lw), 4))
}

if (!is.null(params$fit_res$res_tab$NAA_lw)) {
  name  <- c(name, "Mean NAA linewidth (ppm)")
  value <- c(value, round_dp(mean(params$fit_res$res_tab$NAA_lw), 4))
}

if (!is.null(params$fit_res$res_tab$tCho_lw)) {
  name  <- c(name, "Mean tCho linewidth (ppm)")
  value <- c(value, round_dp(mean(params$fit_res$res_tab$tCho_lw), 4))
}

if (!is.null(params$fit_res$res_tab$Cho_lw)) {
  name  <- c(name, "Mean Cho linewidth (ppm)")
  value <- c(value, round_dp(mean(params$fit_res$res_tab$Cho_lw), 4))
}

if (!is.null(params$fit_res$res_tab$tCr_lw)) {
  name  <- c(name, "Mean tCr linewidth (ppm)")
  value <- c(value, round_dp(mean(params$fit_res$res_tab$tCr_lw), 4))
}

if (!is.null(params$fit_res$res_tab$Cr_lw)) {
  name  <- c(name, "Mean Cr linewidth (ppm)")
  value <- c(value, round_dp(mean(params$fit_res$res_tab$Cr_lw), 4))
}

if (params$w_ref_available) {
  name  <- c(name,  "Water amplitude", "Water suppression efficiency (%)")
  value <- c(value, format(params$res_tab_molal$w_amp[1]),
             round_dp(params$res_tab_molal$ws_eff[1], 3))
}

name  <- c(name, "Mean fit quality number (FQN)", "Mean baseline effective d.f. per ppm",
           "Mean lineshape asymmetry", "Mean spectral signal to residual ratio")
value <- c(value, round_dp(mean(params$fit_res$res_tab$FQN), 2),
           round_dp(mean(params$fit_res$res_tab$bl_ed_pppm), 2),
           round_dp(mean(params$fit_res$res_tab$asym), 2),
           round_dp(mean(params$fit_res$res_tab$SRR), 2))

diag_tab <- data.frame(name, value)
boot_opts <- c("striped", "hover", "condensed")
kableExtra::kable_styling(kableExtra::kbl(diag_tab, align = c("l", "r"),
                                          col.names = c("Name", "Value")),
                          full_width = FALSE, position = "left",
                          bootstrap_options = boot_opts)
```

## Provenance
```{r, echo = TRUE}
packageVersion("spant")
Sys.time()
print(params$fit_res$data, full = TRUE)
print(params$w_ref, full = TRUE)
print(argg)
```

# {-}

**Please cite the following if you found ABfit and spant useful in your research:**

Wilson M. Adaptive baseline fitting for 1H MR spectroscopy analysis. Magn Reson 
Med. 2021 Jan;85(1):13-29. https://doi.org/10.1002/mrm.28385

Wilson, M. spant: An R package for magnetic resonance spectroscopy 
analysis. Journal of Open Source Software. 2021 6(67), 3646. 
https://doi.org/10.21105/joss.03646

Wilson M. Robust retrospective frequency and phase correction for single-voxel 
MR spectroscopy. Magn Reson Med. 2019 May;81(5):2878-2886. 
https://doi.org/10.1002/mrm.27605