def test_reduce(self):
     "reduce(histogram)"
     from histogram import makeHistogram
     name = "h"
     axes = [ ('x', [1,2,3]), ('yID', [1]) ]
     data = [ [1],[2],[3] ]
     errs = [ [1],[2],[3] ]
     h = makeHistogram( name, axes, data, errs )
     h.reduce()
     shape = h.shape()
     self.assertEqual( shape, (3,))
     return
Пример #2
0
def saveDOSasHistogram( dos, filename ):
    e = dos.e
    g = dos.g
    from histogram import makeHistogram
    name = 'dos'
    eaxis = 'Energy', e
    data = g
    import numpy
    errs = numpy.zeros( len(g ) )
    h = makeHistogram(name, [eaxis], data, errs )
    from pickle import dump
    dump( h, open(filename, 'w') )
    return
Пример #3
0
def test2():
    import numpy as n
    from histogram import makeHistogram
    det = ( "detectorID", range(10) )
    pxl = ( "pixelID", range(40) )
    E   = ( "energy", n.arange( -6,6, 1.0) )
    data = n.ones( (10,40,12) )
    errs = n.ones( (10,40,12) ) * 0.5
    I_pe =  makeHistogram( "I_pe", [det, pxl, E], data, errs)
    
    data1 = n.ones( (10,40) )
    errs1 = n.ones( (10,40) ) * 0.
    phi_p = makeHistogram( "phi_p", [det,pxl], data1, errs1)

    writer.write_spe( I_pe, phi_p, "tmp.spe" )

    data2 = n.ones( (10,40) )
    errs2 = n.ones( (10,40) ) * 0.
    psi_p = makeHistogram( "psi_p", [det,pxl], data1, errs1)

    sa_p = psi_p = dphi_p = dpsi_p = phi_p
    writer.write_phx(sa_p, psi_p, dphi_p, dpsi_p, "tmp.phx" )
    return
Пример #4
0
def test3():
    import numpy as n
    from histogram import makeHistogram
    det = ( "detectorID", range(10) )
    pxl = ( "pixelID", range(40) )
    E   = ( "energy", n.arange( -6,6, 1.0) )
    data = n.ones( (10,40,12) )
    errs = n.ones( (10,40,12) ) * 0.5
    I_pe =  makeHistogram( "I_pe", [det, pxl, E], data, errs)
    
    data1 = n.ones( (10,40) )
    errs1 = n.ones( (10,40) ) * 0.
    phi_p = makeHistogram( "phi_p", [det,pxl], data1, errs1)

    data2 = n.ones( (10,40),  int )
    errs2 = n.zeros( (10,40),  int )
    data2[:,0] = 0
    mask_p = makeHistogram( "mask_p", [det,pxl], data2, errs2)

    writer.write_spe( I_pe, phi_p, "tmp-withmask.spe", mask_p=mask_p)

    sa_p = psi_p = dphi_p = dpsi_p = phi_p
    writer.write_phx(sa_p, psi_p, dphi_p, dpsi_p, "tmp-withmask.phx" , mask_p=mask_p)
    return
    def testSetSlice(self):
        "Histogram: h[ slices ] = rhs"
        histogram = self._histogram.copy()
        #set element
        histogram[1.5,1] = (2,4)
        self.assertVectorEqual( histogram[1.5,1], (2,4) )

        #set slice
        from histogram.SlicingInfo import SlicingInfo, all, front, back
        from histogram import createDataset
        from numpy import array
        
        data = createDataset( "data", shape = [2,2] )
        data[:,:] = array( [ [1,2],
                             [3,4], ] )
        errs = createDataset( "errs", shape = [2,2] )
        errs[:,:] = array( [ [1,2],
                             [3,4], ] )

        histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = data,errs
        self.assertVectorEqual( histogram[0.5,1], (1,1) )
        self.assertVectorEqual( histogram[0.5,3], (2,2) )
        self.assertVectorEqual( histogram[1,1], (3,3) )
        self.assertVectorEqual( histogram[1,3], (4,4) )

        histogram[(0.5, 1.5),(1,3)] = data,errs
        self.assertVectorEqual( histogram[0.5,1], (1,1) )
        self.assertVectorEqual( histogram[0.5,3], (2,2) )
        self.assertVectorEqual( histogram[1,1], (3,3) )
        self.assertVectorEqual( histogram[1,3], (4,4) )

        
        from histogram import makeHistogram
        name = "h"
        axes = [
            ('x', [1,2]),
            ('y', [1,2]),
            ]
        data = array( [ [1,2], [3,4] ] )
        errs = array( [ [1,2], [3,4] ] )
        h2 = makeHistogram( name, axes, data, errs )
        histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = h2

        #setslice: rhs is a histogram
        histogram[(0.5, 1.5), (1,3)] = h2

        # setslice: rhs is a list of arrays
        histogram[(0.5, 1.5), (1,3)] = data, errs
        
        # setslice: rhs is a list of arrays including None
        histogram[(0.5, 1.5), (1,3)] = data, None

        # setslice: rhs is a lit of arrays. histogram is 1D
        data = array([1,2]); errs = data;
        histogram[ 0.5, () ] [ (1,3) ] = data, errs

        # setslice: rhs is a tuple of two numbers
        histogram[ 0.5, () ] [ (1,3 )]  = 0., 0.
        
        #setitem using dictionary
        histogram[ {'E':1.5, 'tubeId':3} ] = 3, 3
        self.assertVectorAlmostEqual(
            histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) )

        histogram[ {'E':1.5} ][ {'tubeId':3} ] = 3, 3
        self.assertVectorAlmostEqual(
            histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) )

        #
        histogram[ {'E':1.5} ] /= 3,3
        histogram[ {'E':1.5} ] /= 3,3
        return
