From faddf893236f88ed5681ef3576207c58e67c9ae3 Mon Sep 17 00:00:00 2001 From: kponder Date: Fri, 22 Jul 2016 14:24:12 +0100 Subject: [PATCH] ipython notebook to generate mock data from 3 simulations --- Generate_Mock_Lightcurves.ipynb | 366 ++++++++++++++++++++++++++++++++ 1 file changed, 366 insertions(+) create mode 100644 Generate_Mock_Lightcurves.ipynb diff --git a/Generate_Mock_Lightcurves.ipynb b/Generate_Mock_Lightcurves.ipynb new file mode 100644 index 0000000..a249c81 --- /dev/null +++ b/Generate_Mock_Lightcurves.ipynb @@ -0,0 +1,366 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Neural networks not available in this version of scikit-learn. Neural networks are available from development version 0.18.\n" + ] + } + ], + "source": [ + "%load_ext autoreload\n", + "%autoreload #Use this to reload modules if they are changed on disk while the notebook is running\n", + "from __future__ import division\n", + "from snmachine import sndata, snfeatures, snclassifier, tsne_plot\n", + "import numpy as np\n", + "import matplotlib.pyplot as plt\n", + "import time, os, pywt,subprocess\n", + "from sklearn.decomposition import PCA\n", + "from astropy.table import Table,join,vstack\n", + "from astropy.io import fits\n", + "import sklearn.metrics \n", + "import sncosmo\n", + "%matplotlib nbagg\n", + "from astropy.table import Column" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## SN numbers with classification code\n", + "SN001695 - 33\n", + "\n", + "2457 - 33\n", + "\n", + "2542 - 1\n", + "\n", + "5399 - 2\n", + "\n", + "13481 - 2\n", + "\n", + "13866 - 1 \n", + "\n", + "16742 - 21\n", + "\n", + "17270 - 32\n", + "\n", + "27266 -32\n", + "\n", + "64968 - 23\n", + "\n", + "149147 -21" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "dataset='spcc'" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# WARNING...\n", + "#Multinest uses a hardcoded character limit for the output file names. I believe it's a limit of 100 characters\n", + "#so avoid making this file path to lengthy if using nested sampling or multinest output file names will be truncated\n", + "\n", + "#Change outdir to somewhere on your computer if you like\n", + "outdir=os.path.join('output_%s_no_z' %dataset,'')\n", + "out_features=os.path.join(outdir,'features') #Where we save the extracted features to\n", + "out_class=os.path.join(outdir,'classifications') #Where we save the classification probabilities and ROC curves\n", + "out_int=os.path.join(outdir,'int') #Any intermediate files (such as multinest chains or GP fits)\n", + "\n", + "subprocess.call(['mkdir',outdir])\n", + "subprocess.call(['mkdir',out_features])\n", + "subprocess.call(['mkdir',out_class])\n", + "subprocess.call(['mkdir',out_int])" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "#Data root\n", + "rt=os.path.join('SPCC_SUBSET','')" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Reading data...\n", + "11 objects read into memory.\n" + ] + } + ], + "source": [ + "dat=sndata.Dataset(rt)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "read_from_file=True #We can use this flag to quickly rerun from saved features\n", + "run_name=os.path.join(out_features,'%s_all' %dataset)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'DES_SN001695.DAT'" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "dat.object_names[0]" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": { + "collapsed": false, + "scrolled": false + }, + "outputs": [], + "source": [ + "test_table = dat.data['DES_SN001695.DAT'] #get_lightcurve" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "type_II = []\n", + "for i in range(len(dat.data['DES_SN001695.DAT']['flux'])):\n", + " type_II.append(np.random.normal(dat.data['DES_SN001695.DAT']['flux'][i], dat.data['DES_SN001695.DAT']['flux_error'][i], 1000))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "type_Ia = []\n", + "for i in range(len(dat.data['DES_SN002542.DAT']['flux'])):\n", + " type_Ia.append(np.random.normal(dat.data['DES_SN002542.DAT']['flux'][i], dat.data['DES_SN002542.DAT']['flux_error'][i], 1000))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "type_Ibc = []\n", + "for i in range(len(dat.data['DES_SN005399.DAT']['flux'])):\n", + " type_Ia.append(np.random.normal(dat.data['DES_SN005399.DAT']['flux'][i], dat.data['DES_SN005399.DAT']['flux_error'][i], 1000))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "type_Ia = np.array(type_Ia)\n", + "type_Ibc = np.array(type_Ibc)\n", + "type_II = np.array(type_II)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "test_table.replace_column('flux', type_II[:,0])" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": { + "collapsed": false + }, + "outputs": [], + "source": [ + "#test_table.write('testing.dat',format = 'ascii')" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "collapsed": false + }, + "outputs": [ + { + "data": { + "text/html": [ + "<Table length=128>\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
mjdfilterfluxflux_errorzpzpsys
float64str4float64float64float64str2
0.0desg3.8744.78727.5ab
0.0160000000033desr-5.4565.33527.5ab
0.0310000000027desi-10.35.43927.5ab
0.0619999999981desz1.4394.69127.5ab
2.0desg-0.72972.23827.5ab
2.14desr-2.8373.0327.5ab
2.156desi-4.2364.227.5ab
2.187desz-5.0863.51827.5ab
9.984desg0.034041.70227.5ab
10.0desr0.82361.25127.5ab
..................
135.918desi4.0083.08127.5ab
139.894desz8.0954.25227.5ab
143.863desg4.4422.91427.5ab
143.871desr0.77181.36527.5ab
143.894desi3.9763.21827.5ab
148.867desz5.9822.60927.5ab
155.871desg-0.1091.81627.5ab
155.879desr0.39821.38327.5ab
156.871desi1.1492.17727.5ab
157.871desz16.463.39427.5ab
" + ], + "text/plain": [ + "\n", + " mjd filter flux flux_error zp zpsys\n", + " float64 str4 float64 float64 float64 str2\n", + "--------------- ------ ------- ---------- ------- -----\n", + " 0.0 desg 3.874 4.787 27.5 ab\n", + "0.0160000000033 desr -5.456 5.335 27.5 ab\n", + "0.0310000000027 desi -10.3 5.439 27.5 ab\n", + "0.0619999999981 desz 1.439 4.691 27.5 ab\n", + " 2.0 desg -0.7297 2.238 27.5 ab\n", + " 2.14 desr -2.837 3.03 27.5 ab\n", + " 2.156 desi -4.236 4.2 27.5 ab\n", + " 2.187 desz -5.086 3.518 27.5 ab\n", + " 9.984 desg 0.03404 1.702 27.5 ab\n", + " 10.0 desr 0.8236 1.251 27.5 ab\n", + " ... ... ... ... ... ...\n", + " 135.918 desi 4.008 3.081 27.5 ab\n", + " 139.894 desz 8.095 4.252 27.5 ab\n", + " 143.863 desg 4.442 2.914 27.5 ab\n", + " 143.871 desr 0.7718 1.365 27.5 ab\n", + " 143.894 desi 3.976 3.218 27.5 ab\n", + " 148.867 desz 5.982 2.609 27.5 ab\n", + " 155.871 desg -0.109 1.816 27.5 ab\n", + " 155.879 desr 0.3982 1.383 27.5 ab\n", + " 156.871 desi 1.149 2.177 27.5 ab\n", + " 157.871 desz 16.46 3.394 27.5 ab" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#dat.data['DES_SN001695.DAT']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.12" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}