C & Python Resources for Problems in Astrophysics for upcoming CS Exam

  • #1
warhammer
161
31
Hi,

I have a Computer Science exam (course contains Unix, C & Python) coming up which is part of my Astrophysics Masters degree. In the exam, I am allowed to carry all kinds of resources including PDFs of books, except connect to the Internet or use ChatGPT etc.

I wanted to ask if anybody had suggestions or recommendations for books that have solved astrophysics problems in C or Python as "cheatsheet" for the upcoming exam.

Below I'm also sharing one of the questions from a previous year exam for greater clarity~

In this exercise, we will use real exoplanet data from the extrasolar system Encyclopedia accessible at https://exoplanet.eu. The data you will use initially are in the file named exoplanets.csv. Files in the csv (comma-separated values) format use commas to separate successive values. When you open the data file, you will notice that there are many columns in it, the first ones being the name of the planet [name], the status of the planet [planet_status] (confirmed or candidate) and the planet mass [mass], where the labels shown in between the square brackets are the names of the columns in the data file. A few columns later, the radii of the exoplanets are given in the column labelled [radius], which will be relevant later on.

Have a look at the .csv file and try to read it with the techniques we learned in the python course. Treat missing values as NaNs ("Not A Number"). You may encounter difficulties. If you do, describe what you have tried and why you think it does not work.

Create a file called new_exoplanets.txt including only the 3 columns of interest that have the following labels: planet_status, mass, radius (see above for what they represent). In the rest of the text, the planet masses will be labelled Mpla and their radii Rpla.

Create a python code that: (a) computes the number of confirmed and candidate planets and writes it to the screen
(b) plots log10(Rpla/R⊕) (y-axis) Vs log10(Mpla/M⊕) (x-axis) for confirmed exoplanets keeping only planets that have both a mass and a radius (use green dot symbols). Note that the data for masses and radii are in Jupiter units initially. Use the following conversion factors: RJ=11.2 R⊕ and MJ=317.8 M⊕.
(c) We want to fit a power law through the data points using the least-squares method. We will only fit the part that does not look flat, i.e. we will exclude planets with masses greater than 120 M⊕. We recall that if we have a series of data points and want to fit a line such that y = ax + b (here with y=log10(Rpla/R⊕) and x=log10(Mpla/M⊕)), the best fitting values for a and b are given by a and b. Find a and b and print them to the screen (hint: you may use numpy functions if needed).
(d) Plot your fit to the data as a red continuous line on the previous figure. Add labels to the axes and save the figure.


This is the Python section of the Exam. I request any guidance & help on sharing resources/books that cover such problems.
 
Physics news on Phys.org
  • #2
warhammer said:
I request any guidance & help
How well did it go answering this sample question?

warhammer said:
Have a look at the .csv file and try to read it with the techniques we learned in the python course.

I request any guidance & help on sharing resources/books that cover such problems.
You are being examined on the techniques you learned in the Python course: the best resources/books are the ones you learned from in the course. And the best way to master those techniques is practice.
 
Last edited:
  • Like
Likes berkeman and BvU
  • #3
pbuk said:
You are being examined on the techniques you learned in the Python course: the best resources/books are the ones you learned from in the course. And the best way to master those techniques is practice.
My thoughts exactly. Rather than look for references that focus on Astrophysics, I think it's better to focus on the coding techniques, as @pbuk recommended.
 
  • #5
pbuk said:
How well did it go answering this sample question?
It was fine, I had to take some help from my seniors regarding a few parts I was not able to figure out. But the major issue is the time constraint - this previous year problem was supposed to be finished in around 70 minutes but I took a lot longer than this.


pbuk said:
You are being examined on the techniques you learned in the Python course: the best resources/books are the ones you learned from in the course. And the best way to master those techniques is practice.
I would be honest - the course doesn't have a well-defined reference text. We only have Lecture Notes/Modules. The C Module is quite nice but the Python is atrocious. The Python instructor is very helpful & knowledgeable but he basically self-taught himself most of the computation skills he has and is not quite adept at transmitting it for others.

Anyway, I think the major issue is my own deficiencies in this area- CS is not my strong forte and it is something I slogged in since my undergrad until it was almost time to graduate (making up any grade shortfalls through other courses) but it is hurting in this phase. I invest a lot of time in practicing, it helps as well, but I am generally very anxious because a) time constraint in the exam and b) not figuring out what things to have with me during the exam that could alleviate my problems to some extent.
 
  • #6
Mark44 said:
My thoughts exactly. Rather than look for references that focus on Astrophysics, I think it's better to focus on the coding techniques, as @pbuk recommended.
I also agree with it, but I apart from documentation of Numpy, I am not sure and quite lost on what references to take with me that correspond to coding techniques (that also come quite handy in these sort of problems).
 
  • #7
Oh dear, in that case it is difficult to know exactly how to help, but I'll offer you a few thoughts.
  • There are a number of libraries for Python that are particularly useful to astrophysicists in two main areas:
    • numerical computations, where numpy and scipy are very powerful, although if your course also covers C it may be that that language is preferred for computation.
    • data analysis, where Pandas is very useful for dealing with astronomical catalogues. Unfortunately it looks like this is not included in your course (otherwise questions like "have a look at the .csv file and try to read it with the techniques we learned in the python [sic] course" would be irrelevant as pandas.read_csv does it all for you).
  • Python is a very versatile language and enables programming using a number of different paradigms: functional programming can be particularly relevant to dealing with large datasets, but again it looks like this is not part of your course as the way the example question is phrased is clearly looking for iterative answers (e.g. my answer to "create a python code that: (a) computes the number of confirmed and candidate planets and writes it to the screen" would look like this but I am afraid this would get zero marks from your professor):
Python:
from pandas import read_csv
from functools import reduce

def categorize(sum, row):
    key = row.planet_status
    sum[key] = sum[key] + 1 if key in sum else 1
    return sum
   
dfAll = read_csv('C:/Users/paul/Downloads/Sl14lO3G.csv')

print(reduce(categorize, dfAll.itertuples(), {}))
Output:
{'Confirmed': 7353, 'Candidate': 2336, 'Retracted': 76, 'Controversial': 68, 'Unconfirmed': 5}

warhammer said:
I am not sure and quite lost on what references to take with me that correspond to coding techniques (that also come quite handy in these sort of problems).
I don't know any references on the sort of coding techniques that it looks like you are going to be examined on i.e. basic iteration, I think it is just going to be practice. Master for i in range(...) and while loops (not forgetting the power of break and continue in the counter-intuitive while True loop).

warhammer said:
but I am generally very anxious because a) time constraint in the exam
For coding as with anything else the only way to improve your performance against time pressure is practice.
 
Back
Top