Esempio n. 1
0
    def testdump2(self):
        'dump two histograms to one hdf'
        filename = 'testdump1.h5'
        import os
        if os.path.exists( filename): 
            os.remove( filename )

        from h5py import File
        fs = File( filename, 'w' )
        
        from histogram import histogram, arange
        h = histogram('h',
                      [('x', arange(0,100, 1.) ),
                       ('y', arange(100, 180, 1.) ),],
                      unit = 'meter',
                      )
        dump( h, None, '/', fs = fs )
        
        h2 = histogram('h2',
                      [('x', arange(0,100, 1.) ),
                       ('y', arange(100, 180, 1.) ),],
                      unit = 'meter',
                      )
        dump( h2, None, '/', fs = fs )

        #load histogram
        h2c = load( filename, '/h2', fs = fs )
        print h2c
        self.assert_( os.path.exists( filename ))
def makeDispersion():
    nAtoms = 2
    nBranches = 3 * nAtoms
    nQx = 10; nQy = 12; nQz = 14
    Qaxes = [ ([1,0,0], nQx),
              ([0,1,0], nQy),
              ([0,0,1], nQz),
              ]

    import histogram as H
    qx = H.axis('qx', H.arange(0, 1+1e-10, 1./(nQx-1)))
    qy = H.axis('qy', H.arange(0, 1+1e-10, 1./(nQy-1)))
    qz = H.axis('qz', H.arange(0, 1+1e-10, 1./(nQz-1)))
    br = H.axis('branchId', range(nBranches))
    atoms = H.axis('atomId', range(nAtoms))
    pols = H.axis('polId', range(3))
    realimags = H.axis('realimagId', range(2))

    eps = H.histogram('eps', [qx,qy,qz,br,atoms,pols,realimags])
    eps.I[:] = 1
    e = H.histogram('e', [qx,qy,qz,br],
                    fromfunction = lambda qx,qy,qz,br: (qx*qx+qy*qy+qz*qz)/3.*50,
                    )

    disp = b.linearlyinterpolateddispersion_3d(nAtoms, Qaxes, eps.I, e.I)
    disp = b.periodicdispersion(disp, ((1,0,0), (0,1,0), (0,0,1)))
    return disp
Esempio n. 3
0
def computeFocusedSpectraForRealMonitors(E, m2sout, out):
    from mcni.utils.conversion import e2v

    v = e2v(E)
    from pyre.units.time import second
    import histogram.hdf as hh, histogram as H

    m1 = hh.load(os.path.join(m2sout, "mon1-tof.h5"), "I(tof)")
    L1 = 11.831
    t1 = L1 / v  # * second
    m1p = m1[(t1 * 0.9, t1 * 1.1)]
    m1pc = H.histogram("I(tof)", m1p.axes(), data=m1p.I, errors=m1p.E2)
    m1pc.setAttribute("title", "Monitor 1 I(tof)")

    hh.dump(m1pc, os.path.join(out, "mon1-itof-focused.h5"), "/", "c")

    m2 = hh.load(os.path.join(m2sout, "mon2-tof.h5"), "I(tof)")
    L2 = 18.5
    t2 = L2 / v  # * second
    m2p = m2[(t2 * 0.9, t2 * 1.1)]
    m2pc = H.histogram("I(tof)", m2p.axes(), data=m2p.I, errors=m2p.E2)
    m2pc.setAttribute("title", "Monitor 2 I(tof)")

    hh.dump(m2pc, os.path.join(out, "mon2-itof-focused.h5"), "/", "c")
    return
Esempio n. 4
0
 def testOneNumber(self):
     
     out = histogram([1])
     self.assertAlmostEqual(out[0,0], 2)
     self.assertAlmostEqual(out[0,1], 1)  
     
     out = histogram([10])
     self.assertAlmostEqual(out[0,0], 16)
     self.assertAlmostEqual(out[0,1], 0.125)
Esempio n. 5
0
def get_hash_mentions(tweets, count=3):
    """ returns stats about hashtags and mentions from tweets
        tweets: a list of strings
    """
    hashtags = strings_startswith(tweets, '#')
    mentions = strings_startswith(tweets, '@')
    freq_hashtags =  sort_dct(histogram(hashtags))
    freq_mentions =  sort_dct(histogram(mentions))
    return freq_hashtags[:count], freq_mentions[:count]
Esempio n. 6
0
def make_energy_collection(length=600):
    collection = dict()
    min_E, max_E = -2, 5
    bins_per_decade = 10
    frac_loss_bins = length / 60
    collection['deltaE'] = histogram.histogram(bins=np.logspace(min_E,max_E,(max_E-min_E)*bins_per_decade+1))
    collection['frac_loss'] = histogram.histogram_nd(2, bins=np.linspace(0,length,frac_loss_bins+1))
    for i in xrange(frac_loss_bins):
        collection['bin_'+str(i)+'_dE'] = histogram.histogram(bins=np.array([0]+list(np.logspace(min_E,max_E,(max_E-min_E)*bins_per_decade+1))))
        collection['bin_'+str(i)+'_frac_loss'] = histogram.histogram(bins=np.array([0]+list(np.logspace(-7,0,7*bins_per_decade+1))))
    return collection
Esempio n. 7
0
def get_stats(tweets, scr_name, count=2):
    """ returns stats about hashtags and user mentions 
        tweets: a dict of tweet dicts from rest api
    """
    hashtags = get_hashtags(tweets)
    mentions = get_mentions(tweets)
    
    freq_hashtags =  sort_dct(histogram(hashtags))
    freq_mentions =  sort_dct(histogram(mentions))
    freq_mentions = remove_self(freq_mentions, scr_name)
    return freq_hashtags[:count], freq_mentions[:count]
Esempio n. 8
0
    def test_pickle_0(self):
        a = histogram(category(0, 1, 2),
                      integer(0, 20, label='ia'),
                      regular(20, 0.0, 20.0, uoflow=False),
                      variable(0.0, 1.0, 2.0),
                      circular(4, label='pa'))
        for i in range(len(a.axis(0))):
            a.fill(i, 0, 0, 0, 0)
            for j in range(len(a.axis(1))):
                a.fill(i, j, 0, 0, 0)
                for k in range(len(a.axis(2))):
                    a.fill(i, j, k, 0, 0)
                    for l in range(len(a.axis(3))):
                        a.fill(i, j, k, l, 0)
                        for m in range(len(a.axis(4))):
                            a.fill(i, j, k, l, m * 0.5 * pi)

        io = BytesIO()
        pickle.dump(a, io)
        io.seek(0)
        b = pickle.load(io)
        self.assertNotEqual(id(a), id(b))
        self.assertEqual(a.dim, b.dim)
        self.assertEqual(a.axis(0), b.axis(0))
        self.assertEqual(a.axis(1), b.axis(1))
        self.assertEqual(a.axis(2), b.axis(2))
        self.assertEqual(a.axis(3), b.axis(3))
        self.assertEqual(a.axis(4), b.axis(4))
        self.assertEqual(a.sum, b.sum)
        self.assertEqual(a, b)
Esempio n. 9
0
def process(path):
    basename = os.path.basename(path)
    
    from nslice.Run import Run
    run = Run(path)
    print "instrument=%s, Ei=%s, psi=%s" % (
        run.instrument, run.Ei, run.psi)

    from nslice.XtalOrientation import XtalOrientation
    a = b = 8.87;  c = 5.2
    from math import pi
    twopi = 2*pi
    ra,rb,rc = [twopi/a, 0,0], [0,twopi/b,0], [0,0,twopi/c]
    u,v = [1,0,0], [0,1,0]
    xtal_ori = XtalOrientation(ra,rb,rc, u,v, run.psi)

    h,k,l,E = run.compute_hklE(xtal_ori)
    I, error = run.read_data()
    h.shape = k.shape = l.shape = E.shape = I.shape = error.shape = -1
    hklEIE = np.vstack((h,k,l,E,I,error))
    
    from nslice.slice import slice_hE
    H, edges = slice_hE(
        hklEIE, 
        k=[0.95,1.05], l=[-1,1], 
        h=(-1, 6, 0.02), E=(-5, 10, 0.1),
        )
    import histogram, histogram.hdf as hh
    axes = [
        histogram.axis('h', boundaries=edges[0]),
        histogram.axis('E', unit='meV', boundaries=edges[1]),
        ]
    h = histogram.histogram('I(h,E)', axes=axes, data=H)
    hh.dump(h, 'I_hE.h5')
    return
 def __init__(self, nAtoms, dimension,
              Qaxes, eps_npyarr, E_npyarr,
              dos = None):
     '''
 natoms: number of atoms in the unit cell
 dimension: 
 Qaxes: a tuple of Q axes. Each item is a 2-tuple of (q, n).
   q vector is the direction of the Q axis.
   n is the number of points along this Q axis. 
   Example: [ ( (2.2,0,0), 20), ((0,3.1,0), 20), ((0,0,1.4), 20) ] for a 3d dispersion
 eps_npyarr: numpy array of poloarization. shape  must be (3d case)
   nQx, nQy, nQz, nBranches, nAtoms, 3, 2 
 E_npyarr: numpy array of phonon energy. shape  must be (3d case)
   nQx, nQy, nQz, nBranches
   dos: a 2-tuple of E,Z
 '''
     base.__init__(self, nAtoms, dimension)
     assert len(Qaxes) == dimension
     self.Qaxes = Qaxes
     self.eps_npyarr = eps_npyarr
     self.E_npyarr = E_npyarr
     from histogram import histogram
     if dos:
         e,Z = dos
         from .utils import nice_dos
         e, Z = nice_dos(e, Z)
         self.dos = histogram( 'dos', [ ('energy', e, 'meV') ], data = Z )
         pass
     return
Esempio n. 11
0
    def test_numpy_conversion_2(self):
        a = histogram(integer(0, 2, uoflow=False),
                      integer(0, 3, uoflow=False),
                      integer(0, 4, uoflow=False))
        r = numpy.zeros((2, 3, 4), dtype=numpy.int8)
        for i in range(len(a.axis(0))):
            for j in range(len(a.axis(1))):
                for k in range(len(a.axis(2))):
                    for m in range(i+j+k):
                        a.fill(i, j, k)
                    r[i,j,k] = i+j+k

        d = numpy.zeros((2, 3, 4), dtype=numpy.int8)
        for i in range(len(a.axis(0))):
            for j in range(len(a.axis(1))):
                for k in range(len(a.axis(2))):
                    d[i,j,k] = a.value(i,j,k)

        self.assertTrue(numpy.all(d == r))

        c = numpy.array(a) # a copy
        v = numpy.asarray(a) # a view

        self.assertTrue(numpy.all(c == r))
        self.assertTrue(numpy.all(v == r))
Esempio n. 12
0
def word_generator():
    my_file = open("./test2.txt", "r")
    lines = my_file.readlines()
    my_histogram = histogram(lines)

    word = weighted_sample(my_histogram)
    return word
Esempio n. 13
0
 def load_mcvine_psf_qE(self, adjust_energy_center=False):
     path = self.sim_path
     qaxis = self.qaxis; Eaxis = self.Eaxis
     dEs = np.load(os.path.join(path, 'dEs.npy'))
     dqs = np.load(os.path.join(path, 'dxs.npy'))
     probs = np.load(os.path.join(path, 'probs.npy'))
     hist, qedges, Eedges = np.histogram2d(dqs, dEs, bins=(np.arange(*qaxis), np.arange(*Eaxis)), weights=probs)
     # remove outliers
     median = np.median(hist[hist>0])
     normal = hist[hist<median*100]; normal_max = np.max(normal)
     hist[hist>median*100] = normal_max
     # adjust energy center
     if adjust_energy_center:
         hist_E = hist.sum(axis=0)
         Ebincenters = (Eedges[1:] + Eedges[:-1])/2
         Ecenter = (Ebincenters*hist_E).sum()/hist_E.sum()
         Eedges -= Ecenter
     # res(q, E) histogram
     qaxis = H.axis('q', boundaries=qedges)
     Eaxis = H.axis('E', boundaries=Eedges)
     self.mcvine_psf_qE = reshist  = H.histogram('res', (qaxis, Eaxis), data=hist)
     self.qEgrids = np.meshgrid(reshist.q, reshist.E)
     # res(E) histogram
     self.mcvine_psf_E = reshist.sum('q')
     # res(q) histogram
     Emin = Eedges[0]; Emax = Eedges[-1]; middle = (Emin+Emax)/2; Erange = Emax-Emin
     self.mcvine_psf_q = reshist[(), (middle-Erange/20, middle+Erange/20)].sum('E')
     return
Esempio n. 14
0
def sqehist(E, g, **kwds):
    "a simple wrapper of method sqe to return a histogram"
    Q,E,S = sqe(E,g, **kwds)
    import histogram as H
    Qaxis = H.axis('Q', Q, '1./angstrom')
    Eaxis = H.axis('E', E, 'meV')
    return H.histogram("SP SQE", [Qaxis, Eaxis], S)
Esempio n. 15
0
def makeSlice(events, xaxis, Eaxis, Qtox):
    """convert input events into a histogram (slice)
    
    Qtox:
    convert Q to x. also return a mask. since some Q points should be
    discarded
    """
    import numpy as np
    data = np.array(list(events))
    # convert Q to x
    Q = data[:, :3]
    x,mask = Qtox(Q)
    E = data[mask, 3]
    sample = np.vstack((x, E)).T
    #
    xbins = np.arange(*xaxis)
    Ebins = np.arange(*Eaxis)
    bins = xbins, Ebins
    weights = data[mask, -1]
    I, edges = np.histogramdd(sample, bins=bins, weights=weights)
    import histogram as H, histogram.hdf as hh
    axes = [
        H.axis('x', boundaries=edges[0]),
        H.axis('E', boundaries=edges[1]),
        ]
    return H.histogram('IxE', axes=axes, data = I)
