Data Carpentry Astronomy Workshop

Please run this notebook before the workshop. There are three sections:

  1. A short introduction to Jupyter, with pointers to more resources.

  2. import statements to check whether you have everything installed that we need.

  3. A cell where you will paste a line of code you copy from Slack, to check for a potential problem with "smart" quotes.

At the end there's a link to a survey where you can let us know you're done, or if you have any problems.

Introduction to Jupyter

This is a Jupyter notebook, which is a computational document that contains text, code, and results.

There are several development environments you can use to work with notebooks. Currently the two most common are:

  • Jupyter Classic Notebook, and

  • JupyterLab, which is a newer environment with some improved features, but also some limitations.

During the workshop, we will use the Classic Notebook environment. If you are new to Jupyter, we suggest you should, too.

If you are familiar with JupyterLab and you would rather use it for the workshop, that's fine. Just be aware that there will be differences between your environment and ours.

If you are new to Jupyter, you can read about it here and follow the tutorial called "Try Classic Notebook".

You also might like this tutorial from DataQuest.

The following sections introduce the features you will need for the workshop.

Selecting and running cells

Notebooks are divided into cells that contain either text or code.

This cell is text; the following cell is code:

In [ ]:
print('Hello')

To select a cell, click in the left margin next to the cell. You should see a blue frame surrounding the selected cell.

To edit a code cell, click inside the cell. You should see a green frame around the selected cell, and you should see a cursor inside the cell.

To edit a text cell, double-click inside the cell. Again, you should see a green frame around the selected cell, and you should see a cursor inside the cell.

Text cells use the Markdown typesetting language, which you can read about here.

To run a cell, hold down Shift and press Enter.

  • If you run a text cell, Jupyter formats the text and displays the result.

  • If you run a code cell, Jupyter runs the code in the cell and displays the result, if any.

To try it out, select the previous code cell and press Shift-Enter. It should run the code and print Hello.

Then edit this cell, change some of the text, and press Shift-Enter to format it.

Adding and removing cells

You can add and remove cells from a notebook using the menu at the top of the page and the tool bar below the menu.

Try the following exercises:

  1. From the Insert menu select "Insert cell below" to add a cell below this one. By default, you get a code cell, as you can see in the pull-down menu that says "Code".

  2. In the new cell, add a line of Python code and run it.

  3. Add another cell, select the new cell, and then click on the pull-down menu that says "Code". Select "Markdown". This makes the new cell a text cell.

  4. In the new cell, type some text and format it.

  5. Use the arrow buttons in the tool bar to move cells up and down.

  6. Use the cut, copy, and paste buttons to delete, add, and move cells.

As you make changes, Jupyter saves your notebook automatically, but if you want to make sure, you can press the save button, which looks like a floppy disk from the 1990s.

Finally, when you are done with a notebook, select "Close and Halt" from the File menu.

Check your installation

Run the following cells to import the libraries we need for the workshop.

If any of the libraries are missing, you'll get an error message.

If you don't get any error messages, you are all set.

In [ ]:
from wget import download
In [ ]:
import pandas as pd
import numpy as np

Let's make sure you have a recent version of Pandas.

If the following cell causes an error, you probably have an old version of Pandas. Please update it before the workshop.

In [ ]:
pd.Series([0]).to_numpy()
In [ ]:
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.patches import Polygon
In [ ]:
import astropy.coordinates as coord
import astropy.units as u
from astropy.table import Table
In [ ]:
import gala.coordinates as gc
In [ ]:
# Note: running this import statement opens a connection
# to a Gaia server, so it will fail if you are not connected
# to the internet.

from astroquery.gaia import Gaia

Check for code-pasting problems

During the workshop, we might put code on Slack and ask you to cut and paste it into the notebook.

If you are on a Mac, you might encounter a problem with smart quotes.

To check, following this link to our Slack workspace and find the pinned message with the setup instructions. It contains a line of Python code.

Copy the code from Slack and paste it in the cell below. If it runs without producing an error, you are all set.

Otherwise, you might have to change your system settings so it does not convert straight quotes to smart quotes. If you have trouble with this, let us know and we will provide more details.

Survey

Please fill out this survey to let us know when you are done.

In [ ]: