Exemplo n.º 1
0
def test_stream_plaindata():
    nb_channel = 16
    chunksize = 1024
    stream_spec = dict(protocol = 'tcp', interface = '127.0.0.1', port='*', 
                       transfermode = 'plaindata', streamtype = 'analogsignal',
                       dtype = 'float32', shape = (-1, nb_channel), compression ='',
                       scale = None, offset = None, units = '')
    
    for protocol in protocols:
        for compression in compressions:
            print(protocol, compression)
            stream_spec['protocol'] = protocol
            stream_spec['compression'] = compression
            outstream = OutputStream()
            outstream.configure(**stream_spec)
            
            instream = InputStream()
            instream.connect(outstream)
            time.sleep(.5)
            
            index = 0
            for i in range(5):
                #~ print(i)
                #send
                index += chunksize
                arr = np.random.rand(1024, nb_channel).astype(stream_spec['dtype'])
                outstream.send(index, arr)
                
                #recv
                index2, arr2 = instream.recv()
                assert index2==index
                assert np.all((arr-arr2)==0.)
        
            outstream.close()
            instream.close()
Exemplo n.º 2
0
def check_stream(chunksize=1024, chan_shape=(16,), **kwds):
    chunk_shape = (chunksize,) + chan_shape
    stream_spec = dict(protocol='tcp', interface='127.0.0.1', port='*', 
                       transfermode='plaindata', streamtype='analogsignal',
                       dtype='float32', shape=chunk_shape, compression='',
                       scale=None, offset=None, units='')
    stream_spec.update(kwds)
    print("  %s" % kwds)
    outstream = OutputStream()
    outstream.configure(**stream_spec)
    
    instream = InputStream()
    instream.connect(outstream)
    time.sleep(.1)
    
    for i in range(5):
        #~ print(i)
        # send
        if i == 1:
            # send non-aligned data
            cs = (chunk_shape[1], chunk_shape[0]) + chunk_shape[2:]
            arr = np.random.rand(*cs).transpose(1,0).astype(stream_spec['dtype'])
        else:
            arr = np.random.rand(*chunk_shape).astype(stream_spec['dtype'])
        outstream.send(arr)
        
        # recv
        index, arr2 = instream.recv(return_data=True)
        assert index == outstream.last_index
        assert np.all((arr-arr2)==0.)

    outstream.close()
    instream.close()
Exemplo n.º 3
0
def test_stream_sharedarray():
    # this test is perform with no autoswapaxes
    #~ nb_channel = 16
    nb_channel = 1
    
    chunksize = 1024
    ring_size = chunksize * 5 - 334
    stream_spec = dict(protocol='tcp', interface='127.0.0.1', port='*',
                        transfermode='sharedarray', streamtype='analogsignal',
                        dtype='float32', shape=(-1, nb_channel), timeaxis = 0, compression ='',
                        scale = None, offset = None, units = '',
                        sharedarray_shape = (ring_size, nb_channel), ring_buffer_method= 'single',
                        )
    protocol = 'tcp'
    for ring_buffer_method in['single', 'double',]:
        for timeaxis in [0, 1]:
            print('sharedarray', ring_buffer_method, 'timeaxis', timeaxis)
            stream_spec['ring_buffer_method'] = ring_buffer_method
            stream_spec['timeaxis'] = timeaxis
            if timeaxis == 0:
                stream_spec['shape'] = (-1, nb_channel)
                stream_spec['sharedarray_shape'] = (ring_size, nb_channel)
            elif timeaxis == 1:
                stream_spec['shape'] = (nb_channel, -1)
                stream_spec['sharedarray_shape'] = (nb_channel, ring_size)
            outstream = OutputStream()
            outstream.configure(**stream_spec)
            instream = InputStream()
            instream.connect(outstream)
            time.sleep(.5)
            
            index = 0
            for i in range(30):
                #~ print(i)
                
                # send
                if timeaxis==0:
                    arr = np.tile(np.arange(index, index+chunksize)[:, None], (1,nb_channel)).astype(stream_spec['dtype'])
                elif timeaxis==1:
                    arr = np.tile(np.arange(index, index+chunksize)[None,:], (nb_channel, 1)).astype(stream_spec['dtype'])
                index += chunksize
                outstream.send(index, arr, autoswapaxes=False)
                
                index2, arr2 = instream.recv(autoswapaxes=False, with_data = False)
                
                assert index2==index
                assert arr2 is None
                
                # get a buffer of size chunksize*3
                if ring_buffer_method == 'double' and index>chunksize*3:
                    arr2 = instream.get_array_slice(index2, chunksize*3, autoswapaxes=False)
                    if timeaxis==0:
                        assert np.all(arr2[:,0]==np.arange(index-chunksize*3, index).astype('float32'))
                    elif timeaxis==1:
                        assert np.all(arr2[0,:]==np.arange(index-chunksize*3, index).astype('float32'))
            
            outstream.close()
            instream.close()