Esempio n. 16
0
    def test_pickle_1(self):
        a = histogram(category(0, 1, 2),
                       integer(0, 3, label='ia'),
                       regular(4, 0.0, 4.0, uoflow=False),
                       variable(0.0, 1.0, 2.0))
        for i in range(len(a.axis(0))):
            a.fill(i, 0, 0, 0, weight=3)
            for j in range(len(a.axis(1))):
                a.fill(i, j, 0, 0, weight=10)
                for k in range(len(a.axis(2))):
                    a.fill(i, j, k, 0, weight=2)
                    for l in range(len(a.axis(3))):
                        a.fill(i, j, k, l, weight=5)

        io = BytesIO()
        pickle.dump(a, io)
        io.seek(0)
        b = pickle.load(io)
        self.assertNotEqual(id(a), id(b))
        self.assertEqual(a.dim, b.dim)
        self.assertEqual(a.axis(0), b.axis(0))
        self.assertEqual(a.axis(1), b.axis(1))
        self.assertEqual(a.axis(2), b.axis(2))
        self.assertEqual(a.axis(3), b.axis(3))
        self.assertEqual(a.sum, b.sum)
        self.assertEqual(a, b)
Esempio n. 17
0
    def test_numpy_conversion_0(self):
        a = histogram(integer(0, 3, uoflow=False))
        for i in range(10):
            a.fill(1)
        a.fill(1, count=90)
        c = numpy.array(a) # a copy
        v = numpy.asarray(a) # a view

        for t in (c, v):
            self.assertEqual(t.dtype, numpy.uint8)
            self.assertTrue(numpy.all(t == numpy.array((0, 100, 0))))

        for i in range(20):
            a.fill(1)
        a.fill(1, count=2 * numpy.ones(40, dtype=numpy.uint32))
        # copy does not change, but view does
        self.assertTrue(numpy.all(c == numpy.array((0, 100, 0))))
        self.assertTrue(numpy.all(v == numpy.array((0, 200, 0))))

        a.fill(1, count=100)
        c = numpy.array(a)
        self.assertEqual(c.dtype, numpy.uint16)
        self.assertTrue(numpy.all(c == numpy.array((0, 300, 0))))
        # view does not follow underlying switch in word size
        self.assertFalse(numpy.all(c == v))
Esempio n. 18
0
def resample( hist1, size ):

    import histogram as H
    
    assert hist1.dimension() == 1
    assert hist1.shape()[0] > display_size*10
    xaxis = hist1.axes()[0]
    xbb = xaxis.binBoundaries().asNumarray()
    front, back = xbb[0], xbb[-1]
    step = (back-front)/size
    newxbb = H.arange( front, back+step/2, step)
    newxaxis = H.axis( xaxis.name(), boundaries = newxbb, unit = xaxis.unit() )

    newhist = H.histogram(
        hist1.name(),
        [ newxaxis ] )

    newxc = newxaxis.binCenters()
    for x in newxc[1:-1] :
        newhist[ x ] = hist1[ (x-step/2, x+step/2) ].sum()
        continue

    newhist[ newxc[0] ]= hist1[ (newxbb[0], newxc[0] + step/2) ].sum()
    newhist[ newxc[-1] ]= hist1[ (newxc[-1]-step/2, newxbb[-1]-step*1.e-10) ].sum()

    return newhist
Esempio n. 19
0
def slice2hist(ifile, ofile):
    import histogram as H, histogram.hdf as hh
    from mantid import simpleapi as msa

    def eliminateUnitDimension(shape):
        for d in shape:
            if d>1: yield d
        return

    ws = msa.Load(ifile)
    I = ws.getSignalArray()
    I.shape = tuple(eliminateUnitDimension(I.shape))
    E2 = ws.getErrorSquaredArray()
    E2.shape = I.shape
    axes = []
    for i in range(ws.getNumDims()):
        dim = ws.getDimension(i)
        if dim.getNBins() > 1:
            axis = H.axis(
                dim.getName(), 
                unit="1",
                centers = [dim.getX(ind) for ind in range(dim.getNBins())]
                )
            axes.append(axis)
        continue
    h = H.histogram("slice", axes, data=I, errors=E2)
    hh.dump(h, ofile)
    return
Esempio n. 20
0
def get_histogram(monitor):
    from mcstas2.utils.carray import bpptr2npyarr
    core = monitor.core()

    nx = core.nx
    ny = core.ny
    assert nx == int(nx)
    nx = int(nx)
    assert ny == int(ny)
    ny = int(ny)
    n = nx * ny
    shape = nx, ny

    Iarr = bpptr2npyarr(core.getPSD_p_00(), 'double', n).copy()
    E2arr = bpptr2npyarr(core.getPSD_p2_00(), 'double', n).copy()
    Iarr.shape = E2arr.shape = shape

    from histogram import histogram, axis, arange
    dx = 360. / nx
    xaxis = axis('x', arange(0, 360, dx), unit='deg')

    dy = 180. / ny
    yaxis = axis('y', arange(-90, 90, dy), unit='deg')

    h = histogram('I(x,y)', [xaxis, yaxis], data=Iarr, errors=E2arr)
    return h
Esempio n. 21
0
def get_histogram( monitor ):
    from mcstas2.utils.carray import bpptr2npyarr
    core = monitor.core()

    xmin = 0; xmax = 1
    ymin = 0; ymax = 1
    dx = 0.1
    dy = 0.1


#    xmin = core.xmin; xmax = core.xmax
#    ymin = core.ymin; ymax = core.ymax
#    dx = xmax - xmin
#    dy = ymax - ymin

    # OUTPUT PARAMETERS (DEFS, Vars)
    #Iarr = bpptr2npyarr( core.getDEFS( ), 'double', n ).copy()  #?
    #E2arr = bpptr2npyarr( core.getPSD_p2_00( ), 'double', n ).copy()
    #Iarr.shape = E2arr.shape = shape

    from histogram import histogram, axis, arange
    xaxis = axis( 'x', arange( xmin, xmax, dx ) )
    yaxis = axis( 'y', arange( ymin, ymax, dy ) )

    h = histogram( 'I(x,y)', [xaxis,yaxis], data = [2,yaxis], errors=[2,yaxis])    #Iarr)    #, errors = E2arr )
    return h
Esempio n. 22
0
 def getHistogram(self, scale_factor=1.):
     import histogram as H
     axes = [('x', self.x_centers, 'm'), ('y', self.y_centers, 'm')]
     return H.histogram(
         'Iyx', axes,
         data=self.out_p*scale_factor,
         errors=self.out_p2*scale_factor*scale_factor)
Esempio n. 23
0
def get_histogram( monitor ):
    from mcstas2.utils.carray import bpptr2npyarr
    core = monitor.core()

    nx = core.nx; ny =core.ny
    n = nx * ny
    shape = nx, ny

#    xmin = core.x_min; xmax = core.x_max
#    ymin = core.y_min; ymax = core.y_max

    xmin = attr(core, "xmin", "x_min");
    xmax = attr(core, "xmax", "x_max");
    ymin = attr(core, "ymin", "y_min");
    ymax = attr(core, "ymax", "y_max");

    dx = (xmax - xmin)/nx
    dy = (ymax - ymin)/ny

    Iarr = bpptr2npyarr( core.getPSD_p_00( ), 'double', n ).copy()
    E2arr = bpptr2npyarr( core.getPSD_p2_00( ), 'double', n ).copy()
    Iarr.shape = E2arr.shape = shape

    from histogram import histogram, axis, arange
    xaxis = axis( 'x', arange( xmin, xmax, dx ) )
    yaxis = axis( 'y', arange( ymin, ymax, dy ) )

    h = histogram( 'I(x,y)', [xaxis,yaxis], data = Iarr, errors = E2arr )
    return h
