예제 #1
0
파일: vtem.py 프로젝트: bionet/vtem
def VTEM_Gabor_IAF(videofile,
                   output_filename,
                   Wt,
                   Wx=2 * np.pi * 4,
                   Wy=2 * np.pi * 4,
                   start_frame=0,
                   end_frame=None,
                   dx=1.0 / 16,
                   dy=1.0 / 16,
                   fps=100,
                   domain=None,
                   h5input=True):
    """
    Encode a video with IAF neurons and Gabor receptive field
    with default parameters

    Parameters
    -----------
    videofile : string
         
         ::
         
              Either
              1. Filename of a file containing the input video
              must be stored using write_memory_to_file in simpleio.py
              or using h5write in matlab
              Video array with shape (a,b,c)
              a: total number of frames
              b: number of pixels in y direction
              c: number of pixels in x direction
              c is the leading dimension
              in matlab should be (c,b,a)
              2. Or filename of a video file if h5input is set to False. 
              Will throw an error if OpenCV and the required codec are
              not installed         

    output_filename : string
         output filename that will contain the spike info
    Wt : float
         bandwidth in t variable
         if not specified, will use the info in spikefile
    Wx : float, optional
         bandwidth in x variable
         if not specified, will use the info in spikefile
    Wy : float, optional 
         bandwidth in y variable
         if not specified, will use the info in spikefile
    start_frame : integer, optional 
         starting frame to be encoded in the video  
    end_frame : integer, optional 
         ending frame to be encoded
         if not specified, will encoding to the end of the video
    dx : integer, optional 
         spatial resolution in x direction, distance between two pixels
    dy: integer, optional
         spatial resolution in y direction, distance between two pixels
    fps : integer, optional 
         frames per second of the video
    domain : list, optional 
         list of 2, [a,b], specifying the domain to encode
         a in x direction, b in y direction
         will only encode the center of the video with size [a,b]
         if not specified, the whole video screen will be encoded.       
    h5input : bool, optional
         True if the file specified is an h5 file.
         False if the file specified is a video file.
         If not specified, is set to True

    Notes
    -----
    The coordinate system is given by the following
    
    ::    
    
            Row (width / X) major
            
            -----------------------------------------> width
            |   Y
            |   ^-------------------->
            |   |-------------------->
            |   |-------------------->
            |   |--------------------> X
            | 
            |
            v
            height
      
    To specify receptive field parameters, create an object using
    vrf.vrf_gabor() with desired parameters and use function
    VTEM_IAF
    
    Examples
    --------
    >>> import atexit
    >>> import pycuda.driver as cuda
    >>> import numpy as np
    >>> from vtem import vtem,vtdm
    >>> cuda.init()
    >>> context1 = cuda.Device(0).make_context()
    >>> atexit.register(cuda.Context.pop)
    >>> vtem.VTEM_Gabor_IAF('video.h5', 'spikes.h5', 2*np.pi*10)
    
    """
    if h5input:
        h5file = tables.openFile(videofile, 'r')
        videoarray = h5file.root.real
    else:
        videoarray = vio.read_video(videofile)

    _, Py, Px = videoarray.shape
    gb = vrf.vrf_gabor((Py, Px), domain=domain, dx=dx, dy=dy)
    gb.load_parameters()

    VTEM_IAF(videoarray,
             output_filename,
             Wt,
             gb,
             Wx,
             Wy,
             start_frame,
             end_frame,
             dx,
             dy,
             fps,
             domain,
             h5input,
             npinput=True)
    if h5input:
        h5file.close()
