RSAbs 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- RSAbs/__init__.py +63 -0
- RSAbs/absorptions.py +26 -0
- RSAbs/absorptions_depth.py +23 -0
- RSAbs/compat.py +79 -0
- RSAbs/core.py +464 -0
- RSAbs/data/usgs_minerals_lut_cr.csv.gz +0 -0
- RSAbs/data/usgs_minerals_lut_raw.csv.gz +0 -0
- RSAbs/features.py +354 -0
- RSAbs/library.py +793 -0
- RSAbs/preprocessing.py +412 -0
- RSAbs/seek_absorptions.py +17 -0
- RSAbs/seek_absorptions_depth.py +60 -0
- RSAbs/viewer.py +270 -0
- RSAbs/visualization.py +232 -0
- rsabs-2.0.0.dist-info/METADATA +310 -0
- rsabs-2.0.0.dist-info/RECORD +20 -0
- rsabs-2.0.0.dist-info/WHEEL +5 -0
- rsabs-2.0.0.dist-info/entry_points.txt +2 -0
- rsabs-2.0.0.dist-info/licenses/LICENSE.txt +17 -0
- rsabs-2.0.0.dist-info/top_level.txt +1 -0
RSAbs/features.py
ADDED
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
"""Absorption feature detection and geometric parameterization.
|
|
2
|
+
|
|
3
|
+
Implements sub-band wavelength minimum refinement (Rodger et al., 2012),
|
|
4
|
+
continuum-removed and local-continuum band depth (Clark & Roush, 1984),
|
|
5
|
+
and geometric absorption parameters including FWHM, asymmetry, shoulder
|
|
6
|
+
positions, and integrated band area (van der Meer, 2004).
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
import numpy as np
|
|
10
|
+
import pandas as pd
|
|
11
|
+
from scipy.signal import find_peaks
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _parabolic_minimum(x, y, idx):
|
|
15
|
+
"""Refine discrete local minimum using 3-point quadratic interpolation.
|
|
16
|
+
|
|
17
|
+
References
|
|
18
|
+
----------
|
|
19
|
+
Rodger, A., Laukamp, C., Haest, M., & Cudahy, T. (2012). A simple
|
|
20
|
+
quadratic method of absorption feature wavelength estimation in continuum
|
|
21
|
+
removed spectra. *Remote Sensing of Environment*, 118, 273-283.
|
|
22
|
+
"""
|
|
23
|
+
if idx <= 0 or idx >= len(x) - 1:
|
|
24
|
+
return float(x[idx]), float(y[idx])
|
|
25
|
+
|
|
26
|
+
xs = x[idx - 1 : idx + 2]
|
|
27
|
+
ys = y[idx - 1 : idx + 2]
|
|
28
|
+
if not (np.all(np.isfinite(xs)) and np.all(np.isfinite(ys))):
|
|
29
|
+
return float(x[idx]), float(y[idx])
|
|
30
|
+
|
|
31
|
+
# Shift x around x[idx] for numerical stability
|
|
32
|
+
dx = xs - xs[1]
|
|
33
|
+
coeffs = np.polyfit(dx, ys, 2)
|
|
34
|
+
a, b, c = coeffs
|
|
35
|
+
if a <= 1e-12:
|
|
36
|
+
return float(x[idx]), float(y[idx])
|
|
37
|
+
|
|
38
|
+
x_vertex_rel = -b / (2.0 * a)
|
|
39
|
+
if x_vertex_rel < dx[0] or x_vertex_rel > dx[2]:
|
|
40
|
+
return float(x[idx]), float(y[idx])
|
|
41
|
+
|
|
42
|
+
x_min = float(xs[1] + x_vertex_rel)
|
|
43
|
+
y_min = float(a * x_vertex_rel ** 2 + b * x_vertex_rel + c)
|
|
44
|
+
return x_min, y_min
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _half_max_crossings(x_seg, depth_seg, min_pos_in_seg, half_depth):
|
|
48
|
+
"""Find left and right wavelengths where absorption depth crosses half_depth."""
|
|
49
|
+
# Left side: walk outward from minimum toward left shoulder
|
|
50
|
+
x_left = float(x_seg[0])
|
|
51
|
+
for j in range(min_pos_in_seg, 0, -1):
|
|
52
|
+
d1 = depth_seg[j]
|
|
53
|
+
d0 = depth_seg[j - 1]
|
|
54
|
+
if d1 >= half_depth >= d0:
|
|
55
|
+
denom = d1 - d0
|
|
56
|
+
frac = (half_depth - d0) / denom if abs(denom) > 1e-12 else 0.5
|
|
57
|
+
x_left = float(x_seg[j - 1] + frac * (x_seg[j] - x_seg[j - 1]))
|
|
58
|
+
break
|
|
59
|
+
|
|
60
|
+
# Right side: walk outward from minimum toward right shoulder
|
|
61
|
+
x_right = float(x_seg[-1])
|
|
62
|
+
for j in range(min_pos_in_seg, len(depth_seg) - 1):
|
|
63
|
+
d0 = depth_seg[j]
|
|
64
|
+
d1 = depth_seg[j + 1]
|
|
65
|
+
if d0 >= half_depth >= d1:
|
|
66
|
+
denom = d0 - d1
|
|
67
|
+
frac = (d0 - half_depth) / denom if abs(denom) > 1e-12 else 0.5
|
|
68
|
+
x_right = float(x_seg[j] + frac * (x_seg[j + 1] - x_seg[j]))
|
|
69
|
+
break
|
|
70
|
+
|
|
71
|
+
return x_left, x_right
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def extract_spectrum_features(
|
|
75
|
+
wavelengths,
|
|
76
|
+
reflectance,
|
|
77
|
+
continuum_removed=True,
|
|
78
|
+
min_depth=0.005,
|
|
79
|
+
min_prominence=0.002,
|
|
80
|
+
min_distance_nm=10.0,
|
|
81
|
+
min_width_nm=4.0,
|
|
82
|
+
refine_wavelength=True,
|
|
83
|
+
):
|
|
84
|
+
"""Detect and parameterize all absorption features in a single spectrum.
|
|
85
|
+
|
|
86
|
+
Refines discrete minima via 3-point quadratic interpolation (Rodger et al.,
|
|
87
|
+
2012), computes continuum-removed or local-continuum band depth (Clark &
|
|
88
|
+
Roush, 1984), and extracts FWHM, integrated band area, shoulder positions,
|
|
89
|
+
and normalized area asymmetry ``(A_right - A_left) / (A_right + A_left)``
|
|
90
|
+
(van der Meer, 2004).
|
|
91
|
+
|
|
92
|
+
References:
|
|
93
|
+
Clark, R. N., & Roush, T. L. (1984). Reflectance spectroscopy:
|
|
94
|
+
Quantitative analysis techniques for remote sensing applications.
|
|
95
|
+
*JGR*, 89(B7), 6329-6340.
|
|
96
|
+
|
|
97
|
+
Rodger, A., Laukamp, C., Haest, M., & Cudahy, T. (2012). A simple
|
|
98
|
+
quadratic method of absorption feature wavelength estimation in
|
|
99
|
+
continuum removed spectra. *Remote Sensing of Environment*, 118, 273-283.
|
|
100
|
+
|
|
101
|
+
van der Meer, F. (2004). Analysis of spectral absorption features in
|
|
102
|
+
hyperspectral imagery. *Int. J. Appl. Earth Obs. Geoinf.*, 5(1), 55-68.
|
|
103
|
+
|
|
104
|
+
Args:
|
|
105
|
+
wavelengths: 1D array-like of wavelengths (nm).
|
|
106
|
+
reflectance: 1D array-like of reflectance (or continuum-removed) values.
|
|
107
|
+
continuum_removed: If True, depth is ``1.0 - R_min``. If False, a local
|
|
108
|
+
linear continuum is constructed between each feature's shoulders.
|
|
109
|
+
min_depth: Minimum absorption depth (0-1) required to retain a feature.
|
|
110
|
+
min_prominence: Minimum peak prominence passed to ``scipy.signal.find_peaks``.
|
|
111
|
+
min_distance_nm: Minimum separation between adjacent minima in nm.
|
|
112
|
+
min_width_nm: Minimum peak width in nm.
|
|
113
|
+
refine_wavelength: If True, apply 3-point parabolic interpolation.
|
|
114
|
+
|
|
115
|
+
Returns:
|
|
116
|
+
pd.DataFrame: Tidy DataFrame with columns ``['wavelength', 'depth',
|
|
117
|
+
'prominence', 'fwhm', 'asymmetry', 'area', 'left_shoulder',
|
|
118
|
+
'right_shoulder', 'width_full']``.
|
|
119
|
+
"""
|
|
120
|
+
wvl = np.asarray(wavelengths, dtype=float).flatten()
|
|
121
|
+
y = np.asarray(reflectance, dtype=float).flatten()
|
|
122
|
+
|
|
123
|
+
valid = np.isfinite(wvl) & np.isfinite(y)
|
|
124
|
+
if np.sum(valid) < 5:
|
|
125
|
+
return pd.DataFrame(
|
|
126
|
+
columns=[
|
|
127
|
+
"wavelength",
|
|
128
|
+
"depth",
|
|
129
|
+
"prominence",
|
|
130
|
+
"fwhm",
|
|
131
|
+
"asymmetry",
|
|
132
|
+
"area",
|
|
133
|
+
"left_shoulder",
|
|
134
|
+
"right_shoulder",
|
|
135
|
+
"width_full",
|
|
136
|
+
]
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
wv = wvl[valid]
|
|
140
|
+
yv = y[valid]
|
|
141
|
+
order = np.argsort(wv)
|
|
142
|
+
wv = wv[order]
|
|
143
|
+
yv = yv[order]
|
|
144
|
+
|
|
145
|
+
# Convert physical nm constraints to sample counts
|
|
146
|
+
d_wvl = float(np.median(np.diff(wv)))
|
|
147
|
+
d_wvl = max(d_wvl, 1e-6)
|
|
148
|
+
distance_pts = max(1, int(round(min_distance_nm / d_wvl)))
|
|
149
|
+
width_pts = max(1, min_width_nm / d_wvl)
|
|
150
|
+
|
|
151
|
+
peaks, props = find_peaks(
|
|
152
|
+
-yv,
|
|
153
|
+
prominence=min_prominence,
|
|
154
|
+
distance=distance_pts,
|
|
155
|
+
width=width_pts,
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
records = []
|
|
159
|
+
for k, idx in enumerate(peaks):
|
|
160
|
+
lb = int(props["left_bases"][k])
|
|
161
|
+
rb = int(props["right_bases"][k])
|
|
162
|
+
prom = float(props["prominences"][k])
|
|
163
|
+
|
|
164
|
+
if refine_wavelength:
|
|
165
|
+
w_min, y_min = _parabolic_minimum(wv, yv, idx)
|
|
166
|
+
else:
|
|
167
|
+
w_min, y_min = float(wv[idx]), float(yv[idx])
|
|
168
|
+
|
|
169
|
+
x_seg = wv[lb : rb + 1]
|
|
170
|
+
y_seg = yv[lb : rb + 1]
|
|
171
|
+
|
|
172
|
+
if continuum_removed:
|
|
173
|
+
cont_seg = np.ones_like(y_seg)
|
|
174
|
+
cont_at_min = 1.0
|
|
175
|
+
else:
|
|
176
|
+
cont_seg = np.interp(x_seg, [wv[lb], wv[rb]], [yv[lb], yv[rb]])
|
|
177
|
+
cont_at_min = float(np.interp(w_min, [wv[lb], wv[rb]], [yv[lb], yv[rb]]))
|
|
178
|
+
|
|
179
|
+
with np.errstate(divide="ignore", invalid="ignore"):
|
|
180
|
+
rel_seg = np.where(cont_seg > 1e-12, y_seg / cont_seg, 1.0)
|
|
181
|
+
depth_seg = np.clip(1.0 - rel_seg, 0.0, None)
|
|
182
|
+
|
|
183
|
+
if cont_at_min > 1e-12:
|
|
184
|
+
depth = max(0.0, 1.0 - (y_min / cont_at_min))
|
|
185
|
+
else:
|
|
186
|
+
depth = 0.0
|
|
187
|
+
|
|
188
|
+
if depth < min_depth:
|
|
189
|
+
continue
|
|
190
|
+
|
|
191
|
+
min_pos_in_seg = idx - lb
|
|
192
|
+
half_depth = 0.5 * depth
|
|
193
|
+
x_half_l, x_half_r = _half_max_crossings(x_seg, depth_seg, min_pos_in_seg, half_depth)
|
|
194
|
+
fwhm = max(0.0, x_half_r - x_half_l)
|
|
195
|
+
|
|
196
|
+
# Integrated band area and asymmetry (van der Meer, 2004)
|
|
197
|
+
total_area = float(np.trapezoid(depth_seg, x_seg))
|
|
198
|
+
left_x = np.append(x_seg[: min_pos_in_seg + 1], w_min) if w_min > x_seg[min_pos_in_seg] else x_seg[: min_pos_in_seg + 1]
|
|
199
|
+
left_d = np.append(depth_seg[: min_pos_in_seg + 1], depth) if w_min > x_seg[min_pos_in_seg] else depth_seg[: min_pos_in_seg + 1]
|
|
200
|
+
right_x = np.insert(x_seg[min_pos_in_seg:], 0, w_min) if w_min < x_seg[min_pos_in_seg] else x_seg[min_pos_in_seg:]
|
|
201
|
+
right_d = np.insert(depth_seg[min_pos_in_seg:], 0, depth) if w_min < x_seg[min_pos_in_seg] else depth_seg[min_pos_in_seg:]
|
|
202
|
+
|
|
203
|
+
# Ensure monotonic x for trapezoid integration
|
|
204
|
+
if len(left_x) >= 2 and np.all(np.diff(left_x) >= 0):
|
|
205
|
+
area_left = float(np.trapezoid(left_d, left_x))
|
|
206
|
+
else:
|
|
207
|
+
area_left = float(np.trapezoid(depth_seg[: min_pos_in_seg + 1], x_seg[: min_pos_in_seg + 1]))
|
|
208
|
+
|
|
209
|
+
if len(right_x) >= 2 and np.all(np.diff(right_x) >= 0):
|
|
210
|
+
area_right = float(np.trapezoid(right_d, right_x))
|
|
211
|
+
else:
|
|
212
|
+
area_right = float(np.trapezoid(depth_seg[min_pos_in_seg:], x_seg[min_pos_in_seg:]))
|
|
213
|
+
|
|
214
|
+
denom_area = area_left + area_right
|
|
215
|
+
asymmetry = (area_right - area_left) / denom_area if denom_area > 1e-12 else 0.0
|
|
216
|
+
|
|
217
|
+
records.append(
|
|
218
|
+
{
|
|
219
|
+
"wavelength": w_min,
|
|
220
|
+
"depth": float(depth),
|
|
221
|
+
"prominence": prom,
|
|
222
|
+
"fwhm": float(fwhm),
|
|
223
|
+
"asymmetry": float(asymmetry),
|
|
224
|
+
"area": total_area,
|
|
225
|
+
"left_shoulder": float(wv[lb]),
|
|
226
|
+
"right_shoulder": float(wv[rb]),
|
|
227
|
+
"width_full": float(wv[rb] - wv[lb]),
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
return pd.DataFrame(records)
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def extract_features(
|
|
235
|
+
spectra_df,
|
|
236
|
+
wavelengths,
|
|
237
|
+
continuum_removed=True,
|
|
238
|
+
min_depth=0.005,
|
|
239
|
+
min_prominence=0.002,
|
|
240
|
+
min_distance_nm=10.0,
|
|
241
|
+
min_width_nm=4.0,
|
|
242
|
+
refine_wavelength=True,
|
|
243
|
+
):
|
|
244
|
+
"""Extract absorption features across all spectra in a DataFrame.
|
|
245
|
+
|
|
246
|
+
Args:
|
|
247
|
+
spectra_df: Spectra DataFrame with samples as columns.
|
|
248
|
+
wavelengths: 1D array-like of wavelengths (nm).
|
|
249
|
+
continuum_removed: Whether ``spectra_df`` is already continuum-removed.
|
|
250
|
+
min_depth: Minimum absorption depth (0-1).
|
|
251
|
+
min_prominence: Minimum peak prominence.
|
|
252
|
+
min_distance_nm: Minimum separation between adjacent minima (nm).
|
|
253
|
+
min_width_nm: Minimum peak width (nm).
|
|
254
|
+
refine_wavelength: Whether to apply 3-point parabolic refinement.
|
|
255
|
+
|
|
256
|
+
Returns:
|
|
257
|
+
pd.DataFrame: Long-format (tidy) DataFrame with a ``'sample'`` column
|
|
258
|
+
and one row per detected absorption feature.
|
|
259
|
+
"""
|
|
260
|
+
frames = []
|
|
261
|
+
for col in spectra_df.columns:
|
|
262
|
+
feat_df = extract_spectrum_features(
|
|
263
|
+
wavelengths=wavelengths,
|
|
264
|
+
reflectance=spectra_df[col].to_numpy(dtype=float),
|
|
265
|
+
continuum_removed=continuum_removed,
|
|
266
|
+
min_depth=min_depth,
|
|
267
|
+
min_prominence=min_prominence,
|
|
268
|
+
min_distance_nm=min_distance_nm,
|
|
269
|
+
min_width_nm=min_width_nm,
|
|
270
|
+
refine_wavelength=refine_wavelength,
|
|
271
|
+
)
|
|
272
|
+
if not feat_df.empty:
|
|
273
|
+
feat_df.insert(0, "sample", col)
|
|
274
|
+
frames.append(feat_df)
|
|
275
|
+
|
|
276
|
+
if not frames:
|
|
277
|
+
return pd.DataFrame(
|
|
278
|
+
columns=[
|
|
279
|
+
"sample",
|
|
280
|
+
"wavelength",
|
|
281
|
+
"depth",
|
|
282
|
+
"prominence",
|
|
283
|
+
"fwhm",
|
|
284
|
+
"asymmetry",
|
|
285
|
+
"area",
|
|
286
|
+
"left_shoulder",
|
|
287
|
+
"right_shoulder",
|
|
288
|
+
"width_full",
|
|
289
|
+
]
|
|
290
|
+
)
|
|
291
|
+
return pd.concat(frames, ignore_index=True)
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def filter_features(
|
|
295
|
+
features_df,
|
|
296
|
+
min_wavelength=None,
|
|
297
|
+
max_wavelength=None,
|
|
298
|
+
wavelength_ranges=None,
|
|
299
|
+
min_depth=None,
|
|
300
|
+
max_depth=None,
|
|
301
|
+
min_prominence=None,
|
|
302
|
+
min_fwhm=None,
|
|
303
|
+
max_fwhm=None,
|
|
304
|
+
min_area=None,
|
|
305
|
+
samples=None,
|
|
306
|
+
):
|
|
307
|
+
"""Filter a tidy absorption features DataFrame by wavelength, size, or sample.
|
|
308
|
+
|
|
309
|
+
Args:
|
|
310
|
+
features_df: Output DataFrame from :func:`extract_features`.
|
|
311
|
+
min_wavelength: Minimum wavelength (nm), inclusive.
|
|
312
|
+
max_wavelength: Maximum wavelength (nm), inclusive.
|
|
313
|
+
wavelength_ranges: Optional list of ``(start_nm, end_nm)`` intervals;
|
|
314
|
+
retains features falling within any interval.
|
|
315
|
+
min_depth: Minimum absorption depth.
|
|
316
|
+
max_depth: Maximum absorption depth.
|
|
317
|
+
min_prominence: Minimum peak prominence.
|
|
318
|
+
min_fwhm: Minimum FWHM (nm).
|
|
319
|
+
max_fwhm: Maximum FWHM (nm).
|
|
320
|
+
min_area: Minimum integrated band area.
|
|
321
|
+
samples: Optional list of sample names to retain.
|
|
322
|
+
|
|
323
|
+
Returns:
|
|
324
|
+
pd.DataFrame: Filtered copy of ``features_df``.
|
|
325
|
+
"""
|
|
326
|
+
if features_df.empty:
|
|
327
|
+
return features_df.copy()
|
|
328
|
+
|
|
329
|
+
mask = np.ones(len(features_df), dtype=bool)
|
|
330
|
+
if samples is not None:
|
|
331
|
+
mask &= features_df["sample"].isin(samples)
|
|
332
|
+
if min_wavelength is not None:
|
|
333
|
+
mask &= features_df["wavelength"] >= min_wavelength
|
|
334
|
+
if max_wavelength is not None:
|
|
335
|
+
mask &= features_df["wavelength"] <= max_wavelength
|
|
336
|
+
if wavelength_ranges is not None:
|
|
337
|
+
range_mask = np.zeros(len(features_df), dtype=bool)
|
|
338
|
+
for lo, hi in wavelength_ranges:
|
|
339
|
+
range_mask |= (features_df["wavelength"] >= lo) & (features_df["wavelength"] <= hi)
|
|
340
|
+
mask &= range_mask
|
|
341
|
+
if min_depth is not None:
|
|
342
|
+
mask &= features_df["depth"] >= min_depth
|
|
343
|
+
if max_depth is not None:
|
|
344
|
+
mask &= features_df["depth"] <= max_depth
|
|
345
|
+
if min_prominence is not None:
|
|
346
|
+
mask &= features_df["prominence"] >= min_prominence
|
|
347
|
+
if min_fwhm is not None:
|
|
348
|
+
mask &= features_df["fwhm"] >= min_fwhm
|
|
349
|
+
if max_fwhm is not None:
|
|
350
|
+
mask &= features_df["fwhm"] <= max_fwhm
|
|
351
|
+
if min_area is not None:
|
|
352
|
+
mask &= features_df["area"] >= min_area
|
|
353
|
+
|
|
354
|
+
return features_df.loc[mask].reset_index(drop=True)
|