def test_mean(self):
        h = Histogram(10,[0,10])
        h.fill([3,3,3])
        assert_almost_equal(h.mean()[0],3.5)

        h.fill([1,5])
        assert_almost_equal(h.mean()[0],3.5)
Exemple #2
0
 def test_hist_1d_line(self):
     rand.seed(1)
     h = Histogram(40, [0, 1], 'χ (cm)', 'counts', 'Some Data')
     h.fill(rand.normal(0.5, 0.1, 300))
     fig, ax = plt.subplots()
     pt = ax.plothist(h, style='line')
     self.assertTrue(
         conf.comparator.compare_images(fig.savefig, 'hist_1d_line.png'))
Exemple #3
0
 def test_hist_2d(self):
     rand.seed(1)
     h = Histogram(40, [0, 1], 'χ (cm)', 30, [-5, 5], 'ψ', 'counts',
                   'Some Data')
     h.fill(rand.normal(0.5, 0.1, 1000), rand.normal(0, 1, 1000))
     fig, ax = plt.subplots()
     pt = ax.plothist(h)
     self.assertTrue(
         conf.comparator.compare_images(fig.savefig, 'hist_2d.png'))
Exemple #4
0
def test_plot_hist2d():
    npoints = 100000
    h2 = Histogram((100, (0, 10), 'x'), (100, (0, 10), 'y'), 'z', 'title')
    h2.fill(rand.normal(5, 2, npoints), rand.uniform(0, 10, npoints))
    fig, ax = pyplot.subplots(1, 2)
    ax[0].plothist(h2)
    ax[1].plothist(h2)
    ax[1].plothist(h2.smooth(1), style='contour', overlay=True)

    pyplot.savefig('test_images/test_plotting_fig_hist2d.png')
def test_plot_hist2d():
    npoints = 100000
    h2 = Histogram((100,(0,10),'x'),(100,(0,10),'y'),'z','title')
    h2.fill(rand.normal(5,2,npoints),
            rand.uniform(0,10,npoints))
    fig,ax = pyplot.subplots(1,2)
    ax[0].plothist(h2)
    ax[1].plothist(h2)
    ax[1].plothist(h2.smooth(1), style='contour', overlay=True)

    pyplot.savefig('test_images/test_plotting_fig_hist2d.png')
def test_plot_hist1d():
    npoints = 100000
    h1 = Histogram(100,(0,10),'x','y','title')
    h1.fill(rand.normal(5,2,npoints))

    fig,ax = pyplot.subplots(2,2)
    ax[0,0].plothist(h1, style='polygon' , baseline='bottom')
    ax[0,1].plothist(h1, style='errorbar', baseline='bottom')
    ax[1,0].plothist(h1, style='polygon' )#, baseline='left')
    ax[1,1].plothist(h1, style='errorbar')#, baseline='left')

    pyplot.savefig('test_images/test_plotting_fig_hist1d.png')
Exemple #7
0
def test_plot_hist1d():
    npoints = 100000
    h1 = Histogram(100, (0, 10), 'x', 'y', 'title')
    h1.fill(rand.normal(5, 2, npoints))

    fig, ax = pyplot.subplots(2, 2)
    ax[0, 0].plothist(h1, style='polygon', baseline='bottom')
    ax[0, 1].plothist(h1, style='errorbar', baseline='bottom')
    ax[1, 0].plothist(h1, style='polygon')  #, baseline='left')
    ax[1, 1].plothist(h1, style='errorbar')  #, baseline='left')

    pyplot.savefig('test_images/test_plotting_fig_hist1d.png')
    def setUp(self):
        rc.overwrite.overwrite = 'always'
        np.random.seed(1)
        h = Histogram(100,[0,10],'Δx', 'y', 'title')
        h.fill(np.random.normal(5,2,10000))
        h.uncert = np.sqrt(h.data)

        if sys.version_info < (3,0):
            def _to_unicode(s):
                if not isinstance(s,unicode):
                    return unicode(s,'utf-8')
                else:
                    return s
            h.title = _to_unicode(h.title)
            h.label = _to_unicode(h.label)
            for ax in h.axes:
                ax.label = _to_unicode(ax.label)

        self.h = h
'''
Simple 2D histogram example.
'''

import numpy as np
from matplotlib import pyplot, cm
from histogram import Histogram, plothist

np.random.seed(1)

# 2D histogram with 30x40 bins
h = Histogram(30,[0,10],40,[-5,5])

# filling the histogram with some random data
npoints = 100000
datax = np.random.normal(5,1,npoints)
datay = np.random.uniform(-5,5,npoints)
h.fill(datax,datay)

# filling with even more data
datax = np.random.uniform(0,10,npoints)
datay = np.random.normal(0,1,npoints)
h.fill(datax,datay)

# using the plothist() convenience method
fig,ax,pt = plothist(h, cmap=cm.Blues)

# display figure to screen
pyplot.show()
Exemple #10
0
'''
Simple 2D histogram example.
'''

import numpy as np
from matplotlib import pyplot, cm
from histogram import Histogram, plothist

np.random.seed(1)

# 2D histogram with 30x40 bins
h = Histogram(30, [0, 10], 40, [-5, 5])

# filling the histogram with some random data
npoints = 100000
datax = np.random.normal(5, 1, npoints)
datay = np.random.uniform(-5, 5, npoints)
h.fill(datax, datay)

