Skip to content

Commit

Permalink
🧹 Remove redundant backsweep flag from GaussianActivationParameters (g…
Browse files Browse the repository at this point in the history
…lotaran#1423)

Just a small additional clean-up after glotaran#1422 to get rid of the backsweep boolean flag, which is a residual of the < 0.8 model syntax when we had this as user input.
  • Loading branch information
s-weigand authored and jsnel committed Jun 2, 2024
1 parent e16ad5b commit 2771d4c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 17 deletions.
2 changes: 0 additions & 2 deletions glotaran/builtin/elements/kinetic/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ def calculate_matrix_gaussian_activation(
np.array([[p.center for p in ps] for ps in parameters]), # type:ignore[union-attr]
np.array([[p.width for p in ps] for ps in parameters]), # type:ignore[union-attr]
scales,
parameters[0][0].backsweep, # type:ignore[index]
parameters[0][0].backsweep_period, # type:ignore[index]
)
else:
Expand All @@ -147,7 +146,6 @@ def calculate_matrix_gaussian_activation(
np.array([p.center for p in parameters]), # type:ignore[union-attr]
np.array([p.width for p in parameters]), # type:ignore[union-attr]
scales,
parameters[0].backsweep, # type:ignore[union-attr]
parameters[0].backsweep_period, # type:ignore[union-attr]
)
if activation.normalize:
Expand Down
15 changes: 5 additions & 10 deletions glotaran/builtin/elements/kinetic/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ def calculate_matrix_gaussian_activation_on_index(
centers: ArrayLike,
widths: ArrayLike,
scales: ArrayLike,
backsweep: bool,
backsweep_period: float | None,
backsweep_period: float,
):
"""Calculates a decay matrix with a gaussian irf."""
for n_i in nb.prange(centers.size):
center, width, scale = centers[n_i], widths[n_i], scales[n_i]
for n_r in nb.prange(rates.size):
r_n = rates[n_r]
backsweep_valid = backsweep and abs(r_n) * backsweep_period > 0.001
backsweep_valid = abs(r_n) * backsweep_period > 0.001
alpha = (r_n * width) / SQRT2
for n_t in nb.prange(times.size):
t_n = times[n_t]
Expand All @@ -52,11 +51,9 @@ def calculate_matrix_gaussian_activation_on_index(
matrix[n_t, n_r] += (
scale * 0.5 * (1 + erf(thresh)) * np.exp(alpha * (alpha - 2 * beta))
)
if backsweep and backsweep_valid:
if backsweep_valid:
x1 = np.exp(-r_n * (t_n - center + backsweep_period))
x2 = np.exp(
-r_n * ((backsweep_period / 2) - (t_n - center)) # type:ignore[operator]
)
x2 = np.exp(-r_n * ((backsweep_period / 2) - (t_n - center)))
x3 = np.exp(-r_n * backsweep_period)
matrix[n_t, n_r] += scale * (x1 + x2) / (1 - x3)

Expand All @@ -69,8 +66,7 @@ def calculate_matrix_gaussian_activation(
all_centers: ArrayLike,
all_widths: ArrayLike,
scales: ArrayLike,
backsweep: bool,
backsweep_period: float | None,
backsweep_period: float,
):
for n_w in nb.prange(all_centers.shape[0]):
calculate_matrix_gaussian_activation_on_index(
Expand All @@ -80,6 +76,5 @@ def calculate_matrix_gaussian_activation(
all_centers[n_w],
all_widths[n_w],
scales,
backsweep,
backsweep_period,
)
3 changes: 0 additions & 3 deletions glotaran/builtin/items/activation/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class GaussianActivationParameters:
center: float
width: float
scale: float
backsweep: bool
backsweep_period: float

def shift(self, value: float):
Expand Down Expand Up @@ -170,15 +169,13 @@ def parameters(
widths = widths * nr_gaussians

scales = self.scale or [1.0] * nr_gaussians
backsweep = self.backsweep is not None
backsweep_period = float(self.backsweep) if self.backsweep is not None else 0

parameters: list[GaussianActivationParameters] = [
GaussianActivationParameters(
float(center),
float(width),
float(scale),
backsweep,
backsweep_period,
)
for center, width, scale in zip(centers, widths, scales)
Expand Down
3 changes: 1 addition & 2 deletions tests/builtin/items/activation/test_activation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def test_gaussian_activation():
)
parameter = activation.parameters(np.array([0]))
assert len(parameter) == 1
assert parameter[0].backsweep
assert parameter[0].backsweep_period == 2
assert parameter[0].center == 1
assert parameter[0].width == 10
Expand All @@ -33,7 +32,7 @@ def test_multi_gaussian_activation():
)
parameter = activation.parameters(np.array([0]))
assert len(parameter) == 2
assert not parameter[0].backsweep
assert parameter[0].backsweep_period == 0
assert parameter[0].center == 1
assert parameter[0].width == 10
assert parameter[1].center == 2
Expand Down

0 comments on commit 2771d4c

Please sign in to comment.