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

Plotting func/peak finder #31

Closed
wants to merge 10 commits into from
23 changes: 23 additions & 0 deletions cmeutils/plot_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scipy.signal as signal


def find_peaks(data, height=None):
"""Finds peaks in 1-D data.
The peaks are any value greater than the variable entered for height. The data values are 1-D values (like x, y, or z)
that you wish to find the peaks of.
Any points/values that excede the height will be identified as peaks.

Parameters
----------
data : numpy.ndarray, shape (N,1)
Such as x, y, OR z.
max_height : int, default None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the height can be a float

Suggested change
max_height : int, default None
height : float, default None

https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks.html

Required height of peaks.

Returns
----------
tuple
The indices of peaks
"""
peaks = signal.find_peaks(data, height=height)
return peaks
101 changes: 101 additions & 0 deletions cmeutils/tests/assets/cztptz8fitic_rdf.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# bin_centers, rdf
3.994999825954437256e-02,0.000000000000000000e+00
9.984999895095825195e-02,0.000000000000000000e+00
1.597499847412109375e-01,0.000000000000000000e+00
2.196500003337860107e-01,0.000000000000000000e+00
2.795499861240386963e-01,0.000000000000000000e+00
3.394499719142913818e-01,0.000000000000000000e+00
3.993499875068664551e-01,0.000000000000000000e+00
4.592499732971191406e-01,0.000000000000000000e+00
5.191499590873718262e-01,0.000000000000000000e+00
5.790499448776245117e-01,0.000000000000000000e+00
6.389499902725219727e-01,0.000000000000000000e+00
6.988499164581298828e-01,0.000000000000000000e+00
7.587499618530273438e-01,0.000000000000000000e+00
8.186499476432800293e-01,0.000000000000000000e+00
8.785499334335327148e-01,0.000000000000000000e+00
9.384499788284301758e-01,1.151131615042686462e-01
9.983499050140380859e-01,6.846419572830200195e-01
1.058249950408935547e+00,1.048080801963806152e+00
1.118149995803833008e+00,1.041752099990844727e+00
1.178049921989440918e+00,1.242001771926879883e+00
1.237949848175048828e+00,1.033135056495666504e+00
1.297849893569946289e+00,8.265385031700134277e-01
1.357749938964843750e+00,7.361899018287658691e-01
1.417649984359741211e+00,6.888862848281860352e-01
1.477549910545349121e+00,6.145194768905639648e-01
1.537449836730957031e+00,6.286224722862243652e-01
1.597349882125854492e+00,7.642579078674316406e-01
1.657249927520751953e+00,6.986548304557800293e-01
1.717149972915649414e+00,6.917718648910522461e-01
1.777049899101257324e+00,7.743688821792602539e-01
1.836949825286865234e+00,8.194733262062072754e-01
1.896849870681762695e+00,8.899429440498352051e-01
1.956749916076660156e+00,9.014871716499328613e-01
2.016649961471557617e+00,9.532688856124877930e-01
2.076550006866455078e+00,1.045596480369567871e+00
2.136449813842773438e+00,1.139888644218444824e+00
2.196349859237670898e+00,1.218445539474487305e+00
2.256249904632568359e+00,1.176826119422912598e+00
2.316149711608886719e+00,1.253429651260375977e+00
2.376049995422363281e+00,1.361675977706909180e+00
2.435949802398681641e+00,1.382297039031982422e+00
2.495849847793579102e+00,1.433213591575622559e+00
2.555749893188476562e+00,1.312477111816406250e+00
2.615649700164794922e+00,1.224551677703857422e+00
2.675549983978271484e+00,1.134376645088195801e+00
2.735449790954589844e+00,1.086285829544067383e+00
2.795349836349487305e+00,1.051714539527893066e+00
2.855249881744384766e+00,1.009961009025573730e+00
2.915149688720703125e+00,1.000555276870727539e+00
2.975049972534179688e+00,9.923966526985168457e-01
3.034949779510498047e+00,9.976525902748107910e-01
3.094849824905395508e+00,1.042484164237976074e+00
3.154749870300292969e+00,1.041678071022033691e+00
3.214649677276611328e+00,1.033413648605346680e+00
3.274549961090087891e+00,1.049424052238464355e+00
3.334449768066406250e+00,1.036617636680603027e+00
3.394349813461303711e+00,1.084991216659545898e+00
3.454249858856201172e+00,1.049972891807556152e+00
3.514149665832519531e+00,9.781625866889953613e-01
3.574049949645996094e+00,9.548109173774719238e-01
3.633949756622314453e+00,9.191635847091674805e-01
3.693849802017211914e+00,9.239028096199035645e-01
3.753749847412109375e+00,9.223296046257019043e-01
3.813649654388427734e+00,9.212057590484619141e-01
3.873549938201904297e+00,9.389476776123046875e-01
3.933449745178222656e+00,9.609893560409545898e-01
3.993350028991699219e+00,9.558511376380920410e-01
4.053250312805175781e+00,9.536971449851989746e-01
4.113149642944335938e+00,9.888313412666320801e-01
4.173049926757812500e+00,1.027833461761474609e+00
4.232950210571289062e+00,1.000262975692749023e+00
4.292850017547607422e+00,9.397307038307189941e-01
4.352749824523925781e+00,9.313457608222961426e-01
4.412650108337402344e+00,9.218565821647644043e-01
4.472549915313720703e+00,9.478248953819274902e-01
4.532449722290039062e+00,9.352869987487792969e-01
4.592350006103515625e+00,9.461796283721923828e-01
4.652250289916992188e+00,9.286469221115112305e-01
4.712149620056152344e+00,9.612235426902770996e-01
4.772049903869628906e+00,1.003864526748657227e+00
4.831950187683105469e+00,1.050471663475036621e+00
4.891849994659423828e+00,1.035001754760742188e+00
4.951749801635742188e+00,1.022211790084838867e+00
5.011650085449218750e+00,9.648450016975402832e-01
5.071549892425537109e+00,9.438485503196716309e-01
5.131449699401855469e+00,9.375031590461730957e-01
5.191349983215332031e+00,9.009332060813903809e-01
5.251250267028808594e+00,9.108443856239318848e-01
5.311149597167968750e+00,9.003736972808837891e-01
5.371049880981445312e+00,9.083141684532165527e-01
5.430950164794921875e+00,8.886610865592956543e-01
5.490849971771240234e+00,9.240934848785400391e-01
5.550749778747558594e+00,9.480689764022827148e-01
5.610650062561035156e+00,9.633672833442687988e-01
5.670549869537353516e+00,9.480882883071899414e-01
5.730449676513671875e+00,9.471498727798461914e-01
5.790349960327148438e+00,9.670808315277099609e-01
5.850250244140625000e+00,9.884146451950073242e-01
5.910149574279785156e+00,9.464807510375976562e-01
5.970049858093261719e+00,9.529694914817810059e-01
4 changes: 4 additions & 0 deletions cmeutils/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ def mapping(self):
@pytest.fixture
def p3ht_xml(self):
return path.join(asset_dir, "p3ht.xml")

