Exemple #1
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
Exemple #2
0
def loadsimdata(filename):
    # load data and apply efficiency
    itof = hh.load(filename, 'I(tof)')
    eff = hh.load('mon1-eff.h5')
    i = itof.I * eff.I
    itof.I[:] = i

    # clean up
    itof[(0.019, None)].I = 0

    #
    x = itof.tof
    y = itof.I

    # convert to counts/10 mus
    #  counts * 10, bins / 10
    from smooth import smooth
    y = smooth(y, window_len=10, window='flat')
    # y = y[:len(x)]
    # y *= 10.
    indexes = range(5, len(x), 10)
    x = x[indexes]
    y = y[indexes] * 10

    # convert to arcs run #5
    # according to ARCS_runinfo.xml of run #5
    # beam power 110kW
    # total run time is 22590/30 seconds
    # the mc simulated was 2MW, 60Hz
    y *= 22590 / 30 * 110e3 / (2e6 / 60)

    # extra scaling factor, why?
    y *= 0.83
    return x, y
    def testload3(self):
        'load a slice of a histogram '
        tmpfile = "tmp.h5"
        if os.path.exists( tmpfile): os.remove(tmpfile)

        cmd = "from histogram.hdf import dump; from histogram import histogram, arange; h = histogram( 'h', [ ('xID', range(10)), ('y', arange(0, 1,0.1)) ] ); from numpy import array; d = array(range(100)); d.shape = 10,10; h[{}] = d, 0; dump(h, %r, '/', 'c') " % tmpfile
        os.system( 'python -c %r' % cmd )
        if not os.path.exists( tmpfile ): raise "failed to create tmp h5 file of historam"

        h1 = load( tmpfile, 'h', xID=(3,6) )
        assert h1.shape() == (4,10)
        d = h1.data().storage().asNumarray().copy()
        d.shape = 40,
        print d
        self.assertVectorAlmostEqual( d, range(30,70) )

        self.assertVectorEqual(
            h1.axisFromId(1).binCenters(), (3,4,5,6) )

        from numpy import arange
        self.assertVectorAlmostEqual(
            h1.axisFromId(2).binCenters(), arange(0,1,0.1) )
        h2 = load( tmpfile, 'h', xID=(3,6), y=(0.4,0.5) )
        assert h2.shape() == (4,2)
        d = h2.data().storage().asNumarray().copy()
        d.shape = 8,
        self.assertVectorAlmostEqual(
            d, (34,35,44,45,54,55,64,65) )

        self.assertVectorEqual(
            h2.axisFromId(1).binCenters(), (3,4,5,6) )

        from numpy import arange
        self.assertVectorAlmostEqual(
            h2.axisFromId(2).binCenters(), (0.4,0.5) )
 def test1c1(self):
     "singlephonon_sqe2dos: partial update -- keep area"
     iqehist = hh.load(os.path.join(datadir, "graphite-Ei_130-iqe.h5"))
     initdos = hh.load(os.path.join(datadir, "graphite-Ei_300-dos.h5"))
     newdos = sqe2dos.singlephonon_sqe2dos(iqehist,
                                           T=300,
                                           Ecutoff=110.,
                                           elastic_E_cutoff=(-30., 15),
                                           M=12.,
                                           initdos=initdos,
                                           update_weights=[0., 1.])
     path = os.path.join(here, 'expected_results', 'test1c1-dos.h5')
     # hh.dump(newdos, path)
     expected = hh.load(path)
     self.assert_(np.allclose(newdos.I, expected.I))
     self.assert_(np.allclose(newdos.E, expected.E))
     self.assert_(np.allclose(newdos.E2, expected.E2))
     # plot
     if interactive:
         pylab.errorbar(initdos.E, initdos.I, initdos.E2**.5, label='init')
         pylab.errorbar(newdos.E, newdos.I, newdos.E2**.5, label='new')
         # pylab.errorbar(expected.E, expected.I, expected.E2**.5, label='expected')
         pylab.legend()
         pylab.show()
     return
    def testload3(self):
        'load a slice of a histogram '
        tmpfile = "tmp.h5"
        if os.path.exists(tmpfile): os.remove(tmpfile)

        cmd = "from histogram.hdf import dump; from histogram import histogram, arange; h = histogram( 'h', [ ('xID', range(10)), ('y', arange(0, 1,0.1)) ] ); from numpy import array; d = array(range(100)); d.shape = 10,10; h[{}] = d, 0;"
        cmd += "dump(h, {!r}, '/', 'c') ".format(tmpfile)
        os.system('python -c {0!r}'.format(cmd))
        if not os.path.exists(tmpfile):
            raise "failed to create tmp h5 file of historam"

        h1 = load(tmpfile, 'h', xID=(3, 6))
        assert h1.shape() == (4, 10)
        d = h1.data().storage().asNumarray().copy()
        d.shape = 40,
        print(d)
        self.assertVectorAlmostEqual(d, list(range(30, 70)))

        self.assertVectorEqual(h1.axisFromId(1).binCenters(), (3, 4, 5, 6))

        from numpy import arange
        self.assertVectorAlmostEqual(
            h1.axisFromId(2).binCenters(), arange(0, 1, 0.1))
        h2 = load(tmpfile, 'h', xID=(3, 6), y=(0.4, 0.5))
        assert h2.shape() == (4, 2)
        d = h2.data().storage().asNumarray().copy()
        d.shape = 8,
        self.assertVectorAlmostEqual(d, (34, 35, 44, 45, 54, 55, 64, 65))

        self.assertVectorEqual(h2.axisFromId(1).binCenters(), (3, 4, 5, 6))

        from numpy import arange
        self.assertVectorAlmostEqual(h2.axisFromId(2).binCenters(), (0.4, 0.5))
