Ever looked at the night sky and wondered, "Hey, how far away is that star?" We wondered about it too. And we wanted an app that could calculate this distance in one click. So we made CosmΩracle!
In this post, youâll learn:
- What is CosmΩracle?
- How it all started
- How we worked together in a virtual setting
- Where to go from here?
Want to jump right in? Here's the app and the source code.
But first, letâs talk about...
What is CosmΩracle?
CosmΩracle is an app that calculates distances for different sets of cosmological parameters. Go to www.cosmoracle.com and click on Cosmological Distances. Youâll see a menu in the sidebar with fields for redshift, the Hubble constant, the energy content in the Universe from matter, radiation, dark energy, and different equation of state parameters.
Some sets of parameters are more popular than others (theyâre favored by different observational datasets), so we have pre-programmed some values for you. The only thing you need to get started is the value of the redshift. Want to know a certain distance of those galaxies at redshift 2? Simply plug that value into the redshift field, and CosmΩracle will get cracking!
Itâs important in science that everyone agrees on what the input parameters represent, and CosmΩracle is no exception. As you can see above, weâve included a Definitions page where you can see how each parameter is defined and how CosmΩracle uses these parameters to compute quantities that are used in cosmology.
How it all started
There are plenty of âsomethingsâ in the field of physics. If youâre working in cosmology and/or astronomy, at some point youâll need to use one of the many âdistances.â The need for many different types of distances is due to the Universe being very complicatedâspace expanding, objects moving, interacting, and mixing, things getting shifted and distorted. Depending on the problem youâre working on, youâll need a certain distance calculated.
On top of this, these distances will depend on the kind of Universe youâre working with and on how much matter it contains. It probably sounds weird at first, but give it some thought. The amount of matter affects the expansion rate of the Universe, so different quantities lead to different increases in distance.
Other things that affect distances are the curvature of the Universe and dark matter or dark energy. If you have all the necessary ingredients, itâs possible to calculate these distances. Most of the time, you just âwant the thing calculatedâ without going through the process of installing Python libraries and writing the code just to get that number.
Until recently, we calculated these distances with the first online version of the famous Ned Wrightâs Cosmo Calculator from 1999 or with the astropy cosmology package. Although these are great tools, we felt that there was a need for an improved and modern version.
The three of us met online and decided that our app needs to:
- be user-friendly (everyone should be able to use it without spending a lot of time on learning it)
- work across all devices: desktop, tablet, and mobile (a la âastronomy on the goâ)
- be fast and accessible
- be correct in terms of physics
- be designed in a modern way
- represent the results in a numerical and illustrative way (think plots)
- have an option to download the calculated data (to be used for further analysis)
- be designed in a way thatâs easy to maintain and upgrade
- be open-source
How to achieve it?
All three of us have a programming background. We work in physics and love exploring the latest advancements in the field of data visualization. Sure, we could âcode upâ functions to calculate all sorts of things. But we wanted a simple and neat way of putting apps online. Every option we found needed knowledge beyond our skillset or wasn't open-source.
In the spring of 2021, Nikoâs best friend Robert (also an astrophysicist) shared âa super cool new way of making apps online called Streamlitđ.â While transitioning from research into data science, Robert made an incredible app called âDistribution Analyserâ. It analyses all scipy.stats functions. You can read more about it in this Towards Data science article. When the time came to test the best way to combine Python code and make it into an app, Streamlit was one of our first choices.
How we worked together in a virtual setting
Since weâre all researchers at different institutions, it was hard for us to find the time to work together. We dedicated a few days during the holidays to exclusively work on CosmΩracle. After we decided on the functions to start with, we distributed the tasks, finalized the first Python code, and discussed how to deploy it.
CosmΩracle calculates the time that passed between a distant object emitting a photon and that same photon reaching Earth. Functions like this form the backbone of CosmΩracle but need to be deployed in order to be useful.
def get_lookback_time(z, H0=constants['Hubble0'], ΩM=constants['matter-density'],
ΩDE=constants['DE-density'], ΩR=constants['rad-density'],
w0=constants['w0'], wa=constants['wa']):
"""
Method to compute the lookback time in Gyrs
"""
integrand = lambda x: 1/(get_E_z(x, ΩM, ΩDE, ΩR, w0, wa)*(1+x))
if isinstance(z, float) or isinstance(z, int):
if z < 0:
raise ValueError("Enter a non-negative redshift.")
result, _ = integrate.quad(integrand, 0, z)
elif isinstance(z, np.ndarray):
if any(t < 0 for t in z):
raise ValueError("Enter a non-negative redshift.")
result = np.vectorize(lambda x: integrate.quad(integrand, 0, x)[0])(z)
else:
raise TypeError(f'Expected "Union[float, np.ndarray]", got {type(z)}')
c0 = constants['speed-of-light']
return result*hubble_time(H0)
That first day we worked until midnight. Too tired to keep going, we decided to talk later. But Niko stayed up and tried Streamlit. It worked! She immediately wrote to Matthijs and Marco, âWe need to meet. This is wonderful. This is like magic." So we met on Zoom and continued working into the morning. In a week, we had the first working version of CosmΩracle!
The unofficial fourth member of the gang: coffee. It supported every one of us during the small hours of the morning. Photo by Craig McKay on UnsplashWhat worked well is the combination of several important factors:
- We had a clear goal in mind
- We had a lot of enthusiasm
- We were having fun
Also, the crucial part was that our communication was wonderful. Weâre friends in real life. That makes everything easier.
Last but definitely not least, the Streamlit platform was essential. Itâs very intuitive, powerful, customizable, and very easy to use. As scientists, weâre used to computations but have no experience in deploying web apps. Without Streamlit, we wouldn't have been able to build the momentum to finish this project.
We also used:
- Slack for creating tasks/private discussions or sending messages.
- GitHub for version control, keeping track of issues that have to be dealt with, and for hosting a GitHub Page that links to the page hosted on Streamlit.
- Zoom for in-person discussions.
- Python for coding.
Where to go from here?
Thanks to Streamlit, the code implementation was very easy. We have organized the code into blocks dedicated to configuration, computation, or presentation. The result is that CosmΩracle is structured in a way that makes it easy to create extensions.
For example, here is the script thatâs at the center of the Streamlit app:
import streamlit as st
import page_introduction as pi
import page_distances as pd
import page_documentation as doc
st.sidebar.write(" ")
pages = {
"Introduction": pi,
"Cosmological Distances": pd,
"Definitions": doc,
}
st.sidebar.title("Main options")
# Radio buttons to select desired option
page = st.sidebar.radio("", tuple(pages.keys()))
pages[page].show_page()
The parts of CosmΩracle that do all the work (Introduction, Cosmological Distances, and Definitions) are stored in separate scripts. This makes CosmΩracle modular and extensible.
We plan to build a new feature that will let CosmΩracle compute the growth of matter density fluctuations in inflationary cosmology. After we finish the script, adding this new functionality to the app will be a simple matter of adding a line like import page_perturbations
to the code above. This will make the corresponding adjustments to the list of pages and will be available on the website.
We also plan to extend the existing features of CosmΩracle.
For example, the current pre-set parameters are based on the latest analysis of the data of the Planck satellite. Theyâre hardcoded into the app in page_distances.py
above. People from different parts of astronomy and cosmology will have different preferences for CosmΩracleâs parameters. Weâll be adding more sets (based on WMAP or the astronomical 70-30-70 cosmology).
Here is a sneak peek for you:
We want to give CosmΩracle more functionality for quantities that are directly calculated from the cosmological distance measures. Currently, it can calculate angular sizes and convert between these angular sizes and physical sizes. But it can only give you an indication of the physical size of an object for a fixed angular extension of 1ââ (1 arcsecond or 1/3600th of a degree). We plan to add an option for you to set the angular size yourself. So if you want to know the real size of those 0.3ââ galaxy lobes, stay tuned!
Wrapping up
With the increasing amount of knowledge and data, we need to develop ways of communicating findings and creating better and more accessible ways to disseminate knowledge. Accessibility and open-source are key to achieving it.
Weâre very impressed with how easy it is to use Streamlit. And itâs open-source! For us, to have the code open to the public is extremely valuable because the community can inspect our work, point out our mistakes, and give suggestions for improvements.
We believe that science belongs to everyone. If our app helps the astronomy community with research and teaching, then weâve achieved a great deal. And all of this was possible because Streamlit is so easy to use.
We hope you enjoyed our story. If you have any questions, please leave them in the comments below or reach out to Niko at nikolina.sarcevic@gmail.com, Matthijs at primarius@gmail.com, and Marco at bonici.marco@gmail.com.
Happy Streamlit-ing! đ
This is a companion discussion topic for the original entry at https://blog.streamlit.io/calculating-distances-in-cosmology-with-streamlit/