Spaces:
Sleeping
Sleeping
Update ir_process.py
Browse files- ir_process.py +46 -34
ir_process.py
CHANGED
|
@@ -9,11 +9,15 @@ def unified_preprocess_spectrum(
|
|
| 9 |
target_start=500.0,
|
| 10 |
target_end=4000.0,
|
| 11 |
num_points=3500,
|
|
|
|
|
|
|
| 12 |
):
|
| 13 |
"""Unified standard physical preprocessing pipeline for IR spectra.
|
| 14 |
|
| 15 |
-
Strictly applies:
|
| 16 |
-
for
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
wn = np.array(wavenumbers, dtype=np.float64)
|
|
@@ -31,39 +35,45 @@ def unified_preprocess_spectrum(
|
|
| 31 |
wn = wn[sort_idx]
|
| 32 |
spec = spec[sort_idx]
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
is_transmittance = False
|
| 40 |
-
if any(t in declared_upper for t in ["TRANSMITTANCE", "%T", "PERCENT"]):
|
| 41 |
-
is_transmittance = True
|
| 42 |
-
elif any(a in declared_upper for a in ["ABSORBANCE", "ABS", "OD"]):
|
| 43 |
is_transmittance = False
|
| 44 |
-
|
| 45 |
-
if (y_max > 10.0 and y_median >= 40.0) or (
|
| 46 |
-
y_median < 40.0 and y_max >= 95.0
|
| 47 |
-
):
|
| 48 |
is_transmittance = True
|
| 49 |
-
elif
|
| 50 |
-
is_transmittance =
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
if
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Remove duplicates to ensure strictly monotonically increasing wavenumbers
|
| 69 |
unique_wn, unique_idx = np.unique(wn, return_index=True)
|
|
@@ -84,10 +94,12 @@ def unified_preprocess_spectrum(
|
|
| 84 |
right_mask = target_grid > orig_max
|
| 85 |
resampled_abs[right_mask] = unique_abs[-1]
|
| 86 |
|
| 87 |
-
# 5.3 Points within the measured range:
|
|
|
|
|
|
|
| 88 |
overlap_mask = (~left_mask) & (~right_mask)
|
| 89 |
if np.any(overlap_mask):
|
| 90 |
-
interp_func = interp1d(unique_wn, unique_abs, kind=
|
| 91 |
resampled_abs[overlap_mask] = interp_func(target_grid[overlap_mask])
|
| 92 |
|
| 93 |
# 6. Min-Max normalization to [0, 1]
|
|
|
|
| 9 |
target_start=500.0,
|
| 10 |
target_end=4000.0,
|
| 11 |
num_points=3500,
|
| 12 |
+
bypass_domain_conversion=False,
|
| 13 |
+
interp_kind="linear",
|
| 14 |
):
|
| 15 |
"""Unified standard physical preprocessing pipeline for IR spectra.
|
| 16 |
|
| 17 |
+
Strictly applies: Data cleaning/sorting, unification into the absorbance domain
|
| 18 |
+
(bypassed for pre-converted SDBS data and natively generated simulated absorbance spectra),
|
| 19 |
+
truncation of out-of-range ends, flat-padding with edge values for missing ends,
|
| 20 |
+
interpolation within the measured region, and Min-Max normalization to [0, 1].
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
wn = np.array(wavenumbers, dtype=np.float64)
|
|
|
|
| 35 |
wn = wn[sort_idx]
|
| 36 |
spec = spec[sort_idx]
|
| 37 |
|
| 38 |
+
# Note: Natively generated simulated absorbance spectra and pre-converted SDBS
|
| 39 |
+
# absorbance spectra bypass the transmittance-to-absorbance conversion steps (Steps 3 & 4)
|
| 40 |
+
# and proceed directly to grid resampling and normalization (Steps 5 & 6).
|
| 41 |
+
if bypass_domain_conversion:
|
| 42 |
+
abs_spec = spec
|
| 43 |
+
else:
|
| 44 |
+
# 3. Determine spectrum type (Transmittance vs. Absorbance)
|
| 45 |
+
y_max = np.max(spec)
|
| 46 |
+
y_median = np.median(spec)
|
| 47 |
+
declared_upper = declared_yunits.upper()
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
is_transmittance = False
|
| 50 |
+
if any(t in declared_upper for t in ["TRANSMITTANCE", "%T", "PERCENT"]):
|
|
|
|
|
|
|
|
|
|
| 51 |
is_transmittance = True
|
| 52 |
+
elif any(a in declared_upper for a in ["ABSORBANCE", "ABS", "OD"]):
|
| 53 |
+
is_transmittance = False
|
| 54 |
+
else:
|
| 55 |
+
if (y_max > 10.0 and y_median >= 40.0) or (
|
| 56 |
+
y_median < 40.0 and y_max >= 95.0
|
| 57 |
+
):
|
| 58 |
+
is_transmittance = True
|
| 59 |
+
elif y_max <= 1.5 and y_median >= 0.6:
|
| 60 |
+
is_transmittance = True
|
| 61 |
+
|
| 62 |
+
# 4. Convert to absorbance (including removal of zero-transmittance dead zones)
|
| 63 |
+
if is_transmittance:
|
| 64 |
+
pos_mask = spec > 1e-5
|
| 65 |
+
wn = wn[pos_mask]
|
| 66 |
+
spec = spec[pos_mask]
|
| 67 |
+
if len(wn) < 10:
|
| 68 |
+
return None
|
| 69 |
+
|
| 70 |
+
if np.max(spec) > 2.0:
|
| 71 |
+
spec = spec / 100.0
|
| 72 |
+
|
| 73 |
+
spec_clipped = np.clip(spec, 1e-4, 1.0)
|
| 74 |
+
abs_spec = -np.log10(spec_clipped)
|
| 75 |
+
else:
|
| 76 |
+
abs_spec = spec - np.min(spec) if np.min(spec) < 0 else spec
|
| 77 |
|
| 78 |
# Remove duplicates to ensure strictly monotonically increasing wavenumbers
|
| 79 |
unique_wn, unique_idx = np.unique(wn, return_index=True)
|
|
|
|
| 94 |
right_mask = target_grid > orig_max
|
| 95 |
resampled_abs[right_mask] = unique_abs[-1]
|
| 96 |
|
| 97 |
+
# 5.3 Points within the measured range: interpolate onto the uniform target grid
|
| 98 |
+
# Default 'linear' for experimental spectra to prevent overshooting on noisy baselines;
|
| 99 |
+
# higher-order (e.g., 'cubic') is also supported for smooth theoretical/simulated profiles.
|
| 100 |
overlap_mask = (~left_mask) & (~right_mask)
|
| 101 |
if np.any(overlap_mask):
|
| 102 |
+
interp_func = interp1d(unique_wn, unique_abs, kind=interp_kind)
|
| 103 |
resampled_abs[overlap_mask] = interp_func(target_grid[overlap_mask])
|
| 104 |
|
| 105 |
# 6. Min-Max normalization to [0, 1]
|