This is the sixth in a series of notebooks related to astronomy data.
As a continuing example, we will replicate part of the analysis in a recent paper, “Off the beaten path: Gaia reveals GD-1 stars outside of the main stream” by Adrian M. Price-Whelan and Ana Bonaca.
In the previous lesson we downloaded photometry data from Pan-STARRS, which is available from the same server we’ve been using to get Gaia data.
The next step in the analysis is to select candidate stars based on the photometry data.
The following figure from the paper is a color-magnitude diagram showing the stars we previously selected based on proper motion:
In red is a theoretical isochrone, showing where we expect the stars in GD-1 to fall based on the metallicity and age of their original globular cluster.
By selecting stars in the shaded area, we can further distinguish the main sequence of GD-1 from mostly younger background stars.
Here are the steps in this notebook:
We’ll reload the data from the previous notebook and make a color-magnitude diagram.
We’ll use an isochrone computed by MIST to specify a polygonal region in the color-magnitude diagram and select the stars inside it.
Then we’ll merge the photometry data with the list of candidate stars, storing the result in a Pandas
After completing this lesson, you should be able to
Use Matplotlib to specify a
Polygonand determine which points fall inside it.
Use Pandas to merge data from multiple
DataFrames, much like a database
Reload the data¶
The following cell downloads the photometry data we created in the previous notebook.
import os from wget import download filename = 'gd1_photo.fits' filepath = 'https://github.com/AllenDowney/AstronomicalData/raw/main/data/' if not os.path.exists(filename): print(download(filepath+filename))
Now we can read the data back into an Astropy
from astropy.table import Table photo_table = Table.read(filename)
Plotting photometry data¶
Now that we have photometry data from Pan-STARRS, we can replicate the color-magnitude diagram from the original paper:
The y-axis shows the apparent magnitude of each source with the g filter.
The x-axis shows the difference in apparent magnitude between the g and i filters, which indicates color.
Stars with lower values of (g-i) are brighter in g-band than in i-band, compared to other stars, which means they are bluer.
Stars in the lower-left quadrant of this diagram are less bright than the others, and have lower metallicity, which means they are likely to be older.
Since we expect the stars in GD-1 to be older than the background stars, the stars in the lower-left are more likely to be in GD-1.
The following function takes a table containing photometry data and draws a color-magnitude diagram.
The input can be an Astropy
Table or Pandas
DataFrame, as long as it has columns named
import matplotlib.pyplot as plt def plot_cmd(table): """Plot a color magnitude diagram. table: Table or DataFrame with photometry data """ y = table['g_mean_psf_mag'] x = table['g_mean_psf_mag'] - table['i_mean_psf_mag'] plt.plot(x, y, 'ko', markersize=0.3, alpha=0.3) plt.xlim([0, 1.5]) plt.ylim([14, 22]) plt.gca().invert_yaxis() plt.ylabel('$Magnitude (g)$') plt.xlabel('$Color (g-i)$')
plot_cmd uses a new function,
invert_yaxis, to invert the
y axis, which is conventional when plotting magnitudes, since lower magnitude indicates higher brightness.
invert_yaxis is a little different from the other functions we’ve used. You can’t call it like this:
plt.invert_yaxis() # doesn't work
You have to call it like this:
plt.gca().invert_yaxis() # works
gca stands for “get current axis”. It returns an object that represents the axes of the current figure, and that object provides
In case anyone asks: The most likely reason for this inconsistency in the interface is that
invert_yaxis is a lesser-used function, so it’s not made available at the top level of the interface.
Here’s what the results look like.
Our figure does not look exactly like the one in the paper because we are working with a smaller region of the sky, so we don’t have as many stars. But we can see an overdense region in the lower left that contains stars with the photometry we expect for GD-1.
In the next section we’ll use an isochrone to specify a polygon that contains this overdense regioin.
Based on our best estimates for the ages of the stars in GD-1 and their metallicity, we can compute a stellar isochrone that predicts the relationship between their magnitude and color.
In fact, we can use MESA Isochrones & Stellar Tracks (MIST) to compute it for us.
Using the MIST Version 1.2 web interface, we computed an isochrone with the following parameters:
Rotation initial v/v_crit = 0.4
Single age, linear scale = 12e9
Composition [Fe/H] = -1.35
Synthetic Photometry, PanStarrs
Extinction av = 0
The following cell downloads the results:
import os from wget import download filename = 'MIST_iso_5fd2532653c27.iso.cmd' filepath = 'https://github.com/AllenDowney/AstronomicalData/raw/main/data/' if not os.path.exists(filename): print(download(filepath+filename))
To read this file we’ll download a Python module from this repository.
import os from wget import download filename = 'read_mist_models.py' filepath = 'https://github.com/jieunchoi/MIST_codes/raw/master/scripts/' if not os.path.exists(filename): print(download(filepath+filename))
Now we can read the file:
import read_mist_models filename = 'MIST_iso_5fd2532653c27.iso.cmd' iso = read_mist_models.ISOCMD(filename)
Reading in: MIST_iso_5fd2532653c27.iso.cmd
The result is an
It contains a list of arrays, one for each isochrone.
We only got one isochrone.
So we can select it like this:
iso_array = iso.isocmds
It’s a NumPy array:
But it’s an unusual NumPy array, because it contains names for the columns.
dtype([('EEP', '<i4'), ('isochrone_age_yr', '<f8'), ('initial_mass', '<f8'), ('star_mass', '<f8'), ('log_Teff', '<f8'), ('log_g', '<f8'), ('log_L', '<f8'), ('[Fe/H]_init', '<f8'), ('[Fe/H]', '<f8'), ('PS_g', '<f8'), ('PS_r', '<f8'), ('PS_i', '<f8'), ('PS_z', '<f8'), ('PS_y', '<f8'), ('PS_w', '<f8'), ('PS_open', '<f8'), ('phase', '<f8')])
Which means we can select columns using the bracket operator:
array([0., 0., 0., ..., 6., 6., 6.])
We can use
phase to select the part of the isochrone for stars in the main sequence and red giant phases.
phase_mask = (iso_array['phase'] >= 0) & (iso_array['phase'] < 3) phase_mask.sum()
main_sequence = iso_array[phase_mask] len(main_sequence)
The other two columns we’ll use are
PS_i, which contain simulated photometry data for stars with the given age and metallicity, based on a model of the Pan-STARRS sensors.
We’ll use these columns to superimpose the isochrone on the color-magnitude diagram, but first we have to use a distance modulus to scale the isochrone based on the estimated distance of GD-1.
We can use the
Distance object from Astropy to compute the distance modulus.
import astropy.coordinates as coord import astropy.units as u distance = 7.8 * u.kpc distmod = coord.Distance(distance).distmod.value distmod
Now we can compute the scaled magnitude and color of the isochrone.
mag_g = main_sequence['PS_g'] + distmod color_g_i = main_sequence['PS_g'] - main_sequence['PS_i']
Now we can plot it on the color-magnitude diagram like this.
plot_cmd(photo_table) plt.plot(color_g_i, mag_g);
The theoretical isochrone passes through the overdense region where we expect to find stars in GD-1.
Let’s save this result so we can reload it later without repeating the steps in this section.
So we can save the data in an HDF5 file, we’ll put it in a Pandas
import pandas as pd iso_df = pd.DataFrame() iso_df['mag_g'] = mag_g iso_df['color_g_i'] = color_g_i iso_df.head()
And then save it.
filename = 'gd1_isochrone.hdf5' iso_df.to_hdf(filename, 'iso_df')
Making a polygon¶
The following cell downloads the isochrone we made in the previous section, if necessary.
import os from wget import download filename = 'gd1_isochrone.hdf5' filepath = 'https://github.com/AllenDowney/AstronomicalData/raw/main/data/' if not os.path.exists(filename): print(download(filepath+filename))
Now we can read it back in.
iso_df = pd.read_hdf(filename, 'iso_df') iso_df.head()
Here’s what the isochrone looks like on the color-magnitude diagram.
plot_cmd(photo_table) plt.plot(iso_df['color_g_i'], iso_df['mag_g']);
In the bottom half of the figure, the isochrone passes through the overdense region where the stars are likely to belong to GD-1.
In the top half, the isochrone passes through other regions where the stars have higher magnitude and metallicity than we expect for stars in GD-1.
So we’ll select the part of the isochrone that lies in the overdense region.
g_mask is a Boolean Series that is
g is between 18.0 and 21.5.
g = iso_df['mag_g'] g_mask = (g > 18.0) & (g < 21.5) g_mask.sum()
We can use it to select the corresponding rows in
iso_masked = iso_df[g_mask] iso_masked.head()
Now, to select the stars in the overdense region, we have to define a polygon that includes stars near the isochrone.
The original paper uses the following formulas to define the left and right boundaries.
g = iso_masked['mag_g'] left_color = iso_masked['color_g_i'] - 0.4 * (g/28)**5 right_color = iso_masked['color_g_i'] + 0.8 * (g/28)**5
The intention is to define a polygon that gets wider as
g increases, to reflect increasing uncertainty.
But we can do about as well with a simpler formula:
g = iso_masked['mag_g'] left_color = iso_masked['color_g_i'] - 0.06 right_color = iso_masked['color_g_i'] + 0.12
Here’s what these boundaries look like:
plot_cmd(photo_table) plt.plot(left_color, g, label='left color') plt.plot(right_color, g, label='right color') plt.legend();
Which points are in the polygon?¶
Matplotlib provides a
Polygon object that we can use to check which points fall in the polygon we just constructed.
To make a
Polygon, we need to assemble
right_color into a loop, so the points in
left_color are connected to the points of
right_color in reverse order.
We’ll use the following function, which takes two arrays and joins them front-to-back:
import numpy as np def front_to_back(first, second): """Join two arrays front to back.""" return np.append(first, second[::-1])
front_to_back uses a “slice index” to reverse the elements of
As explained in the NumPy documentation, a slice index has three parts separated by colons:
start: The index of the element where the slice starts.
stop: The index of the element where the slice ends.
step: The step size between elements.
In this example,
stop are omitted, which means all elements are selected.
-1, which means the elements are in reverse order.
We can use
front_to_back to make a loop that includes the elements of
color_loop = front_to_back(left_color, right_color) color_loop.shape
And a corresponding loop with the elements of
g in forward and reverse order.
mag_loop = front_to_back(g, g) mag_loop.shape
Here’s what the loop looks like.
plot_cmd(photo_table) plt.plot(color_loop, mag_loop);
To make a
Polygon, it will be convenient to put
mag_loop into a
loop_df = pd.DataFrame() loop_df['color_loop'] = color_loop loop_df['mag_loop'] = mag_loop
Now we can pass
from matplotlib.patches import Polygon polygon = Polygon(loop_df) polygon
<matplotlib.patches.Polygon at 0x7fe98cd29400>
The result is a
Polygon object , which provides
contains_points, which figures out which points are inside the polygon.
To test it, we’ll create a list with two points, one inside the polygon and one outside.
points = [(0.4, 20), (0.4, 16)]
Now we can make sure
contains_points does what we expect.
inside = polygon.contains_points(points) inside
array([ True, False])
The result is an array of Boolean values.
We are almost ready to select stars whose photometry data falls in this polygon. But first we need to do some data cleaning.
Save the polygon¶
Reproducibile research is “the idea that … the full computational environment used to produce the results in the paper such as the code, data, etc. can be used to reproduce the results and create new work based on the research.”
This Jupyter notebook is an example of reproducible research because it contains all of the code needed to reproduce the results, including the database queries that download the data and and analysis.
In this lesson we used an isochrone to derive a polygon, which we used to select stars based on photometry. So it is important to record the polygon as part of the data analysis pipeline.
Here’s how we can save it in an HDF file.
filename = 'gd1_polygon.hdf5' loop_df.to_hdf(filename, 'loop_df')
Reloading the data¶
Now we need to combine the photometry data with the list of candidate stars we identified in a previous notebook. The following cell downloads it:
import os from wget import download filename = 'gd1_candidates.hdf5' filepath = 'https://github.com/AllenDowney/AstronomicalData/raw/main/data/' if not os.path.exists(filename): print(download(filepath+filename))
import pandas as pd candidate_df = pd.read_hdf(filename, 'candidate_df')
candidate_df is the Pandas DataFrame that contains the results from Lesson 4, which selects stars likely to be in GD-1 based on proper motion. It also includes position and proper motion transformed to the ICRS frame.
Merging photometry data¶
Before we select stars based on photometry data, we have to solve two problems:
We only have Pan-STARRS data for some stars in
Even for the stars where we have Pan-STARRS data in
photo_table, some photometry data is missing.
We will solve these problems in two step:
We’ll merge the data from
photo_tableinto a single Pandas
We’ll use Pandas functions to deal with missing data.
candidate_df is already a
results is an Astropy
Table. Let’s convert it to Pandas:
photo_df = photo_table.to_pandas() for colname in photo_df.columns: print(colname)
source_id g_mean_psf_mag i_mean_psf_mag
Now we want to combine
photo_df into a single table, using
source_id to match up the rows.
You might recognize this task; it’s the same as the JOIN operation in ADQL/SQL.
Pandas provides a function called
merge that does what we want. Here’s how we use it.
merged = pd.merge(candidate_df, photo_df, on='source_id') merged.head()
The first argument is the “left” table, the second argument is the “right” table, and the keyword argument
on='source_id' specifies a column to use to match up the rows.
The result is a
DataFrame that contains the same number of rows as
len(candidate_df), len(photo_df), len(merged)
(7346, 3724, 3724)
And it contains all columns from both tables.
for colname in merged.columns: print(colname)
source_id ra dec pmra pmdec parallax radial_velocity phi1 phi2 pm_phi1 pm_phi2 g_mean_psf_mag i_mean_psf_mag
Detail You might notice that Pandas also provides a function called
join; it does almost the same thing, but the interface is slightly different. We think
merge is a little easier to use, so that’s what we chose. It’s also more consistent with JOIN in SQL, so if you learn how to use
pd.merge, you are also learning how to use SQL JOIN.
Also, someone might ask why we have to use Pandas to do this join; why didn’t we do it in ADQL. The answer is that we could have done that, but since we already have the data we need, we should probably do the computation locally rather than make another round trip to the Gaia server.
Selecting based on photometry¶
Now let’s see how many of these points are inside the polygon we chose.
We’ll put color and magnitude data from
merged into a new
points = pd.DataFrame() points['color'] = merged['g_mean_psf_mag'] - merged['i_mean_psf_mag'] points['mag'] = merged['g_mean_psf_mag'] points.head()
Which we can pass to
inside = polygon.contains_points(points) inside
array([False, False, False, ..., False, False, False])
The result is a Boolean array. We can use
sum to see how many stars fall in the polygon.
Now we can use
inside as a mask to select stars that fall inside the polygon.
selected2 = merged[inside] points2 = points[inside]
Let’s make a color-magnitude plot one more time, highlighting the selected stars with green markers.
plot_cmd(photo_table) plt.plot(color_g_i, mag_g) plt.plot(color_loop, mag_loop) plt.plot(points2['color'], points2['mag'], 'g.');
It looks like the selected stars are, in fact, inside the polygon, which means they have photometry data consistent with GD-1.
Finally, we can plot the coordinates of the selected stars:
plt.figure(figsize=(10,2.5)) x = selected2['phi1'] y = selected2['phi2'] plt.plot(x, y, 'ko', markersize=0.7, alpha=0.9) plt.xlabel('ra (degree GD1)') plt.ylabel('dec (degree GD1)') plt.axis('equal');
This example includes two new Matplotlib commands:
figurecreates the figure. In previous examples, we didn’t have to use this function; the figure was created automatically. But when we call it explicitly, we can provide arguments like
figsize, which sets the size of the figure.
axiswith the parameter
equalsets up the axes so a unit is the same size along the
In an example like this, where
y represent coordinates in space, equal axes ensures that the distance between points is represented accurately.
Write the data¶
Finally, let’s write the merged DataFrame to a file.
filename = 'gd1_merged.hdf5' merged.to_hdf(filename, 'merged') selected2.to_hdf(filename, 'selected2')
!ls -lh gd1_merged.hdf5
-rw-rw-r-- 1 downey downey 1.1M Dec 29 11:51 gd1_merged.hdf5
If you are using Windows,
ls might not work; in that case, try:
In this lesson, we worked with three datasets:
The list of candidate stars from Gaia,
The photometry data from Pan-STARRS, and
An isochrone computed by MIST.
We drew a color-magnitude diagram and used it to identify stars we think are likely to be in GD-1.
We used the isochrone to define a polygon that includes those stars.
Then we used a Pandas
merge operation to combine Gaia and Pan-STARRS data into a single
Plotting the results, we have a clear picture of GD-1, similar to Figure 1 in the original paper.
Matplotlib provides operations for working with points, polygons, and other geometric entities, so it’s not just for making figures.
If you want to perform something like a database
JOINoperation with data that is in a Pandas
DataFrame, you can use the
mergefunction. In many cases,
mergeis easier to use because the arguments are more like SQL.
Use Matplotlib options to control the size and aspect ratio of figures to make them easier to interpret. In this example, we scaled the axes so the size of a degree is equal along both axes.
Be sure to record every element of the data analysis pipeline that would be needed to replicate the results.