Esempio n. 24
0
def histplot(data, ax=None, bins=10, **kwargs):
    """
    Histogram plot

    Parameters
    ----------
    data : array_like
        data
    ax : Axes, optional
        Axes object. Default is None.

    Returns
    -------
    ax : Axes
        The new Axes object
    """
    if ax is None:
        ax = plt.gca()
    counts, bins = histogram(data, bins=bins)
    ax.hist(bins[:-1],
            bins=bins,
            weights=counts,
            histtype='bar',
            edgecolor=None,
            color='steelblue',
            alpha=0.9,
            **kwargs)
def get_histogram( monitor ):
    from mcstas2.utils.carray import bpptr2npyarr
    core = monitor.core()

    nx = core.nxchan; ny =core.nychan; nb = core.nbchan
    n = nx * ny * nb
    shape = nx, ny, nb

    xmin = -core.xwidth/2; xmax = core.xwidth/2
    ymin = -core.yheight/2; ymax = core.yheight/2
    dx = (xmax - xmin)/nx
    dy = (ymax - ymin)/ny

    if core.bmax!=0:
        bmax=core.bmax
        bmin=core.bmin
        db=(bmax-bmin)/nb
    else :
        db = core.deltab
        bmin=0;
        bmax=nb*db+bmin

    Iarr = bpptr2npyarr( core.getTOF_p_00( ), 'double', n ).copy()
    E2arr = bpptr2npyarr( core.getTOF_p2_00( ), 'double', n ).copy()
    Iarr.shape = E2arr.shape = shape

    from histogram import histogram, axis, arange
    xaxis = axis( 'x', boundaries=arange( xmin, xmax+dx/10, dx ) )
    yaxis = axis( 'y', boundaries=arange( ymin, ymax+dy/10, dy ) )
    baxis = axis( 'b', boundaries=arange( bmin, bmax+db/10, db ) )

    h = histogram(
        'I(x,y,b)', [xaxis,yaxis,baxis],
        data = Iarr, errors = E2arr )
    return h
Esempio n. 26
0
def resample(hist1, size):

    import histogram as H

    assert hist1.dimension() == 1
    assert hist1.shape()[0] > display_size * 10
    xaxis = hist1.axes()[0]
    xbb = xaxis.binBoundaries().asNumarray()
    front, back = xbb[0], xbb[-1]
    step = 1. * (back - front) / size
    newxbb = H.arange(front, back + step / 2., step)
    newxaxis = H.axis(xaxis.name(), boundaries=newxbb, unit=xaxis.unit())

    newhist = H.histogram(hist1.name(), [newxaxis])

    newxc = newxaxis.binCenters()
    for x in newxc[1:-1]:
        newhist[x] = hist1[(x - step / 2., x + step / 2.)].sum()
        continue

    newhist[newxc[0]] = hist1[(newxbb[0], newxc[0] + step / 2.)].sum()
    newhist[newxc[-1]] = hist1[(newxc[-1] - step / 2.,
                                newxbb[-1] - step * 1.e-10)].sum()

    return newhist
 def __init__(self,
              nAtoms,
              dimension,
              Qaxes,
              eps_npyarr,
              E_npyarr,
              dos=None):
     '''
 natoms: number of atoms in the unit cell
 dimension: 
 Qaxes: a tuple of Q axes. Each item is a 2-tuple of (q, n).
   q vector is the direction of the Q axis.
   n is the number of points along this Q axis. 
   Example: [ ( (2.2,0,0), 20), ((0,3.1,0), 20), ((0,0,1.4), 20) ] for a 3d dispersion
 eps_npyarr: numpy array of poloarization. shape  must be (3d case)
   nQx, nQy, nQz, nBranches, nAtoms, 3, 2 
 E_npyarr: numpy array of phonon energy. shape  must be (3d case)
   nQx, nQy, nQz, nBranches
   dos: a 2-tuple of E,Z
 '''
     base.__init__(self, nAtoms, dimension)
     assert len(Qaxes) == dimension
     self.Qaxes = Qaxes
     self.eps_npyarr = eps_npyarr
     self.E_npyarr = E_npyarr
     from histogram import histogram
     if dos:
         e, Z = dos
         from .utils import nice_dos
         e, Z = nice_dos(e, Z)
         self.dos = histogram('dos', [('energy', e, 'meV')], data=Z)
         pass
     return
Esempio n. 28
0
def get_histogram(monitor):
    from mcstas2.utils.carray import bpptr2npyarr
    core = monitor.core()

    xmin = 0
    xmax = 1
    ymin = 0
    ymax = 1
    dx = 0.1
    dy = 0.1

    #    xmin = core.xmin; xmax = core.xmax
    #    ymin = core.ymin; ymax = core.ymax
    #    dx = xmax - xmin
    #    dy = ymax - ymin

    # OUTPUT PARAMETERS (DEFS, Vars)
    #Iarr = bpptr2npyarr( core.getDEFS( ), 'double', n ).copy()  #?
    #E2arr = bpptr2npyarr( core.getPSD_p2_00( ), 'double', n ).copy()
    #Iarr.shape = E2arr.shape = shape

    from histogram import histogram, axis, arange
    xaxis = axis('x', arange(xmin, xmax, dx))
    yaxis = axis('y', arange(ymin, ymax, dy))

    h = histogram('I(x,y)', [xaxis, yaxis], data=[2, yaxis],
                  errors=[2, yaxis])  #Iarr)    #, errors = E2arr )
    return h
Esempio n. 29
0
def compare_6d(n, distrib):
    if distrib == 0:
        r = np.random.rand(6 * n)
    else:
        r = 0.3 * np.random.randn(6 * n)
    r = r.reshape(n, 6)

    best_numpy = float("infinity")
    best_boost = float("infinity")
    for k in xrange(10):
        t = timer()
        w, xe = np.histogramdd(r,
                               bins=(10, 10, 10, 10, 10, 10),
                               range=((0.0, 1.0), (0.0, 1.0), (0.0, 1.0),
                                      (0.0, 1.0), (0.0, 1.0), (0.0, 1.0)))
        t = timer() - t
        best_numpy = min(t, best_numpy)

        h = histogram(regular_axis(10, 0, 1), regular_axis(10, 0, 1),
                      regular_axis(10, 0, 1), regular_axis(10, 0, 1),
                      regular_axis(10, 0, 1), regular_axis(10, 0, 1))
        t = timer()
        h.fill(r)
        t = timer() - t
        best_boost = min(t, best_boost)

    print "6D"
    print "t[numpy] = %.3f" % best_numpy
    print "t[boost] = %.3f" % best_boost