Exemple #6
0
def loadsimdata(filename):
    # load data and apply efficiency
    itof = hh.load(filename, 'I(tof)')
    eff = hh.load('mon1-eff.h5')
    i = itof.I * eff.I
    itof.I[:] = i

    # clean up
    itof[(0.019,None)].I = 0
    
    #
    x = itof.tof
    y = itof.I

    # convert to counts/10 mus
    #  counts * 10, bins / 10
    from smooth import smooth
    y = smooth(y, window_len=10, window='flat')
    # y = y[:len(x)]
    # y *= 10.
    indexes = range(5, len(x), 10)
    x = x[indexes]
    y = y[indexes] * 10

    # convert to arcs run #5
    # according to ARCS_runinfo.xml of run #5
    # beam power 110kW
    # total run time is 22590/30 seconds
    # the mc simulated was 2MW, 60Hz
    y *= 22590/30*110e3/(2e6/60)

    # extra scaling factor, why?
    y *= 0.83
    return x,y
Exemple #7
0
 def test1(self):
     "multiphonon.getdos"
     list(getDOS(os.path.join(datadir, "ARCS_V_annulus.nxs")))
     self.assert_(
         close_hist(
             hh.load('work/final-dos.h5'),
             hh.load(
                 os.path.join(here, 'expected_results',
                              'getdos-test1-final-dos.h5'))))
     return
Exemple #8
0
def main():
    iqe1 = hh.load(p1)
    iqe2 = hh.load(p2)
    sigma_coh, sigma_inc = 1.495, 0.0082
    getinel = lambda iqe: iqe[(2.5, 7.), ()].sum('Q')
    coh_inel = getinel(iqe1)
    incoh_inel = getinel(iqe2)
    pl.plot(coh_inel.energy, coh_inel.I / sigma_coh, 'r')
    pl.plot(incoh_inel.energy, incoh_inel.I / sigma_inc, 'g')
    pl.show()
    return
def main():
    iqe1 = hh.load(p1)
    iqe2 = hh.load(p2)
    sigma_coh, sigma_inc = 1.495, 0.0082
    getinel = lambda iqe: iqe[(2.5, 7.), ()].sum('Q')
    coh_inel = getinel(iqe1)
    incoh_inel = getinel(iqe2) 
    pl.plot(coh_inel.energy, coh_inel.I/sigma_coh, 'r')
    pl.plot(incoh_inel.energy, incoh_inel.I/sigma_inc, 'g')
    pl.show()
    return
Exemple #10
0
    def test_sizes(self):
        'ndmonitor: w; tests: xwidth, yheight'
        outdir = 'out-test_sizes'
        histfile = '%s/iw2.h5' % outdir
        cmd_fmt = 'mcvine-simulate -components=source,monitor --- \
                                -overwrite-datafiles=on \
                                -ncount=1 \
                                -buffer_size=1 \
                                -source=Source_simple \
                                -monitor="NDMonitor(w)" \
                                -source.xw=0.1 \
                                -source.yh=0.1 \
                                -source.radius=0.05 \
                                -source.dist=1.0 \
                                -source.dE=10.0 \
                                -source.E0=60.0 \
                                -source.gauss=0.0 \
                                -source.flux=1.0 \
                                -monitor.wmin=0 \
                                -monitor.wmax=100 \
                                -monitor.nw=100 \
                                -monitor.filename=iw2.h5 \
                                -monitor.xwidth=%f \
                                -monitor.yheight=%f \
                                -geometer.monitor="%s" \
                                --output-dir=%s'

        # Misses monitor
        (xw, yh)    = (0.1, 0.1)
        position    = "(0,1,1),(0,0,0)"
        cmd = cmd_fmt % (xw, yh, position, outdir)
        import os
        if os.system(cmd):
            raise RuntimeError, "%r failed" % cmd

        from histogram.hdf import load
        from histogram.hdf.utils import getOnlyEntry
        f = histfile
        h = load(f, getOnlyEntry(f))
        self.assertEqual( h.I.sum() , 0.0)

        # Hits monitor
        (xw, yh)    = (0.1, 0.1)
        position    = "(0,0,1),(0,0,0)"
        outdir = 'out-test_sizes-hits'
        histfile = '%s/iw2.h5' % outdir
        cmd = cmd_fmt % (xw, yh, position, outdir)
        if os.system(cmd):
            raise RuntimeError, "%r failed" % cmd

        f = histfile
        h = load(f, getOnlyEntry(f))
        self.assert_( h.I.sum() > 0)
Exemple #11
0
    def test_sizes(self):
        'ndmonitor: w; tests: xwidth, yheight'
        outdir = 'out-test_sizes'
        histfile = '%s/iw2.h5' % outdir
        cmd_fmt = 'mcvine-simulate -components=source,monitor --- \
                                -overwrite-datafiles=on \
                                -ncount=1 \
                                -buffer_size=1 \
                                -source=Source_simple \
                                -monitor="NDMonitor(w)" \
                                -source.xw=0.1 \
                                -source.yh=0.1 \
                                -source.radius=0.05 \
                                -source.dist=1.0 \
                                -source.dE=10.0 \
                                -source.E0=60.0 \
                                -source.gauss=0.0 \
                                -source.flux=1.0 \
                                -monitor.wmin=0 \
                                -monitor.wmax=100 \
                                -monitor.nw=100 \
                                -monitor.filename=iw2.h5 \
                                -monitor.xwidth=%f \
                                -monitor.yheight=%f \
                                -geometer.monitor="%s" \
                                --output-dir=%s'

        # Misses monitor
        (xw, yh) = (0.1, 0.1)
        position = "(0,1,1),(0,0,0)"
        cmd = cmd_fmt % (xw, yh, position, outdir)
        import os
        if os.system(cmd):
            raise RuntimeError, "%r failed" % cmd

        from histogram.hdf import load
        from histogram.hdf.utils import getOnlyEntry
        f = histfile
        h = load(f, getOnlyEntry(f))
        self.assertEqual(h.I.sum(), 0.0)

        # Hits monitor
        (xw, yh) = (0.1, 0.1)
        position = "(0,0,1),(0,0,0)"
        outdir = 'out-test_sizes-hits'
        histfile = '%s/iw2.h5' % outdir
        cmd = cmd_fmt % (xw, yh, position, outdir)
        if os.system(cmd):
            raise RuntimeError, "%r failed" % cmd

        f = histfile
        h = load(f, getOnlyEntry(f))
        self.assert_(h.I.sum() > 0)
