diff --git a/linearalgebra/code/spikesortingwave.m b/linearalgebra/code/spikesortingwave.m index d833ad2..ce2ebec 100644 --- a/linearalgebra/code/spikesortingwave.m +++ b/linearalgebra/code/spikesortingwave.m @@ -117,4 +117,26 @@ hold on; scatter( time(tinx(kinx1)), voltage(tinx(kinx1)), 10.0, 'r', 'filled' ); scatter( time(tinx(kinx2)), voltage(tinx(kinx2)), 10.0, 'g', 'filled' ); hold off; + +% ISIs: +figure(4); +clf; +subplot(1, 3, 1) +allisis = diff(spiketimes); +hist(allisis, 20); +title('all spikes') +xlabel('ISI [ms]') +subplot(1, 3, 2) +spikes1 = time(tinx(kinx1)); +isis1 = diff(spikes1); +hist(isis1, 20); +title('neuron 1') +xlabel('ISI [ms]') +subplot(1, 3, 3) +spikes2 = time(tinx(kinx2)); +isis2 = diff(spikes2); +hist(isis2, 20); +title('neuron 2') +xlabel('ISI [ms]') pause + diff --git a/linearalgebra/exercises/Makefile b/linearalgebra/exercises/Makefile new file mode 100644 index 0000000..27691d9 --- /dev/null +++ b/linearalgebra/exercises/Makefile @@ -0,0 +1,34 @@ +TEXFILES=$(wildcard exercises??.tex) +EXERCISES=$(TEXFILES:.tex=.pdf) +SOLUTIONS=$(EXERCISES:exercises%=solutions%) + +.PHONY: pdf exercises solutions watch watchexercises watchsolutions clean + +pdf : $(SOLUTIONS) $(EXERCISES) + +exercises : $(EXERCISES) + +solutions : $(SOLUTIONS) + +$(SOLUTIONS) : solutions%.pdf : exercises%.tex instructions.tex + { echo "\\documentclass[answers,12pt,a4paper,pdftex]{exam}"; sed -e '1d' $<; } > $(patsubst %.pdf,%.tex,$@) + pdflatex -interaction=scrollmode $(patsubst %.pdf,%.tex,$@) | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $(patsubst %.pdf,%.tex,$@) || true + rm $(patsubst %.pdf,%,$@).[!p]* + +$(EXERCISES) : %.pdf : %.tex instructions.tex + pdflatex -interaction=scrollmode $< | tee /dev/stderr | fgrep -q "Rerun to get cross-references right" && pdflatex -interaction=scrollmode $< || true + +watch : + while true; do ! make -q pdf && make pdf; sleep 0.5; done + +watchexercises : + while true; do ! make -q exercises && make exercises; sleep 0.5; done + +watchsolutions : + while true; do ! make -q solutions && make solutions; sleep 0.5; done + +clean : + rm -f *~ *.aux *.log *.out + +cleanup : clean + rm -f $(SOLUTIONS) $(EXERCISES) diff --git a/linearalgebra/exercises/UT_WBMW_Black_RGB.pdf b/linearalgebra/exercises/UT_WBMW_Black_RGB.pdf new file mode 100644 index 0000000..9aed921 Binary files /dev/null and b/linearalgebra/exercises/UT_WBMW_Black_RGB.pdf differ diff --git a/linearalgebra/exercises/exercises01.tex b/linearalgebra/exercises/exercises01.tex new file mode 100644 index 0000000..80433df --- /dev/null +++ b/linearalgebra/exercises/exercises01.tex @@ -0,0 +1,192 @@ +\documentclass[12pt,a4paper,pdftex]{exam} + +\usepackage[english]{babel} +\usepackage{pslatex} +\usepackage[mediumspace,mediumqspace,Gray]{SIunits} % \ohm, \micro +\usepackage{xcolor} +\usepackage{graphicx} +\usepackage[breaklinks=true,bookmarks=true,bookmarksopen=true,pdfpagemode=UseNone,pdfstartview=FitH,colorlinks=true,citecolor=blue]{hyperref} + +%%%%% layout %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\usepackage[left=20mm,right=20mm,top=25mm,bottom=25mm]{geometry} +\pagestyle{headandfoot} +\ifprintanswers +\newcommand{\stitle}{: Solutions} +\else +\newcommand{\stitle}{} +\fi +\header{{\bfseries\large Exercise\stitle}}{{\bfseries\large PCA}}{{\bfseries\large January 7th, 2019}} +\firstpagefooter{Prof. Dr. Jan Benda}{Phone: 29 74573}{Email: +jan.benda@uni-tuebingen.de} +\runningfooter{}{\thepage}{} + +\setlength{\baselineskip}{15pt} +\setlength{\parindent}{0.0cm} +\setlength{\parskip}{0.3cm} +\renewcommand{\baselinestretch}{1.15} + +%%%%% listings %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\usepackage{listings} +\lstset{ + language=Matlab, + basicstyle=\ttfamily\footnotesize, + numbers=left, + numberstyle=\tiny, + title=\lstname, + showstringspaces=false, + commentstyle=\itshape\color{darkgray}, + breaklines=true, + breakautoindent=true, + columns=flexible, + frame=single, + xleftmargin=1em, + xrightmargin=1em, + aboveskip=10pt +} + +%%%%% math stuff: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\usepackage{amsmath} +\usepackage{amssymb} +\usepackage{bm} +\usepackage{dsfont} +\newcommand{\naZ}{\mathds{N}} +\newcommand{\gaZ}{\mathds{Z}} +\newcommand{\raZ}{\mathds{Q}} +\newcommand{\reZ}{\mathds{R}} +\newcommand{\reZp}{\mathds{R^+}} +\newcommand{\reZpN}{\mathds{R^+_0}} +\newcommand{\koZ}{\mathds{C}} + +%%%%% page breaks %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\newcommand{\continue}{\ifprintanswers% +\else +\vfill\hspace*{\fill}$\rightarrow$\newpage% +\fi} +\newcommand{\continuepage}{\ifprintanswers% +\newpage +\else +\vfill\hspace*{\fill}$\rightarrow$\newpage% +\fi} +\newcommand{\newsolutionpage}{\ifprintanswers% +\newpage% +\else +\fi} + +%%%%% new commands %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\newcommand{\qt}[1]{\textbf{#1}\\} +\newcommand{\pref}[1]{(\ref{#1})} +\newcommand{\extra}{--- Zusatzaufgabe ---\ \mbox{}} +\newcommand{\code}[1]{\texttt{#1}} + + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +\begin{document} + +\input{instructions} + +\begin{questions} + + \question \qt{Covariance and correlation coefficient\vspace{-3ex}} + \begin{parts} + \part Generate two vectors $x$ and $z$ with $n=1000$ Gausian distributed random numbers. + \part Compute $y$ as a linear combination of $x$ and $z$ according to + \[ y = r \cdot x + \sqrt{1-r^2}\cdot z \] + where $r$ is a parameter $-1 \le r \le 1$. + What does $r$ do? + \part Plot a scatter plot of $y$ versus $x$ for about 10 different values of $r$. + What do you observe? + \part Also compute the covariance matrix and the correlation + coefficient matrix between $x$ and $y$ (functions \texttt{cov} and + \texttt{corrcoef}). How do these matrices look like for different + values of $r$? How do the values of the matrices change if you generate + $x$ and $z$ with larger variances? + \part Do the same analysis (scatter plot, covariance, and correlation coefficient) + for \[ y = x^2 + 0.5 \cdot z \] + Are $x$ and $y$ really independent? + \end{parts} + + \question \qt{Principal component analysis in 2D\vspace{-3ex}} + \begin{parts} + \part Generate pairs $(x,y)$ of Gaussian distributed random numbers such + that all $x$ values have zero mean, half of the $y$ values have mean $+d$ + and the other half mean $-d$, with $d \ge0$. + \part Plot scatter plots of the pairs $(x,y)$ for $d=0$, 1, 2, 3, 4 and 5. + Also plot a histogram of the $x$ values. + \part Apply PCA on the data and plot a histogram of the data projected onto + the PCA axis with the largest eigenvalue. + What do you observe? + \end{parts} + + \question \qt{Principal component analysis in 3D\vspace{-3ex}} + \begin{parts} + \part Generate triplets $(x,y,z)$ of Gaussian distributed random numbers such + that all $x$ values have zero mean, half of the $y$ and $z$ values have mean $+d$ + and the other half mean $-d$, with $d \ge0$. + \part Plot 3D scatter plots of the pairs $(x,y)$ for $d=0$, 1, 2, 3, 4 and 5. + Also plot a histogram of the $x$ values. + \part Apply PCA on the data and plot a histogram of the data projected onto + the PCA axis with the largest eigenvalue. + What do you observe? + \end{parts} + + \continue + \question \qt{Spike sorting} + Extracellular recordings often pick up action potentials originating + from more than a single neuron. In case the waveforms of the action + potentials differ between the neurons one could assign each action + potential to the neuron it originated from. This process is called + ``spike sorting''. Here we explore this methods on a simulated + recording that contains action potentials from two different + neurons. + \begin{parts} + \part Load the data from the file \texttt{extdata.mat}. This file + contains the voltage trace of the recording (\texttt{voltage}), + the corresponding time vector in seconds (\texttt{time}), and a + vector containing the times of the peaks of detected action + potentials (\texttt{spiketimes}). Further, and in contrast to real + data, the waveforms of the actionpotentials of the two neurons + (\texttt{waveform1} and \texttt{waveform2}) and the corresponding + time vector (\texttt{waveformt}) are also contained in the file. + + \part Plot the voltage trace and mark the peaks of the detected + action potentials. Zoom into the plot and look whether you can + differentiate between two different waveforms of action + potentials. How do they differ? + + \part Cut out the waveform of each action potential (5\,ms before + and after the peak). Plot all these snippets in a single + plot. Can you differentiate the two actionpotential waveforms? + + \part Apply PCA on the waveform snippets, that is compute the + eigenvalues and eigenvectors of their covariance matrix, and plot + the sorted eigenvalues (the ``eigenvalue spectrum''). How many + eigenvalues are clearly larger than zero? + + \part Plot the two eigenvectors (``features'') with the two + largest eigenvalues. + + \part Project the waveform snippets onto these two eigenvectors + and display them with a scatter plot. What do you observe? Can you + separate two ``clouds'' of data points (``clusters'')? + + \part Think about a very simply way how to separate the two + clusters. Generate a vector whose elements label the action + potentials, e.g. that contains '1' for all snippets belonging to + the one cluster and '2' for the waveforms of the other + cluster. Use this vector to mark the two clusters in the previous + plot with two different colors. + + \part Plot the waveform snippets of each cluster together with the + true waveform obtained from the data file. Do they match? + + \part Mark the action potentials in the recording according to + their cluster identity. + + \part Compute interspike-interval histograms of all the (unsorted) + action potentials, and of each of the two neurons. What do they + tell you? + \end{parts} + +\end{questions} + +\end{document} \ No newline at end of file diff --git a/linearalgebra/exercises/instructions.tex b/linearalgebra/exercises/instructions.tex new file mode 100644 index 0000000..3041d3e --- /dev/null +++ b/linearalgebra/exercises/instructions.tex @@ -0,0 +1,6 @@ +\vspace*{-7.8ex} +\begin{center} +\textbf{\Large Introduction to Scientific Computing}\\[2.3ex] +{\large Jan Grewe, Jan Benda}\\[-3ex] +Neuroethology Lab \hfill --- \hfill Institute for Neurobiology \hfill --- \hfill \includegraphics[width=0.28\textwidth]{UT_WBMW_Black_RGB} \\ +\end{center}