added opulation vector project

This commit is contained in:
Jan Benda 2017-01-24 00:15:50 +01:00
parent 4f3dd321f4
commit e22e81fa2a
62 changed files with 296 additions and 0 deletions

View File

@ -0,0 +1,10 @@
latex:
pdflatex *.tex > /dev/null
pdflatex *.tex > /dev/null
clean:
rm -rf *.log *.aux *.zip *.out auto
rm -f `basename *.tex .tex`.pdf
zip: latex
zip `basename *.tex .tex`.zip *.pdf *.dat *.mat *.m

View File

@ -0,0 +1,105 @@
import numpy as np
from scipy.io import savemat
import matplotlib.pyplot as plt
def lifadaptspikes(stimulus, gain=10.0, trials=50, duration=200.0, before=200.0, after=400.0):
dt = 0.1
s0 = 9.5
tau = 10.0
D = 1.0
taua = 60.0
da = 100.0
Vth = 10.0
n = int((duration+before+after)/dt)
delay = 10.0
sig = np.zeros(n) + s0
n1 = int((before+delay)/dt)
n2 = int((before+duration+delay)/dt)
sig[n1:n2] += (gain * stimulus - 1.0) * np.exp(-np.arange(n2-n1)*dt/0.4/duration)
spikes = []
for j in range(trials):
noise = np.sqrt(2.0*D/dt)*np.random.randn(n)
V = np.random.rand()*Vth
A = 0.0
s = s0
As = np.zeros(n)
times = []
for k in range(n):
As[k] = A
V += (-V-A+sig[k]+noise[k])*dt/tau
A += (-A)*dt/taua
if V >= Vth:
V = 0.0
A += da/taua
times.append(k*dt-before)
spikes.append(np.array(times))
#plt.plot(np.arange(n)*dt-before, As)
#plt.plot(np.arange(n)*dt-before, sig)
#return spikes
return spikes
def firingrate(spikes, tmin, tmax):
rates = []
for st in spikes:
times = st[(st>=tmin)&(st<=tmax)]
r = len(times)/(tmax-tmin)
rates.append(1000.0*r)
return np.mean(rates), np.std(rates)
"""
nangles = 12
angles = 180.0*np.arange(nangles)/nangles
rates = np.zeros(nangles)
ratessd = np.zeros(nangles)
allspikes = []
for k, angle in enumerate(angles):
spikes = lifadaptspikes(0.5*(1.0-np.cos(2.0*np.pi*angle/180.0)))
rm, rsd = firingrate(spikes, 0.0, 200.0)
rates[k] = rm
ratessd[k] = rsd
allspikes.append(spikes)
#plt.subplot(2, 5, k+1)
#plt.title('%g' % (angle/2.0/np.pi))
#plt.eventplot(spikes, colors=['r'])
plt.plot(angles, rates, 'r', lw=2)
plt.plot(angles, rates+ratessd, 'b')
plt.plot(angles, rates-ratessd, 'b')
plt.show()
"""
# tuning curves:
nunits = 6
unitphases = np.linspace(0.0, 1.0, nunits) + 0.05*np.random.randn(nunits)/float(nunits)
unitgains = 15.0 + 5.0*(2.0*np.random.rand(nunits)-1.0)
nangles = 12
angles = 180.0*np.arange(nangles)/nangles
for unit, (phase, gain) in enumerate(zip(unitphases, unitgains)):
print '%.1f %.0f' % (gain, phase*180.0)
allspikes = []
for k, angle in enumerate(angles):
spikes = lifadaptspikes(0.5*(1.0-np.cos(2.0*np.pi*(angle/180.0-phase))), gain)
allspikes.append(spikes)
spikesobj = np.zeros((len(allspikes), len(allspikes[0])), dtype=np.object)
for k in range(len(allspikes)):
for j in range(len(allspikes[k])):
spikesobj[k, j] = 0.001*allspikes[k][j]
savemat('unit%d.mat'%(unit+1), mdict={'angles': angles, 'spikes': spikesobj})
# population activity:
nangles = 50
angles = 180.0*np.random.rand(nangles)
for k, angle in enumerate(angles):
print '%.0f' % angle
allspikes = []
for unit, (phase, gain) in enumerate(zip(unitphases, unitgains)):
spikes = lifadaptspikes(0.5*(1.0-np.cos(2.0*np.pi*(angle/180.0-phase))), gain)
allspikes.append(spikes)
spikesobj = np.zeros((len(allspikes), len(allspikes[0])), dtype=np.object)
for i in range(len(allspikes)):
for j in range(len(allspikes[i])):
spikesobj[i, j] = 0.001*allspikes[i][j]
savemat('population%02d.mat'%(k+1), mdict={'spikes': spikesobj,
'angle': angle,
'phases': unitphases,
'gains': unitgains})

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,99 @@
\documentclass[addpoints,11pt]{exam}
\usepackage{url}
\usepackage{color}
\usepackage{hyperref}
\pagestyle{headandfoot}
\runningheadrule
\firstpageheadrule
\firstpageheader{Scientific Computing}{Project Assignment}{11/05/2014
-- 11/06/2014}
%\runningheader{Homework 01}{Page \thepage\ of \numpages}{23. October 2014}
\firstpagefooter{}{}{}
\runningfooter{}{}{}
\pointsinmargin
\bracketedpoints
%\printanswers
%\shadedsolutions
\begin{document}
%%%%%%%%%%%%%%%%%%%%% Submission instructions %%%%%%%%%%%%%%%%%%%%%%%%%
\sffamily
% \begin{flushright}
% \gradetable[h][questions]
% \end{flushright}
\begin{center}
\input{../disclaimer.tex}
\end{center}
%%%%%%%%%%%%%% Questions %%%%%%%%%%%%%%%%%%%%%%%%%
\begin{questions}
\question In the visual cortex V1 orientation sensitive neurons
respond to bars in dependence on their orientation.
How is the orientation of a bar encoded by the activity of a
population of orientation sensisitive neurons?
In an electrophysiological experiment, 6 neurons have been recorded
simultaneously. First, the tuning of these neurons was characteried
by presenting them bars in a range of 12 orientation angles. Each
orientation was presented 50 times. Each of the \texttt{unit*.mat}
files contains the responses of one of the neurons. In there,
\texttt{angles} is a vector with the orientation angles of the bars
in degrees. \texttt{spikes} is a cell array that contains the
vectors of spike times for each angle and presentation. The spike
times are given in seconds. The stimulation with the bar starts a
time $t_0=0$ and ends at time $t_1=200$\,ms.
Then the population activity of the 6 neurons was measured in
response to arbitrarily oriented bars. The responses of the 6
neurons to 50 presentation of a bar are stored in the
\texttt{spikes} variables of the \texttt{population*.mat} files.
The \texttt{angle} variable holds the angle of the presented bar.
\begin{parts}
\part Illustrate the spiking activity of the V1 cells in response
to different orientation angles of the bars by means of spike
raster plots (of one unit).
\part Plot the firing rate of each of the 6 neurons as a function
of the orientation angle of the bar. As the firing rate compute
the number of spikes in the time interval $0<t<200$\,ms divided by
200\,ms. The resulting curves are the tuning curves $r(\varphi)$
of the neurons.
\part Fit the function \[ r(\varphi) =
g(1-\cos(\varphi-\varphi_0))/2 \] to the measured tuning curves in
order to estimated the orientation angle at which the neurons
respond strongest. In this function $\varphi_0$ is the position of
the peak (really? How exactly is $\varphi_0$ related to the
position of the peak? Do you find a better function where
$\varphi_0$ is identical with the peak position?) and $g$ is a
gain factor that sets the maximum firing rate.
\part How can the orientation angle of the presented bar be read
out from the population activity of the 6 neurons? One is the so
called ``population vector''. Think of another (simpler) method.
Load one of the \texttt{population*.mat} files, illustrate the data,
and estimate the orientation angle of the bar by two different methods.
\part Compare, illustrate and discuss the performance of your two
decoding methods.
\end{parts}
\end{questions}
\end{document}
gains and angles of the 6 neurons:
14.6 0
17.1 36
17.6 72
14.1 107
10.7 144
11.4 181