Exemplo n.º 4
0
def test_stream_plaindata():
    nb_channel = 16
    chunksize = 1024
    stream_spec = dict(protocol='tcp',
                       interface='127.0.0.1',
                       port='*',
                       transfermode='plaindata',
                       streamtype='analogsignal',
                       dtype='float32',
                       shape=(-1, nb_channel),
                       compression='',
                       scale=None,
                       offset=None,
                       units='')

    for protocol in protocols:
        for compression in compressions:
            print(protocol, compression)
            stream_spec['protocol'] = protocol
            stream_spec['compression'] = compression
            outstream = OutputStream()
            outstream.configure(**stream_spec)

            instream = InputStream()
            instream.connect(outstream)
            time.sleep(.5)

            index = 0
            for i in range(5):
                #~ print(i)
                # send
                index += chunksize
                arr = np.random.rand(1024,
                                     nb_channel).astype(stream_spec['dtype'])
                outstream.send(index, arr, autoswapaxes=False)

                # recv
                index2, arr2 = instream.recv(autoswapaxes=False)
                assert index2 == index
                assert np.all((arr - arr2) == 0.)

            outstream.close()
            instream.close()
Exemplo n.º 5
0
def check_stream(chunksize=1024, chan_shape=(16, ), **kwds):
    chunk_shape = (chunksize, ) + chan_shape
    stream_spec = dict(protocol='tcp',
                       interface='127.0.0.1',
                       port='*',
                       transfermode='plaindata',
                       streamtype='analogsignal',
                       dtype='float32',
                       shape=chunk_shape,
                       compression='',
                       scale=None,
                       offset=None,
                       units='')
    stream_spec.update(kwds)
    print("  %s" % kwds)
    outstream = OutputStream()
    outstream.configure(**stream_spec)

    instream = InputStream()
    instream.connect(outstream)
    time.sleep(.1)

    for i in range(5):
        #~ print(i)
        # send
        if i == 1:
            # send non-aligned data
            cs = (chunk_shape[1], chunk_shape[0]) + chunk_shape[2:]
            arr = np.random.rand(*cs).transpose(1,
                                                0).astype(stream_spec['dtype'])
        else:
            arr = np.random.rand(*chunk_shape).astype(stream_spec['dtype'])
        outstream.send(arr)

        # recv
        index, arr2 = instream.recv(return_data=True)
        assert index == outstream.last_index
        assert np.all((arr - arr2) == 0.)

    outstream.close()
    instream.close()