Exemple #12
0
 def test3(self):
     "multiphonon.getdos: low T"
     list(
         getDOS(os.path.join(datadir, "ARCS_V_annulus.nxs"),
                T=1.5,
                workdir='work-lowT'))
     self.assert_(
         close_hist(
             hh.load('work-lowT/final-dos.h5'),
             hh.load(
                 os.path.join(here, 'expected_results',
                              'getdos-test3-final-dos.h5'))))
     return
Exemple #13
0
 def test2(self):
     "multiphonon.getdos: MT can"
     list(
         getDOS(os.path.join(datadir, "ARCS_V_annulus.nxs"),
                mt_nxs=os.path.join(datadir, "ARCS_V_annulus.nxs"),
                mt_fraction=0.01,
                workdir='work-MT'))
     self.assert_(
         close_hist(
             hh.load('work-MT/final-dos.h5'),
             hh.load(
                 os.path.join(here, 'expected_results',
                              'getdos-test2-final-dos.h5'))))
     return
Exemple #14
0
 def test1(self):
     "multiphonon.ui.batch"
     _p = lambda f: os.path.join(self.tmpdir, f)
     sample_nxs_list = [_p('1.nxs'), _p('2.nxs')]
     mt_nxs_list = [None, None]
     batch.process(sample_nxs_list, mt_nxs_list,
                   os.path.join(here, 'V-params.yaml'))
     self.assert_(
         np.allclose(
             hh.load('work-1.nxs,None/final-dos.h5').I,
             hh.load(
                 os.path.join(here, 'expected_results',
                              'batch-1-final-dos.h5')).I))
     return
Exemple #15
0
    def test_rename(self):
        "Histogram: rename"
        import tempfile
        outh5 = tempfile.mktemp()
        code = """
from histogram import histogram, arange
h = histogram('h', [('x', arange(10), 'meter')])
h.I = h.x*h.x
h.setAttribute('name', 'abc')
from histogram.hdf import load, dump
dump(h, %r, '/', 'c')
""" % outh5
        script = tempfile.mktemp()
        open(script, 'w').write(code)
        cmd = 'python %s' % script
        import os
        
        if os.system(cmd):
            raise RuntimeError("%s failed" % cmd)
        
        from histogram.hdf import load
        h = load(outh5, 'abc')

        os.remove(outh5)
        os.remove(script)
        return        
    def test_rename_sliced_histogram_using_rename_method(self):
        "Histogram: rename()"
        import tempfile
        outh5 = tempfile.mktemp()
        code = """
from histogram import histogram, arange
h = histogram(
  'h', 
  [('x', arange(10), 'meter'),
   ('y', arange(15), 'meter'),
  ]
  )
h1 = h[(2,5), ()].sum('x')
h1.rename('abc')
from histogram.hdf import load, dump
dump(h1, %r, '/', 'c')
""" % outh5
        script = tempfile.mktemp()
        open(script, 'w').write(code)
        cmd = 'python %s' % script
        import os
        
        if os.system(cmd):
            raise RuntimeError, "%s failed" % cmd
        
        from histogram.hdf import load
        try:
            h = load(outh5, 'abc')
        except:
            raise RuntimeError, "failed to load histogram from %s" %(
                outh5,)

        os.remove(outh5)
        os.remove(script)
        return        
    def testdump_and_load1c(self):
        'dump and load: meta data'
        tmpfile = 'test_dump_load_1c.h5'
        cmd = '''
from histogram import histogram
x = y = range(10)
h = histogram( 
    'h',
    [ ('tID', x) ],
    data = y, errors = y )
h.setAttribute('instrument', 'ARCS')
axis = h.axes()[0]
axis.setAttribute('pi', 3.14)
import histogram.hdf as hh
hh.dump( h, '{0!s}', '/', 'c' )
'''.format(tmpfile)
        import os
        if os.path.exists(tmpfile):
            os.remove(tmpfile)
        cmd = 'python -c "{0!s}"'.format(cmd)
        if os.system(cmd):
            raise "{0!s} failed".format(cmd)
        h = load(tmpfile, 'h')
        axis = h.axes()[0]
        self.assertTrue(axis.isDiscrete())
        self.assertEqual(axis.name(), 'tID')
        self.assertEqual(h.getAttribute('instrument'), 'ARCS')
        data = h.data()
        self.assertEqual(axis.attribute('pi'), 3.14)
        return
 def test1c(self):
     "singlephonon_sqe2dos: partial update"
     iqehist = hh.load(os.path.join(datadir, "graphite-Ei_130-iqe.h5"))
     initdos = hh.load(os.path.join(datadir, "graphite-Ei_300-dos.h5"))
     newdos = sqe2dos.singlephonon_sqe2dos(iqehist,
                                           T=300,
                                           Ecutoff=100.,
                                           elastic_E_cutoff=(-30., 15),
                                           M=12.,
                                           initdos=initdos)
     # plot
     if interactive:
         pylab.plot(initdos.E, initdos.I)
         pylab.plot(newdos.E, newdos.I)
         pylab.show()
     return
