-
Notifications
You must be signed in to change notification settings - Fork 3
/
README.Rmd
199 lines (161 loc) · 5.92 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
library(tidyverse)
theme_set(theme_bw())
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-",
dpi = 300
)
```
# xrftools
![R-CMD-check](https://github.com/paleolimbot/xrftools/workflows/R-CMD-check/badge.svg) [![Coverage status](https://codecov.io/gh/paleolimbot/xrftools/branch/master/graph/badge.svg)](https://codecov.io/github/paleolimbot/xrftools?branch=master)
The goal of xrftools is to provide tools to read, plot, and interpret X-Ray fluorescence spectra.
## Installation
You can install xrftools from github with:
```{r gh-installation, eval = FALSE}
# install.packages("remotes")
remotes::install_github("paleolimbot/xrftools")
```
## Example
Read in a Panalytical XRF spectrum and plot it.
```{r example}
library(tidyverse)
library(xrftools)
pan_example_dir <- system.file("spectra_files/Panalytical", package = "xrftools")
pan_files <- list.files(pan_example_dir, ".mp2", full.names = TRUE)
specs <- read_xrf_panalytical(pan_files)
specs %>%
xrf_despectra() %>%
unnest(.spectra) %>%
ggplot(aes(x = energy_kev, y = cps, col = SampleIdent)) +
geom_line() +
facet_wrap(vars(ConditionSet), scales = "free_y")
```
## Baselines
The **xrf** package can use several existing methods for estimating "background" or "baseline" values. The most useful of these for XRF spectra is the Sensitive Nonlinear Iterative Peak (SNIP) method, implemented in the **Peaks** package.
```{r}
specs %>%
slice(3) %>%
xrf_add_baseline_snip(iterations = 20) %>%
xrf_despectra() %>%
unnest() %>%
filter(energy_kev <= 15) %>%
ggplot(aes(x = energy_kev)) +
geom_line(aes(y = cps, col = "raw")) +
geom_line(aes(y = baseline, col = "baseline")) +
geom_line(aes(y = cps - baseline, col = "cps - baseline"))
```
## Smoothing
```{r}
specs %>%
slice(3) %>%
xrf_add_baseline_snip(iterations = 20) %>%
xrf_add_smooth_filter(filter = xrf_filter_gaussian(alpha = 2.5), .iter = 5) %>%
xrf_despectra() %>%
unnest() %>%
filter(energy_kev <= 15) %>%
ggplot(aes(x = energy_kev)) +
geom_line(aes(y = cps, col = "raw"), alpha = 0.3) +
geom_line(aes(y = smooth - baseline, col = "smooth"))
```
## Deconvolution
```{r}
deconvoluted <- specs %>%
filter(ConditionSet %in% c("Omnian", "Omnian2")) %>%
xrf_add_smooth_filter(filter = xrf_filter_gaussian(width = 5), .iter = 20) %>%
xrf_add_baseline_snip(.values = .spectra$smooth, iterations = 20) %>%
xrf_add_deconvolution_gls(
.spectra$energy_kev,
.spectra$smooth - .spectra$baseline,
energy_max_kev = kV * 0.75, peaks = xrf_energies("major")
)
certified_vals <- system.file("spectra_files/oreas_concentrations.csv", package = "xrftools") %>%
read_csv(col_types = cols(standard = col_character(), value = col_double(), .default = col_guess())) %>%
filter(method == "4-Acid Digestion") %>%
select(SampleIdent = standard, element, certified_value = value)
deconv <- deconvoluted %>%
unnest(.deconvolution_peaks) %>%
select(SampleIdent, ConditionSet, kV, element, height, peak_area, peak_area_se)
deconv %>%
left_join(certified_vals, by = c("SampleIdent", "element")) %>%
ggplot(aes(x = certified_value, y = peak_area, col = SampleIdent, shape = ConditionSet)) +
geom_errorbar(aes(ymin = peak_area - peak_area_se, ymax = peak_area + peak_area_se)) +
geom_point() +
facet_wrap(~element, scales = "free") +
theme_bw(10)
```
```{r}
deconv_element <- deconvoluted %>%
unnest(.deconvolution_components)
ggplot() +
geom_line(
aes(x = energy_kev, y = response),
data = deconvoluted %>% unnest(.deconvolution_response),
size = 0.2
) +
geom_area(
aes(x = energy_kev, y = response_fit, fill = element),
data = deconvoluted %>% unnest(.deconvolution_components),
alpha = 0.5
) +
facet_wrap(~ConditionSet + SampleIdent, scales = "free") +
scale_y_sqrt() +
theme_bw(10)
```
```{r, eval=FALSE}
spec <- specs %>%
slice(7) %>%
xrf_add_baseline_snip(iterations = 20) %>%
xrf_add_smooth_filter(filter = xrf_filter_gaussian(alpha = 2.5), .iter = 5) %>%
pull(.spectra) %>%
first()
sigma_index <- 6
energy_kev <- spec$energy_kev
values <- spec$fit - spec$background
energy_res <- mean(diff(energy_kev))
search <- Peaks::SpectrumSearch(values, threshold = 0.01, sigma = sigma_index)
g <- function(x, mu = 0, sigma = 1, height = 1) height * exp(-0.5 * ((x - mu) / sigma) ^ 2)
tbl <- tibble::tibble(
peak_index = search$pos,
peak_energy_kev = energy_kev[peak_index],
peak_sigma = sigma_index * energy_res,
peak_height = values[peak_index],
peak_area = peak_height * peak_sigma * sqrt(2 * pi)
)
peak_response = pmap(list(mu = tbl$peak_energy_kev, sigma = tbl$peak_sigma, height = tbl$peak_height), g, energy_kev)
spec$deconv <- do.call(cbind, peak_response) %>%
rowSums()
spec$deconv2 <- search$y
ggplot(spec, aes(energy_kev)) +
geom_line(aes(y = fit - background, col = "original")) +
geom_line(aes(y = deconv, col = "deconv")) +
xlim(0, 10) +
stat_xrf_peaks(aes(y = fit - background), epsilon = 10)
```
```{r, eval=FALSE}
oreas22d <- specs %>%
filter(SampleIdent == "oreas 22d") %>%
unnest(.spectra) %>%
mutate(cps = counts / LiveTime)
xrf_en <- x_ray_xrf_energies %>%
crossing(tibble(ConditionSet = unique(oreas22d$ConditionSet))) %>%
group_by(ConditionSet) %>%
mutate(
data = list(oreas22d[oreas22d$ConditionSet == ConditionSet[1],]),
counts = approx(data[[1]]$energy_kev, data[[1]]$counts, energy_kev)$y,
fit = approx(data[[1]]$energy_kev, data[[1]]$fit, energy_kev)$y,
background = approx(data[[1]]$energy_kev, data[[1]]$background, energy_kev)$y,
cps = approx(data[[1]]$energy_kev, data[[1]]$cps, energy_kev)$y
) %>%
select(-data)
library(plotly)
plot_ly() %>%
add_lines(x = ~energy_kev, y = ~cps, color = ~ConditionSet, hoverinfo = "none",
data = oreas22d) %>%
add_markers(x = ~energy_kev, y = ~cps, text = ~element, color = ~ConditionSet, data = xrf_en)
```