Skip to content

Commit

Permalink
Sort Rayleigh to Brillouin peaks by minimizing Brillouin shift
Browse files Browse the repository at this point in the history
  • Loading branch information
raimund-schluessler committed Sep 28, 2022
1 parent b2b57df commit a168083
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 65 deletions.
60 changes: 24 additions & 36 deletions bmlab/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
43 changes: 14 additions & 29 deletions tests/test_evaluation_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()


Expand 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()


Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit a168083

Please sign in to comment.