def test_bregmanSplit():
    RF3_read = genfromtxt(os.path.join(data, "RF3_save.txt"))
    dos_300 = hh.load(os.path.join(data, 'graphite-Ei_300-DOS.h5'))
    DOS_300 = dos_300.I
    spacing = dos_300.E[1] - dos_300.E[0]
    energytobin = np.int(220 / spacing)
    DOS_300[energytobin::] = 0
    E_axis_DOS = dos_300.E
    E_axis_res = np.arange(-50, 240, 0.1)
    left = E_axis_res[E_axis_res < E_axis_DOS[0]]
    extended_E_axis_DOS = np.concatenate((left, E_axis_DOS))
    extended_g = np.concatenate((np.zeros(left.shape), dos_300.I))
    dos_intrp1 = interp1d(extended_E_axis_DOS, extended_g, kind='cubic')  # interpolation
    interpolated_dos = dos_intrp1(E_axis_res)
    E, g = E_axis_res, interpolated_dos
    RF_T = np.transpose(RF3_read)
    m = convolve_NS(RF_T, RF3_read)
    delta = 2. / LA.norm(m, ord=1)

    initial_d = np.zeros(g.shape[0])
    initial_b = initial_d
    mu = 100
    lamda = 1
    ninnner = 1
    nouter = 20
    max_cg = 500
    R0 = S(g, RF3_read, initial_d, initial_b, mu, lamda, ninnner, nouter, max_cg)

    plt.figure()
    plt.plot (E_axis_res,R0)
    plt.legend()
    plt.show()
Exemple #20
0
def run(hfile, p0):
    import histogram.hdf as hh
    h = hh.load(hfile)
    p1, success =  fit(h, p0)
    print p1
    print success
    return
Exemple #21
0
def run(hfile, p0):
    import histogram.hdf as hh
    h = hh.load(hfile)
    success, p1, data =  fit(h, p0)
    print p1
    print success
    return
def main():
    iqe1 = hh.load(p1)
    iqe2 = hh.load(p2)
    sigma_coh, sigma_inc = 1.495, 0.0082
    getel = lambda iqe: iqe[(0.2, None),(-1.2,1.2)].I.sum()
    getinel = lambda iqe: iqe[(2.4, 7.1), (None, -1.2)].I.sum() + iqe[(2.4, 7.1), (1.2, None)].I.sum()
    coh_el = getel(iqe1) / sigma_coh
    coh_inel = getinel(iqe1) / sigma_coh
    incoh_el = getel(iqe2) / sigma_inc
    incoh_inel = getinel(iqe2) / sigma_inc
    print "coherent", coh_el, coh_inel
    print "incoherent", incoh_el, incoh_inel
    # pl.plot(ie1.energy, ie1.I/sigma_coh, 'r')
    # pl.plot(ie2.energy, ie2.I/sigma_inc, 'g')
    # pl.show()
    return
    def testdump_and_load_1d(self):
        'dump and load: axis has unit'
        tmpfile = 'test_dump_load_hdf5_1d.h5'
        cmd = '''
from histogram import histogram
x = y = range(10)
h = histogram( 
    'h',
    [ ('x', x, 'meter') ],
    data = y, errors = y )
import histogram.hdf as hh
hh.dump( h, '%s', '/', 'c' )
''' % tmpfile
        import os
        if os.path.exists(tmpfile): 
            os.remove( tmpfile )
        cmd =  'python -c "%s"' % cmd 
        if os.system( cmd ): 
            raise "%s failed" % cmd
        h = load( tmpfile, 'h' )
        for i in range(10):
            self.assertVectorAlmostEqual( h[i], (i,i) )
            continue
        from histogram._units import tounit
        meter = tounit('meter')
        self.assertEqual(h.axisFromName('x').unit(), meter)
        return
    def testdump_and_load1c(self):
        'dump and load: meta data'
        tmpfile = 'test_dump_load_1c.h5'
        cmd = '''
from histogram import histogram
x = y = range(10)
h = histogram( 
    'h',
    [ ('tID', x) ],
    data = y, errors = y )
h.setAttribute('instrument', 'ARCS')
axis = h.axes()[0]
axis.setAttribute('pi', 3.14)
import histogram.hdf as hh
hh.dump( h, '%s', '/', 'c' )
''' % tmpfile
        import os
        if os.path.exists(tmpfile): 
            os.remove( tmpfile )
        cmd =  'python -c "%s"' % cmd 
        if os.system( cmd ): 
            raise "%s failed" % cmd
        h = load( tmpfile, 'h' )
        axis = h.axes()[0]
        self.assert_(axis.isDiscrete())
        self.assertEqual(axis.name(), 'tID')
        self.assertEqual(h.getAttribute('instrument'), 'ARCS')
        data = h.data()
        self.assertEqual(axis.attribute('pi'), 3.14)
        return