Esempio n. 30
0
def hist_mcs_sum(outdir, histogramfilename):
    """compute the summed histogram and summed number of mc samples"""
    import glob, os
    pattern = os.path.join(outdir, '*', histogramfilename)
    histfiles = glob.glob(pattern)
    pattern = os.path.join(outdir, '*', number_mc_samples_filename)
    mcsamplesfiles = glob.glob(pattern)
    assert len(histfiles) == len(mcsamplesfiles), "histogram files %s does not match #mcsample files %s" %(len(histfiles), len(mcsamplesfiles))
    if not histfiles:
        return None, None
    
    # load histograms
    from histogram.hdf import load
    from histogram.hdf.utils import getOnlyEntry
    loadhist = lambda f: load(f, getOnlyEntry(f))
    h1 = loadhist(histfiles[0])

    # XXX
    # due to a bug in h5py, we have to manually make a copy of the histogram
    # see http://code.google.com/p/h5py/issues/detail?id=121
    import histogram
    h1 = histogram.histogram(h1.name(), h1.axes(), h1.I, h1.E2)
    # XXX

    def _addhistfile(f):
        h = loadhist(f)
        h1.I += h.I
        h1.E2 += h.E2
        return
    map(_addhistfile, histfiles[1:])

    # load number_of_mc_samples
    loadmcs = lambda f: float(open(f).read())
    mcs = map(loadmcs, mcsamplesfiles)
    return h1, sum(mcs)
Esempio n. 31
0
def results_histogram(outcomes):
    #storing outcomes and freq in this empty dict
    results = {}

    for word in outcomes:
        results = histogram(outcomes)
    return results
def most_frequent(doc, typef='char', to_limit=False):
    """
        Sorts the typef in doc in reverse order of frequency.
    """
    if typef == 'char':
        hist = histogram.histogram(doc)

    elif typef == 'word':
        hist = word_histogram(doc)

    t = []
    for x, freq in hist.iteritems():
        t.append((freq, x))

    t.sort(reverse=True)

    result = []
    #list of tuples
    limit = 0  # to limit only 20 most frequent words
    tots = total_freq(t)

    for freq, x in t:

        #if x!=' ':
        # space is not a char
        percentage = get_percent_in2points(freq, tots)
        result.append((x, freq, percentage))

        if typef == 'word' and to_limit == False:
            limit += 1
            if limit == 20:
                break

    return result, tots
Esempio n. 33
0
 def testTwoNumbers(self):
     
     out = histogram([1,2])
     self.assertEqual(out[0,0], 2)
     self.assertEqual(out[0,1], 0.5)  
     self.assertEqual(out[1,0], 4)
     self.assertEqual(out[1,1], 0.25)  
Esempio n. 34
0
    def setParameters(
        self,
        ARCSxml, d_params,
        emission_time):
        
        info.log( 'd_params (unit: AA) = %s' % (d_params, ) )
        info.log( 'emission_time (unit: microsecond) = %s' % (emission_time, ) )
    
        infos = self._readInstrumentInfo(ARCSxml)
        npacks, ndetsperpack, npixelsperdet = infos[
            'detector-system-dimensions']
        mod2sample = infos['moderator-sample distance']
        pixelPositionsFilename = infos[
            'pixelID-position mapping binary file']
    
        d_begin, d_end, d_step = d_params # angstrom

        d_axis = histogram.axis('d spacing', boundaries = histogram.arange(
                d_begin, d_end, d_step) )
        detaxes = infos['detector axes']
        h = histogram.histogram(
            'I(pdpd)',
            detaxes + [d_axis],
            data_type = 'int',
            )

        info.log( "reading pixelID->position map..." )
        pixelPositions = arcseventdata.readpixelpositions(
            pixelPositionsFilename, npacks, ndetsperpack, npixelsperdet)

        self.out_histogram = h
        self.pixelPositions = pixelPositions
        self.mod2sample = mod2sample
        self.emission_time = emission_time
        return
Esempio n. 35
0
def compare_2d(n, distrib):
    if distrib == 0:
        r = np.random.rand(n)
    else:
        r = 0.5 + 0.3 * np.random.randn(n)
    r = r.reshape(2, n / 2)

    best_numpy = float("infinity")
    best_boost = float("infinity")
    for k in xrange(50):
        t = timer()
        w, xe, ye = np.histogram2d(r[0],
                                   r[1],
                                   bins=(100, 100),
                                   range=((0.0, 1.0), (0.0, 1.0)))
        t = timer() - t
        best_numpy = min(t, best_numpy)

        h = histogram(regular(100, 0, 1), regular(100, 0, 1))
        t = timer()
        h.fill(r[0], r[1])
        t = timer() - t
        best_boost = min(t, best_boost)
    assert (np.all(w == np.array(h)[:-2, :-2]))

    print "py:numpy %.3f" % best_numpy
    print "py:hd_sd %.3f" % best_boost
Esempio n. 36
0
def descriptor(imagem, desc, list_of_parameters):
    """
    Function to compute feature vector for an image
    
    Parameters
    ----------
    imagem : numpy array
        RGB or grayscale image
    desc : string
        descriptor to compute the features
    list_of_parameters : numpy array or list
        parameters of each descriptor, can be different sizes depending the descriptor
        
    Returns
    -------
    features : numpy array
        the feature vector computed by the descriptor 'desc'
    """
    if desc == 'glcm':
        features = glcm.glcm(imagem, [], int(list_of_parameters[1]),
                             int(list_of_parameters[0]))
    elif desc == 'fotf':
        features = histogram.histogram(imagem, [])
    elif desc == 'lbp':
        features = lbp.lbpTexture(imagem, [], 8 * int(list_of_parameters[0]),
                                  int(list_of_parameters[0]))
    elif desc == 'hog':
        features = hog_rom.HOG(imagem, [], int(list_of_parameters[0]),
                               int(list_of_parameters[1]))

    return features[0]
Esempio n. 37
0
def coocorrencia(image, d=1, theta=0):

    #image=imread(img, as_grey=True)
    h = hist.histogram(image.astype('int32'))
    Ntonscinza = len(h)

    descritoresMC = np.zeros((1, 4)).astype('double')
    #greycomatrix(imagem, d, theta, niveis de cinza da imagem)
    # g = greycomatrix(image, d, theta, Ntonscinza,normed=True,symmetric=True)

    g = greycomatrix(image, [d], [theta], Ntonscinza)

    g[0, :] = 0
    g[:, 0] = 0

    # Para mostrar a matriz de coocorrencia
    # print g[:,:,0,0]

    # somat =  (np.sum(g)).astype('double')
    #
    # #Matriz de coocorrencia normalizada
    # gn = (g/somat).astype('double')

    # descritoresMC[0][0] = greycoprops(g, 'ASM')
    descritoresMC[0][0] = np.round(greycoprops(g, 'energy'), 2)
    descritoresMC[0][1] = np.round(greycoprops(g, 'contrast'), 2)
    descritoresMC[0][2] = np.round(greycoprops(g, 'homogeneity'), 2)
    # descritoresMC[0][4] = greycoprops(g, 'correlation')
    descritoresMC[0][3] = np.round(greycoprops(g, 'dissimilarity'), 2)

    #return g[:,:,0,0], gn[:,:,0,0], descritoresMC
    return descritoresMC
Esempio n. 38
0
def makeSlice(events, xaxis, Eaxis, Qtox):
    """convert input events into a histogram (slice)
    
    Qtox:
    convert Q to x. also return a mask. since some Q points should be
    discarded
    """
    import numpy as np
    data = np.array(list(events))
    # convert Q to x
    Q = data[:, :3]
    x, mask = Qtox(Q)
    E = data[mask, 3]
    sample = np.vstack((x, E)).T
    #
    xbins = np.arange(*xaxis)
    Ebins = np.arange(*Eaxis)
    bins = xbins, Ebins
    weights = data[mask, -1]
    I, edges = np.histogramdd(sample, bins=bins, weights=weights)
    import histogram as H, histogram.hdf as hh
    axes = [
        H.axis('x', boundaries=edges[0]),
        H.axis('E', boundaries=edges[1]),
    ]
    return H.histogram('IxE', axes=axes, data=I)
