-
Notifications
You must be signed in to change notification settings - Fork 11
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
Add get_histogram utility, option to return histogram of bond lengths and angles #33
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f3b28bf
add get histogram func
chrisjonesBSU e453c50
add histogram option to bond and angle distributions
chrisjonesBSU 9d9cf13
convert bond and angle lists into arrays
chrisjonesBSU 9918275
convert bin_heights to array
chrisjonesBSU 68dae95
return 2D array rather than tuple, fix doc strings
chrisjonesBSU f386346
use numpy stack when returning histogram
chrisjonesBSU 7a830ff
add bins parameter to distribution funcitons
chrisjonesBSU 81e1a1e
unit tests for bond angle histograms
chrisjonesBSU a211e94
fix func call in test
chrisjonesBSU fa4d64f
unit tests for plotting.py:
chrisjonesBSU 999cd22
Merge branch 'master' of github.com:cmelab/cmeutils into fix_distribu…
chrisjonesBSU d882699
move sum out of for loop
chrisjonesBSU 6dd3b05
add to doc strings
chrisjonesBSU 2e68ff1
clean up normalize
chrisjonesBSU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import numpy as np | ||
|
||
|
||
def get_histogram(data, normalize=False, bins="auto"): | ||
"""Bins a 1-D array of data into a histogram using | ||
the numpy.histogram method. | ||
|
||
Parameters | ||
---------- | ||
data : 1-D numpy.array, required | ||
Array of data used to generate the histogram | ||
normalize : boolean, default=False | ||
If set to true, normalizes the histogram bin heights | ||
by the sum of data so that the distribution adds | ||
up to 1 | ||
bins : float, int, or str, default="auto" | ||
Method used by numpy to determine bin borders. | ||
Check the numpy.histogram docs for more details. | ||
|
||
Returns | ||
------- | ||
bin_cetners : 1-D numpy.array | ||
Array of the bin center values | ||
bin_heights : 1-D numpy.array | ||
Array of the bin height values | ||
|
||
""" | ||
bin_heights, bin_borders = np.histogram(data, bins=bins) | ||
if normalize is True: | ||
s = sum(bin_heights) | ||
bin_heights = np.array( | ||
[float(i)/s for i in bin_heights] | ||
) | ||
bin_widths = np.diff(bin_borders) | ||
bin_centers = bin_borders[:-1] + bin_widths / 2 | ||
return bin_centers, bin_heights |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
from cmeutils.plotting import get_histogram | ||
from base_test import BaseTest | ||
|
||
|
||
class TestPlotting(BaseTest): | ||
def test_histogram_bins(self): | ||
sample = np.random.randn(100) | ||
bin_c, bin_h = get_histogram(sample, bins=20) | ||
assert len(bin_c) == len(bin_h) == 20 | ||
|
||
def test_histogram_normalize(self): | ||
sample = np.random.randn(100)*-1 | ||
bin_c, bin_h = get_histogram(sample, normalize=True) | ||
assert all(bin_h <= 1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This throws an error.
I did change it so it's a bit cleaner/faster though.