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

F1 #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

F1 #3

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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: generic
python:
-"3.6"
script:
- pytest
7 changes: 6 additions & 1 deletion maxima.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ def find_maxima(x):
"""

idx = []
max = (len(x)-1)
for i in range(len(x)):
# `i` is a local maximum if the signal decreases before and after it
if x[i-1] < x[i] and x[i+1] < x[i]:
if i ==0 and x[i+1] <= x[i]:
idx.append(i)
elif i ==max and x[i-1] <= x[i]:
idx.append(i)
elif 0<i<max and x[i-1] <= x[i] and x[i+1] <= x[i]:
idx.append(i)
return idx
43 changes: 43 additions & 0 deletions test_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np
import pytest
import maxima
from maxima import find_maxima

#def test_first_set():
# x = [0,1,2,1,2,1,0]
# x = [1, 2, 2, 1]
#assert find_maxima(x) == [1,2]

test_case_1 = [([0, 1, 2, 1, 2, 1, 0],[2,4]),([-i**2 for i in range(-3, 4)],[3]),([np.sin(2*alpha) for alpha in np.linspace(0.0, 5.0, 100)],[16,78]),
([4, 2, 1, 3, 1, 2],[0,3,5]),([4, 2, 1, 3, 1, 5],[0,3,5]),([4, 2, 1, 3, 1],[0,3]),([1, 2, 2, 3, 1],[1,3]),([1, 3, 2, 2, 1],[1,3]),([3, 2, 2, 3],[0,3])]

@pytest.mark.parametrize('inp,exp',test_case_1)
def test_case_1_maxima(inp,exp):
out = find_maxima(inp)
assert out ==exp

def test_randomized():
#given
seedval =0
rand_gen = np.random.RandomState(seed = seedval)
numel = rand_gen.randint(0,1000)
test_vec = rand_gen.random_integers(low=1,high=20,size=numel)
print(f'test_vec:{test_vec}')

#When
out =find_maxima(test_vec)

#Then
if out[0]==0:
assert test_vec[0] > test_vec[1]
else:
for k in range(1,out[0]):
assert test_vec[k]>=test_vec[k-1]
if len(out)>3 :
up=False
for i,j in zip(out[1:-2],out[2:-1]):
for k in range(i,j):
if test_vec[k] >test_vec[k-1]:
up = True
if test_vec[k] < test_vec[k-1]:
assert not up