refactoring

This commit is contained in:
François Boulogne 2025-06-20 09:57:05 +02:00
parent c8868c3e63
commit 3debe82f0d
8 changed files with 202 additions and 106 deletions

View file

@ -18,9 +18,10 @@ plt.rcParams.update({
}) })
def play_oder1(): def minmax():
##### Chemin du dossier contenant le spectre ##### ##### Chemin du dossier contenant le spectre #####
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
DATA_FOLDER = os.path.abspath(os.path.join(os.path.curdir, 'tests', 'basic')) DATA_FOLDER = os.path.abspath(os.path.join(os.path.curdir, 'tests', 'basic'))
@ -57,6 +58,7 @@ def play_oder1():
def play_order1(): def play_order1():
##### Chemin du dossier contenant le spectre ##### ##### Chemin du dossier contenant le spectre #####
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
DATA_FOLDER = os.path.abspath(os.path.join(os.path.curdir, 'tests', 'basic')) DATA_FOLDER = os.path.abspath(os.path.join(os.path.curdir, 'tests', 'basic'))
@ -89,9 +91,19 @@ def play_order1():
min_peak_prominence=prominence, min_peak_prominence=prominence,
plot=True) plot=True)
result = thickness_from_scheludko(lambdas, smoothed_intensities, w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
smoothed_intensities,
refractive_index,
min_peak_prominence=prominence,
plot=True)
result = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=refractive_index, refractive_index=refractive_index,
min_peak_prominence=prominence, wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=True) plot=True)
def play_order0(): def play_order0():

View file