# filling with even more data
datax = np.random.uniform(0, 10, npoints)
datay = np.random.normal(0, 1, npoints)
h.fill(datax, datay)

# using the plothist() convenience method
fig, ax, pt = plothist(h, cmap=cm.Blues)

# display figure to screen
pyplot.show()
 def test_occupancy(self):
     h = Histogram(10,[0,10])
     h.fill([1,1,1,2,2,2,3])
     hocc = h.occupancy(4,[-0.5,3.5])
     assert_array_almost_equal(hocc.data, [7,1,0,2])
Exemple #12
0
from numpy import random as rand
from matplotlib import pyplot, cm
from histogram import Histogram

npoints = 1000000
datax = rand.normal(100,40,npoints)
datay = rand.normal(100,60,npoints)
dataz = rand.normal(50,20,npoints)

d0 = (10,[0,100],'x')
d1 = (9, [0,100],'y')
d2 = (100,[0,100],'z')
h3 = Histogram(d0,d1,d2,'counts','Random Data')

h3.fill(datax,datay,dataz)

fig = pyplot.figure(figsize=(8,8))
axs,axtot,axins = fig.plothist_grid(h3, ymin=0, cmap=cm.viridis) #copper_r)

pyplot.show()
from numpy import random as rand
from matplotlib import pyplot, cm
from histogram import Histogram

npoints = 1000000
datax = rand.normal(100,40,npoints)
datay = rand.normal(100,60,npoints)
dataz = rand.normal(50,20,npoints)

d0 = (10,[0,100],'x')
d1 = (9, [0,100],'y')
d2 = (100,[0,100],'z')
h3 = Histogram(d0,d1,d2,'counts','Random Data')

h3.fill(datax,datay,dataz)

fig = pyplot.figure(figsize=(12,12))
axs,axtot,axins = fig.plothist_grid(h3, cmap=cm.copper_r)

pyplot.show()
Exemple #14
0
import numpy as np
from histogram import Histogram
import matplotlib.pyplot as plt
from matplotlib.style import use
use('ggplot')

hist1 = Histogram(100, [0, 10])
hist2 = Histogram(100, [0, 10])

for i in range(1000):
    hist1.fill(np.random.normal(5, 1, 10000))
    hist2.fill(np.random.exponential(2, 10000))

print(hist2.n_entries)
print(hist2.n_underflow)
print(hist2.n_overflow)

hist1.plot()
hist2.plot(kind='bar', alpha=0.3)
plt.xlim(-0.1, 10.1)
plt.show()
Exemple #15
0
# -*- coding: utf-8 -*-

import numpy as np

from histogram import Histogram

np.random.seed(1)

h = Histogram(100,[0,10],'Δx', 'y', 'title')
h.fill(np.random.normal(5,2,10000))
h.uncert = np.sqrt(h.data)
Exemple #16
0
import matplotlib
matplotlib.use('GTK3Agg')

from numpy import random as rand
from matplotlib import pyplot
from histogram import Histogram

rand.seed(1)

npoints = 10000
xdata = rand.normal(100, 50, npoints)
ydata = rand.normal(50, 10, npoints)

d0 = (10, [0, 100], '$x$')
d1 = (10, [-0.5, 100.5], '$y$')
h2 = Histogram(d0, d1, '$z$', 'Random Data')
h2.fill(xdata, ydata)

h2slices = list(h2.slices())
axslices = h2.axes[0]

fig = pyplot.figure(figsize=(12, 8))
axs, saxs = fig.plothist_strip(h2slices, axslices)

pyplot.show()
from numpy import random as rand
from matplotlib import pyplot
from histogram import Histogram

rand.seed(1)

npoints = 10000
xdata = rand.normal(100,50,npoints)
ydata = rand.normal(50,10,npoints)

d0 = (10, [0,100],'$x$')
d1 = (10,[-0.5,100.5],'$y$')
h2 = Histogram(d0,d1,'$z$','Random Data')
h2.fill(xdata,ydata)

h2slices = list(h2.slices())
axslices = h2.axes[0]

fig = pyplot.figure(figsize=(12,12))
axs,saxs = fig.plothist_strip(h2slices,axslices)

pyplot.show()
Exemple #18
0
'''
About as basic an example as one can get.
'''

import numpy as np
from matplotlib import pyplot
from histogram import Histogram

np.random.seed(1)

h = Histogram(30,[0,10])
h.fill(np.random.normal(5,1,1000))

fig,ax = pyplot.subplots()
pt = ax.plothist(h, alpha=0.3)

pyplot.show()
Exemple #19
0
            else:
                if baseline_image.shape != test_image.shape:
                    good_match = False
                else:
                    rms = ImageComparator._rms(baseline_image, test_image)
                    good_match = rms <= tol

            diff_fpath = path.join(self.diff_directory, fname)
            if good_match:
                if path.exists(diff_fpath):
                    os.remove(diff_fpath)
            else:
                diff_image = ImageComparator._diff(baseline_fpath, test_fpath)
                with Image.fromarray(diff_image, 'L') as img:
                    img.save(diff_fpath)

        return good_match


if __name__ == '__main__':
    rand.seed(1)

    h = Histogram(40, [0, 1], 'χ (cm)', 'counts', 'Some Data')
    h.fill(rand.normal(0.5, 0.1, 2000))

    fig, ax = plt.subplots()
    pt = ax.plothist(h)

    comparator = ImageComparator()
    assert comparator.compare_images(fig.savefig, 'hist_1d.png')