Brian/StimulusArrayGroup: stim2d.py

File stim2d.py, 1.6 KB (added by goodman, 4 years ago)
Line 
1from brian import *
2import numpy
3import scipy.ndimage as im
4
5__all__ = ['bar','StimulusArrayGroup']
6
7def bar(width, height, thickness, angle):
8    '''
9    An array of given dimensions with a bar of given thickness and angle
10    '''
11    stimulus = numpy.zeros((width, height))
12    stimulus[:,int(height/2.-thickness/2.):int(height/2.+thickness/2.)] = 1.
13    stimulus = im.rotate(stimulus, angle, reshape=False)
14    return stimulus
15
16class StimulusArrayGroup(PoissonGroup):
17    '''
18    A group of neurons which fire with a given stimulus at a given rate
19   
20    The argument ``stimulus`` should be a 2D array with values between 0 and 1.
21    The point in the stimulus array at position (y,x) will correspond to the
22    neuron with index i=y*width+x. This neuron will fire Poisson spikes at
23    ``rate*stimulus[y,x]`` Hz. The stimulus will start at time ``onset``
24    for ``duration``.
25    '''
26    def __init__(self, stimulus, rate, onset, duration):
27        height, width = stimulus.shape
28        stim = stimulus.ravel()*rate
29        self.stimulus = stim
30        def stimfunc(t):
31            if onset<t<(onset+duration):
32                return stim
33            else:
34                return 0.*Hz
35        PoissonGroup.__init__(self, width*height, stimfunc)
36
37if __name__=='__main__':
38    import pylab
39    subplot(121)
40    stim = bar(100,100,10,90)*0.9+0.1
41    pylab.imshow(stim, origin='lower')
42    pylab.gray()
43    G = StimulusArrayGroup(stim, 50*Hz, 100*ms, 100*ms)
44    M = SpikeMonitor(G)
45    run(300*ms)
46    subplot(122)
47    raster_plot(M)
48    axis(xmin=0,xmax=300)
49    show()