@ -126,12 +126,43 @@ def _Delta_fit(xdata, thickness, interference_order):
return _Delta(lambdas, thickness, interference_order, r_index) return _Delta(lambdas, thickness, interference_order, r_index)
def get_default_start_stop_wavelengths(wavelengths,
intensities,
refractive_index,
min_peak_prominence,
plot=None):
# idx_min idx max
peaks_min, peaks_max = finds_peak(wavelengths, intensities,
min_peak_prominence=min_peak_prominence,
plot=plot)
failure, message = False, ''
if len(peaks_min) == 0:
message += 'Failed to detect at least one minimum. '
failure = True
if len(peaks_max) == 0:
message += 'Failed to detect at least one maximum. '
failure = True
if failure:
raise RuntimeError(message)
# Get the last oscillation peaks
lambda_min = wavelengths[peaks_min[-1]]
lambda_max = wavelengths[peaks_max[-1]]
# Order them
wavelength_start = min(lambda_min, lambda_max)
wavelength_stop = max(lambda_min, lambda_max)
return wavelength_start, wavelength_stop
def thickness_from_scheludko(wavelengths, def thickness_from_scheludko(wavelengths,
intensities, intensities,
refractive_index, refractive_index,
min_peak_prominence, wavelength_start,
wavelength_stop,
interference_order=None,
plot=None): plot=None):
""" """
@ -160,69 +191,53 @@ def thickness_from_scheludko(wavelengths,
max_tested_order = 12 max_tested_order = 12
r_index = refractive_index r_index = refractive_index
# idx_min idx max
peaks_min, peaks_max = finds_peak(wavelengths, intensities,
min_peak_prominence=min_peak_prominence,
plot=plot)
failure, message = False, ''
if len(peaks_min) == 0:
message += 'Failed to detect at least one minimum. '
failure = True
if len(peaks_max) == 0:
message += 'Failed to detect at least one maximum. '
failure = True
if failure:
raise RuntimeError(message)
# Get the last oscillation peaks
lambda_min = wavelengths[peaks_min[-1]]
lambda_max = wavelengths[peaks_max[-1]]
# Order them
lambda_start = min(lambda_min, lambda_max)
lambda_stop = max(lambda_min, lambda_max)
# Guess the order... if interference_order is None:
# Guess the order...
# mask input data # mask input data
mask = (wavelengths >= lambda_start) & (wavelengths <= lambda_stop) mask = (wavelengths >= wavelength_start) & (wavelengths <= wavelength_stop)
wavelengths_masked = wavelengths[mask] wavelengths_masked = wavelengths[mask]
r_index_masked = r_index[mask] r_index_masked = r_index[mask]
intensities_masked = intensities[mask] intensities_masked = intensities[mask]
min_difference = np.inf
best_m = None
min_difference = np.inf best_h_values = None
best_m = None
best_h_values = None
if plot:
plt.figure()
plt.ylabel(r'$h$ ($\mathrm{{nm}}$)')
plt.xlabel(r'$\lambda$ ($ \mathrm{nm} $)')
for m in range(0, max_tested_order+1):
h_values = _thicknesses_scheludko_at_order(wavelengths_masked,
intensities_masked,
m, r_index_masked)
difference = np.max(h_values) - np.min(h_values)
print(f"h-difference for m={m}: {difference:.1f} nm")
if difference < min_difference:
min_difference = difference
best_m = m
best_h_values = h_values
if plot: if plot:
plt.plot(wavelengths_masked, h_values,'.', markersize=3, label=f"Épaisseur du film (Scheludko, m={m})") plt.figure()
plt.ylabel(r'$h$ ($\mathrm{{nm}}$)')
plt.xlabel(r'$\lambda$ ($ \mathrm{nm} $)')
for m in range(0, max_tested_order+1):
h_values = _thicknesses_scheludko_at_order(wavelengths_masked,
intensities_masked,
m,
r_index_masked)
difference = np.max(h_values) - np.min(h_values)
print(f"h-difference for m={m}: {difference:.1f} nm")
if difference < min_difference:
min_difference = difference
best_m = m
best_h_values = h_values
if plot:
plt.plot(wavelengths_masked, h_values,'.', markersize=3, label=f"Épaisseur du film (Scheludko, m={m})")
else:
h_values = _thicknesses_scheludko_at_order(wavelengths_masked,
intensities_masked,
interference_order,
r_index_masked)
best_m = interference_order
best_h_values = h_values
print(f"Optimized: m={best_m}")
# Compute the thickness for the selected order # Compute the thickness for the selected order

View file

@ -4,6 +4,7 @@ from numpy.testing import assert_allclose
import pytest import pytest
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
from optifik.io import load_spectrum from optifik.io import load_spectrum
from optifik.analysis import smooth_intensities from optifik.analysis import smooth_intensities
@ -26,13 +27,21 @@ def test_SV2o1(spectrum_path, expected):
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
refractive_index = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.020 prominence = 0.020
w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
smoothed_intensities,
refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_from_scheludko(lambdas, smoothed_intensities, result = thickness_from_scheludko(lambdas,
refractive_index=refractive_index, smoothed_intensities,
min_peak_prominence=prominence, refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False) plot=False)
assert_allclose(result.thickness, expected, rtol=1e-1) assert_allclose(result.thickness, expected, rtol=1e-1)

View file

@ -4,6 +4,7 @@ from numpy.testing import assert_allclose
import pytest import pytest
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
from optifik.io import load_spectrum from optifik.io import load_spectrum
from optifik.analysis import smooth_intensities from optifik.analysis import smooth_intensities
@ -25,14 +26,23 @@ def test_SV2o2(spectrum_path, expected):
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
refractive_index = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.01 prominence = 0.01
w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
smoothed_intensities,
refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas, smoothed_intensities,
refractive_index=refractive_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_scheludko.thickness result = thickness_scheludko.thickness
assert_allclose(result, expected, rtol=1e-1) assert_allclose(result, expected, rtol=1e-1)

View file

@ -4,6 +4,7 @@ from numpy.testing import assert_allclose
import pytest import pytest
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
from optifik.io import load_spectrum from optifik.io import load_spectrum
from optifik.analysis import smooth_intensities from optifik.analysis import smooth_intensities
@ -25,14 +26,23 @@ def test_SV2o3(spectrum_path, expected):
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
refractive_index = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.02 prominence = 0.02
w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
smoothed_intensities,
refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas, smoothed_intensities,
refractive_index=refractive_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_scheludko.thickness result = thickness_scheludko.thickness
assert_allclose(result, expected, rtol=1e-1) assert_allclose(result, expected, rtol=1e-1)

View file

@ -4,6 +4,7 @@ from numpy.testing import assert_allclose
import pytest import pytest
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
from optifik.io import load_spectrum from optifik.io import load_spectrum
from optifik.analysis import smooth_intensities from optifik.analysis import smooth_intensities
@ -25,14 +26,23 @@ def test_SV2o4(spectrum_path, expected):
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
refractive_index = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.02 prominence = 0.02
w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
smoothed_intensities,
refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas, smoothed_intensities,
refractive_index=refractive_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_scheludko.thickness result = thickness_scheludko.thickness
assert_allclose(result, expected, rtol=1e-1) assert_allclose(result, expected, rtol=1e-1)

View file

@ -4,6 +4,7 @@ from numpy.testing import assert_allclose
import pytest import pytest
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
from optifik.io import load_spectrum from optifik.io import load_spectrum
from optifik.analysis import smooth_intensities from optifik.analysis import smooth_intensities
@ -25,14 +26,24 @@ def test_SV2o5(spectrum_path, expected):
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
refractive_index = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.02 prominence = 0.02
w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
smoothed_intensities,
refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False)
thickness_scheludko = thickness_from_scheludko(lambdas, smoothed_intensities,
refractive_index=refractive_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_scheludko.thickness result = thickness_scheludko.thickness
assert_allclose(result, expected, rtol=1e-1) assert_allclose(result, expected, rtol=1e-1)

View file

@ -6,6 +6,7 @@ import pytest
from optifik.fft import thickness_from_fft from optifik.fft import thickness_from_fft
from optifik.minmax import thickness_from_minmax from optifik.minmax import thickness_from_minmax
from optifik.scheludko import thickness_from_scheludko from optifik.scheludko import thickness_from_scheludko
from optifik.scheludko import get_default_start_stop_wavelengths
from optifik.scheludko import thickness_for_order0 from optifik.scheludko import thickness_for_order0
from optifik.analysis import smooth_intensities from optifik.analysis import smooth_intensities
from optifik.io import load_spectrum from optifik.io import load_spectrum
@ -19,15 +20,16 @@ def test_FFT():
spectrum_path = os.path.join(FOLDER, FILE_NAME) spectrum_path = os.path.join(FOLDER, FILE_NAME)
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
indice = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
thickness_FFT = thickness_from_fft(lambdas, thickness_FFT = thickness_from_fft(lambdas,
smoothed_intensities, smoothed_intensities,
refractive_index=indice) refractive_index=r_index)
result = thickness_FFT.thickness result = thickness_FFT.thickness
assert_allclose(result, expected, rtol=1e-1) assert_allclose(result, expected, rtol=1e-1)
def test_minmax_ransac(): def test_minmax_ransac():
FOLDER = os.path.join('tests', 'basic') FOLDER = os.path.join('tests', 'basic')
FILE_NAME = '000004310.xy' FILE_NAME = '000004310.xy'
@ -36,13 +38,13 @@ def test_minmax_ransac():
spectrum_path = os.path.join(FOLDER, FILE_NAME) spectrum_path = os.path.join(FOLDER, FILE_NAME)
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
indice = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.02 prominence = 0.02
result = thickness_from_minmax(lambdas, result = thickness_from_minmax(lambdas,
smoothed_intensities, smoothed_intensities,
refractive_index=indice, refractive_index=r_index,
min_peak_prominence=prominence, min_peak_prominence=prominence,
method='ransac', method='ransac',
plot=False) plot=False)
@ -58,19 +60,29 @@ def test_scheludko_4peaks():
spectrum_path = os.path.join(FOLDER, FILE_NAME) spectrum_path = os.path.join(FOLDER, FILE_NAME)
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
indice = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.02 prominence = 0.02
result = thickness_from_scheludko(lambdas, smoothed_intensities,
refractive_index=indice, w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
min_peak_prominence=prominence, smoothed_intensities,
plot=False) refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False)
assert_allclose(result.thickness, expected, rtol=1e-1) assert_allclose(result.thickness, expected, rtol=1e-1)
def test_scheludko_2peaks(): def test_scheludko_2peaks():
FOLDER = os.path.join('tests', 'basic') FOLDER = os.path.join('tests', 'basic')
FILE_NAME = '000006544.xy' FILE_NAME = '000006544.xy'
@ -79,21 +91,28 @@ def test_scheludko_2peaks():
spectrum_path = os.path.join(FOLDER, FILE_NAME) spectrum_path = os.path.join(FOLDER, FILE_NAME)
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
indice = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.03 prominence = 0.03
result = thickness_from_scheludko(lambdas, smoothed_intensities,
refractive_index=indice, w_start, w_stop = get_default_start_stop_wavelengths(lambdas,
min_peak_prominence=prominence, smoothed_intensities,
plot=False) refractive_index=r_index,
min_peak_prominence=prominence,
plot=False)
result = thickness_from_scheludko(lambdas,
smoothed_intensities,
refractive_index=r_index,
wavelength_start=w_start,
wavelength_stop=w_stop,
interference_order=None,
plot=False)
assert_allclose(result.thickness, expected, rtol=1e-1) assert_allclose(result.thickness, expected, rtol=1e-1)
def test_order0(): def test_order0():
FOLDER = os.path.join('tests', 'basic') FOLDER = os.path.join('tests', 'basic')
FILE_NAME = '000018918.xy' FILE_NAME = '000018918.xy'
@ -102,13 +121,13 @@ def test_order0():
spectrum_path = os.path.join(FOLDER, FILE_NAME) spectrum_path = os.path.join(FOLDER, FILE_NAME)
lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450) lambdas, raw_intensities = load_spectrum(spectrum_path, lambda_min=450)
smoothed_intensities = smooth_intensities(raw_intensities) smoothed_intensities = smooth_intensities(raw_intensities)
indice = 1.324188 + 3102.060378 / (lambdas**2) r_index = 1.324188 + 3102.060378 / (lambdas**2)
prominence = 0.03 prominence = 0.03
result = thickness_for_order0(lambdas, smoothed_intensities, result = thickness_for_order0(lambdas, smoothed_intensities,
refractive_index=indice, refractive_index=r_index,
min_peak_prominence=prominence, min_peak_prominence=prominence,
plot=False) plot=False)