-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_centers.R
executable file
·129 lines (125 loc) · 4.1 KB
/
find_centers.R
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
#' Initialize centroids for `cluster_xmap()`
#'
#' @param xmap an `qm_xmap` class object generated by `read_xmap()``
#' @param qnt an `qm_qnt` class object generated by `read_qnt()``
#' @param fine_phase A character vector to specify fine grained phases which tend to comprise multi-phase pixels e.g., c('Pl', 'Amp')
#' @param saveas File name to save result. FALSE if not saving.
#'
#' @importFrom data.table fwrite
#' @importFrom dplyr bind_cols
#' @importFrom dplyr bind_rows
#' @importFrom dplyr filter
#' @importFrom dplyr group_by
#' @importFrom dplyr mutate
#' @importFrom dplyr summarise
#' @importFrom dplyr ungroup
#' @importFrom pipeR pipeline
#' @importFrom stats lsfit
#' @importFrom stats qnbinom
#' @importFrom stats median
#' @importFrom tidyr spread
#'
#' @export
find_centers <- function(
xmap,
qnt,
fine_phase = NULL,
saveas = 'centers0.csv'
) {
quantified <- names(xmap) %in% qnt$elm$elint
xmap_df <- as.data.frame(lapply(xmap, unlist, use.names = FALSE))
pipeline({
tidy_epma(qnt = qnt, xmap = xmap, cluster = NULL)
group_by(phase)
filter(all(is.na(map)) | !is.na(map))
ungroup
mutate( # Let weighting for least squares to be 0 for phases those who listed in fine_phase
w = if(is.null(fine_phase)) 1 else as.integer(phase %nin% fine_phase)
)
group_by(elint) # Peform least squares and estimate 99% prediction interval
mutate(
.tmp = is.finite(map),
map_est =
pkint * lsfit(pkint[.tmp], map[.tmp], w[.tmp], intercept = FALSE)$coef,
map_fnite = NULL,
pi_L = qnbinom(0.005, map_est, 0.5),
pi_H = qnbinom(0.995, map_est + 1, 0.5)
)
group_by(id)
mutate(within_pi = all((pi_L <= map) & (map <= pi_H))) # pi = prediction interval
group_by(elint, phase)
mutate(n_within_pi = sum(within_pi))
ungroup
select(elint, map, map_est, within_pi, n_within_pi, phase, id, nr)
bind_rows(
if(!all(quantified)) {
pipeline({
.
filter(elint == elint[1])
select(-elint, -map, -map_est)
bind_cols(
lapply(xmap_df[!quantified], `[`, .$nr)
)
gather(
elint, map, -within_pi, -n_within_pi, -phase, -id, -nr
)
mutate(map_est = map)
})
}
)
group_by(phase)
mutate(not_mapped_phase = all(is.na(map)))
ungroup
mutate(map = ifelse(not_mapped_phase | within_pi == 0, map_est, map))
filter(not_mapped_phase | within_pi | n_within_pi == 0)
group_by(elint, phase)
summarise(map = median(map))
ungroup
spread(elint, map)
# guess mapping intensity of certain phases in case
# target elements are not analyzed by reference point analysis
x ~ {
miss <- Reduce(`|`, lapply(x, is.na))
if(all(!miss)) return(x)
x[miss, -1] <- pipeline({
map2(
list(t(xmap_df[names(x[-1])])),
pmap(x[miss, -1], c),
`-`
)
map(square)
map(colSums, na.rm = TRUE)
map(which.min)
map(function(i) xmap_df[i, ])
bind_rows
`[`(names(x[-1]))
map2(x[miss, -1], function(y, x) ifelse(is.na(x), y, x))
bind_cols
})
x
}
save4qm(nm = saveas, saving = is.character(saveas))
})
}
#' (Deprecated) Use find_centers
#' @param wd working directory which contains .qnt directory. Use current directory if NULL
#' @param dir_map path to the directory which contains mapping data.
#' @param phase_fine fine grained phases which tend to be appear in multi-phase pixels e.g., c('Pl', 'Amp')
#' @param qnt an object generated by qnt_load or a path to the .RDS file created by qnt_load
#' @param qltmap an object generated by qltmap_load or a path to the .RDS file created by qltmap_load
#' @param saving file name to save. FALSE if not saving.
#' @export
qltmap_cls_centers <- function(
wd = NULL,
dir_map,
phase_fine = NULL,
qnt = read_qnt(paste0(wd, '/.qnt')),
qltmap = read_xmap(dir_map),
saving = 'centers_initial0.csv'
) {
.Deprecated(new = 'find_centers')
cd <- getwd()
on.exit(cd)
setwd(wd)
find_centers(fine_phase = phase_fine, qnt = qnt, xmap = qltmap, saveas = saving)
}