def test1(self): 'numpy array --> spin' arr = numpy.array( [1., 2.] ) ptr = getdataptr( arr ) spin = bpext.wrap_ptr( ptr, 'NeutronSpin' ) self.assertEqual( spin.s1, 1 ) self.assertEqual( spin.s2, 2 ) return
def test2(self): 'numpy array --> cevent' arr = numpy.arange( 0, 10, 1. ) ptr = getdataptr( arr ) event = bpext.wrap_ptr( ptr, 'cNeutronEvent' ) self.assertEqual( event.x, 0 ) self.assertEqual( event.y, 1 ) self.assertEqual( event.z, 2 ) return
def test3(self): 'numpy array --> event buffer' arr = numpy.arange( 0, 20, 1. ) ptr = getdataptr( arr ) cevents = bpext.wrap_ptr( ptr, 'cNeutronEvent' ) events = mcni.neutron_buffer(2) events.fromCevents( cevents, 2 ) for event in events: print event return
def test(self): a = numpy.arange(12, dtype = numpy.double) a.shape = 3,4 ptr = numpyext.getdataptr( a ) 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
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" ptr = numpyext.getdataptr( npyarr ) 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
def cevents_from_npyarr(npyarr): '''convert a numpy array to a boost-python instance of Neutron::cEvent pointer''' try: from danse.ins.numpyext import getdataptr except ImportError: from numpyext import getdataptr import warnings warnings.warn("Using old numpyext. Should use danse.ins.numpyext") ptr = getdataptr( npyarr ) try: from danse.ins import bpext except ImportError: import bpext import warnings warnings.warn("Using old bpext. Should use danse.ins.bpext") import mcni.mcni cevents = bpext.wrap_ptr( ptr, 'cNeutronEvent' ) cevents.origin = npyarr return cevents