Exemple #25
0
 def test1(self):
     "multiphonon.ms"
     mpsqe = hh.load(os.path.join(datadir, "V-S2..5.h5"))
     mssqe = ms.sqe(mpsqe, Ei=110.)
     if interactive:
         H.plot(mssqe, min=0)
     return
    def testdump_and_load_1d(self):
        'dump and load: axis has unit'
        tmpfile = 'test_dump_load_hdf5_1d.h5'
        cmd = '''
from histogram import histogram
x = y = range(10)
h = histogram( 
    'h',
    [ ('x', x, 'meter') ],
    data = y, errors = y )
import histogram.hdf as hh
hh.dump( h, '{0!s}', '/', 'c' )
'''.format(tmpfile)
        import os
        if os.path.exists(tmpfile):
            os.remove(tmpfile)
        cmd = 'python -c "{0!s}"'.format(cmd)
        if os.system(cmd):
            raise "{0!s} failed".format(cmd)
        h = load(tmpfile, 'h')
        for i in range(10):
            self.assertVectorAlmostEqual(h[i], (i, i))
            continue
        from histogram._units import tounit
        meter = tounit('meter')
        self.assertEqual(h.axisFromName('x').unit(), meter)
        return
Exemple #27
0
 def test2(self):
     datadir = self.datadir
     from mcvine.phonon.powderSQE.IDF import from_data_dir
     import mcvine.phonon.powderSQE.IDF as psidf
     from mccomponents.sample import phonon as mcphonon
     doshist = mcphonon.read_dos.dos_fromidf(os.path.join(datadir, 'DOS')).doshist
     disp = psidf.disp_from_datadir(datadir)
     IQEhist, mphhist = psidf.from_data_dir(
         datadir=datadir,
         disp=disp,
         N = int(1e6),
         Q_bins=np.arange(0, 14, 0.1), E_bins=np.arange(0,90,.5),
         doshist=doshist,
         T=300., Ei=120., max_det_angle=140.,
         include_multiphonon=True,
     )
     IQEhist = IQEhist + mphhist
     hh.dump(IQEhist, 'Si-iqe-test2.h5')
     expected = hh.load(os.path.join(here, 'saved_results/Si-all-phonon-Ei_120-T_300-N_1e6.h5'))
     max = np.nanmax(expected.I)
     reldiff = IQEhist-expected
     reldiff.I/=max; reldiff.E2/=max*max
     Nbigdiff = (np.abs(reldiff.I)>0.03).sum()
     Ngood = (IQEhist.I==IQEhist.I).sum()
     Ntotal = IQEhist.size()
     self.assertTrue(Ngood*1./Ntotal>.65)
     self.assertTrue(Nbigdiff*1./Ngood<.10)
     return
Exemple #28
0
    def test_rename_sliced_histogram_using_rename_method(self):
        "Histogram: rename()"
        import tempfile
        outh5 = tempfile.mktemp()
        code = """
from histogram import histogram, arange
h = histogram(
  'h', 
  [('x', arange(10), 'meter'),
   ('y', arange(15), 'meter'),
  ]
  )
h1 = h[(2,5), ()].sum('x')
h1.rename('abc')
from histogram.hdf import load, dump
dump(h1, %r, '/', 'c')
""" % outh5
        script = tempfile.mktemp()
        open(script, 'w').write(code)
        cmd = 'python %s' % script
        import os
        
        if os.system(cmd):
            raise RuntimeError("%s failed" % cmd)
        
        from histogram.hdf import load
        try:
            h = load(outh5, 'abc')
        except:
            raise RuntimeError("failed to load histogram from %s" %(
                outh5,))

        os.remove(outh5)
        os.remove(script)
        return        
Exemple #29
0
def computeAverageEnergy():
    from histogram.hdf import load
    import os
    out = '../mod2sample/out-analyzer'
    h = load(os.path.join(out, 'ienergy.h5'), 'ienergy')
    e = (h.energy * h.I).sum() / h.I.sum()
    return e
Exemple #30
0
 def test1(self):
     "multiphonon.ms"
     mpsqe = hh.load(os.path.join(datadir, "V-S2..5.h5"))
     mssqe = ms.sqe(mpsqe, Ei=110.)
     if interactive:
         H.plot(mssqe, min=0)
     return
Exemple #31
0
 def crossCheckInputs(self):
     inputs = self.inputs
     # path = inputs['SQEhist']
     path = self.context.excitation_params['SQEhist']
     import histogram.hdf as hh
     h = hh.load(path)
     # Q
     Q = h.Q
     from pyre.units import parser
     parser = parser()
     Qmin, Qmax = parser.parse(inputs['Qrange'])
     AA = parser.parse('angstrom')
     Qmin, Qmax = Qmin * AA, Qmax * AA
     if Qmin <= Q[0]:
         raise ValueError("Qmin too small. should be larger than %s" % Q[0])
     if Qmax >= Q[-1]:
         raise ValueError("Qmax too large. should be less than %s" % Q[-1])
     # E
     E = getattr(h, 'E', None)
     if E is None:
         E = h.energy
     Emin, Emax = parser.parse(inputs['Erange'])
     meV = parser.parse('meV')
     Emin, Emax = Emin / meV, Emax / meV
     if Emin <= E[0]:
         raise ValueError("Emin too small. should be larger than %s" % E[0])
     if Emax >= E[-1]:
         raise ValueError("Emax too large. should be less than %s" % E[-1])
     return
