Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

includes test #2

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions maxima.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,36 @@ def find_maxima(x):
"""

idx = []
plateau = []

for i in range(len(x)):

if i != len(x) - 1 and x[i] == x[i+1]:
if i not in plateau:
plateau.append(i)
plateau.append(i+1)
if i == len(x) - 1:
if x[i-1] < x[i]:
idx.append(i)
elif i == 0:
if x[i+1] < x[i]:
idx.append(i)
# `i` is a local maximum if the signal decreases before and after it
elif x[i-1] < x[i] and x[i+1] < x[i]:
idx.append(i)

print(plateau)
if plateau != []:
begin = plateau[0]
end = plateau[-1]
if end == len(x) - 1:
if x[begin-1] < x[begin]:
idx = idx + plateau
elif begin == 0:
if x[end+1] < x[end]:
idx = idx + plateau
# `i` is a local maximum if the signal decreases before and after it
elif x[begin-1] < x[begin] and x[end+1] < x[end]:
idx = idx + plateau
idx.sort()
return idx
84 changes: 44 additions & 40 deletions test_maxima.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
from maxima import *
import numpy as np

from maxima import find_maxima

def test_simple_sequence_two_maxima():
inp = [0, 1, 2, 1, 2, 1, 0]
out = find_maxima(inp)
exp = [2, 4]
assert exp == out

def test_simple_sequence_one_maximum():
inp = [-i**2 for i in range(-3, 4)]
out = find_maxima(inp)
exp = [3]
assert exp == out

def test_sine_wave():
inp = [np.sin(2*alpha) for alpha in np.linspace(0.0, 5.0, 100)]
out = find_maxima(inp)
exp = [16,78]
assert exp == out

def test_max_on_both_borders():
inp = [4, 2, 1, 3, 1, 2]
out = find_maxima(inp)
exp = [0,3,5]
assert exp == out

# additional tests for
# - max on both borders
# x = [4, 2, 1, 3, 1, 2]
# - max on both borders, absolute max on right border
# x = [4, 2, 1, 3, 1, 5]
# - one max (absolute) on left border
# x = [4, 2, 1, 3, 1]
# - plateau
# x = [1, 2, 2, 1]
# (decide for a sensible output in this case)
# - test cases for plateau
# x = [1, 2, 2, 3, 1]
# x = [1, 3, 2, 2, 1]
# x = [3, 2, 2, 3]
def test_find_maxima_1():
x = [0, 1, 2, 1, 2, 1, 0]
assert find_maxima(x) == [2,4]

def test_find_maxima_2():
x = [-i**2 for i in range(-3, 4)]
assert find_maxima(x) == [3]

def test_find_maxima_3():
x = [np.sin(2*alpha) for alpha in np.linspace(0.0, 5.0, 100)]
assert find_maxima(x) == [16,78]

def test_find_maxima_4():
x = [4, 2, 1, 3, 1, 2]
assert find_maxima(x) == [0,3,5]

def test_find_maxima_5():
x = [4, 2, 1, 3, 1, 5]
assert find_maxima(x) == [0,3,5]

def test_find_maxima_6():
x = [4, 2, 1, 3, 1]
assert find_maxima(x) == [0,3]

def test_find_maxima_7():
x = [1, 2, 2, 1]
assert find_maxima(x) == [1,2]

def test_find_maxima_8():
x = [1, 2, 2, 3, 1]
assert find_maxima(x) == [3]

def test_find_maxima_9():
x = [1, 3, 2, 2, 1]
assert find_maxima(x) == [1]

def test_find_maxima_10():
x = [3, 2, 2, 3]
assert find_maxima(x) == [0,3]

def test_find_maxima_11():
x = [1, 2, 2, 2, 1]
assert find_maxima(x) == [1,2,3]