-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new method for noise + rng_seed can be a rng_state
rng_seed is updated inplace. This requires processing data to be deeply copied before execution.
- Loading branch information
1 parent
3761814
commit 51b6a78
Showing
5 changed files
with
110 additions
and
12 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
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
74 changes: 74 additions & 0 deletions
74
src/ramanchada2/spectrum/filters/add_gaussian_noise_drift.py
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import numpy as np | ||
from pydantic import PositiveFloat, confloat, validate_arguments | ||
|
||
from ramanchada2.misc.spectrum_deco import add_spectrum_filter | ||
|
||
from ..spectrum import Spectrum | ||
|
||
|
||
@validate_arguments(config=dict(arbitrary_types_allowed=True)) | ||
def generate_add_gaussian_noise_drift(y, /, | ||
sigma: PositiveFloat, | ||
coef: confloat(ge=0, le=1), # type: ignore [valid-type] | ||
# validation for rng_seed is removed because | ||
# it makes in-place modification impossible | ||
rng_seed=None): | ||
if isinstance(rng_seed, dict): | ||
rng = np.random.default_rng() | ||
rng.__setstate__(rng_seed) | ||
else: | ||
rng = np.random.default_rng(rng_seed) | ||
gaus = rng.normal(0., sigma+coef/np.sqrt(2), size=len(y)) | ||
cs = np.cumsum(gaus) | ||
# coef*sum(cs[:i]) + (1-coef)*gaus is identical to | ||
# coef*sum(cs[:i-1]) + gaus | ||
noise = coef*cs + gaus*(1-coef) | ||
noise -= np.std(noise) | ||
dat = y + noise | ||
if any(dat < 0): | ||
dat += abs(dat.min()) | ||
if isinstance(rng_seed, dict): | ||
rng_seed.update(rng.__getstate__()) | ||
return np.array(dat) | ||
|
||
|
||
@add_spectrum_filter | ||
@validate_arguments(config=dict(arbitrary_types_allowed=True)) | ||
def add_gaussian_noise_drift( | ||
old_spe: Spectrum, | ||
new_spe: Spectrum, /, | ||
sigma: PositiveFloat, | ||
coef: confloat(ge=0, le=1), # type: ignore [valid-type] | ||
# validation for rng_seed is removed because | ||
# it makes in-place modification impossible | ||
rng_seed=None): | ||
r""" | ||
Add cumulative gaussian noise to the spectrum. | ||
Exponential-moving-average-like gaussian noise is added | ||
to each sample. The goal is to mimic the low-frequency noise | ||
(or random substructures in spectra). | ||
The additive noise is | ||
.. math:: | ||
a_i = coef*\sum_{j=0}^{i-1}g_j + g_i, | ||
where | ||
.. math:: | ||
g_i = \mathcal{N}(0, 1+\frac{coef}{\sqrt 2}). | ||
This way drifting is possible while keeping the | ||
.. math:: | ||
\sigma(\Delta(a)) \approx 1. | ||
Parameters | ||
---------- | ||
sigma : float | ||
sigma of the gaussian noise | ||
coef : float in [0, 1] | ||
drifting coefficient. if `coef == 0` the result is | ||
identical to `add_gaussian_noise()`. | ||
rng_seed : int or rng state, optional | ||
seed for the random generator. | ||
If a state is provided it is updated in-place | ||
""" | ||
new_spe.y = generate_add_gaussian_noise_drift(old_spe.y, | ||
sigma=sigma, | ||
coef=coef, | ||
rng_seed=rng_seed) |
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