Exemple #32
0
 def _gethist(path):
     from histogram.hdf import load
     from histogram.hdf.utils import getOnlyEntry
     computation = domaccess.getComputationRecord('arcsbeamconfigurations', id)
     dds = director.dds
     p = dds.abspath(computation, path)
     return load(p, getOnlyEntry(p))
    def test_rename(self):
        "Histogram: rename"
        import tempfile
        outh5 = tempfile.mktemp()
        code = """
from histogram import histogram, arange
h = histogram('h', [('x', arange(10), 'meter')])
h.I = h.x*h.x
h.setAttribute('name', 'abc')
from histogram.hdf import load, dump
dump(h, %r, '/', 'c')
""" % outh5
        script = tempfile.mktemp()
        open(script, 'w').write(code)
        cmd = 'python %s' % script
        import os
        
        if os.system(cmd):
            raise RuntimeError, "%s failed" % cmd
        
        from histogram.hdf import load
        h = load(outh5, 'abc')

        os.remove(outh5)
        os.remove(script)
        return        
    def test1(self):
        # remove the output directory
        if os.path.exists(outputdir):
            shutil.rmtree(outputdir)
        
        # build the command to ru
        cmd = ['python E_monitor_TestCase_app2.py']
        if usempi:
            cmd.append('--mpirun.nodes=2')
        cmd = ' '.join(cmd)
        
        # run command
        if os.system(cmd):
            raise RuntimeError, "%s failed" % cmd

        # checks
        import time
        ctime = time.time()

        #check output directory exists
        self.assert_( os.path.exists( outputdir ) )
        
        # make sure that the final histogram is identical to the 
        # sum of all the final histograms in different nodes
        from histogram.hdf import load
        from histogram.hdf.utils import getOnlyEntry
        p = os.path.join(outputdir, 'IE.h5')
        h = load(p, getOnlyEntry(p))
        
        self.assertEqual(h.I.sum(), 1)
        return
 def test1a(self):
     "singlephonon_sqe2dos: simulated vanadium SQE -> DOS"
     S = hh.load(os.path.join(datadir, "V-S1.h5"))
     with warnings.catch_warnings(record=True) as ws:
         warnings.simplefilter('always')
         DOS = sqe2dos.singlephonon_sqe2dos(S,
                                            T=300,
                                            Ecutoff=55.,
                                            elastic_E_cutoff=(0., 0.),
                                            M=50.94)
         for w in ws:
             assert 'Scaling factor' not in str(w)
     E = DOS.E
     g = DOS.I
     # compare to the original dos data
     E1, g1 = loadDOS()
     ginterp = np.interp(E1, E, g)
     self.assert_(np.allclose(g1, ginterp))
     # plot
     if interactive:
         import pylab
         pylab.plot(E1, g1, label="Original DOS")
         pylab.plot(E1, ginterp, label="DOS from SQE")
         pylab.legend()
         pylab.show()
     return
Exemple #36
0
    def test1(self):
        # remove the output directory
        if os.path.exists(outputdir):
            shutil.rmtree(outputdir)
        
        # build the command to ru
        cmd = ['python E_monitor_TestCase_app2.py']
        if usempi():
            cmd.append('--%s.nodes=2' % launcher)
        cmd = ' '.join(cmd)
        
        # run command
        if os.system(cmd):
            raise RuntimeError, "%s failed" % cmd

        # checks
        import time
        ctime = time.time()

        #check output directory exists
        self.assert_( os.path.exists( outputdir ) )
        
        # make sure that the final histogram is identical to the 
        # sum of all the final histograms in different nodes
        from histogram.hdf import load
        from histogram.hdf.utils import getOnlyEntry
        p = os.path.join(outputdir, 'IE.h5')
        h = load(p, getOnlyEntry(p))
        
        self.assertEqual(h.I.sum(), 1)
        return
    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 ))
Exemple #38
0
def plot_dos_iteration(curdir, total_rounds=None):
    """
    plot the  DOS for each iteration
   
    Parameters
    ----------
    curdir : str
        path to the root of the working directory for SQE->DOS calculation
 
    total_rounds : integer
        number of iterations

    """
    if total_rounds is None:
        import glob
        dirs = glob.glob(os.path.join(curdir, 'round-*'))
        _get_round_no = lambda p: int(os.path.basename(p).lstrip('round-'))
        total_rounds = max(map(_get_round_no, dirs)) + 1
    # mpl.rcParams['figure.figsize'] = 6,4.5
    for round_no in range(total_rounds):
        fn = os.path.join(curdir, 'round-' + str(round_no), 'dos.h5')
        dos = hh.load(fn)
        plt.errorbar(dos.E, dos.I, dos.E2**.5, label=str(round_no))
        continue
    plt.legend()
    return
Exemple #39
0
def computeAverageEnergy():
    from histogram.hdf import load
    import os
    out = '../mod2sample/out-analyzer'
    h = load(os.path.join(out, 'ienergy.h5'), 'ienergy')
    e = (h.energy * h.I).sum()/h.I.sum()
    return e
Exemple #40
0
def test():
    Q = 20.
    import histogram.hdf as hh
    iqe = hh.load('iqe.h5')

    run(Q, iqe=iqe)
    return
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)
    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.assertTrue(os.path.exists(filename))
Exemple #43
0
    def test2a(self):
        "sqe2dos: V exp"
        iqehist = hh.load(os.path.join(datadir, "V-iqe.h5"))
        from multiphonon.sqe import interp
        newiqe = interp(iqehist, newE=np.arange(-15, 80, 1.))
        hh.dump(newiqe, 'V-iqe-interped.h5')
        iterdos = sqe2dos.sqe2dos(newiqe,
                                  T=300,
                                  Ecutoff=55.,
                                  elastic_E_cutoff=(-12., 6.7),
                                  M=50.94,
                                  C_ms=.2,
                                  Ei=120.,
                                  workdir='work-V')

        with warnings.catch_warnings(record=True) as ws:
            warnings.simplefilter('always')
            for i, dos in enumerate(iterdos):
                # print dos
                # plot
                if interactive:
                    # print '*' * 70
                    pylab.plot(dos.E, dos.I, label='%d' % i)
                    pass
            # check warning
            for w in ws:
                assert 'Scaling factor' not in str(w)

        path = os.path.join(here, 'expected_results',
                            'sqe2dos-test2a-final-dos.h5')
        # hh.dump(dos, path)
        expected = hh.load(path)
        self.assert_(np.allclose(dos.I, expected.I))
        self.assert_(np.allclose(dos.E2, expected.E2))
        if interactive:
            pylab.figure()
            pylab.errorbar(dos.E,
                           dos.I + dos.I.max() / 5.,
                           dos.E2**.5,
                           label='new')
            pylab.errorbar(expected.E,
                           expected.I,
                           expected.E2**.5,
                           label='expected')
            pylab.legend()
            pylab.show()
        return