View File

@ -0,0 +1,9 @@
function rate = firingrate(spikes, tmin, tmax)
% mean firing rate between tmin and tmax.
rates = zeros(length(spikes), 1);
for i = 1:length(spikes)
times= spikes{i};
rates(i) = length(times((times>=tmin)&(times<=tmax)))/(tmax-tmin);
end
rate = mean(rates);
end

View File

@ -0,0 +1,43 @@
close all
files = dir('../unit*.mat');
for file = files'
a = load(strcat('../', file.name));
spikes = a.spikes;
angles = a.angles;
figure()
for k = 1:size(spikes, 1)
subplot(3, 4, k)
spikeraster(spikes(k,:), -0.2, 0.6);
end
end
%% tuning curves:
close all
cosine = @(p,xdata)0.5*p(1).*(1.0-cos(2.0*pi*(xdata/180.0-p(2))));
files = dir('../unit*.mat');
figure()
for j = 1:length(files)
file = files(j);
a = load(strcat('../', file.name));
spikes = a.spikes;
angles = a.angles;
rates = zeros(size(spikes, 1), 1);
for k = 1:size(spikes, 1)
r = firingrate(spikes(k,:), 0.0, 0.2);
rates(k) = r;
end
[mr, maxi] = max(rates);
p0 = [mr, angles(maxi)/180.0-0.5];
%p = p0;
p = lsqcurvefit(cosine, p0, angles, rates');
phase = p(2)*180.0
subplot(2, 3, j);
plot(angles, rates, 'b');
hold on;
plot(angles, cosine(p, angles), 'r');
hold off;
xlim([0.0 180.0])
ylim([0.0 50.0])
title(sprintf('unit %d', j))
end

View File

@ -0,0 +1,30 @@
function spikeraster(spikes, tmin, tmax)
% Display a spike raster of the spike times given in spikes.
%
% spikeraster(spikes, tmax)
% spikes: a cell array of vectors of spike times in seconds
% tmin: plot spike raster starting at tmin seconds
% tmax: plot spike raster upto tmax seconds
ntrials = length(spikes);
for k = 1:ntrials
times = spikes{k};
times = times((times>=tmin) & (times<=tmax));
if tmax < 1.5
times = 1000.0*times; % conversion to ms
end
for i = 1:length( times )
line([times(i) times(i)],[k-0.4 k+0.4], 'Color', 'k');
end
end
if (tmax-tmin) < 1.5
xlabel('Time [ms]');
xlim([1000.0*tmin 1000.0*tmax]);
else
xlabel('Time [s]');
xlim([tmin tmax]);
end
ylabel('Trials');
ylim([0.3 ntrials+0.7 ]);
end

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.