hw4 Astro

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Homework 4: Astronomical Data Mining

DS2000: Programming with Data (Sp24)

Introduc)on
In this homework, you will use your knowledge of funcAons, file processing, and visualizaAon to explore the legacy of
a scienAfic legend: Johannes Kepler (1571-1630), a German astronomer and mathemaAcian famous for his discovery
of the three laws of planetary moAon:

1. Planets orbit the sun in an ellipse (not a circle),

2. Planets sweep out equal areas of the ellipse in equal Ame, meaning essenAally that planets move faster when they
are closer to the sun, and slower when they are farther away, and

3. The Ame it takes for the planet to complete one full orbit around the Sun, it’s orbital period (T), is related to the
distance of the planet from the sun (a) according to the following formula:

T2 = k × a3
where k is some constant. Another way of interpreAng this formula is to say that T2 is propor$onal to a3
and that if we were to plot T2 vs a3 we would see that the points fall along a straight line. Data scienAsts would say
that there is a linear rela$onship between the two quanAAes.

In some ways, Kepler can be considered one of the first data scienAsts. Despite his deeply held convicAon that there
was a more geometrical soluAon to the orbits of the planets based on the five Platonic solids, he abandoned this
idea when he realized that the carefully observed posiAons of the planets (compiled by fellow Astronomer Tycho
Brahe), did not fit his model1. The Kepler Exoplanet survey telescope, named aXer the famed astronomer, and more
recent space-based telescopes have now discovered thousands of planets orbiAng other stars throughout the galaxy,
and they all follow Kepler’s three laws of planetary moAon. In other words, Kepler’s law are universal.

Program 1: thirdlaw.py (50 points)


Create a visualizaAon that demonstrates Kepler’s third law of planetary moAon.

• Break your program down into three funcAons:

main(): The entry point of your program. The main funcAon will call each of the next two funcAons. Going
forward, ALL your programs should have a main() funcAon, declared at the bo[om of your program. The
very last line of your program will simply call the main() funcAon to get your program started.

1
“Without data, you’re just another person with an opinion.” ¾ W. Edwards Deming
read_planet_data(filename): accepts the name of a file as input, returns data nicely parsed and forma[ed
into a list of lists. We recommend that each list be one column of data. For this program, you will feed your
read_planet_data funcAon the file planets.csv.

visualize_third_law(data): Generates your visualizaAon demonstraAng that Kepler’s 3rd law is true. This
funcAon accepts the data returned from read_planet_data and generates the desired visualizaAon. This
funcAon does not return any value, it simply renders the visualizaAon.

• Use a sca[er plot to plot T2 (y-axis) vs a3 (x-axis). Use plt.xscale(‘log’) and plt.yscale(‘log’) to generate a
“log-log” plot. When we log-scale an axis, factors of 10 are evenly spaced apart, so that the Ack marks
become, for example, 10, 100, 1000, 10000, and so on. A log-log plot means that we have log-scaled both
the X and Y axis. A log-log plot allows us to render widely ranging values onto a single visualizaAon.

• Make the size of your markers proporAonal to the actual radius of each planet. To do this you will call
plt.sca[er() with an s parameter, s for “size”. You will give the s parameter a list of NINE2 planet diameters,
but those diameters will need to be scaled down by some amount, (i.e., divided by a constant amount) so
that the markers look good on your plot (i.e., are not too big and not too small.) You’ll need to experiment
with different scaling factors to make your plot look good.

• Color each marker with a disAncAve color suggesAve of the planet’s true color. You will find a list of official
matplotlib colors here: h[ps://matplotlib.org/stable/gallery/color/named_colors.html. To do this, you can
assign a list of NINE colors to the color parameter of plt.sca[er(), one color for each of the NINE planets in
the solar system.

• Altogether, to generate our sca[er plot, we have:

plt.sca[er(distances, periods, marker=’o’, s=diameters, colors=planet_color_list)

Remember: it is the periods squared and the distances cubed that we are plokng, and that the diameters
will need to be scaled down by some amount to produce a beauAful visualizaAon.

How will you know that your visualizaAon demonstrates Kepler’s 3rd Law? Your planets will all fall along a straight
line!

Program 2: exoplanets.py (50 points)


Parse the exoplanets.csv data into column lists. Extract only the following columns:

Column 3: Planetary mass [Jupiter masses]


Column 4: Radius [Jupiter radii]

Other columns may be ignored.

The units “Jupiter masses” and “Jupiter radii” mean that the value in the dataset is a mulAple of the mass or radius of
Jupiter. For example, if the exoplanet is 2.5 Jupiter masses, this means that the planet is 2.5 Ames more massive
than Jupiter. For this exercise, convert the Jupiter masses value to Earth masses, and the Jupiter radii value to Earth
radii. Look up the physical properAes of the planets to determine the correct conversion factor.

2
Suck it, Neil DeGrasse Tyson! Kidding, he’s great! h[ps://www.youtube.com/watch?v=93f3dHfV-QY
Now that your values have been scaled to Earth radii and Earth masses, plot Earth radii (y-axis) vs Earth masses (x-
axis) on a log-log plot. Clearly mark where the Earth would appear on this plot and add a text label ‘You are here’
next to your special marker. Save this visualizaAon as exoplanets.png. As with Program 1, create separate funcAons
for reading the data and generaAng your visualizaAon.

Add a 2-3 sentence interpretaAon of your visualizaAon in the header of your program. Imagine you were capAoning
the figure for publicaAon. What would you say? Are the exoplanets discovered by the Kepler mission similar to
Earth or are they very different?

Part of the goal of this exercise is to think about modular design. In general, we strive to break down programs into
digesAble, individually testable funcAons. Professional coders write and test one funcAon at a Ame. Each funcAon
should probably be no more than about 10 lines long. Your main method should be li[le more than a bunch of
funcAon calls, carrying out the primary steps of your program one step at a Ame. Remember to add docstrings to
document your funcAon. The docstring should explain:

1. What the funcAon does in one or two sentences


2. What is the purpose of each input parameter
3. What is the return value (if there is one). If the funcAon returns None (nothing) then say so!

Deliverables
Submit FOUR files:

Program 1: thirdlaw.py and thirdlaw.png

Program 2: exoplanets.py and exoplanets.png


Don’t forget to idenAfy humanity’s new home in the header of your program and to capAon your figure by
include 2-3 sentences in the header that explains, describes, and interprets your figure and answers the
quesAon as to whether exoplanets discovered by the Kepler mission are similar or very different from the
Earth.

• Verify your submission for full credit.

• Use of Internet resources such as ChatGPT to write more than 1 or 2 lines of code or making use of advanced
techniques beyond the scope of the material discussed so far in lecture will earn zero credit. If you are not sure,
ASK! And always cite your sources in accordance with scienAfic best pracAces.

• Copying the program of another student will earn both students zero credit.

You might also like