Tutorial 0a: Setting Up Python For Scientific Computing

© 2020 Griffin Chure and Vahe Galstyan. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license


In this tutorial, we will set up a scientific Python computing environment using the Anaconda python distribution by Continuum Analytics.

Why Python?

As is true in human language, there are hundreds of computer programming languages. While each has its own merit, the major languages for scientific computing are C, C++, R, MATLAB, Python, Java, Julia, and Fortran. MATLAB, Julia, and Python are similar in syntax and typically read as if they were written in plain english. This makes both languages a useful tool for teaching but they are also very powerful languages and are very actively used in real-life research. MATLAB is proprietary while Python is open source. A benefit of being open source is that anyone can write and release Python packages. For science, there are many wonderful community-driven packages such as NumPy, SciPy, scikit-image, and Pandas just to name a few.

Installing Python 3.7 with Anaconda

Python 3.7 vs Python 2.7

There are two dominant versions of Python (available through the Anaconda distribution) used for scientific computing, Python 2.7 and Python 3.7. We are at an interesting crossroads between these two versions. The most recent release (Python 3.7 as of December ) is not backwards compatible with previous versions of Python. While there are still some packages written for Python 2.7 that have not been modified for compatibility with Python 3.7, a large number have transitioned and Python 2.7 will no longer be supported as of January 1, 2020. As this will be the future for scientific computing with Python, we will use Python 3.7 for these tutorials.

Note that Python 3.8 was officially released on October 14, 2019 but is not yet released via Anaconda. For the purposes of this course, we will use Python 3.7, which is the latest stable build available through Anaconda.

Anaconda

There are several scientific Python distributions available for MacOS, Windows, and Linux. The two most popular, Enthought Canopy and Anaconda are specifically designed for scientific computing and data science work. For this course, we will use the Anaconda Python 3.7 distribution. To install the correct version, follow the instructions below.

  1. Navigate to the Anaconda download page and download the Python 3.7 graphical installer.

  2. Launch the installer and follow the onscreen instructions.

Congratulations! You now have the beginnings of a scientific Python distribution.

Using JupyterLab as a Scientific Development Environment

Packaged with the Anaconda Python distribution is the Jupyter project. This environment is incredibly useful for interactive programming and development and is widely used across scientific computing. Jupyter allows for interactive programming in a large array of programming languages including Julia, R, and MATLAB. As you've guessed by this point, we will be focusing on using Python through the Jupyter Environment.

The key component of the Jupyter interactive programming environment is the Jupyter Notebook. This acts lkike an interactive script which allows one to interweave code, mathematics, and text to create a complete narrative around your computational project. In fact, you are reading a Jupyter Notebook right now!

While Jupyter Notebooks are fantastic alone, we will be using them throughout the course via the JupyterLab Integrated Development Environment (IDE). JupyterLab allows omne to write code in notebooks, navigate around your file system, write isolated python scripts, and even access a UNIX terminal, all of which we will do throughout this class. Even better, JupyterLab comes prepackaged with your Anaconda Python distribution.

Launching JupyterLab

When you installed Anaconda, you also installed the Anaconda Navigator, an app that allows you to easily launch a JupyterLab instance. When you open up Anaconda Navigator, you should see a screen that looks like this,

where I have boxed in the JupyterLab prompt with a red box. Launch the JupyterLab IDE by clicking the 'launch' button. This should automatically open a browser window with the JupyterLab interface,

Starting A Jupyter Notebook

Let's open a new notebook. Navigate to the folder where you store your code files (aph161_in_class/code in my case) and click the + in the sidebar. This will open a new "Launcher" window where a variety of new filetypes can be opened. One of them will be a "Python 3 Notebook".

Clicking this will open a new Jupyter Nook named Untitled.ipynb.

Right-click the "Untitled.ipynb" in the sidebar and rename it to something more informative, say testing_out_python.ipynb.

The right-ha d side of your screen is the actual notebook. You will see a "code cell" (grey rectangle) along with a bunch of other boxes above it. In the Jupyter Notebook Tutorial we cover these buttons in detail. For now, we'll just check to make sure you have a working Python distribution.

Hello, Pangaea

Let's write our first bit of Python code to make sure that everything is working correctly on your system. In Jupyter Notebooks, all code is typed in grey rectangles called "code cells". When a cell is "run", the result of the computation is shown underneath the code cell. Double-click the code cell on the right-hand side of your JupyterLab window and type the following:

In [1]:
# This a comment and won't be read by Python. All comments start with `#`
print('Hello, Pangaea. Long time, no see. This sentence should be printed below by pressing `Shift + Enter` ')
Hello, Pangaea. Long time, no see. This sentence should be printed below by pressing `Shift + Enter` 

Note that you cannot edit the text below the code cell. This is the output of the print() function in Python.

Our First Plot

This class will often require you to generate plots of your computations coupled with some comments about your interpretation. Let's try to generate a simple plot here to make sure everything is working with your distribution. Don't worry too much about the syntax for right now. The basics of Python syntax are given in Tutorial 0c.

Add a new code cell beneath the one that contains print('Hello, Pangaea'). When you execute a cell using Shift + Enter, a new cell should appear beneath what you just ran. If it's not there, you can make a new cell by clicking the + icon in the notebook menu bar. In the new cell, type the following:

In [2]:
# Import Python packages necessary for this script
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set()

# Generate a beautiful sinusoidal curve
x = np.linspace(0, 2*np.pi, 500)
y = np.sin(2 * np.sin(2 * np.sin(2 * x)))
plt.plot(x, y)
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.show()
In [ ]: