include peak detection in scheludko
This commit is contained in:
parent
56aac01151
commit
ccd1f2c1f0
7 changed files with 100 additions and 82 deletions
44
check.py
44
check.py
|
@ -14,6 +14,7 @@ import matplotlib.pyplot as plt
|
|||
|
||||
from optifik.analysis import *
|
||||
from optifik.auto import auto
|
||||
from optifik import io
|
||||
|
||||
plt.rc('text', usetex=True)
|
||||
plt.rcParams.update({
|
||||
|
@ -25,6 +26,36 @@ plt.rcParams.update({
|
|||
|
||||
|
||||
|
||||
def play():
|
||||
##### Chemin du dossier contenant le spectre #####
|
||||
|
||||
DATA_FOLDER = os.path.abspath(os.path.join(os.path.curdir, 'tests', 'basic'))
|
||||
|
||||
#SAVE_FOLDER = DATA_FOLDER
|
||||
|
||||
# FILE_NAME = '003582.xy' #FFT Exemple -> FFT 3524.51
|
||||
# FILE_NAME = '000004310.xy' #OOspectro Exemple -> minmax 1338.35
|
||||
# FILE_NAME = '000005253.xy'#Scheludko 4 pics Exemple -> scheludko ²
|
||||
# FILE_NAME = '000006544.xy'#Scheludko 2 pics Exemple -> ombre ## Diviser prominence FFT par 2
|
||||
# FILE_NAME = '000018918.xy' #Scheludko 1 pic max Exemple -> ombre ## Diviser prominence FFT par 2
|
||||
|
||||
FILE_NAME = '000004310.xy' #TEST#
|
||||
spectrum_file = os.path.join(DATA_FOLDER, FILE_NAME)
|
||||
lambdas, intensities = io.load_spectrum(spectrum_file)
|
||||
plot_spectrum(lambdas, intensities, title='Raw')
|
||||
|
||||
lambdas, intensities = io.load_spectrum(spectrum_file, lambda_min=450)
|
||||
plot_spectrum(lambdas, intensities, title='Raw, cropped')
|
||||
|
||||
smoothed_intensities = smooth_intensities(intensities)
|
||||
plot_spectrum(lambdas, smoothed_intensities, title='Smoothed')
|
||||
|
||||
prominence = 0.02
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=True)
|
||||
|
||||
|
||||
def check_basic():
|
||||
|
||||
##### Chemin du dossier contenant le spectre #####
|
||||
|
@ -41,7 +72,8 @@ def check_basic():
|
|||
|
||||
FILE_NAME = '000004310.xy' #TEST#
|
||||
|
||||
auto(DATA_FOLDER, FILE_NAME, plot=False)
|
||||
spectrum_file = os.path.join(DATA_FOLDER, FILE_NAME)
|
||||
auto(spectrum_file, plot=False)
|
||||
|
||||
|
||||
def check_SV1():
|
||||
|
@ -80,8 +112,9 @@ def check_SV1():
|
|||
|
||||
##### Find Peak #####
|
||||
|
||||
total_extrema, smoothed_intensities, raw_intensities, lambdas, peaks_min, peaks_max = finds_peak(spectre_file,
|
||||
min_peak_prominence=prominence)
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
thickness_minmax = thickness_from_minmax(lambdas,
|
||||
smoothed_intensities,
|
||||
|
@ -99,5 +132,6 @@ def check_SV1():
|
|||
|
||||
if __name__ == '__main__':
|
||||
|
||||
check_basic()
|
||||
check_SV1()
|
||||
#check_basic()
|
||||
#check_SV1()
|
||||
play()
|
|
@ -15,13 +15,23 @@ plt.rcParams.update({
|
|||
|
||||
from .io import load_spectrum
|
||||
from .fft import *
|
||||
from .scheludko import *
|
||||
from .minmax import *
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def plot_spectrum(lambdas, intensities, title=''):
|
||||
|
||||
plt.figure(figsize=(10, 6),dpi =600)
|
||||
plt.plot(lambdas, intensities, 'o-', markersize=2)
|
||||
plt.xlabel(r'$\lambda$ (nm)')
|
||||
plt.ylabel(r'$I^*$')
|
||||
plt.title(title)
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
def plot_xy(file_path, plot=True):
|
||||
try:
|
||||
|
@ -62,40 +72,26 @@ def finds_peak(lambdas, intensities, min_peak_prominence, min_peak_distance=10,
|
|||
Distance minimale entre les pics.
|
||||
"""
|
||||
|
||||
smoothed_intensities = intensities
|
||||
|
||||
|
||||
|
||||
# Trouver les maxima et minima sur le signal lissé
|
||||
peaks_max, _ = find_peaks(smoothed_intensities, prominence=min_peak_prominence, distance=min_peak_distance)
|
||||
peaks_min, _ = find_peaks(-smoothed_intensities, prominence=min_peak_prominence, distance=min_peak_distance)
|
||||
|
||||
peaks_max, _ = find_peaks(intensities, prominence=min_peak_prominence, distance=min_peak_distance)
|
||||
peaks_min, _ = find_peaks(-intensities, prominence=min_peak_prominence, distance=min_peak_distance)
|
||||
|
||||
if plot:
|
||||
plt.figure(figsize=(10, 6),dpi =600)
|
||||
plt.plot(lambdas, smoothed_intensities, 'o-', markersize=2, label="Smoothed data")
|
||||
plt.plot(lambdas[peaks_max], smoothed_intensities[peaks_max], 'ro')
|
||||
plt.plot(lambdas[peaks_min], smoothed_intensities[peaks_min], 'ro')
|
||||
plt.plot(lambdas, intensities, 'o-', markersize=2, label="Smoothed data")
|
||||
plt.plot(lambdas[peaks_max], intensities[peaks_max], 'ro')
|
||||
plt.plot(lambdas[peaks_min], intensities[peaks_min], 'ro')
|
||||
plt.xlabel(r'$\lambda$ (nm)')
|
||||
plt.ylabel(r'$I^*$')
|
||||
plt.legend()
|
||||
plt.tight_layout()
|
||||
plt.show()
|
||||
|
||||
# Nombre total d’extremums
|
||||
total_extrema = len(peaks_max) + len(peaks_min)
|
||||
if total_extrema >= 15:
|
||||
print('Number of extrema', total_extrema,'.')
|
||||
print('FFT method')
|
||||
|
||||
if total_extrema <= 15 and total_extrema > 4:
|
||||
print('Number of extrema', total_extrema,'.')
|
||||
print('OOSpectro method')
|
||||
|
||||
if total_extrema <= 4:
|
||||
print('Number of extrema', total_extrema,'.')
|
||||
print('Scheludko method')
|
||||
|
||||
return total_extrema, peaks_min, peaks_max
|
||||
|
||||
return peaks_min, peaks_max
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -3,10 +3,10 @@ import os.path
|
|||
from .analysis import *
|
||||
from .io import load_spectrum
|
||||
|
||||
def auto(DATA_FOLDER, FILE_NAME, plot=None):
|
||||
def auto(spectrum_file, plot=None):
|
||||
|
||||
|
||||
spectre_file = os.path.join(DATA_FOLDER, FILE_NAME)
|
||||
spectre_file = spectrum_file
|
||||
|
||||
##### Affichage du spectre brut et récupération des Intesités brutes#####
|
||||
|
||||
|
@ -32,17 +32,20 @@ def auto(DATA_FOLDER, FILE_NAME, plot=None):
|
|||
prominence = 0.03
|
||||
##### Find Peak #####
|
||||
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=plot)
|
||||
plot=False)
|
||||
|
||||
##### Epaisseur selon la methode #####
|
||||
|
||||
#thickness_FFT = thickness_from_fft(lambdas,smoothed_intensities,refractive_index=1.33)
|
||||
|
||||
|
||||
total_extrema = len(peaks_max) + len(peaks_min)
|
||||
|
||||
if total_extrema > 15 and total_extrema > 4:
|
||||
print('Apply method FFT')
|
||||
thickness_FFT = thickness_from_fft(lambdas,smoothed_intensities,
|
||||
thickness_FFT = thickness_from_fft(lambdas, smoothed_intensities,
|
||||
refractive_index=indice,
|
||||
plot=plot)
|
||||
thickness = thickness_FFT.thickness
|
||||
|
@ -51,7 +54,7 @@ def auto(DATA_FOLDER, FILE_NAME, plot=None):
|
|||
|
||||
if total_extrema <= 15 and total_extrema > 4:
|
||||
print('Apply method minmax')
|
||||
thickness_minmax = thickness_from_minmax(lambdas,smoothed_intensities,
|
||||
thickness_minmax = thickness_from_minmax(lambdas, smoothed_intensities,
|
||||
refractive_index=indice,
|
||||
min_peak_prominence=prominence,
|
||||
plot=plot)
|
||||
|
@ -61,8 +64,8 @@ def auto(DATA_FOLDER, FILE_NAME, plot=None):
|
|||
if total_extrema <= 4 and total_extrema >= 2: #& 2peak minimum:
|
||||
print('Apply method Scheludko')
|
||||
thickness = thickness_from_scheludko(lambdas, smoothed_intensities,
|
||||
peaks_min, peaks_max,
|
||||
refractive_index=indice,
|
||||
min_peak_prominence=prominence,
|
||||
plot=plot)
|
||||
print(f'thickness: {thickness:.2f} nm')
|
||||
|
||||
|
@ -70,8 +73,8 @@ def auto(DATA_FOLDER, FILE_NAME, plot=None):
|
|||
print('Apply method ordre0')
|
||||
|
||||
thickness = thickness_for_order0(lambdas, smoothed_intensities,
|
||||
peaks_min, peaks_max,
|
||||
refractive_index=indice,
|
||||
min_peak_prominence=prominence,
|
||||
plot=plot)
|
||||
|
||||
print(f'thickness: {thickness:.2f} nm')
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import numpy as np
|
||||
|
||||
from .io import load_spectrum
|
||||
|
||||
from scipy.optimize import curve_fit
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
@ -12,8 +12,9 @@ plt.rcParams.update({
|
|||
'legend.fontsize': 23,
|
||||
})
|
||||
|
||||
|
||||
from .io import load_spectrum
|
||||
from .utils import OptimizeResult
|
||||
from .analysis import finds_peak
|
||||
|
||||
def thickness_scheludko_at_order(wavelengths,
|
||||
intensity,
|
||||
|
@ -114,9 +115,8 @@ def Delta_fit(xdata, thickness, interference_order):
|
|||
|
||||
def thickness_from_scheludko(lambdas,
|
||||
smoothed_intensities,
|
||||
peaks_min,
|
||||
peaks_max,
|
||||
refractive_index,
|
||||
min_peak_prominence,
|
||||
plot=None):
|
||||
"""
|
||||
|
||||
|
@ -129,10 +129,6 @@ def thickness_from_scheludko(lambdas,
|
|||
DESCRIPTION.
|
||||
smoothed_intensities : TYPE
|
||||
DESCRIPTION.
|
||||
peaks_min : TYPE
|
||||
DESCRIPTION.
|
||||
peaks_max : TYPE
|
||||
DESCRIPTION.
|
||||
refractive_index : TYPE
|
||||
DESCRIPTION.
|
||||
plot : TYPE, optional
|
||||
|
@ -144,9 +140,13 @@ def thickness_from_scheludko(lambdas,
|
|||
DESCRIPTION.
|
||||
|
||||
"""
|
||||
|
||||
max_tested_order = 12
|
||||
r_index = refractive_index
|
||||
|
||||
peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=min_peak_prominence,
|
||||
plot=False)
|
||||
|
||||
|
||||
lambda_min = lambdas[peaks_min[-1]]
|
||||
lambda_max = lambdas[peaks_max[-1]]
|
||||
|
@ -169,11 +169,12 @@ def thickness_from_scheludko(lambdas,
|
|||
plt.ylabel(r'$h$ ($\mathrm{{nm}}$)')
|
||||
plt.xlabel(r'$\lambda$ ($ \mathrm{nm} $)')
|
||||
|
||||
for m in range(0, 9):
|
||||
|
||||
for m in range(0, max_tested_order+1):
|
||||
h_values = thickness_scheludko_at_order(lambdas_masked, intensities_masked, m, r_index_masked)
|
||||
|
||||
if plot:
|
||||
plt.plot(lambdas_masked, h_values,'.', markersize =3, label=f"Épaisseur du film (Scheludko, m={m})")
|
||||
plt.plot(lambdas_masked, h_values,'.', markersize=3, label=f"Épaisseur du film (Scheludko, m={m})")
|
||||
ecart = np.max(h_values)-np.min(h_values)
|
||||
|
||||
print(f"Écart pour m={m} : {ecart:.3f} nm")
|
||||
|
@ -213,15 +214,20 @@ def thickness_from_scheludko(lambdas,
|
|||
|
||||
def thickness_for_order0(lambdas,
|
||||
smoothed_intensities,
|
||||
peaks_min,
|
||||
peaks_max,
|
||||
refractive_index,
|
||||
min_peak_prominence,
|
||||
plot=None):
|
||||
|
||||
|
||||
File_I_min = 'tests/spectre_trou/000043641.xy'
|
||||
r_index = refractive_index
|
||||
|
||||
peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=min_peak_prominence,
|
||||
plot=False)
|
||||
|
||||
|
||||
|
||||
|
||||
lambdas_I_min, intensities_I_min = load_spectrum(File_I_min, lambda_min=450)
|
||||
|
||||
|
|
|
@ -29,10 +29,6 @@ def test_minmax(spectrum_path, expected):
|
|||
indice = 1.324188 + 3102.060378 / (lambdas**2)
|
||||
prominence = 0.02
|
||||
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
thickness_minmax = thickness_from_minmax(lambdas,
|
||||
smoothed_intensities,
|
||||
refractive_index=indice,
|
||||
|
|
|
@ -6,7 +6,6 @@ import pytest
|
|||
from optifik.scheludko import thickness_from_scheludko
|
||||
from optifik.io import load_spectrum
|
||||
from optifik.analysis import smooth_intensities
|
||||
from optifik.analysis import finds_peak
|
||||
|
||||
import yaml
|
||||
|
||||
|
@ -22,22 +21,18 @@ def load():
|
|||
|
||||
|
||||
@pytest.mark.parametrize("spectrum_path, expected", load())
|
||||
def test_minmax(spectrum_path, expected):
|
||||
def test_SV2o5(spectrum_path, expected):
|
||||
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
|
||||
smoothed_intensities = smooth_intensities(raw_intensities)
|
||||
|
||||
refractive_index = 1.324188 + 3102.060378 / (lambdas**2)
|
||||
prominence = 0.02
|
||||
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
thickness_scheludko = thickness_from_scheludko(lambdas,
|
||||
smoothed_intensities,
|
||||
peaks_min,
|
||||
peaks_max,
|
||||
refractive_index)
|
||||
thickness_scheludko = thickness_from_scheludko(lambdas, smoothed_intensities,
|
||||
refractive_index=refractive_index,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
result = thickness_scheludko.thickness
|
||||
|
||||
assert_allclose(result, expected, rtol=1e-1)
|
||||
|
|
|
@ -3,13 +3,12 @@ import numpy as np
|
|||
from numpy.testing import assert_allclose
|
||||
import pytest
|
||||
|
||||
from optifik.analysis import thickness_from_fft
|
||||
from optifik.analysis import thickness_from_minmax
|
||||
from optifik.analysis import thickness_from_scheludko
|
||||
from optifik.analysis import thickness_for_order0
|
||||
from optifik.fft import thickness_from_fft
|
||||
from optifik.minmax import thickness_from_minmax
|
||||
from optifik.scheludko import thickness_from_scheludko
|
||||
from optifik.scheludko import thickness_for_order0
|
||||
from optifik.analysis import smooth_intensities
|
||||
from optifik.analysis import Prominence_from_fft
|
||||
from optifik.analysis import finds_peak
|
||||
from optifik.io import load_spectrum
|
||||
|
||||
|
||||
|
@ -69,14 +68,10 @@ def test_scheludko_4peaks():
|
|||
|
||||
prominence = Prominence_from_fft(lambdas=lambdas, intensities=smoothed_intensities, refractive_index=indice, plot=False)
|
||||
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
|
||||
result = thickness_from_scheludko(lambdas, smoothed_intensities,
|
||||
peaks_min, peaks_max,
|
||||
refractive_index=indice,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
assert_allclose(result.thickness, expected, rtol=1e-1)
|
||||
|
@ -95,13 +90,9 @@ def test_scheludko_2peaks():
|
|||
|
||||
prominence = 0.03
|
||||
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
result = thickness_from_scheludko(lambdas, smoothed_intensities,
|
||||
peaks_min, peaks_max,
|
||||
refractive_index=indice,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
assert_allclose(result.thickness, expected, rtol=1e-1)
|
||||
|
@ -122,13 +113,10 @@ def test_order0():
|
|||
|
||||
prominence = 0.03
|
||||
|
||||
total_extrema, peaks_min, peaks_max = finds_peak(lambdas, smoothed_intensities,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
result = thickness_for_order0(lambdas, smoothed_intensities,
|
||||
peaks_min, peaks_max,
|
||||
refractive_index=indice,
|
||||
min_peak_prominence=prominence,
|
||||
plot=False)
|
||||
|
||||
assert_allclose(result.thickness, expected, rtol=1e-1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue