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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
"""Peak detection algorithms."""
import warnings
import numpy as np
from scipy import optimize
from scipy.integrate import simpson
eps = np.finfo(float).eps
def indexes(y, thres=0.3, min_dist=1, thres_abs=False):
"""Peak detection routine.
Finds the numeric index of the peaks in *y* by taking its first order difference. By using
*thres* and *min_dist* parameters, it is possible to reduce the number of
detected peaks. *y* must be signed.
Parameters
----------
y : ndarray (signed)
1D amplitude data to search for peaks.
thres : float between [0., 1.]
Normalized threshold. Only the peaks with amplitude higher than the
threshold will be detected.
min_dist : int
Minimum distance between each detected peak. The peak with the highest
amplitude is preferred to satisfy this constraint.
thres_abs: boolean
If True, the thres value will be interpreted as an absolute value, instead of
a normalized threshold.
Returns
-------
ndarray
Array containing the numeric indexes of the peaks that were detected.
When using with Pandas DataFrames, iloc should be used to access the values at the returned positions.
"""
if isinstance(y, np.ndarray) and np.issubdtype(y.dtype, np.unsignedinteger):
raise ValueError("y must be signed")
if not thres_abs:
thres = thres * (np.max(y) - np.min(y)) + np.min(y)
min_dist = int(min_dist)
# compute first order difference
dy = np.diff(y)
# propagate left and right values successively to fill all plateau pixels (0-value)
zeros, = np.where(dy == 0)
# check if the signal is totally flat
if len(zeros) == len(y) - 1:
return np.array([])
if len(zeros):
# compute first order difference of zero indexes
zeros_diff = np.diff(zeros)
# check when zeros are not chained together
zeros_diff_not_one, = np.add(np.where(zeros_diff != 1), 1)
# make an array of the chained zero indexes
zero_plateaus = np.split(zeros, zeros_diff_not_one)
# fix if leftmost value in dy is zero
if zero_plateaus[0][0] == 0:
dy[zero_plateaus[0]] = dy[zero_plateaus[0][-1] + 1]
zero_plateaus.pop(0)
# fix if rightmost value of dy is zero
if len(zero_plateaus) and zero_plateaus[-1][-1] == len(dy) - 1:
dy[zero_plateaus[-1]] = dy[zero_plateaus[-1][0] - 1]
zero_plateaus.pop(-1)
# for each chain of zero indexes
for plateau in zero_plateaus:
median = np.median(plateau)
# set leftmost values to leftmost non zero values
dy[plateau[plateau < median]] = dy[plateau[0] - 1]
# set rightmost and middle values to rightmost non zero values
dy[plateau[plateau >= median]] = dy[plateau[-1] + 1]
# find the peaks by using the first order difference
peaks = np.where(
(np.hstack([dy, 0.0]) < 0.0)
& (np.hstack([0.0, dy]) > 0.0)
& (np.greater(y, thres))
)[0]
# handle multiple peaks, respecting the minimum distance
if peaks.size > 1 and min_dist > 1:
highest = peaks[np.argsort(y[peaks])][::-1]
rem = np.ones(y.size, dtype=bool)
rem[peaks] = False
for peak in highest:
if not rem[peak]:
sl = slice(max(0, peak - min_dist), peak + min_dist + 1)
rem[sl] = True
rem[peak] = False
peaks = np.arange(y.size)[~rem]
return peaks
def centroid(x, y):
"""Computes the centroid for the specified data.
Refer to centroid2 for a more complete, albeit slower version.
Parameters
----------
x : ndarray
Data on the x axis.
y : ndarray
Data on the y axis.
Returns
-------
float
Centroid of the data.
"""
return np.sum(x * y) / np.sum(y)
def centroid2(y, x=None, dx=1.0):
"""Computes the centroid for the specified data.
Not intended to be used
Parameters
----------
y : array_like
Array whose centroid is to be calculated.
x : array_like, optional
The points at which y is sampled.
Returns
-------
(centroid, sd)
Centroid and standard deviation of the data.
"""
yt = np.array(y)
if x is None:
x = np.arange(yt.size, dtype="float") * dx
normaliser = simpson(y=yt, x=x)
centroid = simpson(y=x * yt, x=x) / normaliser
var = simpson(y=(x - centroid) ** 2 * yt, x=x) / normaliser
return centroid, np.sqrt(var)
def gaussian(x, ampl, center, dev):
"""Computes the Gaussian function.
Parameters
----------
x : number
Point to evaluate the Gaussian for.
a : number
Amplitude.
b : number
Center.
c : number
Width.
Returns
-------
float
Value of the specified Gaussian at *x*
"""
return ampl * np.exp(-(x - float(center)) ** 2 / (2.0 * dev ** 2 + eps))
def gaussian_fit(x, y, center_only=True):
"""Performs a Gaussian fitting of the specified data.
Parameters
----------
x : ndarray
Data on the x axis.
y : ndarray
Data on the y axis.
center_only: bool
If True, returns only the center of the Gaussian for `interpolate` compatibility
Returns
-------
ndarray or float
If center_only is `False`, returns the parameters of the Gaussian that fits the specified data
If center_only is `True`, returns the center position of the Gaussian
"""
if len(x) < 3:
# used RuntimeError to match errors raised in scipy.optimize
raise RuntimeError("At least 3 points required for Gaussian fitting")
initial = [np.max(y), x[0], (x[1] - x[0]) * 5]
params, pcov = optimize.curve_fit(gaussian, x, y, initial)
if center_only:
return params[1]
else:
return params
def interpolate(x, y, ind=None, width=10, func=gaussian_fit):
"""Tries to enhance the resolution of the peak detection by using
Gaussian fitting, centroid computation or an arbitrary function on the
neighborhood of each previously detected peak index.
RuntimeErrors raised in the fitting function will be converted to warnings, with the peak
being mantained as the original one (in the ind array).
Parameters
----------
x : ndarray
Data on the x dimension.
y : ndarray
Data on the y dimension.
ind : ndarray
Indexes of the previously detected peaks. If None, indexes() will be
called with the default parameters.
width : int
Number of points (before and after) each peak index to pass to *func*
in order to increase the resolution in *x*.
func : function(x,y)
Function that will be called to detect an unique peak in the x,y data.
Returns
-------
ndarray :
Array with the adjusted peak positions (in *x*)
"""
assert x.shape == y.shape
if ind is None:
ind = indexes(y)
out = []
for i in ind:
slice_ = slice(i - width, i + width + 1)
try:
best_idx = func(x[slice_], y[slice_])
except RuntimeError as e:
warnings.warn(str(e))
best_idx = i
out.append(best_idx)
return np.array(out)
|