Exemple #44
0
def getTof(beampath):
    import json, os
    beam_outdir = os.path.join(beampath, 'out')
    tof_hist_path = os.path.join(beam_outdir, 'itof.h5')
    import histogram.hdf as hh
    tofhist = hh.load(tof_hist_path)
    tofcenter = (tofhist.tof * tofhist.I).sum() / tofhist.I.sum()
    return tofcenter
Exemple #45
0
def test_compare_mcvine():
    """
    Tests the acc cpu implementation of a straight guide against mcvine
    """
    num_neutrons = 100000
    # Run the mcvine instrument first
    instr = os.path.join(thisdir, "guide_instrument.py")
    mcvine_outdir = 'out.debug-mcvine_guide_cpu_instrument'
    if os.path.exists(mcvine_outdir):
        shutil.rmtree(mcvine_outdir)
    run_script.run1(instr,
                    mcvine_outdir,
                    ncount=num_neutrons,
                    buffer_size=num_neutrons,
                    guide_factory="mcvine.components.optics.Guide",
                    overwrite_datafiles=True)

    # Run our guide implementation
    outdir = 'out.debug-guide_cpu_instrument'
    if os.path.exists(outdir):
        shutil.rmtree(outdir)
    run_script.run1(instr,
                    outdir,
                    ncount=num_neutrons,
                    buffer_size=num_neutrons,
                    guide_mod="mcvine.acc.components.optics.numpy_guide",
                    overwrite_datafiles=True)

    # Compare output files
    mcvine_Ixy = hh.load(os.path.join(mcvine_outdir, "Ixy.h5"))
    mcvine_Ixdivx = hh.load(os.path.join(mcvine_outdir, "Ixdivx.h5"))
    Ixy = hh.load(os.path.join(outdir, "Ixy.h5"))
    Ixdivx = hh.load(os.path.join(outdir, "Ixdivx.h5"))

    global interactive
    if interactive:
        from histogram import plot as plotHist
        plotHist(mcvine_Ixy)
        plotHist(mcvine_Ixdivx)
        plotHist(Ixy)
        plotHist(Ixdivx)
    assert mcvine_Ixy.shape() == Ixy.shape()
    assert mcvine_Ixdivx.shape() == Ixdivx.shape()
    assert np.allclose(mcvine_Ixy.data().storage(), Ixy.data().storage())
    assert np.allclose(mcvine_Ixdivx.data().storage(), Ixdivx.data().storage())
    return
def main():
    iqe1 = hh.load(p1)
    iqe2 = hh.load(p2)
    sigma_coh, sigma_inc = 1.495, 0.0082
    getel = lambda iqe: iqe[(0.2, None), (-1.2, 1.2)].I.sum()
    getinel = lambda iqe: iqe[(2.4, 7.1), (None, -1.2)].I.sum() + iqe[
        (2.4, 7.1), (1.2, None)].I.sum()
    coh_el = getel(iqe1) / sigma_coh
    coh_inel = getinel(iqe1) / sigma_coh
    incoh_el = getel(iqe2) / sigma_inc
    incoh_inel = getinel(iqe2) / sigma_inc
    print "coherent", coh_el, coh_inel
    print "incoherent", incoh_el, incoh_inel
    # pl.plot(ie1.energy, ie1.I/sigma_coh, 'r')
    # pl.plot(ie2.energy, ie2.I/sigma_inc, 'g')
    # pl.show()
    return
 def testload5(self):
     'load with fs specified'
     filename = 'tmp.h5'
     filename = 'test_dump_load2.h5'
     from h5py import File
     fs = File(filename)
     h = load( filename, '/h', fs = fs )
     return
Exemple #48
0
 def _gethist(path):
     from histogram.hdf import load
     from histogram.hdf.utils import getOnlyEntry
     computation = domaccess.getComputationRecord(
         'arcsbeamconfigurations', id)
     dds = director.dds
     p = dds.abspath(computation, path)
     return load(p, getOnlyEntry(p))
Exemple #49
0
def main():
    import sys
    Q = eval(sys.argv[1])
    iqe = sys.argv[2]
    import histogram.hdf as hh
    iqe = hh.load(iqe)
    p1 = run(Q, iqe=iqe, dQ=0.5)
    line = '%s\t%s\n' % (Q, p1[-1])
    open('res.dat', 'a').write(line)
    return
    def test1b(self):
        iqehist = hh.load(os.path.join(datadir, "V-iqe.h5"))
        from multiphonon.sqe import interp

        newiqe = interp(iqehist, newE=np.arange(-50, 50, 1.0))
        DOS = sqe2dos.singlephonon_sqe2dos(newiqe, T=300, Ecutoff=65.0, elastic_E_cutoff=(-20.0, 6.7), M=50.94)
        # plot
        if interactive:
            H.plot(DOS)
        return
