-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,108 @@ | ||
import logging | ||
from typing import Optional | ||
|
||
import numpy as np | ||
from scipy.stats import linregress | ||
|
||
from .splitter import get_default_spectrum_windows | ||
from .splitter import get_default_spectrum_windows, split_spectrum_data_in_windows | ||
from .spectrum_template import SpectrumData | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def subtract_baseline_per_window(spec, label, windows_data, window_limits): | ||
ramanshift = spec.ramanshift | ||
intensity = spec.intensity | ||
window_name = spec.window_name | ||
if not ramanshift.any(): | ||
return intensity, (0, 0) | ||
# breakpoint() | ||
lbl_1st_order = list(filter(lambda x: "1st_order" in x, windows_data.keys())) | ||
if any(i in label for i in ("full", "norm")) and lbl_1st_order: | ||
i_fltrd_dspkd_fit = windows_data.get(lbl_1st_order[0]).intensity | ||
else: | ||
i_fltrd_dspkd_fit = intensity | ||
window_config = window_limits.get(window_name) | ||
|
||
bl_linear = linregress( | ||
ramanshift[[0, -1]], | ||
[ | ||
np.mean(i_fltrd_dspkd_fit[0 : window_config.min]), | ||
np.mean(i_fltrd_dspkd_fit[window_config.max : :]), | ||
], | ||
) | ||
i_blcor = intensity - (bl_linear[0] * ramanshift + bl_linear[1]) | ||
return i_blcor, bl_linear | ||
|
||
|
||
def get_normalization_factor(data, norm_method="simple") -> float: | ||
try: | ||
if norm_method == "simple": | ||
normalization_intensity = np.nanmax(data["normalization"].intensity) | ||
elif norm_method == "fit": | ||
raise NotImplementedError("NormalizeFit not yet implemented") | ||
# IDEA not implemented | ||
# normalization = NormalizeFit( | ||
# self.blcorr_data["1st_order"], plotprint=False | ||
# ) # IDEA still implement this NormalizeFit | ||
# normalization_intensity = normalization["IG"] | ||
else: | ||
logger.warning(f"unknown normalization method {norm_method}") | ||
normalization_intensity = 1 | ||
except Exception as exc: | ||
logger.error(f"normalization error {exc}") | ||
normalization_intensity = 1 | ||
|
||
return normalization_intensity | ||
|
||
|
||
def normalize_data(windows_data, norm_factor, label: Optional[str] = None) -> dict: | ||
ret = {} | ||
for window_name, spec in windows_data.items(): | ||
norm_label = f"norm_blcorr_{window_name}" | ||
if label: | ||
norm_label = f"{label}_{norm_label}" | ||
_data = SpectrumData( | ||
spec.ramanshift, spec.intensity * norm_factor, norm_label, window_name | ||
) | ||
ret.update(**{window_name: _data}) | ||
return ret | ||
|
||
|
||
def subtract_loop(windows_data: dict, window_limits: dict, label=None): | ||
_blcorr = {} | ||
_info = {} | ||
for window_name, spec in windows_data.items(): | ||
blcorr_int, blcorr_lin = subtract_baseline_per_window( | ||
spec, window_name, windows_data, window_limits | ||
) | ||
label = f"blcorr_{window_name}" | ||
if label: | ||
label = f"{label}_{label}" | ||
_data = SpectrumData(spec.ramanshift, blcorr_int, label, window_name) | ||
_blcorr.update(**{window_name: _data}) | ||
_info.update(**{window_name: blcorr_lin}) | ||
return _blcorr, _info | ||
|
||
|
||
class BaselineSubtractorNormalizer: | ||
""" | ||
For baseline subtraction as well as normalization of a spectrum | ||
""" | ||
|
||
def __init__(self, *args, **kws): | ||
self.split_spectrum_data_in_windows() | ||
self.windowlimits = get_default_spectrum_windows() | ||
blcorr_data, blcorr_info = self.subtract_loop() | ||
def __init__(self, ramanshift: np.array, intensity: np.array, label: str = None): | ||
self._ramanshift = ramanshift | ||
self._intensity = intensity | ||
self._label = label | ||
self.windows_data = split_spectrum_data_in_windows( | ||
ramanshift=ramanshift, intensity=intensity, label=label | ||
) | ||
self.window_limits = get_default_spectrum_windows() | ||
blcorr_data, blcorr_info = subtract_loop( | ||
self.windows_data, self.window_limits, label=self._label | ||
) | ||
self.blcorr_data = blcorr_data | ||
self.blcorr_info = blcorr_info | ||
normalization_intensity = self.get_normalization_factor() | ||
normalization_intensity = get_normalization_factor(self.blcorr_data) | ||
self.norm_factor = 1 / normalization_intensity | ||
self.norm_data = self.normalize_data(self.blcorr_data, self.norm_factor) | ||
|
||
def subtract_loop(self): | ||
_blcorr = {} | ||
_info = {} | ||
for windowname, spec in self.windows_data.items(): | ||
blcorr_int, blcorr_lin = self.subtract_baseline_per_window(windowname, spec) | ||
label = f"blcorr_{windowname}" | ||
if self.label: | ||
label = f"{self.label}_{label}" | ||
_data = self.data(spec.ramanshift, blcorr_int, label) | ||
_blcorr.update(**{windowname: _data}) | ||
_info.update(**{windowname: blcorr_lin}) | ||
return _blcorr, _info | ||
|
||
def subtract_baseline_per_window(self, windowname, spec): | ||
rs = spec.ramanshift | ||
if not rs.any(): | ||
return spec.intensity, (0, 0) | ||
|
||
if windowname[0:4] in ("full", "norm"): | ||
i_fltrd_dspkd_fit = self.windows_data.get("1st_order").intensity | ||
else: | ||
i_fltrd_dspkd_fit = spec.intensity | ||
_limits = self.windowlimits.get(windowname) | ||
|
||
bl_linear = linregress( | ||
rs[[0, -1]], | ||
[ | ||
np.mean(i_fltrd_dspkd_fit[0 : _limits[0]]), | ||
np.mean(i_fltrd_dspkd_fit[_limits[1] : :]), | ||
], | ||
) | ||
i_blcor = spec.intensity - (bl_linear[0] * rs + bl_linear[1]) | ||
return i_blcor, bl_linear | ||
|
||
def get_normalization_factor(self, norm_method="simple") -> float: | ||
try: | ||
if norm_method == "simple": | ||
normalization_intensity = np.nanmax( | ||
self.blcorr_data["normalization"].intensity | ||
) | ||
elif norm_method == "fit": | ||
raise NotImplementedError("NormalizeFit not yet implemented") | ||
# IDEA not implemented | ||
# normalization = NormalizeFit( | ||
# self.blcorr_data["1st_order"], plotprint=False | ||
# ) # IDEA still implement this NormalizeFit | ||
# normalization_intensity = normalization["IG"] | ||
else: | ||
logger.warning(f"unknown normalization method {norm_method}") | ||
normalization_intensity = 1 | ||
except Exception as exc: | ||
logger.error(f"normalization error {exc}") | ||
normalization_intensity = 1 | ||
|
||
return normalization_intensity | ||
|
||
def normalize_data(self, data, norm_factor) -> dict: | ||
ret = {} | ||
for windowname, spec in data.items(): | ||
label = f"norm_blcorr_{windowname}" | ||
if self.label: | ||
label = f"{self.label}_{label}" | ||
|
||
_data = self.data(spec.ramanshift, spec.intensity * self.norm_factor, label) | ||
ret.update(**{windowname: _data}) | ||
return ret | ||
self.norm_data = normalize_data(self.blcorr_data, self.norm_factor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters