Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
amanmdesai committed Jun 21, 2023
1 parent 08d1afb commit 01fced8
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ jobs:
name: artifact
path: dist

- uses: pypa/gh-action-pypi-publish@v1.6.4
- uses: pypa/gh-action-pypi-publish@v1.8.5
with:
password: ${{ secrets.pypi_password }}
66 changes: 63 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![publish](https://github.com/amanmdesai/pygrc/actions/workflows/publish.yml/badge.svg)](https://github.com/amanmdesai/pygrc/actions/workflows/publish.yml)
[![test](https://github.com/amanmdesai/pygrc/actions/workflows/test.yaml/badge.svg)](https://github.com/amanmdesai/pygrc/actions/workflows/test.yaml)
[![PyPI Package latest release](https://img.shields.io/pypi/v/pygrc.svg)](https://pypi.python.org/pypi/pygrc)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.7950550.svg)](https://doi.org/10.5281/zenodo.7950550)

## Author

Expand All @@ -14,6 +15,67 @@ Aman Desai
A package to read and fit SPARC data for Galactic Rotation Curves


Galaxy rotation curves are a class of plots which have velocity of the galactic objects on the y-axis and the x-axis represents their radial distances from the galactic center.


## Analysis Steps

We present the pygrc package to simpify testing new models against data as well as to make galaxy rotation curve analysis accessible to undergraduates. At present, this package is designed to read the SPARC Data (in particular the data stored under newton models). We have used pandas for reading data, iminuit for fitting the data and matplotlib for plotting. The definition of model can be implemented by using functions defined in python language and by sharing these function via calls to pygrc package. The fitting procedure uses the least square approach as implemented in the iminuit package.

An example workflow to carry out analysis with pygrc is as follows:

- Read data from the SPARC Dataset - which gives among other things, the observed rotation velocity and the distance of the object from the center of the galaxy.

```python
df = gr.Reader.read(filepath=<path to the file in double quotation>)
```
- (optional) Make some initial plots for visualization and understanding the SPARC data for the given galaxy.

```python
gr.Plot().overlap(df,"Rad",["Vobs","Vgas","Vbul","Vdisk"],"observed velocity")
```

- User needs to define mass distribution along with useful parameters (for example to define the mass density given in \ref{eq}, use:

```python
def mass(r, M0, R0):
return M0*(1- (1+(r/R0))*np.exp(-r/R0))
```

- User then defines a model whose compatibility with data is to be checked in the following way:

```python
def newton(r, M0, rc, R0,beta):
G = 4.30e-6
m = mass(r,M0, R0)
f = G*m/r
return np.sqrt(f)*10e-3
```

- Fit the model with Data. First define the radius variable and its range. Then define the


```python
r = np.linspace(1e-5,df["Rad"].max(),2000)
```

Then define the variables to fit and the initial values to be used as follows:

```python
m_1=gr.Fit(df["Rad"],df["Vobs"],1.,1.,3,.35,1.)
```

where initial parameters values are in the same order as defined in the function *newton*. In the next step we define the range to scan for the parameters as well as define the error steps and finally define which parameters needs to be scanned and which are taken as fixed.

```python
m_1.fit_lsq(f, [(1e4,None),(1e-1,None),(1,10),(0.1,2),(0.1,2)],df['errV'],\\
[False,False,True,True,True])
```

\end{enumerate}



## Installation

```bash
Expand Down Expand Up @@ -42,8 +104,6 @@ df
- 3. Some sample function
```python

mond and mass function are based on Galaxies 2018, 6(3), 70; https://doi.org/10.3390/galaxies6030070

def mass(r, M0, R0):
return M0*(1- (1+(r/R0))*np.exp(-r/R0))

Expand Down Expand Up @@ -103,7 +163,7 @@ plt.savefig('1.pdf')
```


References:
## References:

- SPARC Data can be obtained from here: http://astroweb.cwru.edu/SPARC/
- Mond and Mass function are based on Galaxies 2018, 6(3), 70; https://doi.org/10.3390/galaxies6030070
175 changes: 175 additions & 0 deletions notebooks/detail-example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "e375f8cb",
"metadata": {},
"outputs": [],
"source": [
"import pygrc as gr\n",
"import math\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8332c16c",
"metadata": {},
"outputs": [],
"source": [
"galaxy = [\n",
"\"NGC4217\",\n",
"\"NGC5055\",\n",
"\"NGC5585\",\n",
"\"NGC6015\",\n",
"\"NGC6503\",\n",
"\"NGC7331\",\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a81f659b",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"df_all = []\n",
"for g in galaxy:\n",
" df = gr.Reader.read(filepath=\"../notebooks/data/\"+g+\"_rotmod.dat\")\n",
" df_all.append(df)\n",
" gr.Plot().overlap(df,\"Rad\",[\"Vobs\",\"Vgas\",\"Vbul\",\"Vdisk\"],\"Distance (kpc)\",\"Velocity\",g)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "149622af",
"metadata": {},
"outputs": [],
"source": [
"r = np.linspace(0.01,50,2000)\n",
"def mass(r, M0, R0):\n",
"#def mass(r, M0, rc, R0,beta):\n",
" #return M0*(np.sqrt(R0/rc)*(r/(r+rc)))**(3*beta)\n",
" return M0*(1- (1+(r/R0))*np.exp(-r/R0))\n",
"\n",
"def mond(r, M0, rc, R0,b, beta):\n",
" a = 1.2e-10\n",
" G = 4.300e-6 #parsecs \n",
" m = mass(r,M0, R0)\n",
" f = (G*m/r)*(1 + b*(1+(r/rc)))#*10e-3\n",
" return np.sqrt(f)*10e-3\n",
"\n",
"def newton(r, M0, rc, R0,beta):\n",
" a = 1.2e-10\n",
" G = 4.30e-6 #parsecs \n",
" m = mass(r,M0, R0)\n",
" f = G*m/r\n",
" #f = (G*m/r)*(1/np.sqrt(2)) * np.sqrt(1 + np.sqrt(1 + (r**4) * (2*a/(G*m))**2))\n",
" return np.sqrt(f)*10e-3"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9ad18e72",
"metadata": {},
"outputs": [],
"source": [
"for df in df_all:\n",
" print(df['Rad'].max())\n",
" print(df['Rad'].min())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d49750d3",
"metadata": {},
"outputs": [],
"source": [
"m_mond=[]\n",
"m_newton=[]\n",
"\n",
"for df in df_all:\n",
" m_mond.append(gr.Fit(df['Rad'],df['Vobs'],1.,1.,3,.35,1.))\n",
" m_newton.append(gr.Fit(df['Rad'],df['Vobs'],1.,1.,3,1.))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7e2450de",
"metadata": {},
"outputs": [],
"source": [
"fit_mond = [0]*len(m_mond)\n",
"fit_newton = [0]*len(m_newton)\n",
"for i in range(len(m_mond)):\n",
" fit_mond[i] = m_mond[i].fit_lsq(mond, [(1.,1e14),(1.,10.),(2.,5.),(0.1,2),(0.1,2)],df_all[i]['errV'],[False,False,True,True,True])\n",
" fit_newton[i] = m_newton[i].fit_lsq(newton, [(1.,1e14),(1.,10.),(1,10),(0.1,2)],df_all[i]['errV'],[False,True,True,True])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f770703a",
"metadata": {},
"outputs": [],
"source": [
"for i in range(len(m_mond)):\n",
" fig, ax =plt.subplots()\n",
" gr.Plot().plot_grc(df_all[i],fit_mond[i],mond,'MOND',galaxy[i],ax)\n",
" gr.Plot().plot_grc(df_all[i],fit_newton[i],newton,'Newton',galaxy[i],ax)\n",
" plt.savefig(galaxy[i]+'_rad_vars_fit.pdf')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b4339cbf",
"metadata": {},
"outputs": [],
"source": [
"fit_mond[1].draw_mnprofile('M0', band=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4aa72f7",
"metadata": {},
"outputs": [],
"source": [
"fit_mond[4].draw_mncontour('rc', 'M0',cl=[.67,.9,.95])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="pygrc",
version="0.2.0",
version="0.3.0",
description= "A package to read SPARC data for Galactic Rotation Curves",
author="Aman Desai",
author_email="[email protected]",
Expand Down

0 comments on commit 01fced8

Please sign in to comment.