Exemple #51
0
def getDOS(sample_nxs, mt_nxs=None, mt_fraction=0.9,
           Emin=-100, Emax=100, dE=1.,
           Qmin=0, Qmax=15., dQ=0.1, T=300, Ecutoff=50., 
           elastic_E_cutoff=(-20., 7), M=50.94,
           C_ms=0.3, Ei=116.446, workdir='work',
           iqe_nxs="iqe.nxs", iqe_h5="iqe.h5"):
    # prepare paths
    if not os.path.exists(workdir):
        os.makedirs(workdir)
    if not os.path.isabs(iqe_nxs):
        iqe_nxs = os.path.abspath(os.path.join(workdir, iqe_nxs))
    if not os.path.isabs(iqe_h5):
        iqe_h5 = os.path.abspath(os.path.join(workdir, iqe_h5))
    # reduce
    Eaxis = Emin, Emax, dE
    Qaxis = Qmin, Qmax, dQ
    raw2iqe(sample_nxs, iqe_nxs, iqe_h5, Eaxis, Qaxis)
    iqehist = hh.load(iqe_h5)
    if mt_nxs is not None:
        _tomtpath = lambda p: os.path.join(
            os.path.dirname(p), 'mt-'+os.path.basename(p))
        mtiqe_nxs = _tomtpath(iqe_nxs)
        mtiqe_h5 = _tomtpath(iqe_h5)
        raw2iqe(mt_nxs, mtiqe_nxs, mtiqe_h5, Eaxis, Qaxis)
        iqehist -= hh.load(mtiqe_h5) * (mt_fraction, 0)
    # to DOS
    # interpolate data
    from .sqe import interp
    # probably don't need this line
    newiqe = interp(iqehist, newE = np.arange(Emin, Emax, dE))
    # save interpolated data
    hh.dump(newiqe, 'iqe-interped.h5')
    # create processing engine
    from .backward import sqe2dos
    print("iterative computation of DOS...")
    iterdos = sqe2dos.sqe2dos(
      newiqe, T=T, Ecutoff=Ecutoff, 
      elastic_E_cutoff=elastic_E_cutoff, M=M,
      C_ms=C_ms, Ei=Ei, workdir='work')
    doslist = list(iterdos)
    print("done.")
    return doslist
    def test3(self):
        "plot sqe"
        import histogram.hdf as hh, pylab

        sqe = hh.load("sqe.h5/S(Q,E)")
        self.plotter.plot(sqe)
        if interactive:
            raw_input("Press ENTER to continue")
        pylab.clf()
        pylab.close()
        return
    def test3a(self):
        "plot sqe with interpolation"
        import histogram.hdf as hh, pylab

        sqe = hh.load("sqe.h5/S(Q,E)")
        self.plotter.plot(sqe, interpolation="bicubic")
        if interactive:
            raw_input("Press ENTER to continue")
        pylab.clf()
        pylab.close()
        return
    def test_load_slice_and_dump(self):
        'load a histogram and dump a slice of it'
        tmpfile = 'test_load_slice_and_dump.h5'
        if os.path.exists( tmpfile ):
            os.remove( tmpfile )

        import histogram.hdf as hh
        h = hh.load('testload.h5', '/h')
        s = h[(10, 30), ()]
        hh.dump(s, tmpfile, '/',  'c')
        return
def computeFWHM(out):
    from histogram.hdf import load
    import numpy as np
    itof = load(os.path.join(out, 'itof.h5'), 'itof')
    max = itof.I.max()
    indmax = np.where(itof.I==max)[0][0]
    left = itof.I[:indmax]
    right = itof.I[indmax:]
    leftindex = np.where(left > max/2)[0][0]
    rightindex = np.where(right > max/2)[0][-1] + indmax
    fwhm = (rightindex-leftindex) * (itof.tof[1]-itof.tof[0])
    return fwhm
Exemple #56
0
 def read_pixE(self):
     from histogram.hdf import load
     self.histogram = load(self.path)
     phi = self.histogram.phi * deg2rad
     nphi = len(phi)
     theta = self.histogram.theta * deg2rad
     ntheta = len(theta)
     import numpy as np
     phi = np.tile(phi, ntheta)
     theta = np.repeat(theta, nphi)
     energy = self.histogram.E
     return energy, theta, phi
 def test2(self):
     iqehist = hh.load(os.path.join(datadir, "V-iqe.h5"))
     newsqe = interp(iqehist, newE=np.arange(-50, 50, 1.))
     mask = dynamical_range_mask(newsqe, Ei=120)
     # plot
     if interactive:
         import pylab
         pylab.imshow(mask.T[::-1])
         pylab.clim(0, None)
         pylab.colorbar()
         pylab.show()
     return
def main():
    import sys
    argv = sys.argv
    filenames = argv[1:]
    
    from histogram.hdf import load
    hs = [load(f) for f in filenames]
    
    r = compare(hs[0], hs[1])
    if r: raise RuntimeError, r
    else:  print 'same'
    return
Exemple #59
0
    def test3(self):
        'ndmonitor: energy'
        cmd = 'mcvine-simulate -components=source,monitor --- -ncount=1e4 -buffer_size=1000 -source=MonochromaticSource -monitor="NDMonitor(energy)" -geometer.monitor="(0,0,1),(0,0,0)" -source.energy=60 -monitor.energymin=0 -monitor.energymax=100 -monitor.nenergy=100 -monitor.filename=ienergy.h5 --output-dir=out-test3'
        import os
        if os.system(cmd):
            raise RuntimeError, "%r failed" % cmd

        from histogram.hdf import load
        from histogram.hdf.utils import getOnlyEntry
        f = 'out-test3/ienergy.h5'
        h = load(f, getOnlyEntry(f))
        self.assertEqual( h[(58, 62)].sum() , (1., 1.e-4))
        return