Пример #6
0
    def testSetSlice(self):
        "Histogram: h[ slices ] = rhs"
        histogram = self._histogram.copy()
        #set element
        histogram[1.5,1] = (2,4)
        self.assertVectorEqual( histogram[1.5,1], (2,4) )

        #set slice
        from histogram.SlicingInfo import SlicingInfo, all, front, back
        from histogram import createDataset
        from numpy import array
        
        data = createDataset( "data", shape = [2,2] )
        data[:,:] = array( [ [1,2],
                             [3,4], ] )
        errs = createDataset( "errs", shape = [2,2] )
        errs[:,:] = array( [ [1,2],
                             [3,4], ] )

        histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = data,errs
        self.assertVectorEqual( histogram[0.5,1], (1,1) )
        self.assertVectorEqual( histogram[0.5,3], (2,2) )
        self.assertVectorEqual( histogram[1,1], (3,3) )
        self.assertVectorEqual( histogram[1,3], (4,4) )

        histogram[(0.5, 1.5),(1,3)] = data,errs
        self.assertVectorEqual( histogram[0.5,1], (1,1) )
        self.assertVectorEqual( histogram[0.5,3], (2,2) )
        self.assertVectorEqual( histogram[1,1], (3,3) )
        self.assertVectorEqual( histogram[1,3], (4,4) )

        
        from histogram import makeHistogram
        name = "h"
        axes = [
            ('x', [1,2]),
            ('y', [1,2]),
            ]
        data = array( [ [1,2], [3,4] ] )
        errs = array( [ [1,2], [3,4] ] )
        h2 = makeHistogram( name, axes, data, errs )
        histogram[SlicingInfo((0.5, 1.5)), SlicingInfo((1,3))] = h2

        #setslice: rhs is a histogram
        histogram[(0.5, 1.5), (1,3)] = h2

        # setslice: rhs is a list of arrays
        histogram[(0.5, 1.5), (1,3)] = data, errs
        
        # setslice: rhs is a list of arrays including None
        histogram[(0.5, 1.5), (1,3)] = data, None

        # setslice: rhs is a lit of arrays. histogram is 1D
        data = array([1,2]); errs = data;
        histogram[ 0.5, () ] [ (1,3) ] = data, errs

        # setslice: rhs is a tuple of two numbers
        histogram[ 0.5, () ] [ (1,3 )]  = 0., 0.
        
        #setitem using dictionary
        histogram[ {'E':1.5, 'tubeId':3} ] = 3, 3
        self.assertVectorAlmostEqual(
            histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) )

        histogram[ {'E':1.5} ][ {'tubeId':3} ] = 3, 3
        self.assertVectorAlmostEqual(
            histogram[ {'E':1.5, 'tubeId':3} ], (3, 3) )

        #
        histogram[ {'E':1.5} ] /= 3.,3.
        histogram[ {'E':1.5} ] /= 3.,3.
        return