From a168083d66b23d8f4d341c52664b1bff2194d343 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raimund=20Schl=C3=BC=C3=9Fler?= Date: Mon, 26 Sep 2022 11:32:52 +0200 Subject: [PATCH] Sort Rayleigh to Brillouin peaks by minimizing Brillouin shift --- bmlab/controllers.py | 60 ++++++++++++----------------- tests/test_evaluation_controller.py | 43 +++++++-------------- 2 files changed, 38 insertions(+), 65 deletions(-) diff --git a/bmlab/controllers.py b/bmlab/controllers.py index 6e2dd35..7df3215 100644 --- a/bmlab/controllers.py +++ b/bmlab/controllers.py @@ -890,55 +890,43 @@ def get_indices_from_key(resolution, key): def calculate_derived_values(): """ - We calculate the derived parameters here: - - Brillouin shift [GHz] + We calculate the Brillouin shift in GHz here """ session = Session.get_instance() evm = session.evaluation_model() if not evm: return - if len(evm.results['time']) == 0: + if evm.results['brillouin_peak_position_f'].size == 0: + return + + if evm.results['rayleigh_peak_position_f'].size == 0: return shape_brillouin = evm.results['brillouin_peak_position_f'].shape shape_rayleigh = evm.results['rayleigh_peak_position_f'].shape - # If we have the same number of Rayleigh and Brillouin regions, - # we can simply subtract the two arrays (regions are always - # sorted by center in the peak selection model, so corresponding - # regions should be at the same array index) - if shape_brillouin[4] == shape_rayleigh[4]: - evm.results['brillouin_shift_f'] = abs( + + # We calculate every possible combination of + # Brillouin peak and Rayleigh peak position difference + # and then use the smallest absolute value. + # That saves us from sorting Rayleigh peaks to Brillouin peaks, + # because a Brillouin peak always belongs to the Rayleigh peak nearest. + brillouin_shift_f = np.NaN * np.ones((*shape_brillouin, shape_rayleigh[4])) + for idx in range(shape_rayleigh[4]): + brillouin_shift_f[:, :, :, :, :, :, idx] = abs( evm.results['brillouin_peak_position_f'] - - evm.results['rayleigh_peak_position_f'] + np.tile( + evm.results['rayleigh_peak_position_f'][:, :, :, :, [idx], :], + (1, 1, 1, 1, 1, shape_brillouin[5]) + ) ) - # Having a different number of Rayleigh and Brillouin regions - # doesn't really make sense. But in case I am missing something - # here, we assign each Brillouin region the nearest (by center) - # Rayleigh region. - else: - psm = session.peak_selection_model() - if not psm: - return - brillouin_centers = list(map(np.mean, psm.get_brillouin_regions())) - rayleigh_centers = list(map(np.mean, psm.get_rayleigh_regions())) - - for idx in range(len(brillouin_centers)): - # Find corresponding (nearest) Rayleigh region - d = list( - map( - lambda x: abs(x - brillouin_centers[idx]), - rayleigh_centers - ) - ) - idx_r = d.index(min(d)) - evm.results['brillouin_shift_f'][:, :, :, :, idx, :] = abs( - evm.results[ - 'brillouin_peak_position_f'][:, :, :, :, idx, :] - - evm.results[ - 'rayleigh_peak_position_f'][:, :, :, :, idx_r, :] - ) + with warnings.catch_warnings(): + warnings.filterwarnings( + action='ignore', + message='All-NaN slice encountered' + ) + evm.results['brillouin_shift_f'] = np.nanmin(brillouin_shift_f, 6) class Controller(object): diff --git a/tests/test_evaluation_controller.py b/tests/test_evaluation_controller.py index 1f3c5ac..876e586 100644 --- a/tests/test_evaluation_controller.py +++ b/tests/test_evaluation_controller.py @@ -118,7 +118,7 @@ def test_calculate_derived_values_equal_region_count(): evm.results['brillouin_peak_position_f'][:, :, :, :, 0, :] = 1 evm.results['brillouin_peak_position_f'][:, :, :, :, 1, :] = 4 - evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = 3 + evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = -1 evm.results['rayleigh_peak_position_f'][:, :, :, :, 1, :] = 8 calculate_derived_values() @@ -159,14 +159,14 @@ def test_calculate_derived_values_equal_region_count_nr_peaks_2(): evm.results['brillouin_peak_position_f'][:, :, :, :, 1, :] = 4 evm.results['brillouin_peak_position_f'][:, :, :, :, 0, 1] = 2 evm.results['brillouin_peak_position_f'][:, :, :, :, 1, 1] = 5 - evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = 3 + evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = -1 evm.results['rayleigh_peak_position_f'][:, :, :, :, 1, :] = 8 calculate_derived_values() assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 0] == 2).all() assert (evm.results['brillouin_shift_f'][:, :, :, :, 1, 0] == 4).all() - assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 1] == 1).all() + assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 1] == 3).all() assert (evm.results['brillouin_shift_f'][:, :, :, :, 1, 1] == 3).all() @@ -193,22 +193,13 @@ def test_calculate_derived_values_different_region_count(): evm.results['brillouin_peak_position_f'][:, :, :, :, 0, :] = 1 evm.results['brillouin_peak_position_f'][:, :, :, :, 1, :] = 4 - evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = 5 + evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = -2 evm.results['rayleigh_peak_position_f'][:, :, :, :, 1, :] = 9 evm.results['rayleigh_peak_position_f'][:, :, :, :, 2, :] = 10 - psm = evc.session.peak_selection_model() - - psm.add_brillouin_region((1, 2)) - psm.add_brillouin_region((4, 5)) - - psm.add_rayleigh_region((0, 1)) - psm.add_rayleigh_region((6, 7)) - psm.add_rayleigh_region((8, 9)) - calculate_derived_values() - assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, :] == 4).all() + assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, :] == 3).all() assert (evm.results['brillouin_shift_f'][:, :, :, :, 1, :] == 5).all() @@ -237,25 +228,19 @@ def test_calculate_derived_values_different_region_count_nr_peaks_2(): evm.results['brillouin_peak_position_f'][:, :, :, :, 1, :] = 4 evm.results['brillouin_peak_position_f'][:, :, :, :, 0, 1] = 2 evm.results['brillouin_peak_position_f'][:, :, :, :, 1, 1] = 5 - evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = 5 + evm.results['rayleigh_peak_position_f'][:, :, :, :, 0, :] = -2 evm.results['rayleigh_peak_position_f'][:, :, :, :, 1, :] = 9 - evm.results['rayleigh_peak_position_f'][:, :, :, :, 2, :] = 10 - - psm = evc.session.peak_selection_model() - - psm.add_brillouin_region((1, 2)) - psm.add_brillouin_region((4, 5)) - - psm.add_rayleigh_region((0, 1)) - psm.add_rayleigh_region((6, 7)) - psm.add_rayleigh_region((8, 9)) + evm.results['rayleigh_peak_position_f'][0:4, :, :, :, 2, :] = 10 + evm.results['rayleigh_peak_position_f'][4, :, :, :, 2, :] = 8 calculate_derived_values() - assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 0] == 4).all() - assert (evm.results['brillouin_shift_f'][:, :, :, :, 1, 0] == 5).all() - assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 1] == 3).all() - assert (evm.results['brillouin_shift_f'][:, :, :, :, 1, 1] == 4).all() + assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 0] == 3).all() + assert (evm.results['brillouin_shift_f'][0:4, :, :, :, 1, 0] == 5).all() + assert (evm.results['brillouin_shift_f'][4, :, :, :, 1, 0] == 4).all() + assert (evm.results['brillouin_shift_f'][:, :, :, :, 0, 1] == 4).all() + assert (evm.results['brillouin_shift_f'][0:4, :, :, :, 1, 1] == 4).all() + assert (evm.results['brillouin_shift_f'][4, :, :, :, 1, 1] == 3).all() def test_get_data_0D(mocker):