Esempio n. 39
0
 def getHistogram(self, scale_factor=1.):
     import histogram as H
     axes = [('wavelength', self.L_centers, 'angstrom')]
     return H.histogram(
         'I(wavelength)', axes,
         data=self.out_p*scale_factor,
         errors=self.out_p2*scale_factor*scale_factor)
Esempio n. 40
0
def get_histogram(monitor):
    from mcstas2.utils.carray import bpptr2npyarr
    core = monitor.core()

    nx = core.nx
    ny = core.ny
    n = nx * ny
    shape = nx, ny

    #    xmin = core.x_min; xmax = core.x_max
    #    ymin = core.y_min; ymax = core.y_max

    xmin = attr(core, "xmin", "x_min")
    xmax = attr(core, "xmax", "x_max")
    ymin = attr(core, "ymin", "y_min")
    ymax = attr(core, "ymax", "y_max")

    dx = (xmax - xmin) / nx
    dy = (ymax - ymin) / ny

    Iarr = bpptr2npyarr(core.getPSD_p_00(), 'double', n).copy()
    E2arr = bpptr2npyarr(core.getPSD_p2_00(), 'double', n).copy()
    Iarr.shape = E2arr.shape = shape

    from histogram import histogram, axis, arange
    xaxis = axis('x', arange(xmin, xmax, dx))
    yaxis = axis('y', arange(ymin, ymax, dy))

    h = histogram('I(x,y)', [xaxis, yaxis], data=Iarr, errors=E2arr)
    return h
Esempio n. 41
0
def sample(text):

    # converts text to histogram
    histo = histogram(text)
    # print("histogram: ")
    # print(histogram)

    # returns number of tokens in histogram
    # tokens = 0
    # for word in histo:
    #     tokens += histo[word]
    tokens = unique_words(histo)
    # print("tokens: ")
    # print(tokens)

    # cumulative_probability
    cum_prob = 0

    # random number
    ranum = random.uniform(0, 1)
    # print("random number: ")
    # print(ranum)

    # randomly picks one word based on word frequency
    for word in histo:
        cum_prob += (float(histo[word]) / float(tokens))
        # print("cumulative prob: ")
        # print(cum_prob)
        if cum_prob >= ranum:
            return word
Esempio n. 42
0
def compare_3d(n, distrib):
    if distrib == 0:
        r = np.random.rand(3 * n)
    else:
        r = 0.3 * np.random.randn(3 * n)
    r = r.reshape(n, 3)

    best_numpy = float("infinity")
    best_boost = float("infinity")
    for k in xrange(10):
        t = timer()
        w, xe = np.histogramdd(r, bins=(100, 100, 100),
                               range=((0.0, 1.0),
                                      (0.0, 1.0),
                                      (0.0, 1.0)))
        t = timer() - t
        best_numpy = min(t, best_numpy)

        h = histogram(regular_axis(100, 0, 1),
                      regular_axis(100, 0, 1),
                      regular_axis(100, 0, 1))
        t = timer()
        h.fill(r)
        t = timer() - t
        best_boost = min(t, best_boost)

    print "3D"
    print "t[numpy] = %.3f" % best_numpy
    print "t[boost] = %.3f" % best_boost
Esempio n. 43
0
def events2Idpt( events, instrument, tofparams ):
    from mccomponents.detector.utils import \
         getDetectorHierarchyDimensions
    dims = getDetectorHierarchyDimensions( instrument )
    # 1st attempt to create axes
    from histogram import histogram, axis, arange
    axes = [ axis('%sID' % name.lower(), range(n)) for name, n in dims ]

    # the first level of detectors (tubes, packs, or others) need
    # special attention. ids of that level could be not continuous.
    detectorSystem = instrument.getDetectorSystem()
    assert len(detectorSystem.elements()) == dims[0][1]
    ids = [ element.id() for element in detectorSystem.elements() ]
    axes[0] = axis( axes[0].name(), ids )

    detaxes = axes

    #tof axis
    tmin, tmax, tstep = tofparams
    tofaxis = axis( 'tof', boundaries = arange( tmin, tmax+tstep/10., tstep ), unit = 'second' )

    #all axes
    axes = detaxes + [tofaxis]

    #histogram
    hist = histogram( 'Idpt', axes )

    #get the numpy array which will accept events
    npixels = hist.size()/tofaxis.size()
    Ipixtof = hist.data().storage().asNumarray()
    Ipixtof.shape = npixels, -1
    events2Ipixtof( events, Ipixtof )

    return hist
Esempio n. 44
0
    def computeVolume(self, paths=None, **kwds):
        H = None
        sa = None
        edges = None
        for path in paths or self.paths:
            print path
            edges1, H1, sa1 = self.computeVolumeForOneRun(path, **kwds)
            if H is None:
                edges, H, sa = edges1, H1, sa1
            else:
                H += H1
                sa += sa1
            continue

        import histogram
        axes = [
            histogram.axis(kwds['x'], boundaries=edges[0]),
            histogram.axis(kwds['y'], boundaries=edges[1]),
            histogram.axis(kwds['z'], boundaries=edges[2]),
        ]
        return histogram.histogram(
            'I(%(x)s,%(y)s,%(z)s)' % kwds,
            axes=axes,
            data=H / sa,
        )
    def test3(self):
        'LinearlyInterpolatedDispersion_3D'
        import mccomponents.sample.phonon.bindings as bindings
        b = bindings.get('BoostPython')
        
        nAtoms = 2
        nBranches = 3 * nAtoms
        nQx = 10; nQy = 12; nQz = 14
        Qaxes = [ ([1,1,0], nQx),
                  ([1,0,1], nQy),
                  ([0,1,1], nQz),
                  ]

        import histogram as H
        qx = H.axis('qx', H.arange(0, 1+1e-10, 1./(nQx-1)))
        qy = H.axis('qy', H.arange(0, 1+1e-10, 1./(nQy-1)))
        qz = H.axis('qz', H.arange(0, 1+1e-10, 1./(nQz-1)))
        br = H.axis('branchId', range(nBranches))
        atoms = H.axis('atomId', range(nAtoms))
        pols = H.axis('polId', range(3))
        realimags = H.axis('realimagId', range(2))
        
        eps = H.histogram(
            'eps', [qx,qy,qz,br,atoms,pols,realimags],
            fromfunction = lambda qx,qy,qz,br,atom,pol,realimag: qx+qy+qz+br+atom+pol+realimag)
        e = H.histogram(
            'e', [qx,qy,qz,br],
            fromfunction = lambda qx,qy,qz,br: qx+qy+qz+br)

        disp = b.linearlyinterpolateddispersion_3d(nAtoms, Qaxes, eps.I, e.I)

        Q = b.vector3
        # XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
        # these tests failed because we are not doing interpolation
        # right now since we don't have a good way to do 
        # it for the polarization vectors.
        # right now we just take the value of the closest vertex.
        checkDiff = self.assertAlmostEqual
        def checkDiff(a, b):
            print a,b
        checkDiff(disp.energy(0, Q(0,0,0)), 0)
        checkDiff(disp.energy(0, Q(1-1e-5,1-1e-5,0)), 1-1e-5)
        checkDiff(disp.energy(0, Q(0.5,0.5,0)), 0.5)
        checkDiff(disp.energy(0, Q(0.5,0,0.5)), 0.5)
        checkDiff(disp.energy(0, Q(0,0.5,0.5)), 0.5)
        checkDiff(disp.energy(0, Q(0.5,0.5,1-1e-10)), 1)
        return
