Skip to content

Commit

Permalink
use 1D Histogram functions written in Cython
Browse files Browse the repository at this point in the history
Support for Particle Shape Orders 0 and 1.
  • Loading branch information
skuschel committed Mar 10, 2015
1 parent d452351 commit 9b8a7b7
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ virtualenv:
# command to install dependencies
install:
- sudo apt-get update -qq
- if [[ $TRAVIS_PYTHON_VERSION == "2.7" ]]; then sudo apt-get install python-matplotlib python-scipy; fi
- if [[ $TRAVIS_PYTHON_VERSION == "2.7" ]]; then sudo apt-get install python-matplotlib python-scipy python-cython; fi
- pip install -r pip-requirements.txt

# run tests
Expand Down
4 changes: 4 additions & 0 deletions examples/simpleexample.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,8 @@ def r(pa):
# use the plotter with the particle scalars defined above.
plotter.plotField(pa.createField(r, p_r, optargsh={'bins':[400,400]})) # plot 12

# show what particle shapes can do
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 100, 'order': 0}), ylim=(3e0, 2e5))
plotter.plotField(pa.createField(PA.X, optargsh={'bins': 100, 'order': 1}), ylim=(3e0, 2e5))


17 changes: 14 additions & 3 deletions postpic/analyzer/particles.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,9 +598,12 @@ def median(self, func, weights=1.0):

# ---- Functions to create a Histogram. ---

def createHistgram1d(self, scalarfx, optargsh={'bins': 300},
def createHistgram1d(self, scalarfx, optargsh={},
simextent=False, simgrid=False, rangex=None,
weights=lambda x: 1):
optargshdefs = {'bins': 300, 'order': 1}
optargshdefs.update(optargsh)
optargsh = optargshdefs
if simgrid:
simextent = True
xdata = scalarfx(self)
Expand All @@ -616,8 +619,16 @@ def createHistgram1d(self, scalarfx, optargsh={'bins': 300},
if hasattr(scalarfx, 'gridpoints'):
optargsh['bins'] = scalarfx.gridpoints
w = self.weight() * weights(self)
h, edges = np.histogram(xdata, weights=w,
range=rangex, **optargsh)
try:
from .. import cythonfunctions as cyf
h, edges = cyf.histogram(xdata, weights=w,
range=rangex, **optargsh)
except ImportError:
import warnings
warnings.warn('cython libs could not be imported. Falling back to "numpy.histogram".')
optargsh.pop('order')
h, edges = np.histogram(xdata, weights=w,
range=rangex, **optargsh)
h = h / np.diff(edges) # to calculate particles per xunit.
return h, edges

Expand Down
8 changes: 4 additions & 4 deletions test/test_cythonfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ def setUp(self):
def test_histogram(self):
# in this special case, cf.histogram and np.histogram must yield equal results
# check hist and edges
cfh, cfe = cf.histogram(self.data,bins=20, range=(0,1))
cfh, cfe = cf.histogram(self.data, bins=20, range=(0,1), order=0)
nph, npe = np.histogram(self.data, bins=20, range=(0,1))
self.assertListEqual(list(nph), list(cfh))
self.assertListEqual(list(npe), list(cfe))

def test_histogramw(self):
# in this special case, cf.histogram and np.histogram must yield equal results
# check hist and edges
cfh, cfe = cf.histogram(self.data,bins=100, range=(0,1), weights=self.weights)
cfh, cfe = cf.histogram(self.data, bins=100, range=(0,1), order=0, weights=self.weights)
nph, npe = np.histogram(self.data, bins=100, range=(0,1), weights=self.weights)
diff = np.abs(cfh - nph)
self.assertAlmostEqual(np.sum(diff), 0)
self.assertListEqual(list(npe), list(cfe))

def test_histogramo1(self):
cfh, cfe = cf.histogram(self.data,bins=100, range=(-0.1,1.1), order=1)
cfh, cfe = cf.histogram(self.data, bins=100, range=(-0.1,1.1), order=1)
nph, npe = np.histogram(self.data, bins=100, range=(-0.1,1.1))
# just check that no mass is lost
diff = np.sum(cfh) - np.sum(nph)
self.assertAlmostEqual(diff, 0)
self.assertListEqual(list(npe), list(cfe))

def test_histogramo1w(self):
cfh, cfe = cf.histogram(self.data,bins=100, range=(-0.1,1.1), order=1, weights=self.weights)
cfh, cfe = cf.histogram(self.data, bins=100, range=(-0.1,1.1), order=1, weights=self.weights)
nph, npe = np.histogram(self.data, bins=100, range=(-0.1,1.1), weights=self.weights)
# just check that no mass is lost
diff = np.sum(cfh) - np.sum(nph)
Expand Down

0 comments on commit 9b8a7b7

Please sign in to comment.