Esempio n. 1
0
def events2IpdpL(events, n, IpdpL, pixelpositions,
                 tofUnit = 1.e-7, mod2sample = 13.5,
                 emission_time = 0.0):
    axes = IpdpL.axes()
    assert len(axes) == 4
    assert axes[0].name() == 'detectorpackID'
    assert axes[1].name() == 'detectorID'
    assert len(axes[1].binCenters()) == 8
    assert axes[1].binCenters()[0] == 0
    assert axes[1].binCenters()[-1] == 7
    assert axes[2].name() == 'pixelID'
    assert len(axes[2].binCenters()) in [128, 256]

    assert axes[3].name() == 'L'
    L_axis = axes[3]
    L_boundaries = L_axis.binBoundaries().asNumarray()
    L_begin = L_boundaries[0]
    L_step = L_boundaries[1] - L_boundaries[0]
    L_end = L_boundaries[-1] + L_step/10.

    ntotpixels = len( pixelpositions )
    from numpyext import getdataptr
    pixelpositions = getdataptr( pixelpositions )
    
    import arcseventdata as binding
    return binding.events2IpixL_numpyarray(
        events, n,
        0, ntotpixels, 1,
        L_begin, L_end, L_step,
        IpdpL.data().storage().asNumarray(),
        pixelpositions, ntotpixels, tofUnit, mod2sample,
        emission_time)
Esempio n. 2
0
def cevents_from_npyarr(npyarr):
    '''convert a numpy array to a boost-python instance of Neutron::cEvent pointer'''
    from numpyext import getdataptr
    ptr = getdataptr(npyarr)
    from bpext import wrap_ptr
    import mcni.mcni
    cevents = wrap_ptr(ptr, 'cNeutronEvent')
    cevents.origin = npyarr
    return cevents
def cevents_from_npyarr(npyarr):
    '''convert a numpy array to a boost-python instance of Neutron::cEvent pointer'''
    from numpyext import getdataptr
    ptr = getdataptr( npyarr )
    from bpext import wrap_ptr
    import mcni.mcni
    cevents = wrap_ptr( ptr, 'cNeutronEvent' )
    cevents.origin = npyarr
    return cevents
Esempio n. 4
0
def events2IhklE(
    events, n, IhklE, Ei, ub, pixelpositions,
    tofUnit = 1.e-7, mod2sample = 13.5,
    emission_time = 0.0):

    intensity_npy_typecode = _histtype_to_npytypecode( IhklE.typeCode() )
    
    axes = IhklE.axes()
    assert len(axes) == 4
    
    assert axes[0].name() == 'h'
    h_axis = axes[0]
    h_boundaries = h_axis.binBoundaries().asNumarray()
    h_begin = h_boundaries[0]
    h_step = h_boundaries[1] - h_boundaries[0]
    h_end = h_boundaries[-1] + h_step/100.

    assert axes[1].name() == 'k'
    k_axis = axes[1]
    k_boundaries = k_axis.binBoundaries().asNumarray()
    k_begin = k_boundaries[0]
    k_step = k_boundaries[1] - k_boundaries[0]
    k_end = k_boundaries[-1] + k_step/100.
 
    assert axes[2].name() == 'l'
    l_axis = axes[2]
    l_boundaries = l_axis.binBoundaries().asNumarray()
    l_begin = l_boundaries[0]
    l_step = l_boundaries[1] - l_boundaries[0]
    l_end = l_boundaries[-1] + l_step/100.

    assert axes[3].name() == 'energy'
    E_axis = axes[3]
    E_boundaries = E_axis.binBoundaries().asNumarray()
    E_begin = E_boundaries[0]
    E_step = E_boundaries[1] - E_boundaries[0]
    E_end = E_boundaries[-1]

    ntotpixels = len(pixelpositions)
    
    from numpyext import getdataptr
    pixelpositions_ptr = getdataptr( pixelpositions )
    
    import arcseventdata as binding
    binding.events2IhklE_numpyarray(
        events, n,
        h_begin, h_end, h_step,
        k_begin, k_end, k_step,
        l_begin, l_end, l_step,
        E_begin, E_end, E_step,
        IhklE.data().storage().asNumarray(),
        Ei, ub,
        pixelpositions_ptr, ntotpixels, tofUnit, mod2sample,
        emission_time, intensity_npy_typecode)

    return IhklE
Esempio n. 5
0
def events2IpdpE(events, n, IpdpE, Ei, pixelpositions,
                 tofUnit = 1.e-7, mod2sample = 13.5,
                 emission_time = 0.0):
    axes = IpdpE.axes()
    assert len(axes) == 4
    assert axes[0].name() == 'detectorpackID'
    assert axes[1].name() == 'detectorID'
    assert len(axes[1].binCenters()) == 8
    assert axes[1].binCenters()[0] == 0
    assert axes[1].binCenters()[-1] == 7

    assert axes[2].name() == 'pixelID'
    pixelIDs = IpdpE.pixelID
    npixelbinsspertube = len(pixelIDs)
    assert 256 % npixelbinsspertube == 0
    pixelStep = pixelIDs[1]-pixelIDs[0]
    npixelspertube = npixelbinsspertube * pixelStep

    ntubesperpack = 8

    assert axes[3].name() == 'energy'
    E_axis = axes[3]
    E_boundaries = E_axis.binBoundaries().asNumarray()
    E_begin = E_boundaries[0]
    E_step = E_boundaries[1] - E_boundaries[0]
    E_end = E_boundaries[-1]

    ntotpixelbins = IpdpE.size() / E_axis.size()

    #make sure pack is continuous
    packs = IpdpE.detectorpackID
    for i in range(len(packs)-1):
        assert packs[i] + 1 == packs[i+1]
        continue
    startpack = packs[0]
    assert startpack > 0, "pack should start at 1: %s" % startpack

    # 
    startpixelindex = (startpack-1)*ntubesperpack*npixelspertube
    endpixelindex = startpixelindex + ntotpixelbins*pixelStep

    maxpixelindex = endpixelindex

    #
    from numpyext import getdataptr
    pixelpositions = getdataptr( pixelpositions )
    
    import arcseventdata as binding
    return binding.events2IpixE_numpyarray(
        events, n,
        startpixelindex, endpixelindex, pixelStep,
        E_begin, E_end, E_step,
        IpdpE.data().storage().asNumarray(),
        Ei,
        pixelpositions, maxpixelindex, tofUnit, mod2sample,
        emission_time)
Esempio n. 6
0
def events2IQQQE(
    events, n, IQQQE, Ei, pixelpositions,
    tofUnit = 1.e-7, mod2sample = 13.5,
    emission_time = 0.0):

    intensity_npy_typecode = _histtype_to_npytypecode( IQQQE.typeCode() )
    
    axes = IQQQE.axes()
    assert len(axes) == 4
    
    assert axes[0].name() == 'Qx'
    Qx_axis = axes[0]
    Qx_boundaries = Qx_axis.binBoundaries().asNumarray()
    Qx_begin = Qx_boundaries[0]
    Qx_step = Qx_boundaries[1] - Qx_boundaries[0]
    Qx_end = Qx_boundaries[-1] + Qx_step/100.

    assert axes[1].name() == 'Qy'
    Qy_axis = axes[1]
    Qy_boundaries = Qy_axis.binBoundaries().asNumarray()
    Qy_begin = Qy_boundaries[0]
    Qy_step = Qy_boundaries[1] - Qy_boundaries[0]
    Qy_end = Qy_boundaries[-1] + Qy_step/100.
 
    assert axes[2].name() == 'Qz'
    Qz_axis = axes[2]
    Qz_boundaries = Qz_axis.binBoundaries().asNumarray()
    Qz_begin = Qz_boundaries[0]
    Qz_step = Qz_boundaries[1] - Qz_boundaries[0]
    Qz_end = Qz_boundaries[-1] + Qz_step/100.

    assert axes[3].name() == 'energy'
    E_axis = axes[3]
    E_boundaries = E_axis.binBoundaries().asNumarray()
    E_begin = E_boundaries[0]
    E_step = E_boundaries[1] - E_boundaries[0]
    E_end = E_boundaries[-1]

    ntotpixels = len(pixelpositions)
    
    from numpyext import getdataptr
    pixelpositions_ptr = getdataptr( pixelpositions )
    
    import arcseventdata as binding
    binding.events2IQQQE_numpyarray(
        events, n,
        Qx_begin, Qx_end, Qx_step,
        Qy_begin, Qy_end, Qy_step,
        Qz_begin, Qz_end, Qz_step,
        E_begin, E_end, E_step,
        IQQQE.data().storage().asNumarray(),
        Ei,
        pixelpositions_ptr, ntotpixels, tofUnit, mod2sample,
        emission_time, intensity_npy_typecode)

    return IQQQE
Esempio n. 7
0
 def test1(self):
     'numpy array --> spin'
     import numpy
     arr = numpy.array([1., 2.])
     from numpyext import getdataptr
     ptr = getdataptr(arr)
     from bpext import wrap_ptr
     import mcni.mcni
     spin = wrap_ptr(ptr, 'NeutronSpin')
     self.assertEqual(spin.s1, 1)
     self.assertEqual(spin.s2, 2)
     return
Esempio n. 8
0
 def test1(self):
     'numpy array --> spin'
     import numpy
     arr = numpy.array( [1., 2.] )
     from numpyext import getdataptr
     ptr = getdataptr( arr )
     from bpext import wrap_ptr
     import mcni.mcni
     spin = wrap_ptr( ptr, 'NeutronSpin' )
     self.assertEqual( spin.s1, 1 )
     self.assertEqual( spin.s2, 2 )
     return
Esempio n. 9
0
 def test2(self):
     'numpy array --> cevent'
     import numpy
     arr = numpy.arange(0, 10, 1.)
     from numpyext import getdataptr
     ptr = getdataptr(arr)
     from bpext import wrap_ptr
     import mcni.mcni
     event = wrap_ptr(ptr, 'cNeutronEvent')
     self.assertEqual(event.x, 0)
     self.assertEqual(event.y, 1)
     self.assertEqual(event.z, 2)
     return
Esempio n. 10
0
 def test2(self):
     'numpy array --> cevent'
     import numpy
     arr = numpy.arange( 0, 10, 1. )
     from numpyext import getdataptr
     ptr = getdataptr( arr )
     from bpext import wrap_ptr
     import mcni.mcni
     event = wrap_ptr( ptr, 'cNeutronEvent' )
     self.assertEqual( event.x, 0 )
     self.assertEqual( event.y, 1 )
     self.assertEqual( event.z, 2 )
     return
Esempio n. 11
0
    def test3(self):
        'numpy array --> event buffer'
        import numpy
        arr = numpy.arange( 0, 20, 1. )
        from numpyext import getdataptr
        ptr = getdataptr( arr )
        from bpext import wrap_ptr
        import mcni.mcni
        cevents = wrap_ptr( ptr, 'cNeutronEvent' )

        events = mcni.neutron_buffer(2)
        events.fromCevents( cevents, 2 )

        for event in events: print event
        return
Esempio n. 12
0
    def test3(self):
        'numpy array --> event buffer'
        import numpy
        arr = numpy.arange(0, 20, 1.)
        from numpyext import getdataptr
        ptr = getdataptr(arr)
        from bpext import wrap_ptr
        import mcni.mcni
        cevents = wrap_ptr(ptr, 'cNeutronEvent')

        events = mcni.neutron_buffer(2)
        events.fromCevents(cevents, 2)

        for event in events:
            print event
        return
Esempio n. 13
0
    def test(self):
        import numpyext
        import numpy
        a = numpy.arange(12, dtype = numpy.double)
        a.shape = 3,4
        ptr = numpyext.getdataptr( a )
        
        import bpext
        wp = bpext.wrap_native_ptr( ptr )
        
        shape = mccomponentsbp.vector_uint( 0 )
        for i in a.shape: shape.append( i )
        a1 = mccomponentsbp.new_NdArray_dblarr_2( wp, shape )
        a1.origin = a

        indexes = mccomponentsbp.vector_uint( 0 )
        indexes.append( 2 ); indexes.append( 1 )
        self.assertEqual( a1[ indexes ], 9 )
        return
