Package: nnaps
Neural Network assisted Population Synthesis codeNNaPS aims to be a simple and easy to use codebase for building a population synthesis code from a set of 1D
stellar/binary evolution models. For example, with NNaPS, a set of MESA models can be turned into a population
synthesis code.NNaPS can extract interesting parameters from a grid of MESA models and even apply a CE ejection for binary models.Based on the extracted grid, NNaPS trains a neural network to act as an interpolator in the provided models, and can
then be used to predict new models, as long as their starting parameters are in the same range as those used to train
the network.Lets look at an example:The orbital period and mass ratio distribution of wide hot subdwarf binaries shows a very strong correlation. We want
to study this correlation and predict the P-q distribution of wide sdB binaries in the galaxy. Using MESA, 2000 models
covering the observed parameter range were calculated (blue circles in the left figure). Using NNaPS, these models
were used to train a model and predict the P-q distribution of ~1,000,000 binaries (right figure). With this new
BPS model we can now further explore the P-q distribution of different sub populations in our galaxy.If you want to try out a BPS model that was created using NNaPS you can try MESA-bps. It allows you to predict the
orbital properties of a low mass (< 2.0 Msol) binary after its interaction phase on the red giant branch.MESA-bps:https://mesa-bps.herokuapp.com/DocumentationThe detailed documentation is a work in progress, you can read the current version atnnaps.readthedocs.ioInstalation instructionsThe simplest way to install the latest stable release of NNaPS is using pip.pip install nnapsTo uninstall NNaPS, run:pip uninstall nnapsAcknowledgementsIf you use NNaPS in a publication, please cite:@misc{nnaps,
  author       ={{Vos}, Joris and{Bobrick}, Alexey and{Vu{\v{c}}kovi{\'c}}, Maja},
  title        ={Neural Network assisted Population Synthesis},
  month        = aug,
  year         = 2020,
  publisher    ={Zenodo},
  version      ={0.0.1},
  doi          ={10.5281/zenodo.3988965},
  url          ={https://doi.org/10.5281/zenodo.3988965}}Compress and Extract a grid of MESA modelsIf you have a calculate a grid of MESA models the first step is to compress the output and keep only what is of
interest in your study. This can decrease the necessary storage space by a factor of 10 or more, and will make dealing
with a large MESA grid on a laptop much more manageable.Compressing a grid of MESA runs to hdf5 format can be done with the nnaps-mesa command line tool:nnaps-mesa -2h5 <input folder> -o <output folder>Once a MESA grid is compressed, all interesting parameters can be extracted from the models and stored in a 2D csv
table:nnaps-mesa -extract <input folder> -o <output csv filename>The produced csv file can be used to create a predictive model. See the documentation for details on the nnaps-mesa
tool.If you would be looking for a simple tool to inspect a large grid of MESA models and their extracted parameters from
your browser, have a look at:Track ExplorerSimple predictorNNaPS requires a setup file or setup dictionary telling it what to do. The minimaly necessary setup includes a list
of features and targets together with the path to the training data. Using the test data sample, the simplest model
setup file in yaml format is the following:datafile:'<pathtodatafile>'features:-...regressors:-...classifiers:-...datafile: path to the file containing the training data.  This file is read with the pandas.read_csv() function,
and should be structured in an appropriate way.features: a list of the features to use when predicting a model. Think of these as the X parameter of your
prediction function.regressors: a list of the numerical targets for the model. These are the Y parameters of the prediction function.
A regressor has to he a continuous numerical value. For a categorical numerical value use a classifier.classifiers: a list of the categorical targets for the model. These are the Y parameters that are not numerical,
or the not continuous.The entire process of fitting the data and predicting the targets of new data can be as simple as:fromnnapsimportpredictorspredictor=predictors.FCPredictor(setup_file='setup.yaml')predictor.fit(epochs=100)new_predictions=predictor.predict(new_data)Let's go over all steps in a bit more detail:The dataLets check the data. In this example we will use the test data provided in the test directory of the NNaPS package.importpandasaspddata=pd.read_csv('tests/BesanconGalactic_summary.txt')print(data.head())M1PinitqinitFeHinitPfinalqfinalproductbinary_type00.744134.4700051.095729-0.912521294.0315880.608444He-WDsingle-lined10.813225.0000142.524845-0.806781153.6340071.031585He-WDsingle-lined20.876111.5500092.190000-0.918768104.9705870.912802He-WDsingle-lined30.890512.7000452.386059-0.878982394.7294241.396449HBsingle-lined40.893102.6300071.485857-0.731017228.6130650.640067He-WDdouble-linedThe dataframe contains the features, which are all numerical continuous variables: M1, Pinit,qinit and FeHinit.
And the 4 targets: Pfinal, qfinal, product and binary_type. The first two targets are also continuous numerical
variables and are thus regressors. The last two targets are categorical variables, and are thus classifiers.Based on this knownledge, we can make te following setup file:datafile:'tests/BesanconGalactic_summary.txt'features:-M1-qinit-Pinit-FeHinitregressors:-Pfinal-qfinalclassifiers:-product-binary_typeSetting up the model and trainingUsing the setup file a predictor can be constructed and fitted:fromnnapsimportpredictorspredictor=predictors.FCPredictor(setup_file='test_setup.yaml')predictor.fit(epochs=100)Train on 1648 samples, validate on 412 samples
Epoch 1/100
 - 2s - loss: 696880.9575 - Pfinal_loss: 695673.0000 - qfinal_loss: 0.2186 - product_loss: 1.8115 - binary_type_loss: 0.6274 - Pfinal_accuracy: 0.0000e+00 - Pfinal_mae: 607.1440 - qfinal_accuracy: 0.0000e+00 - qfinal_mae: 0.3886 - product_accuracy: 0.1475 - product_mae: 0.2783 - binary_type_accuracy: 0.7737 - binary_type_mae: 0.4420 - val_loss: 808298.6068 - val_Pfinal_loss: 767784.4375 - val_qfinal_loss: 0.1991 - val_product_loss: 1.8002 - val_binary_type_loss: 0.6387 - val_Pfinal_accuracy: 0.0000e+00 - val_Pfinal_mae: 603.3589 - val_qfinal_accuracy: 0.0000e+00 - val_qfinal_mae: 0.3821 - val_product_accuracy: 0.1723 - val_product_mae: 0.2781 - val_binary_type_accuracy: 0.7621 - val_binary_type_mae: 0.4410
Epoch 2/100
 - 0s - loss: 696340.1365 - Pfinal_loss: 696977.0625 - qfinal_loss: 0.2867 - product_loss: 1.8291 - binary_type_loss: 0.5913 - Pfinal_accuracy: 0.0000e+00 - Pfinal_mae: 606.6785 - qfinal_accuracy: 0.0000e+00 - qfinal_mae: 0.4482 - product_accuracy: 0.1056 - product_mae: 0.2791 - binary_type_accuracy: 0.7779 - binary_type_mae: 0.4019 - val_loss: 807560.4800 - val_Pfinal_loss: 767050.8750 - val_qfinal_loss: 0.2407 - val_product_loss: 1.8078 - val_binary_type_loss: 0.6247 - val_Pfinal_accuracy: 0.0000e+00 - val_Pfinal_mae: 602.7472 - val_qfinal_accuracy: 0.0000e+00 - val_qfinal_mae: 0.4202 - val_product_accuracy: 0.0267 - val_product_mae: 0.2784 - val_binary_type_accuracy: 0.7621 - val_binary_type_mae: 0.3860
 ...
 Epoch 100/100
 - 0s - loss: 97412.9127 - Pfinal_loss: 96999.7812 - qfinal_loss: 0.0905 - product_loss: 1.1475 - binary_type_loss: 0.4588 - Pfinal_accuracy: 0.0000e+00 - Pfinal_mae: 161.2016 - qfinal_accuracy: 0.0000e+00 - qfinal_mae: 0.2201 - product_accuracy: 0.6383 - product_mae: 0.1970 - binary_type_accuracy: 0.8149 - binary_type_mae: 0.3309 - val_loss: 223666.4350 - val_Pfinal_loss: 184591.3125 - val_qfinal_loss: 0.0667 - val_product_loss: 1.1004 - val_binary_type_loss: 0.4510 - val_Pfinal_accuracy: 0.0000e+00 - val_Pfinal_mae: 155.4490 - val_qfinal_accuracy: 0.0000e+00 - val_qfinal_mae: 0.2007 - val_product_accuracy: 0.6893 - val_product_mae: 0.2050 - val_binary_type_accuracy: 0.8350 - val_binary_type_mae: 0.3043This creates the predictor using a the 'test_setup.yaml' file, and then trains it on the provided data. The number of
epochs is the number of iterations to be used in the gradient descent learning. After learning you can check the report
if the model is good enough, or if more iterations are necessary.Checking the learning progressAfter training the predictor, you can check how well it managed to fit the training data. Before it started fitting,
the predictor set part of the training data separate to test how well it's predictions are. This is the validation
set. By checking how well the predictor works for this data, you can get an idea of the expected error on new data.You can save the training report to html file as follows:predictor.make_training_history_report('report.html')Making predictionsWhen you are satisfied that your model is good enough, you can make predictions on new data.Predicting new models is then as simple as providing a pandas DataFrame with the features (X parameters) of the new
models you want to predict to the BPS_predictor. We can have the system predict the targets for the 100 - 110th
training sample.new_predictions=predictor.predict(data=data.iloc[100:110])print(new_predictions)Pfinalqfinalproductbinary_type01448.759390.613978HBsingle-lined1771.1500240.432204He-WDsingle-lined2254.8109440.295612He-WDsingle-lined3455.5645450.348135He-WDsingle-lined4583.5797120.381909He-WDsingle-lined5150.1473850.267372He-WDsingle-lined6565.8267820.378209He-WDsingle-lined71213.600220.551243HBsingle-lined82370.943840.859896HBsingle-lined9337.3489070.316895He-WDsingle-linednew_predictions is a pandas dataframe with the predictions and features of the provided.The trained model can be saved to hdf5 format and loaded again for later use:predictor.save_model('model.h5')Loading a saved models can be done in two ways:# load into an existing predictor objectpredictor.load_model('model.h5')# or create a new predictor object from the saved modelpredictors.BPS_predictor(saved_model='model.h5')Advanced predictorIt is possible to define many more setting in the setup file. A complete setupfile would look like:datafile:'tests/BesanconGalactic_summary.txt'features:M1:processor:StandardScalerqinit:processor:StandardScalerPinit:processor:StandardScalerFeHinit:processor:StandardScalerregressors:Pfinal:processor:Noneloss:mean_absolute_percentage_errorqfinal:processor:Noneloss:mean_squared_errorclassifiers:product:processor:OneHotEncoderloss:categorical_crossentropybinary_type:processor:OneHotEncoderloss:categorical_crossentropyrandom_state:42train_test_split:0.2model:-{'layer':'Dense','args':[100],'kwargs':{'activation':'relu','name':'FC_1'}}-{'layer':'Dropout','args':[0.1],'kwargs':{'name':'DO_1'}}-{'layer':'Dense','args':[75],'kwargs':{'activation':'relu','name':'FC_2'}}-{'layer':'Dropout','args':[0.1],'kwargs':{'name':'DO_2'}}-{'layer':'Dense','args':[50],'kwargs':{'activation':'relu','name':'FC_3'}}-{'layer':'Dropout','args':[0.1],'kwargs':{'name':'DO_3'}}optimizer:'adam'optimizer_kwargs:{'learning_rate':0.001,'beta_1':0.9,'beta_2':0.999}batch_size:128