Skip to content

Commit

Permalink
fix subtraction and addition for patterns with different x
Browse files Browse the repository at this point in the history
  • Loading branch information
CPrescher committed Nov 14, 2023
1 parent 5a8281b commit c43592d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions glassure/core/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def __sub__(self, other: Pattern) -> Pattern:
if orig_x.shape != other_x.shape:
# todo different shape subtraction of spectra seems the fail somehow...
# the background will be interpolated
other_fcn = interp1d(other_x, other_x, kind='linear')
other_fcn = interp1d(other_x, other_y, kind='linear')

# find overlapping x and y values:
ind = np.where((orig_x <= np.max(other_x)) &
Expand Down Expand Up @@ -362,7 +362,7 @@ def __add__(self, other: Pattern) -> Pattern:

if orig_x.shape != other_x.shape:
# the background will be interpolated
other_fcn = interp1d(other_x, other_x, kind='linear')
other_fcn = interp1d(other_x, other_y, kind='linear')

# find overlapping x and y values:
ind = np.where((orig_x <= np.max(other_x)) &
Expand Down
14 changes: 14 additions & 0 deletions tests/core_tests/test_pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ def test_plus_and_minus_operators():
assert np.array_equal(pattern1._y, np.sin(x) * 1)


def test_plus_and_minus_operators_with_different_x():
x1 = np.linspace(1, 9, 1000)
x2 = np.linspace(0, 10, 998)
pattern1 = Pattern(x1, np.sin(x1))
pattern2 = Pattern(x2, np.sin(x2))

pattern3 = pattern1 + pattern2
assert np.array_equal(pattern3.x, x1)
np.testing.assert_array_almost_equal(pattern3.y, np.sin(x1) * 2, decimal=5)

pattern3 = pattern1 - pattern2
np.testing.assert_array_almost_equal(pattern3._y, np.sin(x1) * 0, decimal=5)


def test_multiply_operator():
x = np.linspace(0, 10, 100)
pattern = 2 * Pattern(x, np.sin(x))
Expand Down

0 comments on commit c43592d

Please sign in to comment.