Esempio n. 46
0
def e2Id(
    events, n, pixelpositions, 
    dspacing_params = None,
    Idspacing = None,
    preNeXus_tofUnit = 1.e-7, mod2sample = 13.5):
    '''e2Id(events, n, pixelpositions, Idspacing = None,
    dspacing_params = None,
    npacks = 115, ndetsperpack = 8, npixelsperdet = 128,
    preNeXus_tofUnit = 1.e-7, mod2sample = 13.5) --> integrate events to I(d spacing) histogram

    Either Idspacing or dspacing_params must be given

    Idspacing:
      histogram I(d spacing). If this is None, a histogram will be created
      using dspacing_params.
    dspacing_params:
      begin, end, and step of dspacing. unit: angstrom
      If Idspacing is given, this is ignored.
    events:
      neutron events
    n:
      number of neutron events to process
    pixelpositions:
      array of pixel positions
    preNeXus_tofUnit:
      unit of event.tof in the pre-Nexus data format. unit: second
    mod2sample:
      moderator-sample distance. unit: meter

    return:
      the I(d spacing) histogram
    '''
    

    if Idspacing is None:
        if dspacing_params is None:
            raise ValueError, "Must provide either the (min, max, step) of d axis or the I(d) histogram"
        dspacing_begin, dspacing_end, dspacing_step = dspacing_params # angstrom
        
        import histogram 
        daxis = histogram.axis(
            'd',
            boundaries = histogram.arange(dspacing_begin, dspacing_end, dspacing_step),
            unit = 'angstrom',
            )
        
        Idspacing = histogram.histogram(
            'I(d spacing)',
            [daxis],
            data_type = 'int',
            )

        pass

    events2Idspacing(
        events, n, Idspacing, pixelpositions,
        tofUnit = preNeXus_tofUnit, mod2sample = mod2sample)

    return Idspacing
def saveSQE(Q, E, S, name):
    h = H.histogram(
        name,
        [('Q', Q, 'angstrom**-1'),
         ('E', E, 'meV')],
        S)
    hh.dump(h, '%s.h5' % (name,))
    return
Esempio n. 48
0
def get_word(histogram):
    histo, corp_length = histogram()
    index = int(random.random() * corp_length)
    summed = 0
    for word, freq in histo:
        summed += freq
        if summed > index:
            return word
def test_sampler(hist):
    """ tests sampler by running it 10000 times, uncomment line 32 for this"""
    test_words = []
    for _ in range(10000):
        test_words.append(word_sampler(hist))
    test_hist = histogram(test_words)
    for item in test_hist.keys():
        print(f"{item}: {test_hist[item]}")
Esempio n. 50
0
def histogram_equalization(im, verbose=True):

    new_im = np.zeros_like(im)
    try:
        h, w, ch = im.shape
    except ValueError:
        h, w = im.shape
        ch = 1

    hist = histogram(im, verbose=False)[0]
    cum_hist = np.cumsum(hist)
    for j in range(h):
        for i in range(w):
            new_im[j, i] = cum_hist[im[j, i]] * 255

    if verbose:
        fig = plt.figure()
        ax11 = fig.add_subplot(2, 3, 1)
        ax12 = fig.add_subplot(2, 3, 2)
        ax13 = fig.add_subplot(2, 3, 3)
        ax21 = fig.add_subplot(2, 3, 4)
        ax22 = fig.add_subplot(2, 3, 5)
        ax23 = fig.add_subplot(2, 3, 6)

        ax11.plot(np.arange(0, 256, 1), hist)
        ax11.set_title("Prob Distribuition")
        ax12.plot(np.arange(0, 256, 1), cum_hist)
        ax12.set_title("Cumalative Prob Distribuition")
        ax13.imshow(im, cmap='gray')
        ax13.set_title("After")
        ax13.axis('off')

        new_hist = histogram(new_im, verbose=False)[0]
        new_cum_hist = np.cumsum(new_hist)

        ax21.plot(np.arange(0, 256, 1), new_hist)
        ax21.set_title("New Prob Distribuition")
        ax22.plot(np.arange(0, 256, 1), new_cum_hist)
        ax22.set_title("New CDF")
        ax23.imshow(new_im, cmap='gray')
        ax23.set_title("After")
        ax23.axis('off')

        plt.show()

    return new_im
Esempio n. 51
0
def jsonlisttest():
    searchimg = cv2.imread(
        'D:/python/histogram_test/searchimage/wahoo_test.jpg',
        cv2.IMREAD_COLOR)
    findlist_x = histogram.histogram(searchimg)
    lst = histogram.matching(searchimg, findlist_x)

    return jsonify(lst)
Esempio n. 52
0
def histogram_matching(im1, im2, verbose=True):

    try:
        h, w, ch = im1.shape
    except ValueError:
        h, w = im1.shape
        ch = 1

    hist1 = histogram(im1, verbose=False)[0]
    cum_hist1 = np.cumsum(hist1)
    hist2 = histogram(im2, verbose=False)[0]
    cum_hist2 = np.cumsum(hist2)

    if verbose:
        fig = plt.figure()
        ax11 = fig.add_subplot(3, 2, 1)
        ax12 = fig.add_subplot(3, 2, 2)
        ax21 = fig.add_subplot(3, 2, 3)
        ax22 = fig.add_subplot(3, 2, 4)
        ax31 = fig.add_subplot(3, 2, 5)
        ax32 = fig.add_subplot(3, 2, 6)

        ax11.plot(np.arange(0, 256, 1), cum_hist1)
        ax11.set_title("Original CDF")
        ax12.imshow(im1, cmap='gray')
        ax12.set_title("Original")
        ax12.axis('off')

        ax21.plot(np.arange(0, 256, 1), cum_hist2)
        ax21.set_title("Desired CDF")
        ax22.imshow(im2, cmap='gray')
        ax22.set_title("Second Image")
        ax22.axis('off')

        new_hist = histogram(new_im, verbose=False)[0]
        new_cum_hist = np.cumsum(new_hist)

        ax31.plot(np.arange(0, 256, 1), new_cum_hist)
        ax31.set_title("New CDF")
        ax32.imshow(new_im, cmap='gray')
        ax32.set_title("New Image")
        ax32.axis('off')

        plt.show()

    return new_im