예제 #2
0
파일: vtdm.py 프로젝트: bionet/vtem
def VTDM_prepb(spikefile, Dsfilename, dirichfilename, Mx, My = None, 
               domain=None, Wx = None, Wy = None, dtype = np.complex128):
    """
    Prepare decoding with two files Dswfilename and dirichfilename
    must be called before VTDM a new spikefile
    parameters:
    spikefile: the file generated by VTEM containing spike info
    Dsfilename: name of the resulting file, containing the spatial
                dirichlet coefficients for all RFs

    dirichfilename: the name of the resulting file, containing the
                    spatial reconstruction function for all RFs

    Mx: order of dirichlet space in x variable
    My: order of dirichlet space in y variable
        if not specified, My = Mx

    domain: 4-list or 4-tuple, [xstart, xend, ystart, yend]
            the spatial domain to recover
    Wx: bandwidth in x variable
        if not specified, will use the info in spikefile
    Wy: bandwidth in y variable
        if not specified, will use the info in spikefile

    dtype: np.complex128 or np.complex64, accurancy of computing
            dirichlet coefficients and the output files will
            be in the real format derived from dtype

    
    The coordinate system is given by the following:
        
        
        row (width / X) major
        -----------------------------------------> width
        |   Y
        |   ^-------------------->
        |   |-------------------->
        |   |-------------------->
        |   |--------------------> X
        | 
        |
        v
        height
        
    VTDM_prep computes inner product between two RFs and store in Dswfilename. 
    VTDM_prepb computes only the dirichlet coefficient of each RF and stores it in 
    Dsfilename. The inner product is computed in VTDMb.

    """

    if dtype not in [np.complex128, np.complex64]:
        raise TypeError("dtype must be complex128 or complex64")

    spfile = ss.vtem_storage_read(spikefile)

    spfile.read_vrf_type()
    
    num_neurons = spfile.read_num_neurons()
    print "total neurons: %d" % (num_neurons)
    
    Wt,Wx1,Wy1,Px,Py,dx,dy = spfile.read_video_attributes()

    if Wx is None:
        Wx = Wx1
    else:
        Wx = float(Wx)
    if Wy is None:
        Wy = Wy1
    else:
        Wy = float(Wy)

    Mx = int(Mx)
    if My is None:
        My = Mx
    else:
        My = int(My)

    rfSx = (2*np.pi) * (Mx/Wx)
    rfSy = (2*np.pi) * (My/Wy)
    rfPx = int(np.ceil(rfSx/dx/16) * 16)
    rfPy = int(np.ceil(rfSy/dy/16) * 16)
    
    oriSx = Px*dx
    oriSy = Py*dy
    
    if domain is None:
        domain = [-oriSx/2,oriSx/2,-oriSy/2,oriSy/2]
    else:
        domain = [max(float(domain[0]),-oriSx/2), 
                  min(float(domain[1]), oriSx/2), 
                  max(float(domain[2]),-oriSy/2),
                  min(float(domain[3]),oriSy/2)]

    spfile.select_neurons(domain)
    print "select neurons: %d" % (spfile.decode_neurons)

    decSx = domain[1]-domain[0]
    decSy = domain[3]-domain[2]

    decPx = int(np.round(decSx / dx))
    decPy = int(np.round(decSy / dy))


    if spfile.filter_type == "gabor":
        gb = vrf.vrf_gabor((rfPy, rfPx), dx=rfSx/rfPx, dy=rfSy/rfPy, 
                           scale=4, dtype=dtype)
        gb.load_parameters(num_neurons=spfile.decode_neurons, 
                           h_alpha=spfile.alpha, h_l=spfile.l, h_x0=spfile.x0, 
                           h_y0=spfile.y0, h_ab=spfile.ab, KAPPA=spfile.KAPPA)
    else:
        gb = vrf.vrf_cs((rfPy, rfPx), dx=rfSx/rfPx, dy=rfSy/rfPy, 
        	        scale=4, dtype=dtype)
        gb.load_parameters(num_neurons=spfile.decode_neurons, 
                           h_alpha=spfile.alpha, h_x0=spfile.x0, h_y0=spfile.y0,
        		   sigma_center=spfile.sigma_center, 
        		   sigma_surround=spfile.sigma_surround)
    
    d_Ds = gb.compute_Ds(Mx, My)
    write_memory_to_file(d_Ds, Dsfilename)

    d_dirich = gb.compute_dirich_space_fft(d_Ds, Mx, My, decPx, 
                                           decPy, decSx, decSy, Wx, Wy)
    
    write_memory_to_file(d_dirich, dirichfilename)
    del d_dirich

    spfile.close()
예제 #3
0
파일: vtem.py 프로젝트: bionet/vtem
def VTEM_Gabor_IAF(videofile, output_filename, Wt, Wx=2*np.pi*4, Wy=2*np.pi*4,
                   start_frame=0, end_frame=None, dx=1.0/16, dy=1.0/16, 
                   fps=100, domain=None, h5input=True):
    """
    Encode a video with IAF neurons and Gabor receptive field
    with default parameters

    Parameters
    -----------
    videofile : string
         
         ::
         
              Either
              1. Filename of a file containing the input video
              must be stored using write_memory_to_file in simpleio.py
              or using h5write in matlab
              Video array with shape (a,b,c)
              a: total number of frames
              b: number of pixels in y direction
              c: number of pixels in x direction
              c is the leading dimension
              in matlab should be (c,b,a)
              2. Or filename of a video file if h5input is set to False. 
              Will throw an error if OpenCV and the required codec are
              not installed         

    output_filename : string
         output filename that will contain the spike info
    Wt : float
         bandwidth in t variable
         if not specified, will use the info in spikefile
    Wx : float, optional
         bandwidth in x variable
         if not specified, will use the info in spikefile
    Wy : float, optional 
         bandwidth in y variable
         if not specified, will use the info in spikefile
    start_frame : integer, optional 
         starting frame to be encoded in the video  
    end_frame : integer, optional 
         ending frame to be encoded
         if not specified, will encoding to the end of the video
    dx : integer, optional 
         spatial resolution in x direction, distance between two pixels
    dy: integer, optional
         spatial resolution in y direction, distance between two pixels
    fps : integer, optional 
         frames per second of the video
    domain : list, optional 
         list of 2, [a,b], specifying the domain to encode
         a in x direction, b in y direction
         will only encode the center of the video with size [a,b]
         if not specified, the whole video screen will be encoded.       
    h5input : bool, optional
         True if the file specified is an h5 file.
         False if the file specified is a video file.
         If not specified, is set to True

    Notes
    -----
    The coordinate system is given by the following
    
    ::    
    
            Row (width / X) major
            
            -----------------------------------------> width
            |   Y
            |   ^-------------------->
            |   |-------------------->
            |   |-------------------->
            |   |--------------------> X
            | 
            |
            v
            height
      
    To specify receptive field parameters, create an object using
    vrf.vrf_gabor() with desired parameters and use function
    VTEM_IAF
    
    Examples
    --------
    >>> import atexit
    >>> import pycuda.driver as cuda
    >>> import numpy as np
    >>> from vtem import vtem,vtdm
    >>> cuda.init()
    >>> context1 = cuda.Device(0).make_context()
    >>> atexit.register(cuda.Context.pop)
    >>> vtem.VTEM_Gabor_IAF('video.h5', 'spikes.h5', 2*np.pi*10)
    
    """
    if h5input:
        h5file = tables.openFile(videofile, 'r')
        videoarray = h5file.root.real
    else:
        videoarray = vio.read_video(videofile)

    _ , Py, Px = videoarray.shape
    gb = vrf.vrf_gabor((Py,Px), domain=domain, dx=dx, dy=dy)
    gb.load_parameters()
    
    VTEM_IAF(videoarray, output_filename, Wt, gb, Wx, Wy,start_frame, 
             end_frame, dx, dy, fps, domain, h5input, npinput=True)
    if h5input:
        h5file.close()