\documentclass[letterpaper]{article}

\usepackage{amsmath}
\usepackage{gensymb}
\usepackage[vmargin=1in,hmargin=1.25in]{geometry}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{microtype}
\usepackage{lmodern}
\usepackage{tikz}
\usetikzlibrary{matrix}

\author{Philip Pham}
\title{\Large ECE/CSE 576, Spring 2019 Homework 2: Creating Panoramas}
\date{\today}

\begin{document}
\maketitle

\section{Harris Corner Detector}
\label{sec:harris}
Gaussian blurring was applied with $\sigma = 2$. The Harris response threshold
was $50$, and non-maximal suppression was done with a $5 \times 5$
window. Detected corners are denoted with a red cross. As a sanity check, we
apply the algorithm to \texttt{Boxes.png}.

\begin{center}
  \includegraphics{1a.png}
\end{center}

\begin{center}
  \includegraphics[width=0.73\textwidth]{1b.png}

  Applied to \texttt{Rainier1.png}.
\end{center}

\begin{center}
  \includegraphics[width=0.73\textwidth]{1c.png}

  Applied to \texttt{Rainier2.png}.
\end{center}

\section{Match Corner Points}

The $l_1$ norm applied to the feature descriptor was used to find matching
corner points in the other image.

\begin{center}
  \includegraphics[width=0.85\textwidth]{2a.png}

  All matching corner points in \texttt{Rainier1.png}.
\end{center}

\begin{center}
  \includegraphics[width=0.85\textwidth]{2b.png}

  All matching corner points in \texttt{Rainier2.png}.
\end{center}

\section{RANSAC}

Homographies were sampled by choosing 4 matches. The homography with the largest
number of inliers was chosen.

\begin{center}
  \includegraphics[width=0.85\textwidth]{3a.png}

  Matches from the homography with the largest number of inliers in
  \texttt{Rainier1.png}.
\end{center}

\begin{center}
  \includegraphics[width=0.85\textwidth]{3b.png}

  Matches from the homography with the largest number of inliers in
  \texttt{Rainier2.png}.
\end{center}

\section{Stitch}
\label{sec:stitch}

A flat panorama was made by using the first image as the reference coordinate
system.

\begin{center}
  \includegraphics[width=\textwidth]{4.png}

  The result of stitching \texttt{Ranier1.png} and \texttt{Ranier2.png}.
\end{center}

\section*{Bell: Complete Mt. Ranier Panorama}

The same technique in Section \ref{sec:stitch} can be applied repeatedly to get a
complete panorama.

\begin{center}
  \includegraphics[width=\textwidth]{all_stitched.png}

  The result of stitching \texttt{Ranier1.png}, \texttt{Ranier2.png},
  \texttt{Ranier3.png}, \texttt{Ranier4.png}, \texttt{Ranier5.png}, and
  \texttt{Ranier6.png}.
\end{center}

\section*{Whistle: My Own Panorama}

I took pictures of my office building with my cell phone camera and stiched them
together. Images were resized to $480 \times 640$, but otherwise, untouched.

\begin{center}
  \includegraphics[width=0.32\textwidth]{google3.jpg}
  \includegraphics[width=0.32\textwidth]{google1.jpg}
  \includegraphics[width=0.32\textwidth]{google2.jpg}

  3 pictures of my office.
\end{center}

The center image was used as the reference coordinate system.

\begin{center}
  \includegraphics[width=\textwidth]{google_stitched.png}
\end{center}

\section*{Whistle: Hanging}

To deal with the rotated image, I created a new 16-dimensional feature
descriptor. Consider a $9 \times 9$ window. Each dimension is the difference of
the green channel of a pixel on the border with that of center pixel after
applying a Gaussian blurring with $\sigma = 2.0$. The $0$-indexed dimensions
correspond to the following pixels.

\begin{center}
  \begin{tikzpicture}
    \draw[step=0.5cm,color=gray] (-2.0,-2.0) grid (2.5,2.5);
    \node at (-1.75,2.25) {$0$};
    \node at (-0.75,2.25) {$1$};
    \node at (0.25,2.25) {$2$};
    \node at (1.25,2.25) {$3$};
    \node at (2.25,2.25) {$4$};
    \node at (2.25,1.25) {$5$};
    \node at (2.25,0.25) {$6$};
    \node at (2.25,-0.75) {$7$};
    \node at (2.25,-1.75) {$8$};
    \node at (1.25,-1.75) {$9$};
    \node at (0.25,-1.75) {$10$};
    \node at (-0.75,-1.75) {$11$};
    \node at (-1.75,-1.75) {$12$};
    \node at (-1.75,-0.75) {$13$};
    \node at (-1.75,0.25) {$14$};
    \node at (-1.75,1.25) {$15$};
    \node[fill=green] at (0.25,0.25) {$P$};
  \end{tikzpicture}
  \hspace{0.25cm}
\end{center}

Then, to make the distance metric rotation-invariant the distance between two
feature descriptors for pixels $P_1$ and $P_2$ is
\begin{equation}
  d\left(P_1,P_2\right) = \min_k\left(
    \sum_{j=0}^{15}\left\lvert
      P_1\left[j\right] - P_2\left[\left(j + 2k\right) \bmod 16\right]
      \right\rvert
    \right),
    \label{eqn:custom_metric}
\end{equation}
which computes the minimum $l_1$ distance over all $k\pi/4$ rotations.

\texttt{DESC\_SIZE} in \texttt{mainwindow.h} was changed to $16$.

\begin{center}
  \includegraphics[width=\textwidth]{hanging.png}

  \texttt{Hanging1.png} and \texttt{Hanging2.png} by applying a 16-dimensional
  feature descriptor and Equation \ref{eqn:custom_metric}.
\end{center}

\section*{Whistle: Center-weighting}

In Section \ref{sec:stitch} the seam between the two stitched images is
apparent. Center-weighting can be used to make seam invisible.

\begin{center}
  \includegraphics[width=\textwidth]{4_center_weighted.png}

  The result of stitching \texttt{Ranier1.png} and \texttt{Ranier2.png} with
  center-weighting.
\end{center}

The same can be done with the complete panorama.

\begin{center}
  \includegraphics[width=\textwidth]{all_stitched_center_weighted.png}

  The result of stitching \texttt{Ranier1.png}, \texttt{Ranier2.png},
  \texttt{Ranier3.png}, \texttt{Ranier4.png}, \texttt{Ranier5.png}, and
  \texttt{Ranier6.png} with center-weighting.
\end{center}

\begin{center}
  \includegraphics[width=\textwidth]{google_stitched_center_weighted.png}

  The Google campus with centering-weighting.
\end{center}

\begin{center}
  \includegraphics[width=\textwidth]{hanging_center_weighted.png}

  \texttt{Hanging1.png} and \texttt{Hanging2.png} stictched and center-weighted.
\end{center}

\section*{Appendix}