Exemplo n.º 6
0
def benchmark_stream(protocol, transfermode, compression, chunksize, nb_channels=16, nloop=10, profile=False):
    ring_size = chunksize*20
    stream_spec = dict(protocol=protocol, interface='127.0.0.1', port='*',
                       transfermode=transfermode, streamtype = 'analogsignal',
                       dtype='float32', shape=(-1, nb_channels), compression=compression,
                       scale=None, offset=None, units='',
                       # for sharedarray
                       sharedarray_shape = ( ring_size, nb_channels), timeaxis = 0,
                       ring_buffer_method = 'double',
                  )
    outstream = OutputStream()
    outstream.configure(**stream_spec)
    time.sleep(.5)
    instream = InputStream()
    instream.connect(outstream)

    arr = np.random.rand(chunksize, nb_channels).astype(stream_spec['dtype'])
    
    perf = []
    prof = cProfile.Profile()
    for i in range(nloop):
        start = time.perf_counter()
        if profile:
            prof.enable()
        outstream.send(arr)
        index2, arr2 = instream.recv()
        if profile:
            prof.disable()
        perf.append(time.perf_counter() - start)
    if profile:
        prof.print_stats('cumulative')
    
    outstream.close()
    instream.close()
    
    dt = np.min(perf)
    print(chunksize, nloop, transfermode, protocol.ljust(6), compression.ljust(13), 'time = %0.02f ms' % (dt*1000), 'speed = ', chunksize*nb_channels*4*1e-6/dt, 'MB/s')
    
    return dt
Exemplo n.º 7
0
def test_stream_sharedarray():
    # this test is perform with no autoswapaxes
    #~ nb_channel = 16
    nb_channel = 1

    chunksize = 1024
    ring_size = chunksize * 5 - 334
    stream_spec = dict(
        protocol='tcp',
        interface='127.0.0.1',
        port='*',
        transfermode='sharedarray',
        streamtype='analogsignal',
        dtype='float32',
        shape=(-1, nb_channel),
        timeaxis=0,
        compression='',
        scale=None,
        offset=None,
        units='',
        sharedarray_shape=(ring_size, nb_channel),
        ring_buffer_method='single',
    )
    protocol = 'tcp'
    for ring_buffer_method in [
            'single',
            'double',
    ]:
        for timeaxis in [0, 1]:
            print('sharedarray', ring_buffer_method, 'timeaxis', timeaxis)
            stream_spec['ring_buffer_method'] = ring_buffer_method
            stream_spec['timeaxis'] = timeaxis
            if timeaxis == 0:
                stream_spec['shape'] = (-1, nb_channel)
                stream_spec['sharedarray_shape'] = (ring_size, nb_channel)
            elif timeaxis == 1:
                stream_spec['shape'] = (nb_channel, -1)
                stream_spec['sharedarray_shape'] = (nb_channel, ring_size)
            outstream = OutputStream()
            outstream.configure(**stream_spec)
            instream = InputStream()
            instream.connect(outstream)
            time.sleep(.5)

            index = 0
            for i in range(30):
                #~ print(i)

                # send
                if timeaxis == 0:
                    arr = np.tile(
                        np.arange(index, index + chunksize)[:, None],
                        (1, nb_channel)).astype(stream_spec['dtype'])
                elif timeaxis == 1:
                    arr = np.tile(
                        np.arange(index, index + chunksize)[None, :],
                        (nb_channel, 1)).astype(stream_spec['dtype'])
                index += chunksize
                outstream.send(index, arr, autoswapaxes=False)

                index2, arr2 = instream.recv(autoswapaxes=False,
                                             with_data=False)

                assert index2 == index
                assert arr2 is None

                # get a buffer of size chunksize*3
                if ring_buffer_method == 'double' and index > chunksize * 3:
                    arr2 = instream.get_array_slice(index2,
                                                    chunksize * 3,
                                                    autoswapaxes=False)
                    if timeaxis == 0:
                        assert np.all(
                            arr2[:,
                                 0] == np.arange(index - chunksize *
                                                 3, index).astype('float32'))
                    elif timeaxis == 1:
                        assert np.all(arr2[0, :] == np.arange(
                            index - chunksize * 3, index).astype('float32'))

            outstream.close()
            instream.close()