/NeutronStars

Primary LanguageJupyter Notebook

Neutron Stars

Set Up

git clone https://github.com/jordanott/NeutronStars.git

# You'll need to download the onnx file from here: 
# https://drive.google.com/file/d/1vcOBmjZ4knSJUayr4zb6uoC5Z2HLnBwK/view?usp=sharing
mv ~/Downloads/model.onnx NeutronStars/SavedModels/mr+star2spectra/00069/

# For the updated model trained on new data use
# https://drive.google.com/file/d/11nfK3m45k3SqyVZ1HRiU4Pg0WllB487S/view?usp=sharing
mkdir -p NeutronStars/SavedModels/mr+star2spectra/00069_retrained/
mv ~/Downloads/model_retrained.onnx NeutronStars/SavedModels/mr+star2spectra/00069_retrained/model.onnx

# Create a python virtual environment (Optional)
python3 -m venv neutron_stars_env
alias ns_env=neutron_stars_env/bin/python

# Upgrade pip (needed for some packages)
ns_env -m pip install -U pip

# Install the necessary requirements to your virtual environment
ns_env -m pip install -r requirements.txt

# Installs the neutron stars pip package I've created
ns_env -m pip install -Ue .

1) Make predictions on a large batch of files

Preprocess the data

# Extract data from gz file
# I'll use directories on our machines as an example (replace with your own)
# The result of this step must be a directory with .dat or .dat.gz files
tar -xvzf /baldig/physicstest/NeutronStarsData/res.tgz

# Preprocesses data into necessary format to be read
ns_env neutron_stars/data_loader/data_parser.py --data_dir /baldig/physicstest/NeutronStarsData/res/

# The result of preprocessing should be new files with the extension .npz 
$ ls /baldig/physicstest/NeutronStarsData/res | head
skip0-num10-spectra_2Param_MR.dat.gz
skip0-num10-spectra_2Param_MR.npz
skip0-num10-spectra_4Param_MR.dat.gz
skip0-num10-spectra_4Param_MR.npz

Using the Model

ns_env test_scripts/mr_np_to_spectra.py \
--data_dir /baldig/physicstest/NeutronStarsData/res_nonoise10x/ \
--load_settings_from SavedModels/mr+star2spectra/00011/00011.json

Parsing the Results

The inputs, targets, and network predictions are saved in a csv file. You can examine them like this:

import pandas as pd
df = pd.read_csv('Predictions/all_00011_01.csv', index_col=0)

input_cols = ['Mass', 'Radius', 'nH', 'logTeff', 'dist']
true_cols = list(map(str, range(250)))
pred_cols = ['pred_' + col for col in true_cols]

inputs = df[input_cols]
targets = df[true_cols]
predictions = df[pred_cols]

To make plots like the ones I emailed:

import numpy as np
import matplotlib.pyplot as plt

idxs = np.random.choice(np.arange(len(df)), 50)
true_spectra = df[true_cols].values
pred_spectra = df[pred_cols].values

plt.subplot(1,2,1); plt.title('True Spectra')
plt.plot(true_spectra[idxs].T)

plt.subplot(1,2,2); plt.title('Predicted Spectra')
plt.plot(np.maximum(pred_spectra[idxs].T, 0))

2) Make predictions given mass, radius, nH, logTeff, dist

import neutron_stars as ns
import matplotlib.pyplot as plt

spectra_generator = ns.SpectraGenerator()
spectra = spectra_generator(mass=2.581471, radius=12.089365,
                            nH=0.013734, logTeff=6.273879, dist=6.011103)

plt.plot(spectra.T)
plt.savefig('example_generated_spectra.png')

This is a reference for myself, others can ignore it

tf2 -m tf2onnx.convert --saved-model tensorflow-model-path --output model.onnx

Citations

@article{farrell2023deducing,
  title={Deducing neutron star equation of state parameters directly from telescope spectra with uncertainty-aware machine learning},
  author={Farrell, Delaney and Baldi, Pierre and Ott, Jordan and Ghosh, Aishik and Steiner, Andrew W and Kavitkar, Atharva and Lindblom, Lee and Whiteson, Daniel and Weber, Fridolin},
  journal={Journal of Cosmology and Astroparticle Physics},
  volume={2023},
  number={02},
  pages={016},
  year={2023},
  publisher={IOP Publishing}
}

@article{farrell2023deducing,
  title={Deducing Neutron Star Equation of State from Telescope Spectra with Machine-learning-derived Likelihoods},
  author={Farrell, Delaney and Baldi, Pierre and Ott, Jordan and Ghosh, Aishik and Steiner, Andrew W and Kavitkar, Atharva and Lindblom, Lee and Whiteson, Daniel and Weber, Fridolin},
  journal={arXiv preprint arXiv:2305.07442},
  year={2023}
}