-
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.
Merge pull request #190 from h2020charisma/fit_peaks
Add skewed profiles; Extend Ne reference data
- Loading branch information
Showing
10 changed files
with
491 additions
and
144 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import numpy as np | ||
|
||
|
||
def linreg(x, y): | ||
idx = np.isfinite(x) & np.isfinite(y) | ||
x = x[idx] | ||
y = y[idx] | ||
s_xy = np.sum(x*y) | ||
s_x = np.sum(x) | ||
s_xx = np.sum(x**2) | ||
s_y = np.sum(y) | ||
s = len(x) | ||
d = s*s_xx-s_x**2 | ||
a = (s*s_xy - s_x*s_y)/d | ||
b = (s_xx*s_y - s_x*s_xy)/d | ||
return a, b | ||
|
||
|
||
def line_by_points(x, y): | ||
a = (y[1]-y[0])/(x[1]-x[0]) | ||
b = y[0] - a*x[0] | ||
return a, b | ||
|
||
|
||
def poly2_by_points(x, y): | ||
""" | ||
Calculate poly-2 coefficients by 3 points. | ||
>>> x = [1, 2, 4] | ||
>>> y = [1, 0, 4] | ||
>>> a, b, c = poly2_by_points(x, y) | ||
>>> xi = np.linspace(0, 4, 30) | ||
>>> xt = np.array([1, 2, 3, 4]) | ||
>>> yt = np.array([1, 0, 1, 4]) | ||
>>> assert np.allclose(a*xt**2 + b*xt + c, yt) | ||
""" | ||
a = np.array([np.square(x), x, np.ones_like(x)]).T | ||
a_1 = np.linalg.inv(a) | ||
return a_1@y |
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,52 @@ | ||
import logging | ||
|
||
import numpy as np | ||
|
||
from .argmin2d import argmin2d | ||
from .poly_params import poly2_by_points | ||
from .significant_peaks import select_peaks | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def _rough_calib_all_to_all(spe_pos, ref_pos, spe_sel_idx, ref_sel_idx): | ||
last = 0 | ||
for r1_i, r1 in enumerate(ref_pos[ref_sel_idx]): | ||
for r3_i, r3 in reversed(list(enumerate(ref_pos[ref_sel_idx]))[r1_i+1:]): | ||
for r2_i, r2 in list(enumerate(ref_pos[ref_sel_idx]))[r1_i+1:r3_i-1]: | ||
for s1_i, s1 in enumerate(spe_pos[spe_sel_idx]): | ||
for s3_i, s3 in reversed(list(enumerate(spe_pos[spe_sel_idx]))[s1_i+1:]): | ||
for s2_i, s2 in list(enumerate(spe_pos[spe_sel_idx]))[s1_i+1:s3_i-1]: | ||
a, b, c = poly2_by_points(np.array([s1, s2, s3]), np.array([r1, r2, r3])) | ||
test_spe_pos = spe_pos**2*a + spe_pos*b + c | ||
if np.any(np.diff(test_spe_pos) <= 0): | ||
# should be strictly increasing | ||
continue | ||
w = np.subtract.outer(ref_pos, test_spe_pos) | ||
ridx, sidx = argmin2d(np.abs(w)).T | ||
mer = - len(ridx) | ||
mer = - np.log(len(ridx)) | ||
if last > mer: | ||
deviation = np.inf | ||
last = mer | ||
if last == mer: | ||
d = np.average(np.abs(w[ridx, sidx])) | ||
if d < deviation: | ||
deviation = d | ||
logger.info(f'matches={-mer}, deviation={deviation:8.3f}, a={a}, b={b}, c={c}') | ||
print(f'matches={len(ridx)}, deviation={deviation:8.3f}, a={a}, b={b}, c={c}') | ||
lasta = a | ||
lastb = b | ||
lastc = c | ||
return lasta, lastb, lastc | ||
|
||
|
||
def rough_poly2_calibration(spe_dict, ref_dict, npeaks=10, **kwargs): | ||
spe_pos = np.array(list(spe_dict.keys())) | ||
spe_amp = np.array(list(spe_dict.values())) | ||
ref_pos = np.array(list(ref_dict.keys())) | ||
ref_amp = np.array(list(ref_dict.values())) | ||
npeaks = np.min([npeaks, len(spe_pos), len(ref_pos)]) | ||
spe_sel_idx = select_peaks(spe_pos, spe_amp, npeaks=npeaks, **kwargs) | ||
ref_sel_idx = select_peaks(ref_pos, ref_amp, npeaks=npeaks, **kwargs) | ||
return _rough_calib_all_to_all(spe_pos, ref_pos, spe_sel_idx, ref_sel_idx) |
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,16 @@ | ||
import numpy as np | ||
|
||
|
||
def calc_weight(pos, amp, locality_factor=10): | ||
pos = pos - pos.min() | ||
pos /= pos.max() | ||
m = np.subtract.outer(pos, pos) | ||
m = np.abs(m) | ||
w = amp/((1/np.exp(locality_factor*m)) @ amp) | ||
return w | ||
|
||
|
||
def select_peaks(pos, amp, npeaks, **kwargs): | ||
w = calc_weight(pos, amp, **kwargs) | ||
idx = np.sort(np.argsort(w)[-npeaks:]) | ||
return idx |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import numpy as np | ||
|
||
import ramanchada2 as rc2 | ||
|
||
|
||
def test_empty_spectrum(): | ||
spe = rc2.spectrum.Spectrum(x=np.arange(100), y=np.zeros(100)) | ||
spe.find_peak_multipeak() |
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