now the best advice that i could give to
any
aspiring astrophysicist or any type of
scientist out there
is to learn to code and that might come
as a surprise to some of you but it's
actually one of the main parts of my job
as a researcher looking into how
supermassive black holes
affect the galaxies that they live in so
i've compiled a list of the five
main ways that i use code in my job as
an astrophysicist
everything from processing images of
galaxies that i've taken with telescopes
to fitting models to that data but
before we go through
all of those we first need to figure out
what do i actually
mean by code
now if you're thinking cryptic messages
and cracking ciphers
imitation game style not that type
of code i'm talking about coding as a
language
specifically a language that both you
and a computer
can understand so that you can then talk
to it give it a specific
set of instructions a bit like a recipe
so you can program a computer to do a
very specific
task for you anything from telling it to
add some numbers together
or how to format a web page or even to
designing
a video game all of these different
tasks are best
communicated to a computer with a
specific language
usually a language that's defined for
that set of tasks
for example if you wanted to format a
web page
you need a language that lets you tell
it what font to use and
how big to make an image on the page and
what to do if people click
on a specific button to that people use
html and programming
languages like javascript whereas for a
video game
you want a language that's going to let
you communicate to the computer
how a character or an object behaves
as you interact as you play the game so
people use languages like
c plus plus and java to do that
for science though it's a different
story you need a language that's both
powerful and flexible that's going to
let you do things like
read really large data tables
or make plots to visualize that data or
fit models to that data sometimes
performing the same task
over and over again thousands if not
millions of times and ideally you want
it to do that in
seconds or at least as fast as it can
anyway
now there's a couple of languages that
can do this that are both
powerful enough flexible enough but the
easiest to read of those and therefore
the most popular currently as well is
python and that is
my language of choice as well i like to
say i'm a parcel tong
and i use python daily for doing my list
of five
things so let's dive into this and start
with
number one image processing now the
first thing any scientist needs to do
some science
is data and in my case as an
astrophysicist that data is
images of galaxies from telescopes
whether they be on the ground
or in space but there's lots of sources
of
noise in these images and what i mean by
noise is
light not from the object you care about
but from some other source that you
don't care about
whether that might be background noise
from the sky
itself with just light bouncing around
the atmosphere or whether it's noise in
the detector
so if you just left the shutter closed
and you kept the detector dark
and still took an exposure you would
still detect some background residual
of light all of these sources of noise
need to be
accounted for and removed from your
image so that you can do
science with it essentially you do that
pixel by pixel which is an
incredibly tedious task but it's one
that a computer can do
in seconds as long as you give it the
right commands
python is a perfect language for this
because one of its many
strengths is being able to deal with big
arrays of numbers
and what is an image after all but a big
array of numbers with each number
telling you
how much light has been detected in each
pixel with code like this which is
python using a lot of the astro pi
functions and open source library of
lots of coding functions to do astronomy
related tasks
you can turn messy fresh off their
telescope images like this
into useful scientific images like this
with only the light from the thing you
actually care about
left in the image the second thing i use
code for is data analysis
so once you have your scientific image
of an object in my case a galaxy you
might want to
do some measurements on that image so
you might want to measure
how big the galaxy is or how much light
you've received from that galaxy in
total
how bright it is or you might want to
record how bright the central region is
compared to the outside region or the
color and so on and so on
and so on and if you do that for tens of
galaxies ten thousand galaxies a million
galaxies
you're essentially going to end up with
a huge big data table
of properties that you've measured one
of the longest data tables i have is
over 600
000 rows long that's six hundred
thousand galaxies each with different
properties
measured that's far too long for any
excel spreadsheet to be able to handle
but again python is perfect for this
because this is just another form of an
array
of numbers and so i can use python
to play around with my data in this
table so for example for every single
row
in this table i might want to convert
one of the measured properties in a
column
into another unit or i might want to use
it to calculate another property
or i might want to do some statistics on
my sample
either way given the right commands a
computer can do all of this
on every single row in your data table
far quicker
than either you or i would ever be able
to do it by hand or
universe forbid by splitting your data
table into
chunks on an excel spreadsheet someone
might want to tell the uk government
track and trace people
that though number three model fitting
so once you've got all of this data you
want to be able to understand the
underlying physics of what's going on so
that you can do some science
and one of the best ways to do that is
actually to fit a model to your data the
best fit model can actually tell you
a lot it can tell you whether your
assumptions about what physics is
driving the results you're getting
actually right or not instead if you
have a
terrible model then you know that that
idea was definitely wrong because it
doesn't fit the data
at all the simplest form of that that
people i'm sure will have come across
is fitting a straight line to some data
it's just a test of whether two
properties
are correlated or not for example here
i'm showing the mass of the galaxy's
supermassive black hole
and its overall mass in stars
you can see there's quite a lot of
scatter but it is still correlated and
that led to the hypothesis that galaxies
and black holes
co-evolve and grow in mass together
another thing you might want to do in
terms of model fitting
is infer an unknown from something you
do know for example if a galaxy is very
blue in color it has a lot of
hot young big stars but if a galaxy is
very reddish in color it probably has a
lot of
old cooler smaller stars i can then use
those colors to ask
has that galaxy stop forming stars
recently
i do this by modeling the number of
stars a galaxy formed as an
exponential decline of various different
rates
i can then turn that into module colors
knowing what we know about how different
masses of stars are different colors
and then i can say okay well which model
best fits the observed color i have
in this image of the galaxy i took so
again with some well-placed python
commands this time just to find the
minimum
of an equation i.e when the difference
between the modeled and the observed
colors is the smallest it can be
you can find your best fit model and
then off you toddle and do
some science and make some conclusions
about what's going on
in the universe number four data
visualization
now once you've done all this data
analysis and model fitting
you really need to be able to interpret
all that information
and one way of doing that is actually to
be able to see
your data and visualize more clearly
what's going on whether that's with a
simple histogram of the
spread of a certain measurement in your
data or the location of objects on the
sky
or even interactive graphics that you
can play around with
to see relationships more clearly in
your data
a few well-placed commands to your
computer in python and you can make
some of the most beautiful scientific
plots that will one day hopefully
get published in a scientific research
paper now the fifth thing in my list is
simulations this is when you code up
all the laws of physics into a program
and run that program to try and recreate
what happens
when planets form around stars or what
happens when
galaxies collide or maybe even what
happens when a star
gets too close to a black hole and gets
spaghettified
unsurprisingly that is incredibly
complex and it's something you have to
dedicate your entire
career to doing i didn't do that because
i wanted to use telescopes and look at
real images of galaxies in the universe
someone who did do that though is one of
my best friends from my phd dr ricardo
beckmann who is now a research fellow at
the university of cambridge
specializing in simulating how black
holes
grow i rang her up to chat about how she
uses code
in her work so how do you go about
coding up
something as complex as a black hole
so if you want to do a simulation of
pretty much anything like a black hole
or a galaxy or
any other phenomenon the key thing to
think about really is the
underlying physics so we very much have
to think back to sort of
the parts or the components of physics
that are relevant in this particular
context
uh and then we um think about how to
translate the physical laws
uh into some sort of computer algorithm
so for example
if i work with um black holes a lot and
i want to study say the evolution of gas
near a black hole
what i'll do is i'll have to think about
the gas physics so i have to think about
okay how does a gas behave
so when the pressure changes what
happens to it if you cool it or if
you're heated how does it you know move
and evolve over time so it will
code up all these different sort of
reactions on physical phenomena that go
on at the same time
and then have to think about the black
hole and go okay so what does the black
hole contribute to the situation and for
example with black holes
it's typically the gravity that's the
important part so i will code up the
laws of gravity
and then i can put all of this together
and really study how
gas evolves under the gravity of a black
hole and how the gas physics sort of
evolve
in this context what we do is we build
these very big codes that have all these
different
parts of physics in it so i don't do all
of this myself um a lot of these codes
are
the code i work with for example was
started about 20 years ago or so and
there's a whole community working on it
basically we've sort of collectively
built this incredible powerful
software machinery to do these
simulations that's so cool that you
leave your own little contribution
as well like in the history of the field
i guess even like students in 20 years
time or researchers in 20 years time
might be using like
a little bit that you added right
exactly so
you know if it goes well then and if
that's still a phenomenon they're
interested in at the time
and that's relevant for their physics
they will just use yeah my code the way
i added and sort of
so i do my own work but i also
contribute to the sort of future of the
field in a sense so what language do you
actually use to code up
like the laws of physics into this like
what's the best one
for this application so it depends
a little bit on which part of it so
really with doing simulation there's
actually more like two very
parallel things we have to do so the
first thing we have to actually do the
simulation
and then we have to take the data
produced by the simulation and sort of
analyze and visualize
and study it and we use two different
coding languages for the two different
purposes
um so if we're actually running the
simulations this is what we call high
performance computing so it's
computationally very expensive you need
very big supercomputers to do it
so the important thing here is to have a
language that's efficient so you know
any amount of
computational time energy we can save in
this is is good so we tend to
work with all the more formalized
languages in a sense so i personally use
fortran for example
um so these are ones that's maybe a
little more difficult to code but
because it's curved we write once very
carefully and then use many many times
these sort of efficiency gains we get
out of the language
are really important and really worth it
so that's sort of the actual simulation
code but then once we've got all this
data we need to go
and look at it analyze it study it you
know
um and for that i use python because the
point there is that we want to write
quick code we want to have you know be
able something that's intuitive i want
to be able to put something together and
you know follow through on an idea
really quickly and i know you've talked
about earlier about python a lot so i
think for the exact same reasons we
we use it for the analysis as well i
guess the big question though is
why do we actually run simulations like
why are simulations so important
in astrophysics the reason simulations
are so important is because
in astrophysics we have a few very
particular challenges in this sort of
particular science so one of them is
that it's awfully hard to do experiments
because you know there's only one
universe it's really big
the time scales over which things evolve
are incredibly long
so from observational point of view we
often just get the sort of individual
snapshots of objects
and the powerful thing about simulations
is that it makes these big time scales
and these big length scales very
tractable because we can essentially put
together
these simulations which you know a big
simulation would just look like a movie
of an object evolving over giga years of
time and you can watch it in five
minutes
so basically this gives us a chance to
sort of build these evolution histories
and then link that to the observations
to see whether what we get out looks
like what we
think it should look like like the
observations um and that allows us to
sort of test
whether the physics we put in originally
what i talked about earlier
really is all the relevant physics if it
looks like what we'd expected to
um then we think we've understood it if
it doesn't look like what we'd expected
to then we have to go back and sort of
you know
go back and check our understanding what
did we miss would be maybe misunderstand
where could the sort of you know
discrepancy come from so really they're
a way to test our understanding
but also to sort of you know get full
sort of long-term evolution histories of
things that
otherwise might be much more difficult
to study it's nice to know that everyone
can like come together and work together
to create something you know as like
complex is simulating the entire
universe because i guess it feels
overwhelming
who was the first who's the first person
that just like sat down and was like
right time to start a simulation of the
universe like that must have been
so like you know where do you start yeah
i think it was somebody's phd project
he's now a professor about the code i
work with he's now a professor in
uh in zurich so you know his entire
career has been
this code and he started it he published
the original paper that said here's a
new code
and then people have been building on it
ever since that's so cool so
thanks ricardo it was really great to
see you're very welcome
if you want to hear more from my chat
with ricardo i'm going to be posting the
full
unedited version on my channel on sunday
so keep an eye out for that
thank you ricardo for our nice little
catch up there i hope that helped you
realize
how astrophysicists like me and like
ricardo use code
day-to-day in our research work
and after that well you might be quite
inspired and you might want to
learn to code yourself and not really
know how to start
well i always find the best way is to
learn by doing which
brings us to this week's video sponsor
brilliant
brilliant is a problem-solving website
with lots of
interactive courses that break down
problems into easy to understand chunks
that are really fun too they have
courses on a huge range of science and
math topics but i wanted to highlight
their coding courses from
the fundamentals of computer science and
algorithms to
a great course teaching you my favorite
programming language
python it builds you up from the very
basics on how to even read
code and what it means to run code and
then how to deal with
variables and manipulate them to do what
you want to do
they also have a data visualization and
a game writing course coming soon which
i'm
very excited for perfect for all you
budding scientists and astrophysicists
out there so if that
sounds like something that you'd be up
for and you want to support me and my
channel
then head over to brilliant.org forward
slash dr becky that's d-r-b-e-c-k-y
and sign up for free plus the first 200
people that go
to that link will get 20 off an annual
premium
subscription it's pretty good in my book
so thank you so much to brilliant for
that
and now everybody's favorite bit
let's roll those bloopers
i just wanted to sit on the arm of the
chair
oh this is going so well you taught me a
secret language
that i can't speak with every other
computer
it was it was 600 000 galaxies that's
far
too big for any excel spreadsheet
xl spreadsheet oh god
space is hard words are harder