All code used to generate these images can be found at
\href{https://github.com/ppham27/cse576/blob/master/hw2}{\texttt{ppham27/cse576/hw2}}. The
embedded JPEG, PNG files, and the \LaTeX can be found in
\href{https://github.com/ppham27/cse576/blob/master/hw2/report}{\texttt{ppham27/cse576/hw2/report}}.

\end{document}


\section{On image correction}
\label{imaging:correction}

Fluorescence microscopy images contain non-biological data,
distortions, and artifacts that may confound analytical results if
not addressed.
In the terms of the image model from the previous section
(\ref{eq:imaging:simpleFullModel}), the
subject of study in fluorescence imaging is contained within a subset of the
foreground layers. All non-foreground components should thus
be removed in order to obtain meaningful quantitative results.
Ideally, the foreground layers that are not of interest (especially
artifacts) should also be removed, though this is a much more difficult
problem that I do not address in this dissertation.
In essence, from the original image $I=D+S(F+B)\epsilon$, we need to
obtain $F$: obtaining $F$ is the goal of image correction.

It is important to be aware that image correction is not a solved
problem. There are many ways in which it can be done, several of which I review
in \ar{imaging:correction:review}, but the most commonly-used methods produce
incomplete correction and/or are prone
to generating artifacts. Further, methods sections of papers that
rely on imaging often do not explain their correction methodology
at all, making it difficult to evaluate some published results.
I therefore make the importance of image correction a focus
of this chapter, and describe an image correction
approach that I developed for accurate determination of $F$
in the difficult context of high-throughput microscopy
\ar{imaging:correction:myMethod} \cite{Coster2014}.

Before going into the details of image correction, I should
first address whether this step is even necessary.
After all, any processing step
could introduce artifacts and thus potentially do more harm than good.
Normal cell-to-cell variability is already relatively high,
with standard deviations of
$\sim$15-30\% for protein concentrations \cite{Sigal2006a}. One might then
wonder whether error introduced by non-foreground image components
would make much of a difference. Here I show that it can
indeed make a difference. Unsurprisingly,
the size of the difference (and thus the importance) is highly
dependent on the properties of each particular image dataset,
the measurement methods used, and the experimental goals.

\subsection{Non-foreground components distort single-cell phenotypes}
\label{imaging:distortion}

A particular cellular phenotype can be represented
as a set of measurements.
For example, a cell phenotype might be composed of
its measured size, shape, texture,
and average fluorescence intensities in multiple
color channels. Each measurement is
generally referred to as a ``feature.'' The diversity
of features that can be measured
for a  single cell is only limited by the imagination
of the investigator. It would therefore
be impossible to develop a single
comprehensive argument for the importance
of image correction that covers all possible
ways of measuring cellular phenotypes.
Instead, I make a case study of several
commonly-used and biologically-interpretable
single-cell features: the average, total,
and ratios of fluorescence intensity.

    \begin{table}[!bt]
    \caption[Table of symbols for average, total, and ratiometric features.]
    {Symbols used in the mathematical derivations of the consequences of $S$
    and $B$ on single-cell feature distributions.}
    \label{table:imaging:symbols}
    \centering
    \begin{tabular}{cl}
    \hline
    Symbol & Meaning \\ \hline
    $\E[X]$   & Expected value (i.e. the mean) of the random variable $X$ \\
    $F_c$	 & Average foreground intensity within cell $c$ \\
    $S_c$	 & Average shading value within cell $c$ \\
    $B$	     & Background intensity per pixel, a constant\\
    $\alpha_c$ & Area of cell $c$ \\
    $A$	     & Average intensity feature \\
    $T$	     & Total intensity feature \\
    $R$	     & Ratio of intensities feature \\
    $Z_c$    & The value of feature $Z$ for a single cell, $c$.\\
    $\sigma_Z$ & The standard deviation of feature $Z$ across all cells. \\
    $\mu_Z$    & The mean of feature $Z$ across all cells. \\
    $cv_Z$     & The coefficient of variation of feature $Z$ across all cells  ($\sigma_Z/\mu_Z$). \\
    \hline
    \end{tabular}
    \end{table}

How to quantify the effects of image correction on data is not obvious,
since that data can be used in many ways depending on experimental goals.
There are, however, aspects of
single-cell distributions that are meaningful across a broad array of
experiments, and so I use these as metrics when measuring the consequences
of image correction. These are the mean ($\mu$), standard deviation ($\sigma$),
and coefficient of variation ($cv=\sigma/\mu$) of a feature across a population of cells.

The standard deviation and $cv$ are measures of distribution widths, which
are used to determine the statistical separability of distributions.
Inaccurate measurements of the true variability may
consequently reduce statistical power.
The mean of a feature distribution, on the other hand,
is typically used to determine how large of an
effect an experimental perturbation has had. Inaccurate measurements of the true mean
may cause strong results to appear weak, or vice versa, leading to false
negatives or false positives. These distribution metrics are
therefore useful as readouts for the utility of image correction.

The mathematics in the following discussion were
worked out in conjunction with my co-authors on the relevant
publication \cite{Coster2014}: Satwik Rajaram, Chonlarat Wichaidit,
Steven Altschuler, and Lani Wu. The text
and figures draw heavily from the same publication. Those readers
who do not need convincing that image correction is important may skip
to page~\pageref{imaging:correction:review} without a loss in coherence
of this chapter.

  \begin{figure}[!bt]
  \centering
  \includegraphics[width=4.5in]{FIGS/imaging/SBeffects.pdf}
  {\singlespacing
  \caption[ Case study of the effects of $S$ and $B$ on single-cell features.]
            { Image background and shading cause feature-dependent changes
            in estimates of phenotypic variability.
            Human colonic epithelial cells ($n>3700$), stained for DNA
            (using Hoechst) and Smad (using a Smad2/3 antibody), imaged
            at 10X.
            The distributions of
            nuclear feature values were then compared
            before (black histograms) and after (dotted blue histograms)
            artificial background (top row, with
            background $\sim$16\% of Hoechst or $\sim$50\% of Smad foreground)
            or shading (bottom row, linear
            gradient with maximum 1.5 fold intensity difference) were added
            to each image. $\mu$, mean; $\sigma$, standard deviation;
            $cv=\sigma/\mu$. Inset, top left,
            relative size of change to the shown distributions. Inset, top right,
            arrows indicate the direction of
            change in the general case (question marks indicate uncertainty due to
            dependency on other variables). $x$-axes in arbitrary fluorescence units.
            A version of this figure is published as Fig. 1a in \cite{Coster2014}.}
  \label{fig:imaging:SBeffects}}
  \end{figure}

\subsubsection{Mathematical definitions of commonly used single-cell features}

Commonly used single-cell measurements include pixel intensity averages,
totals, and ratios within some cellular compartment $c$ (such as the
nucleus, cytosol, or whole cell).
By defining these features mathematically we
can determine their general behaviors as a consequence of the presence of image background
or shading. Refer to \ar{table:imaging:symbols} for the list of mathematical
symbols, to \ar{table:imaging:properties} for a summary of the statistical
properties used in the mathematical derivations, and
\ar{fig:imaging:SBeffects} for a case study of these behaviors.

For each cellular object, I define $F_c$ and $B_c$ as the average foreground
and background intensities within $c$, while $S_c$ is the
average shading. $B_c$ is a constant for all cells,
as the background is assumed to be the same for all
pixels in the absence of noise, and so I drop the subscript from this term.
I refer to the area of each cell, measured in pixels, as $\alpha_c$.
Finally, for the derivations I assume that the detector value has
been subtracted from all pixels
and that the image contains no measurement noise  ($D=0$
and $\epsilon=1$).

For each cell I can then define the three simple intensity features: total intensity $T$,
average intensity $A$, and the ratio of intensities $R$ between two independent foreground
signals $F_{c1}$ and $F_{c2}$ (e.g. the ratio of nuclear Hoechst and Smad intensities, as in
\ar{fig:imaging:SBeffects}). Because the ratio takes two signals into account, each may
come from a distinct fluorophore and optical setup and so have
distinct shading and background values. Additionally, those features could be
defined within distinct cellular compartments, such that the compartment
sizes may also differ.
The three features are thus defined for single cells by
Equations~\ref{eq:correction:average}\nobreakdash-\ref{eq:correction:ratio}.
	%
	\begin{align}
	A_c & = S_c (F_c+B) \label{eq:correction:average}\\
	T_c & = \alpha_c A_c = \alpha_c S_c (F_c+B) \label{eq:correction:total}\\
	R_c & = \frac{ \alpha_{c1} S_{c1}(F_{c1}+B_1) }{ \alpha_{c2} S_{c2}(F_{c2}+B_2) }  \label{eq:correction:ratio}
	\end{align}

For simplicity of the following analysis, I take the special case where
$F_c$, $S_c$, $\alpha_c$, and $B$ are all
statistically independent (i.e. cells do not
spatially arrange themselves within an image
by phenotype, and foreground intensity is
independent of cell size). Further, I
assume that cells are small relative to the
spatial rate of change of $S$ across an image.
These assumptions
allow me to use the properties in
\ar{table:imaging:properties}. Note that these assumptions
will be valid for some experimental cases, but
certainly not all. For the case study
shown in \ar{fig:imaging:SBeffects} they are appropriate:
I verified that nuclear size and staining intensity
were uncorrelated with each other and with position, and that the two
foreground signals (Hoechst and Smad2/3) are independent (data not shown).

Finally, I noted earlier that units of fluorescence are typically arbitrary
and that $S$ causes a multiplicative change in relative intensity across
an image. This means
that we are free to choose how to define the expected value of $S$: a
value other than 1 would cause a scaling of the foreground and
background values, but this scaling would retain the relative relationship between
all measured intensities and so would be of no consequence.
For convenience, then, I define shading so that
its expectation value across all images and pixels is $\E[S]=1$
so that, for a large number of cells, $\E[S_c]\approx 1$. This definition simplifies
the mathematical derivations below, as the $\E[S_c]$ term can be dropped
from several formulae.

    \begin{table}[!bt]
    \centering
    \caption[Table of useful statistical properties.]{
    Statistical properties used in the derivations of the effects
    of shading $S$ and background on the distributions of single-cell
    average $A$, total $T$, and ratio $R$ intensity features. $X$ and $Y$ are independent
    random variables, and $k$ is a constant.}
    \label{table:imaging:properties}
    \begin{tabular}{ccl}
    \hline
    Index & Property \\ \hline
    1 & $\E[S_c] \equiv 1$  \\
    2 & $\mu_{X+k} \equiv \E[X+k] = \E[X]+k$ \\
    3 & $\mu_{XY} = \E[X]\E[Y]$ \\
    4 & $\sigma^2_X \equiv \Var[X] =
        \E[X^2]-\E[X]^2 \implies \E[X^2]\geq \E[X]^2$\\
    5 & $\sigma^2_{[X+k]} = \sigma^2_X$ \\
    6 & $\sigma^2_{XY} = \E[X^2]\E[Y^2]-\E[X]^2 \E[Y]^2$ \\
    \hline
    \end{tabular}
    \end{table}

\subsubsection{Effects of background $B$ on the average intensity feature $A$}

For this case, we ignore the effects of $S$ and focus on $B$.
We therefore set $B>0$ and $S_c=1$, so that the average feature
from \ar{eq:correction:average} simplifies to $A_c=F_c+B$.
This results in the distribution properties for this feature in
Equations~\ref{eq:correction:bamu}\nobreakdash-\ref{eq:correction:bacv}.
	%
    \begin{align}
    \mu_A    & \equiv \E[A_c] = \E[F_c+B] = \E[F_c] + B  \label{eq:correction:bamu}\\
    \sigma_A & \equiv \sqrt{\Var[A_c]} = \sqrt{\sigma^2_{F_c+B}} = \sigma_{F_c} \label{eq:correction:basd}\\
    cv_A     & \equiv \frac{\sigma_A}{\mu_A} = \frac{\sigma_{F_C}}{\E[F_c]+B} \label{eq:correction:bacv}
    \end{align}

It is clear that $B$ will always cause
an increase in the mean of average intensities, $\mu_A$ \arp{eq:correction:bamu}.
The standard deviation, $\sigma_A$, is
unaffected by background (\ar{eq:correction:bamu}, using Property 5 from \ar{table:imaging:properties}).
As a consequence of the
constant $\sigma_A$ and increased $\mu_A$, the coefficient of variation
will decrease with increasing background. In summary, background will cause
an overestimation of $\mu_A$, an underestimation of $cv_A$, and will not affect
$\sigma_A$ (ee the case study in \ar{fig:imaging:SBeffects}, top left panel).

\subsubsection{Effects of background $B$ on the total intensity feature $T$}

The situation is the same as the previous case,
except with the inclusion of cell size.
The total intensity feature \ar{eq:correction:total}
therefore simplifies to $T_c=\alpha_c (F_c+B)$,
resulting in the distribution properties shown in
Equations~\ref{eq:correction:btmu}\nobreakdash-\ref{eq:correction:btcv}.
	%
    \begin{align}
    \mu_T    & \equiv \E[T_c] = \E[\alpha_c(F_c+B)]=\E[\alpha_c](\E[F_c]+B) =
        \E[\alpha_c]\E[F_c]+\E[\alpha_c]B
        \label{eq:correction:btmu}\\
    \sigma_T & \equiv \sqrt{\Var[T_c]} = \sqrt{\sigma^2_{\alpha_c(F_c+B)}} =
        \sqrt{\sigma^2_{\alpha_c F_c} + 2\sigma^2_{\alpha_c}\E[F_c]B
        + \sigma^2_{\alpha_c}B^2}
        \label{eq:correction:btsd}\\
    cv_T     & \equiv \frac{\sigma_T}{\mu_T} =
        \frac{ \sqrt{\sigma^2_{\alpha_c F_c}+2\sigma^2\E[F_c]B+ \sigma^2_{\alpha_c}B^2 } }
        { \E[\alpha_c]\E[F_c] + \E[\alpha_c]B }
        \label{eq:correction:btcv}
    \end{align}

Though somewhat less obvious than for the average feature,
it should be clear that increasing $B$ will cause
an increase in the mean of total intensities, $\mu_T$
(\ar{eq:correction:btmu}, using Property
3 from \ar{table:imaging:properties}). Importantly,
each cell will be affected differently by background, depending on its size.
Unlike the average feature, the standard deviation $\sigma_T$
increases with background (\ar{eq:correction:btsd},
using Properties 3 \& 6 from \ar{table:imaging:properties}).

Because both $\sigma_T$ and increased $\mu_T$ increase with
increasing $B$, it is not immediately obvious what the effect should be on the
coefficient of variation, $cv_T$ (as the numerator and
denominator in \ar{eq:correction:btcv} both are proportional to $B$).
However, if we take the derivative of the $cv$ with respect to a changing
background, we get \ar{eq:correction:btcvdb}. Because this derivative
is always $\leq 0$, the $cv_T$ will decrease with increasing
background. In summary, background will cause
cell size-dependent overestimation of $\mu_T$ and $\sigma_T$,
and underestimation of $cv_T$ (see the case study in
\ar{fig:imaging:SBeffects}, top middle panel).
	%
    \begin{equation} \label{eq:correction:btcvdb}
    \frac{d}{dB}(cv_T^2)= -2 \frac{ \sigma^2_{F_c}\E[\alpha_c^2] }
        { \E[\alpha_c]^2(\E[F_c]+B)^3 } \leq 0
    \end{equation}

\subsubsection{Effects of shading $S$ on the average intensity feature $A$}

We now move on to the effects of shading on the average and total features,
and therefore set $B=0$. Note that increasing the magnitude of $\E[S_c]$
will have no effect on any of these features, as it is the same as a change in
units. Because shading is a variation in intensity across an image,
we can therefore modulate its strength by
changing the variance of this image component.
The larger the variance, the more shading. For this case, then,
we set $\Var[S_c]>0$.
The average intensity feature from \ar{eq:correction:average} therefore
simplifies to $A_c=S_c F_c$.
This results in the distribution properties shown in
Equations~\ref{eq:correction:samu}\nobreakdash-\ref{eq:correction:sacv}.
	%
    \begin{align}
    \mu_A    & \equiv \E[A_c] = \E[S_c F_c] = \E[S_c]\E[F_c]=\E[F_c]
        \label{eq:correction:samu}\\
    \sigma_A & = \sqrt{\sigma^2_{S_c F_c}} =  \sqrt{ \E[S_c^2]\E[F_c^2]-\E[S_c]^2\E[F_c]^2 }
        = \sqrt{ \E[S_c^2]\E[F_c^2]-\E[F_c]^2 }
        \label{eq:correction:sasd}\\
    cv_A     & \equiv \frac{\sigma_A}{\mu_A} = \frac{ \sqrt{\E[S_c^2]\E[F_c^2]-\E[F_c]^2} }
        { \E[F_c] }
        \label{eq:correction:sacv}
    \end{align}

From \ar{eq:correction:samu} it is obvious that $S$ has no effect on
the mean of average intensities, $\mu_A$, as it falls out of the formula entirely
(using Properties 1 \& 3 from \ar{table:imaging:properties}).
The standard deviation, $\sigma_A$, however will increase
with background (\ar{eq:correction:sasd},
using Properties 1, 4, \& 6 from \ar{table:imaging:properties}).
As a consequence of the
increasing $\sigma_A$ and constant $\mu_A$, the coefficient of variation
increases with increasing background. In summary, shading will cause
overestimation of variation for $A$
(see the case study in \ar{fig:imaging:SBeffects}, bottom left panel).
Shading does not affect $\mu_A$, but this
is not surprising since I defined shading to have a mean of 1 specifically
so that it would not affect the mean.

\subsubsection{Effects of shading $S$ on the total intensity feature $T$}

The situation is the same as the previous case, except with the inclusion of cell size.
The total intensity feature \ar{eq:correction:total}
therefore simplifies to $T_c=\alpha_c S_c F_c$.
This results in the total intensity distribution properties shown in
Equations~\ref{eq:correction:stmu}\nobreakdash-\ref{eq:correction:stcv}.
	%
    \begin{align}
    \mu_T    & \equiv \E[T_c] = \E[\alpha_c S_c F_c] = \E[S_c]\E[\alpha_c F_c]=
        \E[\alpha_c]\E[F_c]
        \label{eq:correction:stmu}\\
    \sigma_T & = \sqrt{\sigma^2_{S_c F_c}} =  \sqrt{ \E[S_c^2]\E[\alpha_c^2 F_c^2]-
        \E[S_c]^2\E[\alpha_c F_c]^2 }
        = \sqrt{ \E[S_c^2]\E[\alpha_c^2 F_c^2]-\E[\alpha_c F_c]^2 }
        \label{eq:correction:stsd}\\
    cv_T     & \equiv \frac{\sigma_A}{\mu_A} = \frac{ \sqrt{\E[S_c^2]\E[\alpha_c^2 F_c^2]-
        \E[\alpha_c F_c]^2} }
        { \E[\alpha_c F_c] }
        \label{eq:correction:stcv}
    \end{align}

The derivation is nearly the same as that for the
previous case for the average feature.
As before, from \ar{eq:correction:stmu} we see that $S$ has no effect on
the mean of total intensities, $\mu_T$. Also as before,
the standard deviation, $\sigma_T$, will increase
with shading, as will $cv_T$
(see the case study in \ar{fig:imaging:SBeffects}, bottom middle panel).
Thus, increasing the shading always artificially
increases the variation of the average and total intensity features.

\subsubsection{Effects of $B$ and $S$ on $R$}

I now turn to the ratiometric feature \arp{eq:correction:ratio},
which turns out to be
the least generalizable even within the narrow constraints defined
at the start of this section. And so to simplify, I further constrain
the discussion to the ratio of average intensities. This allows us
to set $\alpha=1$ for both signals (note that these terms also
cancel when taking the ratio within a single compartment). Thus, we have
\ar{eq:correction:ratioMeans}.
	%
	\begin{equation} \label{eq:correction:ratioMeans}
	R_c = \frac{ S_{c1}(F_{c1}+B_1) }
	{S_{c2}(F_{c2}+B_2) }
	\end{equation}

There are a few distinct cases we can examine to get an idea of
how this ratio behaves. In the first case, we can have the signals
originating from the same channel (for example, the ratio of
nuclear to cytosolic Smad2/3). Here, $B_1=B_2$ and, under my
earlier assumption that cells are small relative to the rate
of change of $S$, $S_{c1}=S_{c2}$. The ratio thus becomes
$R_c=\frac{F_{c1}+B}{F_{c2}+B}$ and is unaffected by shading.
The effects of $B$ are less obvious because it
is in both the numerator and denominator. In the limiting case,
however, $\lim_{B\rightarrow \infty} R_c=1$. Since all values
are forced to 1 with large $B$, the variation across the population
will be forced to 0. In less extreme
cases, however, background can either cause an increase
or decrease in
the apparent variation, depending on its size relative to
both foreground terms. Because of this, the error in the
measured ratio can vary from cell to cell within the same
population. For example, if two cells have the same ratio
but different absolute foreground values, $B$ will cause their
ratios to diverge (e.g. $\frac{1+B}{2+B}\neq\frac{10+B}{20+B}$).
This effect causes the scrambling seen in \ar{fig:imaging:backgroundRatio}.

  \begin{figure}[!bt]
  \centering
  \includegraphics[width=3in]{FIGS/imaging/backgroundRatio.pdf}
  {\singlespacing
  \caption[ Idiosyncratic effects of background on ratiometric features.]
            { Ratiometric features are idiosyncratically affected by background.
            The same initial data as in the right panels of \ar{fig:imaging:SBeffects},
			but where each single-nucleus ratio
            after addition of background is plotted against its true value (1000/3700 cells shown).
            To each average was added 0 (black), 1000 (red),
            or $10^{10}$ (blue) background units before taking the ratio. The first
            two artificial cases are the same as those in \ar{fig:imaging:SBeffects}.
			The impact on the apparent ratio
            is dependent on the relative sizes of the foregrounds and the background,
            such that the data becomes scrambled in the red curve.
            Note that
			the distribution shape is also affected, such that adding background
			decreased the skewness. $F_1$, average
            single-nuclear Hoechst; $F_2$, average nuclear Smad2/3.}
  \label{fig:imaging:backgroundRatio}}
  \end{figure}

In the second case, we could allow $F_{c1}$ and $F_{c2}$ to come
from distinct channels, which would also allow them to have
different shading
and background. As a consequence, the effects on the
distribution properties are extremely difficult to predict
due to the presence of many independent variables
(as indicated in the case study in \ar{fig:imaging:SBeffects}, right panels).
I therefore leave the discussion here, with
the conclusion that the mean and variation of the ratio feature can both
increase or decrease with different ranges of component values.
This unpredictability has important
ramifications for applications such as
fluorescence resonance energy transfer (FRET) where interpretation
relies on accurate cross-channel ratios \cite{Hodgson2010}.

\subsubsection{On the importance of image correction}

Unfortunately, the mathematical discourse above leaves us
with the dissatisfying result that the importance of image correction
is highly dataset dependent. In \ar{fig:imaging:SBeffects} I show
the size of the distortion for an example dataset with
experimentally-reasonable amounts of background and shading, which
shows effects that are measurable and sometimes relatively large. However,
different analytical requirements can tolerate quite different
amounts of feature distribution distortion before
data interpretation is affected.
The best I can do then, besides the easy blanket statement
``always correct your images!'' is to provide a few rules of thumb for
deciding on the importance of image correction.

First, I
completely ignored the detector contribution ($D$) in the above
discussion because
the matrix $D$ is unchanging and is therefore trivial to subtract from
images. Removal of $D$ should be a part of all image
analysis pipelines. This step is rarely explicitly performed
in the literature but can have a large impact
on measurements of weakly-fluorescent samples. In particular,
shading will be underestimated without subtraction of the detector
fluorescence contribution.

Second, shading tends to increase feature distribution widths
and so should be corrected whenever measurement of true biological
variability is important. Additionally, if the foreground to background ratio
is low, then shading may cause foreground values in one part of an image to fall below
background values in another part. This can have
important consequences to image segmentation \arp{imaging:segmentation}
\cite{Herbert2012}. However, in the case that biological
variability is high relative to the variation in shading
it would not be necessary to correct for $S$.
This is because the observed average and total distributions are
the convolution
of the shading and foreground distributions. If two normal distributions
with variances $\sigma^2_1$ and $\sigma^2_2$ are convolved, they yield a new
distribution with $\sigma^2_3=\sigma^2_1+\sigma^2_2$. Because shading
is a relative term, we have to scale it to the size of $F_c$ to make
use of this property: $\sigma^2_{\text{observed}}=(F_c\sigma_{S_c})^2+\sigma_{F_c}^2$.
Thus, if $F_c\sigma_{S_c}^2<<\sigma_{F_c}^2$ the observed
distribution will be close to the true distribution. While this
is a useful rule of thumb, I note that I have never observed normally-distributed
shading values (see examples in \ar{fig:imaging:properties}b).

Third, background can dramatically shift feature distribution
means. Background should therefore be corrected either when accurate means
or accurate ratios between two means are needed. When background
is low compared to even the lowest foreground values, however, it will have
little impact on the feature distributions discussed above.

Finally, when using cross-channel ratios extra care should be
taken to ensure that both background and shading are corrected.
These ratios should always be interpreted with an eye towards
the possible
effects of imaging artifacts, as artifacts can distort rations
in unpredictable ways.

\subsection{Review of image correction methods}
\label{imaging:correction:review}

Now that I have given some motivation for the importance of image correction,
I turn to the available methods for this
process. Here, I briefly review the correction methods that
are commonly employed in the literature. In general, when deciding
on a method the investigator should test its theoretical performance
given the image model described in this chapter, $I=D+S(F+B)$.
By taking this approach, deficiencies or important assumptions of
the methods should become clear.

There are many published methods for fluorescence image correction,
perhaps as many methods as there are labs, due to the idiosyncrasies
of imaging data and the lack of standardized approaches.
I group the most common methods into two broad, non-exhaustive
categories, which I refer to as ``reference-image'' and ``per-image''
correction.
Reference-image methods obtain correction parameters from one image
and then use those parameters to correct another image. Per-image methods
find such parameters in the very image that is to be corrected.

Reference-image methods are straightforward and so are commonly used and
recommended throughout the literature
\cite{VandenDoel1998,Model2001,Zwier2004,Wolf2007,Waters2009}.
These methods make a key assumption: that the reference image
has approximately the same shading and background
as do the images to be corrected. A good example of this approach
uses two reference images to flatten shading, subtract background,
and normalize the fluorescence
intensity to a standard \cite{Model2001}.
One reference image, $I_\text{uniform}$ contains a
dissolved fluorophore; because this image would be flat without shading, it can
be used to determine how much shading is present in the real images. The other
reference, $I_\text{background}$ is the same as the sample images but contains no
sample. This image can thus be used to estimate background.
Equations~\ref{correction:example1}\nobreakdash-\ref{correction:example2} demonstrate this method.
	%
	\begin{align}
	I_\text{sample}     &= D+S(F_\text{sample}+B) \label{correction:example1}\\
	I_\text{background} &= D+SB \label{correction:ib} \\
	I_\text{uniform}    &= D+S(B_\text{uniform}+B) \\
	I_\text{corrected}  &= \frac{ I_\text{sample}-I_\text{background} }{ I_\text{uniform}-I_\text{background} }
		= \frac{ [D+S(F_\text{sample}+B)]-[D+SB]}{ [D+S(B_\text{uniform}+B)]-[D+SB] }
		= \frac{F_\text{sample}}{B_\text{uniform}} \label{correction:example2}
	\end{align}

While reference-image methods are straightforward and often easy to perform,
some of those recommended in the literature are only partially corrective. This is especially
true with respect to the detector value $D$, which
I have not seen explicitly accounted for in these methods.
To find out if a given method performed complete correction, investigators can plug the
image model into the method and check that, algebraically, the output is either
$F$ or some normalized form of it (as in \ar{correction:example2}).
However, note that partial correction may be
sufficient in some cases, particularly when background intensity is low compared
to foreground intensity and when within-image shading variation
is low compared to foreground variation.

Per-image methods, on the other hand, have the challenging task of measuring
all image components ($D$, $S$, $F$, and $B$)
within the image that is to be corrected. These methods typically
work by trying to fit a model to the combination of non-foreground
layers, $D+SB$. Therefore the primary difficulty
is that images typically contain varying fractions of foreground pixels
(e.g. due to variation in cell density),
so that determination of which pixels consist of background
is non-trivial. Further, even if identification of background pixels
is straightforward within an image, the size of the background signal
``underneath'' a foreground object is necessarily unknown and must
be predicted. The method for this prediction is what separates the different
per-image correction algorithms.

Some per-image methods fit a polynomial \cite{L2000}
or spline surface \cite{Lindblad162266,Lindblad2004,Yin2013} to predicted
non-foreground pixels, therefore assuming a
particular structure to the shading patterns.
These methods may also assume properties of
the foreground objects (e.g. fluorescing cells),
such as a maximum size in the case of the rolling ball algorithm employed by
ImageJ\cite{Schneider2012} and Fiji \cite{Schindelin2012},
and may perform non-linear transformations of the underlying
images. Additionally, the accuracy of these
approaches necessarily decreases with
increasing cell density as there are fewer
background pixels from which to estimate
$I_\text{background}$. High confluency or cell clumping can thus cause
per-image methods to introduce artifacts.

When they work properly, per-image based methods generate
$I_\text{background}$ \arp{correction:ib}
from each sample image, $I_\text{sample}$ \arp{correction:example1}.
From this point, then, the reference-based and per-image based
correction methods are the same; the only difference is in how
$I_\text{background}$ is obtained.
The question that then remains in both cases is which
mathematical operation to perform in order to correct the sample images.
In the literature,
subtraction ($I_\text{sample}-I_\text{background}$) is frequently
used. However, subtraction does not remove
shading, as the algebraic result is $SF$ instead of $F$. The standard
rolling ball algorithm employed by ImageJ and
FIJI uses this subtractive approach.

The better approach is to use division to remove shading. This can be
done in combination with subtraction to remove both background and shading,
as in the example above \arp{correction:example2}. However, many studies
use simple division ($I_\text{sample}/I_\text{background}$), which results
in $\frac{D+S(B+F)}{D+SB}=1+\frac{SF}{D+SB}$. This result is a
background-normalized foreground with incompletely-corrected shading.
The correction can be completed by prior subtraction of $D$ from both images,
followed by subtraction of 1, which would yield the normalized image $F/B$.

I use a different approach from those listed above,
which is to independently estimate all
non-foreground parameters so that I can perform the image algebra
that expicitly returns $F$. I discuss this method next.

\subsection{An improved correction method for high-throughput imaging}
\label{imaging:correction:myMethod}

Having determined the importance of image correction, and observed
the diversity and frequent inaccuracy of the available correction methods,
I sought to develop a simple and robust method for my own imaging applications.
All of the work in this dissertation took advantage of the throughput of
microtiter plates (e.g. 96- and 384-well plates), and so in particular I needed
to be able to accurately correct large numbers of images with
minimal computational overhead.
Further, I needed the correction to be robust to cell density,
so that I could correctly measure single-cell biological variability
under a wide variety of experimental conditions.

The approach that I developed (published in \cite{Coster2014}),
is a reference-based
method that relies on a key observation: shading patterns
are highly predictable
in microtiter plates. This allows for a correction method
that forgoes the need to
estimate parameters for every image, yet is more accurate
than using only
a single set of correction parameters. Below I show the
evidence that shading is indeed predictable, and then outline
a correction method for taking advantage of this fact.

\subsubsection{The shading pattern is a function of within-well position}

  \begin{figure}[!bt]
  \centering
  \includegraphics[width=6in]{FIGS/imaging/plateCorrection.pdf}
  {\singlespacing
  \caption[ Demonstration of within-well positional image correction.]
            { For a within-well position, shading patterns are consistent
			throughout the entirety of a microtiter plate.
			I imaged 3x3 grids within every well of a 384-well
			plate containing dissolved fluorescein. Optical setup:
			black plastic 384-well plate (Corning \#3712), FITC filter,
			20X objective, Andor sCMOS camera.
			I cropped all images identically to remove pixels that contained
            well edges. Within-well images were montaged
			into a single image, and then normalized to the median pixel
			value of that montaged image (e.g. in \b{a}, top). This converts
			the images to an estimate of shading as defined in this chapter.
			Reference 3x3 image montages were
            made by taking the per-pixel average
			across all 3x3 montages (from all wells).
            This was done either with all 9 images in
			the 3x3 grid (Position-dependent correction) or with only the
			central image from the grid repeated 9 times
            (Position-independent correction).
			Finally, the resulting grids were montaged to show the image
			properties across the entire 384-well plate (\b{b}). Panel
			\b{a} shows larger thumbnails of the 3x3 image grids in well A1
			before and after position-dependent correction. The histograms
			in \b{b} shows the distributions of all relative pixel intensities
			in the montaged images. Tighter distributions indicate more
			accurate correction. White objects in the montaged images are
			auto-fluorescing debris; small black corners in remaining in
			corrected images are due to inclusion of a small portion
			of the black well edge.}
  \label{fig:imaging:plateCorrection}}
  \end{figure}

I was initially surprised at the high variability in shading patterns that I
observed within imaging datasets from microtiter plates.
In the literature there
appears to be an implicit assumption that such
variability is common and unpredictable,
as the most common correction methods used in big datasets are
per-image. However, shading is an artifact that is
generated by the light path, which is a static aspect of the microscope
optical setup, and so I would have expected it to be
unchanging within a dataset. Indeed, images taken at different positions along a
glass slide seem to show an unchanging shading pattern.
I therefore reasoned that it was the microtiter plates
themselves that modified the
shading pattern. Further, since microtiter plates are essentially
arrays of identical wells, I
predicted that the shading modification must be a function of the
image location
within a single well (as opposed to the position within the plate).
The source of the within-well shading modulation could be due to
reflections from well edges, local distortions
of the imaging surface, or lensing by
the solvent meniscus.

This prediction of within-well positional dependence of
shading patterns bore out, as can be seen in
\ar{fig:imaging:plateCorrection}. Further, this turned out
to be consistent for both 384- and 96-well plates from various
manufacturers and with different physical specifications
and materials. In 96-well plates
this positional effect is less dramatic, which is consistent with
shading patterns being modified by well edges (as these edges
are much further apart than are those in 384-well plates). I also note that
a position-independent correction method (that uses a single reference
image instead of one per within-well position,
\ar{fig:imaging:plateCorrection}, blue) dramatically improves
the images as well. Therefore this even simpler method may be suitable
for some datasets (though the resulting multi-modality in
\ar{fig:imaging:plateCorrection} could, in principle, generate
artificial cellular subpopulations \arp{introduction:variability}).

\subsubsection{Pipeline for within-well position-based image correction}
\label{correction:pipeline}

The within-well positional shading constancy allowed
me to implement a simple and robust image correction pipeline
for images from microtiter plates. This pipeline is based on the
existing reference-based approaches discussed above and in other
sources \cite{Bray2012,Bray2013}.

  \begin{figure}[!bt]
  \centering
  \includegraphics[width=5in]{FIGS/imaging/samplenum.pdf}
  {\singlespacing
  \caption[ Estimating $S$ from sample-containing images.]
            { Shading ($S$) can be estimated using sample-containing
             images. Media-only wells are controls (0\% confluency).
             The spatially uniform fluorescence in these control wells was used
             to estimate the
             ``reference'' shading by per-pixel averaging
             across 42 media-only control wells. I define
             ``confluency'' as the fraction of pixels in an image
             with intensities >3$\sigma$ above background.
             I used A549 cells expressing two differently-colored
             fluorescent proteins (left, nuclear CFP; right,
             cytosolic mCherry) at three different seeding
             densities (each seeding density had 84 replicates),
             effectively yielding six different confluency
             levels. I additionally created mixed-confluency
             image sets by randomly selecting across all
             confluency levels within each color channel.
             For each fixed number of images, $n$,
             selected from the same within-well position, I computed
             the ``estimated shading'' as the per-pixel median of
             $n$ randomly selected images. The error in the shading
             estimates are computed as the $cv$ of the per-pixel
             ratio of (estimated shading)/(reference shading).
             For each confluency level, the inaccuracy of the
             sample-based shading estimate generally decrease
             with increasing numbers of images.}
  \label{fig:correction:samplenum}}
  \end{figure}

\begin{enumerate}

\item \textbf{Calibrate the microscope stage for the microtiter plate.}
Ideally, images should be acquired near well centers,
and relative within-well image positions should have
minimal drift between wells (e.g. images in well A1
should not be closer to the left well edge than those in H12).
It is important to be aware that this method will become
inaccurate with increasing positional drift. Also note that
microscope stage-driving software can vary in its accuracy,
and so custom solutions might be required for plates with
small wells.

\item \textbf{Measure the dark current component, $D$.}
The simplest approach is to capture images without a light
source, or with the light path diverted from the camera,
and per-pixel average the images. In practice, a small
number of images is sufficient ($n$=6-20 in my analyses).

\item \textbf{Subtract dark current, $D$, from all images in
the dataset.} The resulting images ($I-D$) are then modeled
by $S(B+F)$. This important step should be performed
regardless of the correction method subsequently
used, for the reasons explained above.
Without subtracting $D$, the estimated shading
patterns can become increasingly inaccurate with large
foreground values or small background values.

\item \textbf{Estimate shading $S$ for each distinct within-well
position.} This step is the most complicated in the pipeline
and should be performed with care. The goal is to obtain a
reference shading pattern for each within-well position.
For example, if an investigator has 9 images/well she will
need to estimate 9 shading patterns. There are at least two
possible approaches to this estimate.

\begin{itemize}

\item \textbf{Uniform reference images.} This approach
is the most robust, but is only possible if
there are extra wells that can be reserved solely for
acquiring reference images. In these extra wells,
add dissolved fluorophore at the appropriate
concentration for the intended exposure time.
These wells should then be imaged along with the
other sample wells. In practice, I have found that a
small number (e.g. 6) of such uniform images is often
sufficient so long as the wells are free from fluorescing
debris. Across all wells $w$, for a given within-well
position $p$, the images $I_{w,p}$ should be per-pixel averaged
to obtain a reference image $\text{R}_p=\text{mean}_w (I_{w,p}-D)$.
Note that the per-pixel median or other quantile may
be more robust.

\item \textbf{Sample-based reference images.}
In high-throughput studies extra wells may be
unavailable for acquiring reference images.
In this case, the investigator can take advantage
of the large number of available sample images and otherwise
use the same method as for uniform reference images.
Because these sample images contain foreground, many
more values per coordinate are needed to get an
accurate estimate of shading. In \ar{fig:correction:samplenum}
I show how accuracy is dramatically affected by
the number of images used. The same figure suggests that
20-40 cell-containing images may often be sufficient, but this
is dependent on the cellular confluency of those images.
\end{itemize}

The results of this step will be one reference image per
within-well position. As defined earlier, the shading values
should be centered on 1 to maintain the original intensity
range. Therefore once all reference images are collected
each image $\text{R}_p$ should be divided by the median
or mean of all reference image pixel values (with coordinates
$(r,c)$. The resulting shading patterns are described by
$S_p=\frac{\text{R}_p}{\text{mean}_{r,c,p} R_{r,c,p}}$

\item \textbf{Correct for the shading $S$ at each
within-well position.} To correct for shading,
every image $I_p$ should be per-pixel divided by
the corresponding shading pattern $S_p$ obtained in
the previous step: $\frac{I_p}{S_p}$. The resulting
images then contain only $B+F$.

\item \textbf{Subtract the background from each image.}
There are multiple approaches to this task.
The proper choice depends on the
particulars of the dataset, and so I illustrate two
cases here. In both cases, after subtracting background
we end up with the approximate foreground signal, $F$.

\begin{itemize}

\item \textbf{Global background subtraction.}
In some datasets, the background may vary little between
images. In this case, a global background value can be
estimated by averaging background pixels from a
representative image. Subtract this value from all pixels
in all images.

\item \textbf{Per-image background subtraction.}
In other datasets, there may be significant variability
in background from well to well. This could be due to errors
in staining or variation in exposure time.
Background values can be estimated per image
by e.g. Otsu thresholding to automatically identify
background pixels \cite{Otsu1975}. Alternatively, a low
quantile pixel value can be taken as background
(the quantile choice is dependent on cell density).
Then, for each image, subtract its estimated
background value.

I note that, in the case of background
differences being due to something that would imply foreground
differences, such as due to exposure time or staining
variation, these per-image background estimates can be used
to normalize intensities between images. For example, to
normalize all images to some reference $B_{ref}$, determine the
normalization factor by taking its ratio with the per-image
background $B_{ref}/B_{sample}$. The image can then be multiplied by this
factor at every pixel, bringing it to the same intensity level
as the reference image. Care should be taken with this approach,
however, as it is not generally obvious when background variation
is predictive of foreground variation.
\end{itemize}

\end{enumerate}

Note that this pipeline is only meant to remove shading $S$,
background $B$, and detector $D$ from images. What is left will
be all foreground layers, which may include various artifacts.
Further, the foreground values may vary for reasons independent
from imaging. For example, it is well established that assays
in microtiter plates can show batch, edge, row, and column effects.
These effects may non-biologically change the true values of $F$
and should thus be normalized after image correction
\cite{Malo2006,Dragiev2011,Dragiev2012,Carralot2012,Zhong2013}.

\subsection{Image correction quality control}

As with any data manipulation, the image correction
method outlined above should be checked to ensure
that no errors are introduced.
An obvious approach is to simply
visually inspect a subset of images, as in
\ar{fig:imaging:flatfieldCheck}a,
though automated approaches are also feasible.

For background correction,
images should be checked to ensure that only the
background has been subtracted. This can be done
by examining background pixels. Since a perfect
subtraction will have set the centroid of the
background pixel distribution to zero, roughly half
of all post-correction background pixels should
be zero while the other background pixels take on small values.
Unfortunately, this task can be difficult to automate
for the same reason that errors may be introduced:
cell density may vary dramatically within a dataset,
complicating the automated identification of background pixels.

For shading correction, the likely artifacts will be
spatial. In other words, if there is over-correction
or under-correction,
this will cause certain regions of every image (and
the cells within those regions) to have systematically
higher or lower intensities than the global mean. This can be checked
in an automated way after cell segmentation by testing
for dependence of single-cell features on within-image
position (see an example of this approach in
\ar{fig:imaging:flatfieldCheck}b).

 \begin{figure}[!bt]
  \centering
  \includegraphics[width=6in]{FIGS/imaging/flatfieldCheck.pdf}
  {\singlespacing
  \caption[ Quality control for image correction.]
            {Images can be over- or under-corrected, and
            therefore require quality control. \b{a},
            Visual inspection should reveal a flat background
            and a foreground that does not vary in a
            spatially predictable manner after correction. Histone H2B-Cyan
            Fluorescent Protein labeled A549 cells. Image
            courtesy Jungseog Kang (Altschuler \& Wu lab,
            UT Southwestern). \b{b},
            After segmentation and feature-extraction,
            single-cell features can be tested for within-image
            spatial dependence. Top left, a sample image of
            Smad2/3-stained human colonic epithelial cells,
            showing the radial metric of ``distance from image
            center.'' Plots show nuclear area and
            total nuclear Smad2/3 or Hoechst
            as a function of within-image radial position
            ($n$=1000/$\sim$20000 randomly-chosen cells).
            Intensities were median-normalized by well to prevent
            true experimental variation from affecting this
            test of positional phenotype dependence.
            Inset
            numeric value is the Pearson correlation coefficient
            for the two variables plotted. }
  \label{fig:imaging:flatfieldCheck}}
  \end{figure}


\documentclass[a4paper]{article}

\usepackage{fullpage} % Package to use full page
\usepackage{parskip} % Package to tweak paragraph skipping
\usepackage{tikz} % Package for drawing
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{flexisym}

\usepackage[a4paper]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsfonts}
%\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage[]{algorithm2e}
\usepackage{graphicx}
\usepackage{color}
\usepackage[bf,SL,BF]{subfigure}
\usepackage{url}
\usepackage{pdfsync}
\usepackage[affil-it]{authblk}
\usepackage{blindtext}

\usepackage{subeqnarray}
\usepackage{cases}

\makeatletter
\@addtoreset{equation}{section}
\makeatother
\renewcommand{\theequation}{\arabic{section}.\arabic{equation}}

\usepackage{verbatim}

\title{Note about Julia Code}
\author{Haolei Wang}

\begin{document}

\maketitle
%\begin{comment}
\section{Introduction}

In this note, I will try to introduce how I write this Julia code to calculate the energy in a granular system as soon as possible. This code was based on Ortner's \texttt{\textprime JuLIP\textprime} package.

We will compare the performance of this code with my own Matlab code. It is known that Matlab deals the vector manipulation pretty well while loop rather bad. Julia can deal with loop rather well. In our project, it is rather common to use loop, so it is better to design a Julia code.

\section{Problem Setting}
In this section, we will introduce our problem. We need to calculate the inner potential energy of a granular system. The system is a square (2D) with {\color{red}periodic boundary condition}, which is filled with two kind of disks. The number of particle of each are the same, while their radius ratio is 1.4. Particles will interact with each other only when they overlap. We give the following energy model.

\begin{equation} \label{eq: pair potential}
E_{ij} = \left\{
    \begin{aligned}
           & k\frac{(r_i+r_j - \left|\vec{x}_i-\vec{x}_j\right|)^{\alpha}}{\alpha} &  &\text{for} \quad \left|\vec{x}_i-\vec{x}_j\right|< r_i+r_j, \\
           & 0 &  &\text{for} \quad \left|\vec{x}_i-\vec{x}_j\right|\ge r_i+r_j, ,
    \end{aligned}
\right.   i\ne j
\end{equation}
where $k$ represents the particle stiffness, $r_i$ and $r_j$ are the radii of particle $i$ and $j$ and $\vec{x}_i$ and $\vec{x}_j$ are the positions of the their centers. With different $\alpha$, we can get different kinds of pair potentials. Thus the total potential energy of the system is:
\begin{equation}\label{eq: total potential1}
     E_{total} = \sum_{i<j}E_{ij}.
\end{equation}

Since the particles only interact with each other unless they contact, we can rewrite the potential energy for a single contact $c$ as:
\begin{equation}
E_c = k\frac{\delta_c^{\alpha}}{\alpha},
\end{equation}
where $\delta_c$ is the overlap associated with the contact $c$ and according to equation \eqref{eq: pair potential}:
\begin{equation}\label{eq: overlap}
\delta_c = r_i + r_j - \left|\vec{x}_i-\vec{x}_j\right|.
\end{equation}

Thus, the total potential energy of the system is:
\begin{equation}\label{eq: total potential2}
     E_{total} = \sum_{c}E_{c}.
\end{equation}

\begin{figure}[!htbp]
\begin{center}
\begin{tikzpicture}
\draw [black,thick,fill=black!20] (1.5,3.5) circle (1cm) node[left] {$i$};
\fill [black] (1.5,3.5) circle (1.5pt);
\draw [blue,-] (1.5,3.5) -- (2.1,4.3) node[above] at (1.79,3.91) {$r_{i}$};
\draw [black,thick,fill=black!20] (3.5,2) circle (1.5cm) node[below] {$j$};
\fill [black] (3.5,2) circle (1.5pt);
\fill [black] (2.3,2.9) circle (1.5pt) node[right] {$c$};
\draw [blue,-] (3.5,2) -- (5,2) node[above] at (4.25,2) {$r_j$};
\draw [thick, ->] (-0.75,0) -- (5,0) node[above] {$x$};
\draw [thick, ->] (0,-0.75) -- (0,5) node[right] {$y$};
\draw [black,->] (0,0) -- (1.5,3.5) node[above] at (0.7,1.8) {$\vec{x}_{i}$};
\draw [black,->] (0,0) -- (3.5,2) node[below] at (1.75,1) {$\vec{x}_{j}$};
\draw [black,->] (1.5,3.5) -- (3.5,2);
\path[draw] (2.5,2.75) -- (4.25,4.5) -- (4.75,4.5);
\node [above] at (4.5,4.5) {$\vec{l}_c$};
\end{tikzpicture}
\end{center}
\caption{Two particles contact}
\end{figure}

\section{Programming}
In this section, we will introduce the programming approach.
\subsection{Calculating the distance between two points in periodic cell}
From section 2, we can calculate the total potential energy by summing the potential energy of all contacting particles. Thus, we need to find all the contacts. From equation \eqref{eq: overlap}, it's the key to calculate the distance of all particles with each other.

If the cell is not periodic, it's easy to calculate the distance between particles, which is the 2-norm of $\left|\vec{x}_{j}-\vec{x}_{i}\right|$. However, in our project, the cell is periodic, so we need to reconsider this when the particles are near the boundaries. Since we don't consider long-range interaction, so the distance between two points in periodic cell is the smallest of the distance of their periodic projections with each other.

Firstly, we will try to calculate the periodic distance in a unit square. In Figure 2, we have two points $i$ and $j$, with their coordination $X_i$ and $X_j$ respectively. If we set
\begin{equation}\label{eq: unit point difference1}
d\bar{X} = X_{j}-X_{i}=(d\bar{X}_{1},d\bar{X}_{2}),
\end{equation}
we know that $-1<d\bar{X}_{k}<1, k=1,2$. For a periodic unit square, it is important to note that the real difference of two points' coordination
$$
dX=(dX_{1},dX_{2})
$$
should satisfy the following condition:
$$
\left|dX_{k}\right|\le\frac{1}{2}.
$$
The relationship between $dX_{k}$ and $d\bar{X}_{k}$ is:
\begin{equation}\label{eq: unit point difference2}
dX_{k} = \text{mod}(d\bar{X}_{k} + \frac{1}{2}, 1) - \frac{1}{2}.
\end{equation}

The distance between points $i$ and $j$ is:
\begin{equation}
d_{ij}=\sqrt{dX \cdot dX}
\end{equation}

\begin{figure}[!htbp]
\begin{center}
\begin{tikzpicture}
\draw [thick, ->] (-2,0) -- (5.5,0) node[above] {$x$};
\draw [thick, ->] (0,-1) -- (0,5.5) node[right] {$y$};
\draw [thick,-] (4,0) -- (4,4) node[above] at (4.1,0) {1};
\draw [thick,-] (0,4) -- (4,4) node[left] at (0,4.1) {1};
\fill [black] (0.5,0.5) circle (1.5pt) node[right] {$i$};
\fill [black] (3,3.5) circle (1.5pt) node[right] {$j$};
\fill [black] (-1,-0.5) circle (1.5pt) node[right] {$j\textprime$};
\end{tikzpicture}
\end{center}
\caption{Points in periodic unit square}
\end{figure}

Secondly, we will consider a common deformed (simple shear) cell with periodic boundary condition. In Figure 3, the cell can be denoted using a matrix:
$$
M=
\begin{bmatrix}
L & Le\\
0 & H
\end{bmatrix}
$$
The points $i$ and $j$ in this cell can be projected in the unit square reference cell using
\begin{subequations}\label{eq: tranform x2X}
\begin{align}
\hat{X}&=M^{-1}x \\
X&=\hat{X} - \text{floor}(\hat{X})
\end{align}
\end{subequations}
where $X$ and $x$ are the coordination in the reference cell and real cell respectively.

We can calculate the difference between two reference point, $dX$, using equation \eqref{eq: unit point difference1} and \eqref{eq: unit point difference2}. Then we can calculate the real coordination difference using
\begin{equation}\label{eq: points difference}
dx=MdX
\end{equation}

The distance between points $i$ and $j$ can be calculated by
\begin{equation}
d_{ij}=\sqrt{dx \cdot dx}
\end{equation}

\begin{figure}[!htbp]
\begin{center}
\begin{tikzpicture}
\draw [thick, ->] (-1,0) -- (7,0) node[above] {$x$};
\draw [thick, ->] (0,-1) -- (0,6.5) node[right] {$y$};
\draw [thick,-] (5,0) -- (6,5) node[below] at (5.2,0) {$L$};
\draw [thick,-] (6,5) -- (1,5);
\draw [dashed] (1,5) -- (0,5) node[left] at (0,5) {$H$};
\draw [thick,-] (1,5) -- (0,0);
\draw [red,thick,-] (2,0) -- (2,2) node[below] at (2,0) {1};
\draw [red,thick,-] (0,2) -- (2,2) node[left] at (0,2) {1};
\draw [red,thick,-] (0,0) -- (0,2); \draw [red,thick,-] (0,0) -- (2,0);
\fill [black] (4.65,4.5) circle (1.5pt) node[right] {$i$};
\fill [black] (4.6,0.5) circle (1.5pt) node[right] {$j$};
\fill [black] (3.65,-0.5) circle (1.5pt) node[right] {$i\textprime$};
\fill [red] (1.5,1.8) circle (1.5pt) node[right] {$I$};
\fill [red] (1.8,0.2) circle (1.5pt) node[above] {$J$};
\fill [red] (1.5,-0.2) circle (1.5pt) node[right] {$I\textprime$};
\fill [black] (3.75,6.25) circle (1.5pt) node[right] {$k$};
%\fill [black] (2.75,1.25) circle (1.5pt) node[right] {$\hat{k}$};
\fill [red] (1,2.5) circle (1.5pt) node[right] {$\hat{K}$};
\fill [red] (1,0.5) circle (1.5pt) node[right] {$K$};
\end{tikzpicture}
\end{center}
\caption{Points in deformed cell}
\end{figure}

\subsection{Calculate neighbourlist of granular system}
Now we know how to calculate the distance between points in periodic cell, it's identical to calculate the distance between the centers of two particles. Thus, we can determine whether two particles overlap.

Noting that the particles interact with each other only when they overlap, we can firstly find the neighbourlist of the system. The neighbourlist stores the pairs of indexes of every two possibly overlapping particles. Thus when we calculate the total potential energy, we only need to compute the potential energy in the neighbourlist and then sum up.

Ortner provide a package called \texttt{\textprime NeighbourLists\textprime } using Julia language, which is rather fast to determine the neighbourlist based on a cutoff $r_{cut}$. Matlab also provides several functions to calculate the distance and knn (k-nearest neighbour), the relative functions are \texttt{pdist, knnsearch, rangesearch}, etc. However, the Matlab functions cannot compute the periodic distance either, so the above functions are not suitable for our project. I programmed a Matlab code to calculate periodic distance, but it couldn't manipulate large system because the space complexity is $O(N^2)$. While Ortner's code is efficient and {\color{red}with $O(N)$ space complexity (not sure if this is correct)}, this can be a big advantage.

Package \texttt{\textprime NeighbourLists\textprime } computes the distance between particles and then determine whether the distance is smaller than $r_{cut}$.
This package can provide the following information:
\texttt{nlist.i, nlist.j, nlist.r, nlist.R}, here \texttt{nlist} means neighbourlist, \texttt{nlist.i} and \texttt{nlist.j} are two vectors of particles' index, \texttt{nlist.r} is a vector storing distance between particles, \texttt{nlist.R} is a vector that stores $dx$ in equation \eqref{eq: points difference}. Meanwhile, \texttt{nlist.r[k]} is the distance between particle \texttt{nlist.i[k]} and \texttt{nlist.j[k]}, \texttt{nlist.R[k]} is $dx$ associated with particle \texttt{nlist.i[k]} and \texttt{nlist.j[k]}.

Even though package \texttt{\textprime NeighbourLists\textprime } is very efficient, we still find it is a waste to calculate the total neighbourlist every time when we calculate the energy, since the displacement of particles in our system is so small that the neighbourlist is almost unchanged. We only need to update \texttt{nlist.r} and \texttt{nlist.R}. If the maximum displacement reaches a tolerance $r_{tol}$, we will update the total neighbourlist.

Next, we will give the pseudocode.

\begin{verbatim}
function energy(Config, V)
    """
    Function to calculate the total potential energy.
    V(r) is the pair potential.
    """
    nlist_i, nlist_j = update_neighbourlist(Config)
    E = 0.0
    for k = 1:length(nlist_i)
        i, j = nlist_i[k], nlist_j[k];
        xi, xj = Config.X[i], Config.X[j];
        dx = xj - xi;
        dX = inv_bins * dx;                   # using equation (3.4a)
        dX = dX - floor(dX);                  # using equation (3.4b)
        dX = mod(dx + 0.5, 1) - 0.5;          # using equation (3.2)
        dx = bins * dX;                       # using equation (3.5)
        r = norm2(dx);
        E = E + V(r);
    end
    return 0.5*E
end

function update_neighbourlist(Config)
   """
   Function to update the neighbourlist
   has_data(Config, fieldname) judges if the Config structure has this field.
   """
   if has_data(Config, nlist_X)
      X_Old = Config.nlist_X;
      max_displacement = max(norm2.(Config.X - X_Old));
      if max_displacement < r_tol
          return Config.nlist_i, Config.nlist_j;
      end
   end
   nlist = NeighbourList(Config, rcut);
   Config.nlist_X, Config.nlist_i, Config.nlist_j = Config.X, nlist.i, nlist.j;
   return nlist.i, nlist.j;
end

\end{verbatim}

One should note that we compute $dX$ using $dx$ instead of $X$ using $x$ in the \texttt{energy} function. We will now show this.

In the pseudocode,
$$
\begin{aligned}
&X_{i} = (\varepsilon_{i}, \eta_{i})^{T}, \quad X_{j} = (\varepsilon_{j}, \eta_{j})^{T} ,\quad \text{where} \quad 0 \le \varepsilon, \eta < 1\\
 \Longrightarrow &\hat{X}_{i} = (m_{i}+\varepsilon_{i}, n_{i}+\eta_{i})^{T},\quad \hat{X}_{j} = (m_{j}+\varepsilon_{j}, n_{j}+\eta_{j})^{T},\quad \text{where} \quad m,n \in \mathbb{Z}\\
\Longrightarrow &x_{i}=M\hat{X}_{i}, \quad x_{j}=M\hat{X}_{j}\\
\Longrightarrow &dx = x_{j}-x_{i}=M(\hat{X}_{j}-\hat{X}_{i})\\
\Longrightarrow &d\hat{X} = M^{-1}dx=\hat{X}_{j}-\hat{X}_{i}=(m_{j}-m_{i}+\varepsilon_{j}-\varepsilon_{i}, n_{j}-n_{i}+\eta_{j}-\eta_{i})^{T}\\
\Longrightarrow &d\bar{X}=d\hat{X}-\text{floor}(d\hat{X})=\boxed{\text{mod}(\varepsilon_{j}-\varepsilon_{i},\eta_{j}-\eta_{i},1)}\\
\Longrightarrow &dX=\text{mod}(d\bar{X}+\frac{1}{2},1)-\frac{1}{2}.
\end{aligned}
$$
While according to the introduction in section 3.1,
$$
\begin{aligned}
&X_{i} = (\varepsilon_{i}, \eta_{i})^{T}, \quad X_{j} = (\varepsilon_{j}, \eta_{j})^{T} ,\quad \text{where} \quad 0 \le \varepsilon, \eta < 1\\
\Longrightarrow &d\bar{X}=X_{j}-X_{i}=\boxed{(\varepsilon_{j}-\varepsilon_{i}, \eta_{j}-\eta_{i})^{T}}\\
\Longrightarrow &dX=\text{mod}(d\bar{X}+\frac{1}{2},1)-\frac{1}{2}.
\end{aligned}
$$
The boxed part can reach the same $dX$ which is needed.
\begin{figure}[!htbp]
\begin{center}
\begin{tikzpicture}
\draw [thick, ->] (-3,0) -- (3,0) node[above] {$x$};
\draw [thick, ->] (0,-2) -- (0,2) node[right] {$y$};
\draw [thick,-] (-1,-1) -- (1,1) node[above] at(2,0) {1};
\draw [thick,-] (1,-1) -- (2,0) node[above] at(-2,0) {-1};
\draw [thick,-] (-2,0) -- (-1,1);
\draw [dashed] (1,-1) -- (1,1) node[above] at(1.1,0) {$\frac{1}{2}$};
\draw [dashed] (-1,-1) -- (-1,1) node[above] at(-1.1,0) {$-\frac{1}{2}$};
\draw [dashed] (-1,-1) -- (0,-1) node[right] at(0,-1) {$-\frac{1}{2}$};
\draw [dashed] (1,1) -- (0,1) node[left] at(0,1) {$\frac{1}{2}$};
\end{tikzpicture}
\end{center}
\caption{Function $y=\text{mod}(x+\frac{1}{2},1)-\frac{1}{2}$}
\end{figure}

In the real examples, we found that we didn't need to do the periodic correction for each pair of particles in the neighbourlists. In fact, most pairs of particles are very close originally, we can modify the \texttt{eneryg} function code.
\begin{verbatim}
function energy(Config, V)
    """
    Function to calculate the total potential energy.
    V(r) is the pair potential.
    """
    nlist_i, nlist_j = update_neighbourlist(Config)
    E = 0.0
    for k = 1:length(nlist_i)
        i, j = nlist_i[k], nlist_j[k];
        xi, xj = Config.X[i], Config.X[j];
        dx = xj - xi;
        r = norm2(dx);
        if r > 0.5*min(L,H)          # min(L,H) is the length scale of the system
           dX = inv_bins * dx;                   # using equation (3.4a)
           dX = dX - floor(dX);                  # using equation (3.4b)
           dX = dX - (dX > 0.5);                 # using equation (3.2)
           dx = bins * dX;                       # using equation (3.5)
           r = norm2(dx);
        end
        E = E + V(r);
    end
    return 0.5*E;
end
\end{verbatim}

This is much faster than the original code, and also faster than my own Matlab code. We compute the potential energy of one given granular system (with 1024 particles) for 1000 times using this modified code and the original code and my own Matlab code, the time costed are 0.32s, 2.14s and 0.87s respectively.

\end{document}


Software prepared for this thesis is meant to be run on a modern GNU/Linux distribution.
Specifically, we tested it on Ubuntu but it should be possible to compile and run the library on other distributions as long as necessary libraries that we use can be installed.
Here we describe how to prepare clean and up-to-date Ubuntu 16.04 LTS installation to use our library.
Commands should be run from terminal emulator.
\section{Basic configuration}
\noindent
First, install necessary packages.
\begin{verbatim}
sudo apt install git libopenblas-dev python-numpy \
python-dev python3-numpy python3-dev swig
\end{verbatim}
Clone the repository from GitHub together with Faiss or get it from the volume supplied to the thesis.
\begin{verbatim}
git clone --recursive https://github.com/walkowiak/mips
\end{verbatim}
Copy Faiss' makefile and customize it.
\begin{verbatim}
cp mips/faiss/example_makefiles/makefile.inc.Linux mips/makefile.inc
nano mips/makefile.inc
\end{verbatim}
Main changes concern \texttt{BLASLDFLAGS} and \texttt{PYTHONCFLAGS}.
These variables should be adapted to your system but in general they are going to look like this (pick one version of \texttt{PYTHONCFLAGS}).
\begin{verbatim}
BLASLDFLAGS?=/usr/lib/libopenblas.so.0
PYTHONCFLAGS=-I/usr/include/python3.5/ \
 -I/usr/lib/python3/dist-packages/numpy/core/include
PYTHONCFLAGS=-I/home/<username>/anaconda3/include/python3.6m/ \
 -I/home/<username>/anaconda3/lib/python3.6/\
site-packages/numpy/core/include/
\end{verbatim}
Using Anaconda, as shown above, can be helpful (Python 3.6 is not officially available in Ubuntu 16.04 repositories). You can install it by downloading a script from \url{https://www.anaconda.com/download/}.
Allow execution of the script and run the installer.
\begin{verbatim}
chmod u+x Anaconda3-5.0.1-Linux-x86_64.sh
./Anaconda3-5.0.1-Linux-x86_64.sh
\end{verbatim}
You will have to accept the license terms, choose location for Anaconda (e.g. \texttt{/home/ <username>/anaconda3}) and allow it to be added to \texttt{PATH}.\\
Now you can compile our library and Faiss' Python wrapper.
\begin{verbatim}
cd mips
make -j10
cd faiss
make py
\end{verbatim}
\section{Benchmarking on sift dataset}
\noindent
Download the SIFT dataset and extract it to the predefined directory.
\begin{verbatim}
cd ..
wget ftp://ftp.irisa.fr/local/texmex/corpus/sift.tar.gz
tar xf sift.tar.gz -C data
rm sift.tar.gz
mv data/sift data/sift1M
\end{verbatim}
The ground truth of this dataset is in the nearest neighbour format.
We need to transform it to the MIPS groundtruth format.
\begin{verbatim}
export PYTHONPATH=faiss
python3 python/misc/make_gt_IP.py --skip-tests data/sift1M
\end{verbatim}
Benchmark our implementations of algorithms.
\begin{verbatim}
bin/bench_kmeans 2 1 0.85 0 40 80 120
bin/bench_quant 128 32 0 0 0
bin/bench_alsh 64 96 10 1 0.85
\end{verbatim}
\subsection{Benchmarking on LSHTC dataset}
\noindent
Install needed Python libraries using Anaconda.
\begin{verbatim}
conda install pytorch -c pytorch
conda install -c conda-forge tinydb pybind11
\end{verbatim}
Compile bindings and fork of fastText.
\begin{verbatim}
make bind
cd fastText
make -j10
\end{verbatim}
Download WikiLSHTC-325K dataset from the Extreme Classification repository.\footnote{ \url{http://manikvarma.org/downloads/XC/XMLRepository.html}}
Extract the archive, rename files to \texttt{test.txt} and \texttt{train.txt} and move them to \texttt{mips/data/LSHTC} directory.
Run Jupyter Notebook and follow the instructions.
\begin{verbatim}
jupyter-notebook tests/ours-on-wiki-test.ipynb
\end{verbatim}


\documentclass[revision-guide.tex]{subfiles}
%% Current Author:
\setcounter{chapter}{4}
\begin{document}
\chapter{Electricity}
\spec{discuss electrical phenomena in terms of electric charge}
\spec{describe electric current as the rate of flow of charge and recall and use $I = \Delta Q / \Delta t$}

Electric current is the flow of charge. The \emph{current} is defined as the rate of flow of charge. Conventional current flows from positive to negative. This current can consist of positive charges flowing from positive to negative or, more usually, negative charges flowing from negative to positive. The total current depends on the charge carrier density, the cross-sectional area, the charge on the carrier and the drift velocity of the carriers.

\spec{understand potential difference in terms of energy transfer and recall and use VQ = W}

When a charge moves through an electric field it gains or loses potential energy. The energy change per unit charge is defined as the potential difference.

\spec{recall and use the fact that resistance is defined by R = V/I and use this to calculate resistance variation for a variety of voltage-current characteristics}

As well as measuring resistance directly it can be found from a graph of V against I. \emph{Note: the resistance is defined as V/I at all points and is not equal to the gradient of a V/I graph except in the case that V is proportional to I}

\spec{define and use the concepts of emf and internal resistance and distinguish between emf and terminal potential difference}
\spec{derive, recall and use E = I(R + r ) and E = V + Ir}

A real cell or battery can be represented by a cell circuit symbol in series with a resistor. This resistance represents the \emph{internal resistance} of the cell and the fixed, theoretical potential difference across the cell symbol is the emf of the cell (the electromotive force provided). When a voltmeter is connected across the cell the terminal potential difference is measured.

\begin{figure}[h]
\begin{center}
\begin{circuitikz}
  \draw (2,0) to[battery,l=$E$,o-] (4,0) to[R=$r$,-o] (6,0);
\end{circuitikz}
\end{center}
\caption{Terminal potential difference}
\end{figure}

In order to relate the terminal potential difference to the internal characteristics of the cell we must subtract the potential difference across the internal resistance from the emf. In symbols this gives:

$$ V = E - Ir $$

which can be re-arranged to give the formula above.

If our real cell is connected into a circuit with a load resistance $R$, the  circuit in figure \ref{loaded-cell} is produced.

\begin{figure}[h]
\begin{center}
\begin{circuitikz}
  \draw (1,0) to[short] (2,0) to[battery,l=$E$,o-] (4,0) to[R=$r$,-o] (6,0) to[short] (7,0) to[short] (7,-2) to[R=$R$] (1,-2) to[short] (1,0);
\end{circuitikz}
\end{center}
\caption{A loaded real cell}
\label{loaded-cell}
\end{figure}

Now, the terminal potential difference must be equal to the potential difference across the load resistor, $R$.

$$ IR = V = E-Ir $$

Which can be re-arranged to give the second equation in the specification.

\spec{derive, recall and use P = VI and W = VIt, and derive and use P = I$^2$R}

Power is defined as the energy transferred per unit of time. In the case of electrical power this is the product of current (charge per unit time) and potential difference (energy per unit charge). Given a constant voltage and current, the energy transferred (work done) is given by $P = \frac{W}{t} = IVt$

\spec{recall and use $R = \rho l/A$}

This formula allows the calculation of a regular sample of material. In this formula $\rho$ is the resistivity, l is the length of the sample and A its cross-sectional area. Typical resistivities for conductors are of the order of
\SI{e-8}{\ohm\metre} and above \SI{e9}{\ohm\metre} for insulators. Semi-conductors lie between these values.

\spec{recall the formula for the combined resistance of two or more resistors in series and use it to solve problems $R_T = R_1 + R_2 + \ldots$}
\spec{recall the formula for the combined resistance of two or more resistors in parallel and use it to solve problems $\frac{1}{R_T} = \frac{1}{R_1} + \frac{1}{R_2} + \ldots$}

This are fairly simple to derive from Kirchoff's Laws (see below).

\spec{recall Kirchhoff’s first and second laws and apply them to circuits containing no more than two supply components and no more than two linked loops}

\begin{description}
  \item[Kirchoff's First Law] The current that flows into any junction is equal to the current which flows out.
  \item[Kirchoff's Second Law] The sum of the emfs around any closed loop of a circuit must equal the sum of the potential differences across any components. It is important to note that direction matters and if the loop crosses emfs or components against the flow of current they must be subtracted.
\end{description}

These two laws, and the definition of resistance, are the most useful tools in circuit analysis. The important skill is to work methodically through the circuit applying the laws, rather than attempting to solve the circuit all in one go.

\begin{example}
  Calculate the current through the \SI{3}{\volt} cell.
  \begin{center}
    \begin{circuitikz}
        \draw (0,0) to[R=2.0<\ohm>] (0,2) to[battery, l=6.0<\volt>, i=$I_x$] (0,4) to (5,4)
        (0,0) to (2.5,0) to[R=0.5<\ohm>] (2.5,2) to[battery, l=3.0<\volt>, i=$I_y$] (2.5,4)
        (2.5,0) to (5,0) to[R=10.0<\ohm>] (5,4)

    ;\end{circuitikz}
  \end{center}

  \answer

  We can use Kirchoff's Second Law to derive two expressions linking $I_x$ and $I_y$. The first is formed by creating a loop consisting of the two branches containing cells.
  $$ 6 - 3 = 2I_x - 0.5I_y $$
  Note that I am using a clockwise loop so the signs of the two components in the `Y' branch have negative signs.

  The second expression is now arrived at by using the outermost loop of the circuit (and using Kirchoff's First Law to get the current through the \SI{10}{\ohm} resistor):
  $$ 6 = 10(I_x + I_y) + 2I_x$$
  These two equations can now be used to solve for $I_y$, giving
  $$ I_y = \SI{-0.923}{\ampere} $$

  Note the sign of $I_y$ is negative, this means that current is flowing in the opposite direction to the arrow shown. This means that the cell is charging.

\end{example}

\spec{appreciate that Kirchhoff’s first and second laws are a consequence of the conservation of charge and energy, respectively}

The charge flowing into or out of a junction in a given time, $t$, is given by $Q = It$. Given that a junction can neither store nor create charge, Kirchoff's First Law follows directly.

Each charge carrier can only take one loop around the circuit. Once it returns to its original position its energy must be equal to the amount it had when it left. The charge carrier gains energy passing through cells and loses it passing through components. Since the sum of these energies must be zero and $W=qV$, the sum of emfs must equal the sum of potential differences across components.

\spec{use the idea of the potential divider to calculate potential differences and resistances}

When two resistors are in series with a battery we say that the circuit is a \emph{potential divider.}

\begin{figure}[h]
    \begin{center}
        \begin{circuitikz}
        \draw (0,0) to[battery,l=$V$] (0,5) to (3,5) to[R=$R_1$] (3,2.5) to[R=$R_2$] (3,0) to (0,0);
        \end{circuitikz}
    \end{center}
\caption{A potential divider}
\end{figure}

Since there are no junctions in the series circuit we can know that the current is the same in all parts of the circuit and that the total resistance is $R_1 + R_2$. The p.d. across resistor 1 is therefore given by:
\[ V_1 = IR_1 = \frac{V}{R_1 + R_2} R_1 = \frac{R_1}{R_1 + R_2} V \]
In other words, the ratio of p.d.s in the circuit is equal to the ratios of the resistances.

This can also be extended to the ratios between the components as they share the same current.

\[ I_1 = I_2 \implies \frac{V_1}{R_1} = \frac{V_2}{R_2} \implies \frac{R_1}{R_2} = \frac{V_1}{V_2} \]

\begin{example}
Calculate the resistance of the bulb in the circuit below.
\tikzset{component/.style={draw,thick,circle,fill=white,minimum size =0.75    cm,inner sep=0pt}}
\begin{center}
    \begin{circuitikz}
    \draw (0,0) to[battery,l=\SI{9}{\volt}] (0,5) to (3,5) to[lamp] (3,2.5) to[R,l=\SI{800}{\ohm}] (3,0) to (0,0);
    \draw (3,2.5) to (5,2.5) to (5,1.25) node[component]{3V} to[short] (5,0) to (0,0);
    \end{circuitikz}
\end{center}

\answer

The potential difference across the bulb must be \SI{6}{\volt} by Kirchoff's Second Law. Therefore:

\[ \frac{\SI{3}{\volt}}{\SI{6}{\volt}} = \frac{\SI{800}{\ohm}}{R} \]
\[ \implies R = \SI{800}{\ohm}\times \frac{\SI{6}{\volt}}{\SI{3}{\volt}} = \SI{1600}{\ohm}\]

\end{example}
\end{document}

%sagemathcloud={"latex_command":"latexmk -pdf -f -g -bibtex -synctex=1 -interaction=nonstopmode '5-electricity.tex'"}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{plotsid}
\section*{\hspace*{-1.6cm} plotsid}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Schematic interference diagram of FM signals.
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
plotsid(t,iflaws)
plotsid(t,iflaws,K)
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty plotsid} plots the schematic interference diagram of any
        distribution in the affine class which is perfectly localized for
        signals with a power-law group-delay of the form $t_x(\nu)=t_0+c\
        \nu^{K-1}.$ This diagram is computed for any (analytic) FM
        signal. \\

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty t} & time instants\\
        {\ty iflaws} & matrix of instantaneous frequencies,
                 with as many columns as signal components\\
        {\ty K} & distribution parameter                & {\ty 2}\\
        &      {\ty K = 2}   : Wigner-Ville distribution\\
        &      {\ty K = 1/2} : D-Flandrin distribution\\
        &      {\ty K = 0 }  : Bertrand (unitary) distribution\\
        &      {\ty K = -1}  : Unterberger (active) distribution\\
        &      {\ty K = inf} : Margenhau-Hill-Rihaczek dist.\\

\hline
\end{tabular*}

\end{minipage}
\vspace*{1cm}

{\bf \large \sf Example}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Here is the interference diagram corresponding to the Bertrand
distribution, for a signal composed of two components : a linear and a
constant frequency modulation\,:
\begin{verbatim}
         Nt=90; [y,iflaw]=fmlin(Nt,0.05,0.25);
         [y2,iflaw2]=fmconst(50,0.4);
         iflaw(:,2)=[NaN*ones(10,1);iflaw2;NaN*ones(Nt-60,1)];
         plotsid(1:Nt,iflaw,0);
\end{verbatim}
\end{minipage}
\vspace*{.5cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
plotifl, midpoint, tfrqview, tfrview.
\end{verbatim}
\end{minipage}


\section{Introduction}
Shallow water flows can be highly sensitive to uncertainties in input data: uncertainties in rating curve extrapolation, the choice of friction coefficient, digital elevation model errors and river survey measurement errors can substantially alter simulated distributions of flood extent, water depth and flow velocity \citep{bates2014,kim-sanders2016,jung-merwade2012,montanari-dibaldassarre2013}.
Conventional uncertainty quantification methods, including the Generalized Likelihood Uncertainty Estimation (GLUE) method \citep{beven-binley1992}, are based on Monte Carlo sampling with randomised inputs, making conventional methods computationally expensive and slow to converge on flow statistics.
As a result, Monte Carlo simulations of flood events are severely constrained by available computing resources, and many sources of uncertainty must be neglected to make probabilistic simulations feasible \citep{neal2013}.
Stochastic Galerkin methods offer a computationally efficient alternative: repeated sampling is eliminated, and convergence is typically far more rapid than Monte Carlo methods \citep{xiu2009,ge2008}.
%Despite widespread interest in stochastic Galerkin and other polynomial chaos methods, existing stochastic Galerkin shallow water models have lacked any well-balanced treatment, and have only been validated for flows with smooth probability distributions \citep{ge2008}.
%This paper addresses these limitations by formulating a well-balanced stochastic Galerkin shallow water model, and assessing the model using flows with discontinuous, non-Gaussian probability distributions.

Polynomial Chaos methods work by introducing to the governing deterministic equations an additional, stochastic dimension, and approximating it with a so-called Polynomial Chaos expansion.
Polynomial Chaos methods can be classified as non-intrusive or intrusive.
Non-intrusive methods repeatedly sample a deterministic model with different input values, using the outputs to construct a stochastic solution.
While non-intrusive polynomial chaos methods involve repeated sampling, they require far fewer samples than Monte Carlo methods \citep{ge2008,ge2011}.
Intrusive methods reformulate a deterministic model to produce a stochastic formulation that must be implemented as a new stochastic solver.
The stochastic Galerkin method is an intrusive method which makes a Galerkin projection in stochastic space to produce a decoupled system of equations that are solved in a single model run to directly evolve the coefficients in the Polynomial Chaos expansion.
Conventional stochastic Galerkin methods span the entire stochastic space using a single element with a basis chosen from the Askey scheme of orthogonal polynomials \citep{xiu-karniadakis2002}.
When the stochastic flow is sufficiently smooth and the probability distributions are well-represented by the Polynomial Chaos basis, then Polynomial Chaos methods can achieve exponential convergence in the stochastic dimension \citep{xiu-karniadakis2003}.
\citet{sattar-elbeltagy2017} applied a conventional stochastic Galerkin method to the one-dimensional water hammer equations, and their model was able to converge on solutions with smooth, Gaussian probability distributions using a first-order Polynomial Chaos basis with just two basis functions.

Conventional stochastic Galerkin methods have two main shortcomings.
First, strongly nonlinear flows produce steep gradients or discontinuities in stochastic space that are often poorly represented by global Polynomial Chaos bases using a single element \citep{pettersson2014}.
Second, even when exponential convergence is achieved early in the simulation, solution accuracy can deteriorate severely over long simulations \citep{gerritsma2010}.
These shortcomings have motivated a wealth of research into better alternatives, including time-dependent basis functions \citep{gerritsma2010}, multiresolution wavelet decompositions \citep{lemaitre2004a}, multi-element discretisations and adaptive meshing of stochastic space \citep{wan-karniadakis2006,tryoen2010a,pettersson2014,li-stinis2015}.
%Similar adaptive methods have also been developed for non-intrusive stochastic collocation methods, and these are reviewed by \citet{bhaduri2018}.

While stochastic Galerkin methods have received a great deal of attention, few have applied the method to the shallow water equations.
\citet{ge2008} made the first step in this direction, formulating a one-dimensional stochastic Galerkin shallow water model to study solitary wave propagation over uncertain topography.
Their stochastic Galerkin model was 50 times more efficient than Monte Carlo with similar accuracy, demonstrating that stochastic Galerkin shallow water models offer a viable alternative to Monte Carlo methods.
However, their numerical tests were limited to flows with smooth, unimodal probability distributions that are more easily represented by stochastic Galerkin methods.
Furthermore, their stochastic Galerkin formulation used a centred difference approximation of the bed slope source term that is not well-balanced.

\rev{Many well-balanced methods have been developed for deterministic shallow flow modelling \citep{kesserwani2013,fjordholm2011}, but these methods have yet to be extended into a probabilistic setting.}
A well-balanced stochastic Galerkin model was formulated by \citet{jin2016} and applied to scalar nonlinear equations including the inviscid Burgers' equation.
The model inherited the well-balancing property from the underlying deterministic model that used the `interface method' by \citet{jin2001}.
\citet{jin2016} note that the intrusive stochastic Galerkin method has a particular advantage over non-intrusive stochastic collocation:
a stochastic collocation method can only guarantee well-balancing at the collocation nodes where the deterministic well-balanced model is sampled.
In contrast, the stochastic Galerkin model guarantees well-balancing across the entire stochastic space, irrespective of the order of Polynomial Chaos basis.

When applied to the shallow water equations, the well-balanced interface method is equivalent to the surface gradient method by \citet{zhou2001}.
A stochastic reformulation of the surface gradient method is straightforward because it is a linear method involving only linear combinations of the discrete flow variables and topography.
Popular alternatives to the surface gradient method include well-balanced approaches that are also depth-positivity preserving \citep{audusse2004,liang-marche2009}.
Such approaches use nonlinear $\max(\cdot, \cdot)$ operators which greatly complicate their stochastic Galerkin reformulation for realistic applications.

This paper presents a well-balanced stochastic Galerkin model of the one-dimensional shallow water equations for simulating probabilistic flows that account for measurement errors in flood plain topography or open channel bathymetry.
This one-dimensional stochastic model represents the first step towards a two-dimensional probabilistic flood model, and it is also relevant to one-dimensional open channel flows.
Being based on a Godunov-type shock-capturing method, the model is capable of simulating subcritical, supercritical and transcritical flows.
The model uses a stochastic reformulation of the surface gradient method \citep{zhou2001}, and the well-balancedness of this reformulation is studied theoretically and verified numerically for an idealised, motionless lake-at-rest over an uncertain and irregular bed.
Next, a steady-state test is proposed that provokes a strongly nonlinear flow response over an uncertain bed, resulting in probability distributions that are highly non-Gaussian and discontinuous.
Probability distributions produced by the stochastic Galerkin model are validated against a Monte Carlo reference simulation, and the relative computational performance is discussed.
Finally, the stochastic Galerkin model is verified for a more realistic steady-state flow over a highly irregular bed with uncertainties that characterise measurement errors in bed elevation.
Numerical simulation data \citep{shaw-kesserwani2019a} and a Python 3 implementation of the stochastic Galerkin shallow flow model \citep{shaw-kesserwani2019b} are available to download from Zenodo.
Instructions for running the models and interpreting the data are provided in the appendix.

%This paper makes the next step by formulating a well-balanced stochastic Galerkin shallow water model, and assessing the model using flows with discontinuous, non-Gaussian probability distributions.