Esempio n. 53
0
    def test_fill_with_numpy_array_1(self):
        ar = lambda *args: numpy.array(args, dtype=float)
        a = histogram(integer(0, 3, uoflow=True))
        v = ar(-1, 0, 1, 2, 3, 4)
        w = ar( 2, 3, 4, 5, 6, 7)
        a.fill(v, weight=w)
        a.fill((0, 1), weight=(2, 3))
        self.assertEqual(a.value(-1), 2)
        self.assertEqual(a.value(0), 5)
        self.assertEqual(a.value(1), 7)
        self.assertEqual(a.value(2), 5)
        self.assertEqual(a.variance(-1), 4)
        self.assertEqual(a.variance(0), 13)
        self.assertEqual(a.variance(1), 25)
        self.assertEqual(a.variance(2), 25)
        a.fill((1, 2), weight=1)
        a.fill(0, weight=(1, 2))
        self.assertEqual(a.value(0), 8)
        self.assertEqual(a.value(1), 8)
        self.assertEqual(a.value(2), 6)

        with self.assertRaises(RuntimeError):
            a.fill((1, 2), foo=(1, 1))
        with self.assertRaises(ValueError):
            a.fill((1, 2), weight=(1,))
        with self.assertRaises(ValueError):
            a.fill((1, 2), weight="ab")
        with self.assertRaises(RuntimeError):
            a.fill((1, 2), weight=(1, 1), foo=1)
        with self.assertRaises(ValueError):
            a.fill((1, 2), weight=([1, 1], [2, 2]))

        a = histogram(integer(0, 2, uoflow=False),
                      regular(2, 0, 2, uoflow=False))
        a.fill((-1, 0, 1), (-1, 1, 0.1))
        self.assertEqual(a.value(0, 0), 0)
        self.assertEqual(a.value(0, 1), 1)
        self.assertEqual(a.value(1, 0), 1)
        self.assertEqual(a.value(1, 1), 0)
        a = histogram(integer(0, 3, uoflow=False))
        a.fill((0, 0, 1, 2))
        a.fill((1, 0, 2, 2))
        self.assertEqual(a.value(0), 3)
        self.assertEqual(a.value(1), 2)
        self.assertEqual(a.value(2), 3)
Esempio n. 54
0
    def __init__(self, name, axes, size=[0.1, 0.1]):
        """
        axes: a list of axis
        axis: an instance of Axis class, or 
            (obsolete) a tuple of name, expression, bins, range 
        name: name of the axis
        expression: expression of the axis
        bins: number of bins of the axis
        ranges: (min, max) tuple for the axis
        """
        self.name = name
        expressions = self.expressions = []
        bins = self.bins = []
        ranges = self.ranges = []
        haxes = []
        self.xwidth     = size[0]
        self.yheight    = size[1]

        from histogram import histogram, axis as createAxis
        from numpy import histogramdd as hdd, arange

        # n='variable name', e='expression', b='number of bins (divisions)', r='range'
        for axis in axes:  
            if isinstance(axis, tuple):
                import warnings
                warnings.warn(
                    "It is obsolete to specify an axis using tuple. "
                    "Please use an Axis instance instead"
                    )
                n, e, b, r = axis
                unit = None
            else:
                n = axis.name
                e = axis.expression
                b = axis.bins
                r = axis.range
                unit = axis.unit
                
            # validation
            if len(r) != 2:
                raise ValueError, "Invalid range: %s. A range has to be a 2-tuple" % (r, )
            if r[0] >= r[1]:
                raise ValueError, "Invalid range: %s" % (r,)
            
            expressions.append(e)
            ranges.append(r)
            bins.append(b)
            db = (r[1]-r[0])/b
            a = createAxis(
                n, 
                boundaries=arange(r[0], r[1]+db/10., db), 
                unit=unit)
            haxes.append(a)
            continue
        self.histogram = histogram(self.name, haxes)
        return
Esempio n. 55
0
    def setParameters(
        self,
        Q_params, E_params, ARCSxml, Ei, emission_time):
        
        keys = [
            'detector-system-dimensions',
            'moderator-sample distance',
            'pixelID-position mapping binary file',
            'detector axes',
            'solidangles',
            ]
        infos = self._readInstrumentInfo(ARCSxml, keys=keys)
        npacks, ndetsperpack, npixelsperdet = infos[
            'detector-system-dimensions']
        mod2sample = infos['moderator-sample distance']
        pixelPositionsFilename = infos[
            'pixelID-position mapping binary file']
    
        self._debug.log( "pixel-positions-filename=%s" % pixelPositionsFilename)
        self._debug.log( 'E_params (unit: meV) = %s' % (E_params, ) )
        self._debug.log( 'Q_params (unit: angstrom^-1) = %s' % (Q_params, ) )
        self._debug.log( 'mod2sample distance = %s' % mod2sample )
        self._debug.log( 'Incident energy (unit: meV) = %s' % (Ei, ) )
        self._debug.log( 'emission_time (unit: microsecond) = %s' % (emission_time, ) )
    
        E_begin, E_end, E_step = E_params # meV
        Q_begin, Q_end, Q_step = Q_params # angstrom^-1
        
        Q_axis = histogram.axis('Q', boundaries = histogram.arange(
            Q_begin, Q_end, Q_step) )
        E_axis = histogram.axis('energy', boundaries = histogram.arange(
            E_begin, E_end, E_step) )
        h = histogram.histogram(
            'I(Q,E)',
            [
            Q_axis,
            E_axis,
            ],
            data_type = 'int',
            )

        pixelPositions = arcseventdata.readpixelpositions(
            pixelPositionsFilename, npacks, ndetsperpack, npixelsperdet )

        # remember a few things so that we can do normalization
        mpiRank = self.mpiRank
        if mpiRank == 0:
            pixelSolidAngles = infos['solidangles'].I
            self._remember( Ei, pixelPositions, pixelSolidAngles )
            
        self.out_histogram = h
        self.pixelPositions = pixelPositions
        self.Ei = Ei
        self.mod2sample = mod2sample
        self.emission_time = emission_time
        return 
Esempio n. 56
0
def test():
    from histogram import histogram, axis, arange, plot
    xaxis = axis('x', arange(5), unit='meter')
    yaxis = axis('y', arange(7), unit='cm')
    axes = [xaxis, yaxis]
    h = histogram( "intensity", axes, fromfunction=lambda x,y: x**2+y**2)
    print h
    plot(h)
    help(h)
    slice = h[3, ()]
Esempio n. 57
0
def createSqe():
    import histogram as H
    qaxis = H.axis( 'Q', boundaries = H.arange(0, 13.0, 0.1) )
    eaxis = H.axis( 'energy', boundaries = H.arange(-50, 50, 1.0) )
    sqe = H.histogram(
        'sqe',
        [ qaxis, eaxis ],
        fromfunction = sqe_f
        )
    return sqe
 def __init__(self, doshist):
     '''doshist: a histogram instance'''
     from .utils import nice_dos
     E, g = nice_dos(doshist.energy, doshist.I)
     import histogram
     self.doshist =  histogram.histogram(
         'dos', 
         [('energy', E, 'meV')],
         g)
     return
    def test__add__(self):
        "histogram: h+b"
        h = self._histogram + (1,0)
        self.assertVectorEqual( h[0.5, 1], (1,0) )

        h = self._histogram + self._histogram2
        self.assertVectorEqual( h[1.5,1], (6,6) )

        h1 = histogram.histogram( 'h1', [ ['x', range(10)] ] )
        h1[()] = 1,1
        
        h2 = histogram.histogram( 'h2', [ ['x', range(10)] ] )
        h2[()] = 2,2

        h1*=(2.,0)
        h2/=(2.,0)

        h3 = h1+h2
        for x in range(10): self.assertAlmostEqual( h3[x][0], 3 )
        return