Pioneer21/Summaries
This page will provide lecture summaries for the Pioneer Academics Summer 2021 course on Python and Image Processing.
Contents
Lecture 01 - 7/11/2021
- Introductions
- Overleaf Document Preparation System
- Once you have your account, you can go to the Learn LaTeX in 30 minutes tutorial on Overleaf!
- Free document preparation system using $$\LaTeX$$
- Website: https://www.overleaf.com; sign up for a free account
- To create the sample document:
- Create a new blank project in Overleaf and call it whatever you would like (I called mine PioneerDemo); this will move that tab to the project page with a "main.tex" document
- Open another tab and get the demonstration files in a zip file either on Schoology->.Session 01 Items->Sample LaTeX Docs->PioneerDemo.zip or on the Box drive in the Session 01 LaTeX folder
- Expand the zip file to get three files: main.tex, MakeSample.py, and SamplePyplot.png
- Go back to the Overleaf tab for your project.
- Click the upload icon (third one from the left under the Menu icon at the top left of the screen)
- Drag the three files into the window or select them. When Overleaf asks if you want to overwrite the main.tex, select Overwrite to replace the default with the sample.
- On the right-half of the Overleaf window, click the Recompile button to see the sample file
- The document infrastructure will always be provided; this includes something similar to the following:
\documentclass{article} \usepackage{amsmath} % loads AMS-Math package \usepackage{graphicx} % bring in graphics \usepackage{listings} % allows lstlisting environment \usepackage{hyperref} % web page links \usepackage[letterpaper, margin=1.0in]{geometry} % set paper size/margins \begin{document} \begin{center} \rule{6.5in}{0.5mm}\\~\\ \textbf{\large Pioneer - Summer 2021}\\~\\ \textbf{\huge TITLE}\\~\\ NAME\\ \rule{6.5in}{0.5mm}\\ \end{center} \tableofcontents \listoffigures \pagebreak % Repeat as needed \section{SECTION ONE} Stuff \pagebreak \appendix \section{Code} % Repeat as needed - uncomment the lstinputlisting lines when the codes exists \subsection{CODE DESCRIPTION 1} %%%\lstinputlisting[language=Python]{code1.py} \pagebreak \section{Figures \label{FigureList}} % Repeat as needed - uncomment the lstinputlisting lines when the graphs exists \begin{figure}[!h] \begin{center} %%%\includegraphics[width=5in]{figure1.png} \caption{FIGURE 1 CAPTION.} \end{center} \end{figure} \pagebreak \addcontentsline{toc}{section}{References} \begin{thebibliography}{9} % Repeat as needed \bibitem{REFCODE1} REFERENCE 1 \end{thebibliography} \end{document}
- Some commands from the demonstration document:
- Document divisions
- Use
\section{NAME}
and \subsection{NAME} to divide your document
- Use
- Text styles and sizes - for more info see https://www.overleaf.com/learn/latex/Font_sizes,_families,_and_styles
- Text can be made bold, italics, or typewriter font with
\textbf{WORDS}
,\textit{WORDS}
, or\texttt{WORDS}
- Text can be made large or huge with
{\large WORDS}
or{\huge WORDS}
; there are other sizes available
- Text can be made bold, italics, or typewriter font with
- Use
\begin{center} ... \end{center}
to center items such as text, figures, and tables - Use $ ... $ to put math-mode items in a sentence - for instance "The hypotenuse $$c$$ or a right triangle with sides $$a$$ and $$b$$ is $$c=\sqrt{a^2+b^2}$$" which was produced by the code
The hypotenuse $c$ or a right triangle with sides $a$ and $b$ is $c=\sqrt{a^2+b^2}$
. (Note: on Pundit, getting into math mode requires to dollar signs instead of one). - Use
\begin{align} ... \end{align}
for a series of numbered, formal equations and\begin{align} ... \end{align}
for a series of unnumbered, formal equations. For example: $$ \begin{align} \begin{bmatrix} A \end{bmatrix} &= \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}\\ D &= \begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix} =a_{11}a_{22}-a_{12}a_{21} \end{align} $$ which was created with\begin{align} \begin{bmatrix} A \end{bmatrix} &= \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}\\ D &= \begin{vmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{vmatrix} =a_{11}a_{22}-a_{12}a_{21} \end{align}
- Superscripts and subscripts are in math mode; if the superscript or subscript is more than one character, the superscript or subscript needs to be enclosed in curly brackets.
- If you are unsure of the command for a specific symbol, Detexify can be a real help!
- Some math terms should be in regular type versus italics in math mode; they have command versions like
\max
. Otherwise, if you need regular text in math mode, use\mbox{WORDS}
- $$\LaTeX$$ can import text files using
\lstinputlisting[OPTION]{FILE}
where the main option is to set the language; for example:\lstinputlisting[language=Python]{MakeSample.py}
- $$\LaTeX$$ can also import graphics files using
\includegraphics[OPTION]{file}
where the main option is to set the width or height of the imagel for example:\includegraphics[width=5in]{SamplePyplot.png}
- Document divisions
- Anaconda and Python
- Website: Anaconda; download the appropriate version for your operating system.
- Long way to open Spyder is to start Anaconda Navigator and launch Spyder from there; shortcut on Windows is to go to Start -> Anaconda -> Spyder
Lecture 02 - 7/18/2021
During this lecture, we did three main things:
- Developed a program meant to track and display multiple coin-flipping trials,
- Looked at some ways of using the scikit image library, and
- Wrote a program that started with an array of random images and evolved it using a particular rule for what each pixel will look like in the next evolution.
Coin Flipping
The end goal here is to perform multiple trials of a coin-flipping experiment where each trial consists of flipping a coin a certain number of times. We will be keeping track of how many times he flip "heads" on a fair, two-sided coin.
Flipping One Coin
Flipping a coin is a random process. We are going to assume a fair, two-sided coin and thus need to figure out how to represent "heads" and "tails" numerically. We ended up using np.random.randint() (Documentation at numpy.org) to generate a single random number that is either a 0 or a 1 and then returned a 1 if the coin was "heads" (or 1) and a 0 otherwise. Here are some helpful tutorials from w3schools' Python Tutorials:
- Python Functions
- Python If...Else
- NumPy Array Indexing
- NumPy Random Intro
- Note - this module uses "from numpy import random" which means "random.X" will expand to "numpy.random.X" -- we used "import numpy as np" which means we would need to write "np.random.X" to get the same function. Any command in the tutorial that starts with "random." needs to start with "np.random." the way we do things.
Flipping 100 Coins
Now that we can flip a single coin, we want to flip 100 coins and track what the flips are and how many total ended up being heads. The "keeping track" part means starting an empty list and then appending each new flip to it. Once the list is completed, we can convert it into an array in case we want to do math on it. We can then use the .sum() method on the array to get the total number of 1's in the array. Here are some helpful tutorials from w3schools' Python Tutorials:
Flipping 100 Coins 200 Times
Next, we can look at multiple 100-flip trials to see what each trial yields. The goal here is not to simply look at 20000 coin-flips but rather see how each trial differs from the next. We started by moving the trial mechanics to a function so I could just call it from main and get the number of times heads was flipped in a 100-flip trial. In the main program, we ran a loop that in some ways mirrored the trial structure - create an empty list and then loop some number of times, appending the new results to the list as we go along. We then looked at the information three different ways:
- Printing the total sum of heads across all trials,
- Bar chart of the number of heads in a trial as a function of the trial number, and
- Histogram of the number of heads in the trials.
Here are some helpful tutorials from w3schools' Python Tutorials:
Also see the Pundit page on Python:Plotting. Note that the
fig, ax = plt.subplots(num=1, clear=True)
code in our program does two things at once: it creates a figure numbered as requested and assigns it to the variable fig
and within that figure, it creates a set of axes we can use to make a plot and assigns it to the variable ax
. To make a plot, tell ax
what to do. If you want to make two figures, you can either re-use the variable names or create new ones; that is
fig, ax = plt.subplots(num=1, clear=True)
PLOT 1 STUFF
fig, ax = plt.subplots(num=1, clear=True)
PLOT 2 STUFF
works the same as
fig1, ax1 = plt.subplots(num=1, clear=True)
PLOT 1 STUFF
fig2, ax2 = plt.subplots(num=2, clear=True)
PLOT 2 STUFF
with the difference being you can still access the figure 1 variables even after creating figure 2. The two-step version of all of the above (which more closely resembles what is in the Pundit page on plotting) would be:
fig = plt.figure(num=1, clear=True)
ax = fig.add_subplot(1, 1, 1)
PLOT 1 STUFF
fig = plt.figure(num=2, clear=True)
ax = fig.add_subplot(1, 1, 1)
PLOT 2 STUFF
where the "1,1,1" part means "in this figure, make one row and one column of axes and give me access to the first one." See Python:Plotting#Example_Redux if you are interested in learning more on how to use more than one row or column of subplots.
Flipping C Coins N Times
Finally, if we want the program to be more flexible, we can use variables to store how many coins to flip each trial and how many trials to run. This involves giving a parameter to "run_trials()" that defaults to 100 flips per trial but can be overridden. Also, we can use a variable to store the number of trials to run - this will impact both the loop that runs the trials and the x-axis of the bar chart.