function detectPeaks(data, accessor, options) {
let {lookaround, sensitivity, coalesce, full} = Object.assign({
lookaround: 2,
sensitivity: 1.4,
coalesce: 0,
full: false
}, options || accessor)
let values = typeof accessor == "function" ? data.map(accessor) : data
let scores = normalize(
values.map(
(value, index) => peakiness(
values.slice(max(0, index - lookaround), index),
value,
values.slice(index + 1, index + lookaround + 1)
)
)
)
let candidates = d3.range(scores.length).filter(index => scores[index] > sensitivity)
let groups = candidates.length ? [[candidates[0]]] : []
d3.pairs(candidates).forEach(([a, b]) => {
if (b - a < coalesce) {
groups[groups.length - 1].push(b)
} else {
groups.push([b])
}
})
let peaks = groups.map(
group => group[d3.scan(group, (a, b) => values[b] - values[a])]
)
return full ? { data, values, scores, candidates, groups, peaks } : peaks
}