Esempio n. 14
0
    def ndarray( self, npyarr ):
        '''create boost python instance of NdArray object
    arguments:
        npyarr: numpy array. it must be a contiguous array.
        '''
        assert npyarr.dtype == numpy.double, "only work for double array for this time"
        
        import numpyext
        ptr = numpyext.getdataptr( npyarr )
        
        import bpext
        wp = bpext.wrap_native_ptr( ptr )
        
        shape = b.vector_uint( 0 )
        for i in npyarr.shape: shape.append( i )

        factory = 'new_NdArray_dblarr_%d' % len(shape)
        a1 = getattr(b,factory)( wp, shape )
        a1.origin = npyarr # keep a reference to avoid seg fault
        return a1
Esempio n. 15
0
    def test(self):
        import numpyext
        import numpy
        a = numpy.arange(12, dtype=numpy.double)
        a.shape = 3, 4
        ptr = numpyext.getdataptr(a)

        import bpext
        wp = bpext.wrap_native_ptr(ptr)

        shape = mccomponentsbp.vector_uint(0)
        for i in a.shape:
            shape.append(i)
        a1 = mccomponentsbp.new_NdArray_dblarr_2(wp, shape)
        a1.origin = a

        indexes = mccomponentsbp.vector_uint(0)
        indexes.append(2)
        indexes.append(1)
        self.assertEqual(a1[indexes], 9)
        return
Esempio n. 16
0
def events2Idspacing(events, n, Idspacing, pixelpositions,
                     tofUnit = 1.e-7, mod2sample = 13.5):
    '''events2Idspacing(events, n, Idspacing, pixelpositions,
    npacks = 115, ndetsperpack = 8, npixelsperdet = 128,
    tofUnit = 1.e-7, mod2sample = 13.5) --> integrate events into Idspacing histogram

    events:
      neutron events
    n:
      number of neutron events
    Idspacing:
      I(d spacing) histogram. d spacing axis must have units attached
    pixelpositions:
      mapping pixels to positions
    tofUnit:
      unit of tof in pre-NeXus file
    '''
    axes = Idspacing.axes()
    assert len(axes) == 1
    daxis = axes[0]
    
    import units
    unit = daxis.unit()/units.length.angstrom
    boundaries = daxis.binBoundaries().asNumarray() * unit
    
    begin = boundaries[0]
    step = boundaries[1] - boundaries[0]
    end = boundaries[-1]

    ntotpixels = len( pixelpositions )

    from numpyext import getdataptr
    pixelpositions = getdataptr( pixelpositions )

    import arcseventdata as binding
    return binding.events2Idspacing_numpyarray(
        events, n, begin, end, step, Idspacing.data().storage().asNumarray(),
        pixelpositions, ntotpixels, tofUnit, mod2sample)
Esempio n. 17
0
def events2IQE(events, n, IQE, Ei, pixelpositions,
               tofUnit = 1.e-7, mod2sample = 13.5,
               emission_time = 0.0):
    axes = IQE.axes()
    assert len(axes) == 2
    
    assert axes[0].name() == 'Q'
    Q_axis = axes[0]
    Q_boundaries = Q_axis.binBoundaries().asNumarray()
    Q_begin = Q_boundaries[0]
    Q_step = Q_boundaries[1] - Q_boundaries[0]
    Q_end = Q_boundaries[-1] + Q_step/10.

    assert axes[1].name() == 'energy'
    E_axis = axes[1]
    E_boundaries = E_axis.binBoundaries().asNumarray()
    E_begin = E_boundaries[0]
    E_step = E_boundaries[1] - E_boundaries[0]
    E_end = E_boundaries[-1] + E_step/10.

    ntotpixels = len(pixelpositions)
    
    from numpyext import getdataptr
    pixelpositions_ptr = getdataptr( pixelpositions )
    
    import arcseventdata as binding
    binding.events2IQE_numpyarray(
        events, n,
        Q_begin, Q_end, Q_step,
        E_begin, E_end, E_step,
        IQE.data().storage().asNumarray(),
        Ei,
        pixelpositions_ptr, ntotpixels, tofUnit, mod2sample,
        emission_time)

    return IQE
Esempio n. 18
0
def getdataptr( npyarr ):
    '''extract data pointer from a numpy array and return a PyCObject'''
    return binding.getdataptr( npyarr )
Esempio n. 19
0
def getdataptr(npyarr):
    '''extract data pointer from a numpy array and return a PyCObject'''
    return binding.getdataptr(npyarr)