@pytest.fixture
def rdf_txt(self):
return path.join(asset_dir, "cztptz8fitic_rdf.txt")


def create_frame(i, add_bonds, images, seed=42):
Expand Down
2 changes: 1 addition & 1 deletion cmeutils/tests/test_dynamics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from cmeutils.tests.base_test import BaseTest
from base_test import BaseTest
from cmeutils.dynamics import msd_from_gsd

class TestDynamics(BaseTest):
Expand Down
16 changes: 16 additions & 0 deletions cmeutils/tests/test_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

import scipy.signal as signal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import scipy.signal as signal

Here we don't need to import this function because we want to use your find peaks function

import numpy as np

from base_test import BaseTest

from cmeutils.plot_tools import find_peaks

class TestPlot(BaseTest):
def test_find_peaks(self, rdf_txt):
line= np.genfromtxt(rdf_txt, names=True, delimiter=",")
y= line["rdf"]
peaks = signal.find_peaks(y)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
peaks = signal.find_peaks(y)
peaks = find_peaks(y)

assert isinstance(y, np.ndarray)
assert isinstance(peaks, tuple)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, now last step just check that a couple of peaks are in the right place. I do wonder if scipy.signal.find_peaks is used with no arguments (the default of your function), if any peaks will be found... I would plot this to make sure it makes sense :)

2 changes: 1 addition & 1 deletion cmeutils/tests/test_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import numpy as np
import freud

from cmeutils.tests.base_test import BaseTest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were the tests not working for you without this change? It might be because cmeutils is not installed in your environment or not installed in an editable way? (i.e., pip install -e .) Not sure.

from base_test import BaseTest

from cmeutils.structure import gsd_rdf, get_quaternions, order_parameter, all_atom_rdf, get_centers

Expand Down