Commit b64b74c2 authored by moritz's avatar moritz
Browse files

Merge branch 'master' into statsmodels

parents c5fa0f41 6491839f
Pipeline #201 failed with stage
in 0 seconds
......@@ -73,3 +73,4 @@ doc/api
.pytest_cache/
junit.xml
.mypy_cache/
......@@ -36,6 +36,7 @@ def get_stages(docker_image) {
try {
sh """
make dependencies
pip install git+http://git.km3net.de/km3py/km3pipe.git@develop
"""
} catch (e) {
sendChatMessage("Install Dependencies Failed")
......
......@@ -371,22 +371,38 @@ class Morph(stats.rv_histogram):
class hist2d(stats.rv_continuous):
"""Simple implementation of a 2d histogram."""
def __init__(self, H2D, *args, **kwargs):
H, xlims, ylims = H2D
self.H = H
self.xlims = xlims
self.ylims = ylims
self.H /= self.integral
self.xcenters = bincenters(xlims)
self.ycenters = bincenters(ylims)
self.H = zero_pad(self.H)
super(hist2d, self).__init__(*args, **kwargs)
@property
def H_pad(self):
return zero_pad(self.H)
def _pdf(self, x, y):
return self.H[
return self.H_pad[
np.searchsorted(self.xlims, x, side='right'),
np.searchsorted(self.ylims, y, side='right'),
]
@property
def integral(self):
return (self.H * self.area).sum()
@property
def area(self):
xwidths = np.diff(self.xlims)
ywidths = np.diff(self.ylims)
area = np.outer(xwidths, ywidths)
return area
class Ingredient(object):
def __init__(self, hist_1d, hist_2d):
......
......@@ -5,7 +5,7 @@ import numpy as np
from scipy.stats import lognorm
from km3pipe.stats import loguniform
from km3like.ingred import BinnedResolution, NotFittedError
from km3like.ingred import BinnedResolution, NotFittedError, hist2d
class TestBinnedResolution(TestCase):
......@@ -60,3 +60,41 @@ class TestBinnedResolution(TestCase):
binres.fit(self.residuals, self.ene, self.cosz, use_histogram=True)
binres.sample(self.ene, self.cosz, use_histogram=False)
binres.sample(self.ene, self.cosz, use_histogram=True)
class TestHist2D(TestCase):
def setUp(self):
np.random.seed(42)
self.sample = np.random.normal(0, 1, (100, 2))
self.bins = ([-2, 0, 2], [-2, 0, 2])
def test_pdf(self):
hist = np.histogram2d(self.sample[:, 0], self.sample[:, 1],
normed=True, bins=self.bins)
h = hist2d(hist)
assert h.H.shape == (2, 2)
assert h.H_pad.shape == (4, 4)
f = h.pdf(0, 0)
assert f is not None
def test_norm(self):
hist1 = np.histogram2d(self.sample[:, 0], self.sample[:, 1],
normed=True, bins=self.bins)
hist2 = np.histogram2d(self.sample[:, 0], self.sample[:, 1],
normed=False, bins=self.bins)
h1 = hist2d(hist1)
h2 = hist2d(hist2)
print(h1.area)
print(h1.H)
print(h1.H / h1.area)
print((h1.H / h1.area).sum())
print(h1.H.sum())
h1s = h1.H.sum()
assert np.allclose(h1s, h1.H.sum())
assert np.allclose(h1.H.sum(), h2.H.sum())
assert np.allclose(h1.integral, 1)
assert np.allclose(h2.integral, 1)
......@@ -19,4 +19,3 @@ pytest
pytest-cov
pytest-flake8
twine
--e git+http://git.km3net.de/km3py/km3pipe.git@develop#egg=km3pipe
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment