Skip to content

Instantly share code, notes, and snippets.

@rbiswas4
Created September 10, 2019 18:29
Show Gist options
  • Save rbiswas4/44b4a1b1ded6853d9accae029999a6da to your computer and use it in GitHub Desktop.
Save rbiswas4/44b4a1b1ded6853d9accae029999a6da to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import os",
"execution_count": 1,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "survey_file = os.path.join('/Users/rbiswas/data/ztf/ObsTables/', 'test_schedule_v6.db')",
"execution_count": 2,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sfd98_dir = os.path.join('/Users/rbiswas/data/MWDUST')\n\n# This does not give the program access but is the dir which contains \n!ls /Users/rbiswas/data/MWDUST/",
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": "Fitzpatrick99.README SFD_dust_4096_ngp.fits SFD_mask_4096_sgp.fits\r\nFitzpatrick99_xt_3.1.txt SFD_dust_4096_sgp.fits SFD_temp_ngp.fits\r\nFitzpatrick99_xt_3.2.txt SFD_i100_4096_ngp.fits SFD_temp_sgp.fits\r\nFitzpatrick99_xt_3.3.txt SFD_i100_4096_sgp.fits SFD_xmap_ngp.fits\r\nNOTES SFD_mask_4096_ngp.fits SFD_xmap_sgp.fits\r\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import numpy as np\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nimport simsurvey\nimport sncosmo\nfrom astropy.cosmology import Planck15\n\nimport simsurvey_tools as sst ",
"execution_count": 4,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Load the ZTF CCD corners and filters\nccds = sst.load_ztf_ccds()\nsst.load_ztf_filters()",
"execution_count": 5,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "# Load simulated survey from file (download from ftp://ftp.astro.caltech.edu/users/ebellm/one_year_sim_incomplete.db)\n# Currently DES filters are used as proxies for ZTF filters\nplan = simsurvey.SurveyPlan(load_opsim=survey_file, band_dict={'g': 'ztfg', 'r': 'ztfr', 'i': 'desi'}, ccds=ccds)\n\nmjd_range = (plan.cadence['time'].min() - 30, plan.cadence['time'].max() + 30)",
"execution_count": 6,
"outputs": []
},
{
"metadata": {},
"cell_type": "markdown",
"source": "RB: Can't we use ztfi now?"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mjd_range",
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 7,
"data": {
"text/plain": "(57403.16732874228, 57736.41649200727)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "plan.pointings.to_pandas().head()",
"execution_count": 8,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 8,
"data": {
"text/plain": " time band zp skynoise RA Dec field ccd comment\n0 57433.167329 ztfr 30 1235.458255 97.27032 -24.25 258 NaN all_sky\n1 57433.167792 ztfr 30 1032.415561 93.69218 -17.05 307 NaN all_sky\n2 57433.168255 ztfr 30 831.160655 96.84022 -9.85 358 NaN all_sky\n3 57433.168749 ztfr 30 768.014326 89.36968 4.55 459 NaN all_sky\n4 57433.169220 ztfr 30 754.615141 85.58463 11.75 511 NaN all_sky",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>time</th>\n <th>band</th>\n <th>zp</th>\n <th>skynoise</th>\n <th>RA</th>\n <th>Dec</th>\n <th>field</th>\n <th>ccd</th>\n <th>comment</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>57433.167329</td>\n <td>ztfr</td>\n <td>30</td>\n <td>1235.458255</td>\n <td>97.27032</td>\n <td>-24.25</td>\n <td>258</td>\n <td>NaN</td>\n <td>all_sky</td>\n </tr>\n <tr>\n <th>1</th>\n <td>57433.167792</td>\n <td>ztfr</td>\n <td>30</td>\n <td>1032.415561</td>\n <td>93.69218</td>\n <td>-17.05</td>\n <td>307</td>\n <td>NaN</td>\n <td>all_sky</td>\n </tr>\n <tr>\n <th>2</th>\n <td>57433.168255</td>\n <td>ztfr</td>\n <td>30</td>\n <td>831.160655</td>\n <td>96.84022</td>\n <td>-9.85</td>\n <td>358</td>\n <td>NaN</td>\n <td>all_sky</td>\n </tr>\n <tr>\n <th>3</th>\n <td>57433.168749</td>\n <td>ztfr</td>\n <td>30</td>\n <td>768.014326</td>\n <td>89.36968</td>\n <td>4.55</td>\n <td>459</td>\n <td>NaN</td>\n <td>all_sky</td>\n </tr>\n <tr>\n <th>4</th>\n <td>57433.169220</td>\n <td>ztfr</td>\n <td>30</td>\n <td>754.615141</td>\n <td>85.58463</td>\n <td>11.75</td>\n <td>511</td>\n <td>NaN</td>\n <td>all_sky</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "sncosmo.get_source('hsiao', version='3.0')\np, w, f = sncosmo.read_griddata_fits(\n os.path.join(sncosmo.builtins.get_cache_dir(),\n 'sncosmo/models/hsiao/Hsiao_SED_V3.fits')\n)\nmodel = sncosmo.Model(\n source=sncosmo.StretchSource(p, w, f, name='hsiao-stretch'),\n effects=[sncosmo.CCM89Dust()],\n effect_names=['host'],\n effect_frames=['rest']\n)",
"execution_count": 9,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def random_parameters(redshifts, model,\n mag=(-18., 1.),\n r_v=2., ebv_rate=0.11,\n alpha=1.3,\n cosmo=Planck15,\n **kwargs):\n \"\"\"\n \"\"\"\n out = {}\n\n amp = []\n for z in redshifts:\n mabs = np.random.normal(mag[0], mag[1])\n model.set(z=z)\n model.set_source_peakabsmag(mabs, 'bessellb', 'vega', cosmo=cosmo)\n amp.append(model.get('amplitude'))\n\n out['amplitude'] = np.array(amp)\n out['hostr_v'] = r_v * np.ones(len(redshifts))\n out['hostebv'] = np.random.exponential(ebv_rate, len(redshifts))\n\n out['s'] = np.random.normal(1., 0.1, len(redshifts))\n out['amplitude'] *= 10 ** (0.4 * alpha * (out['s'] - 1))\n\n return out\n",
"execution_count": 10,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "transientprop = {\n 'lcmodel': model,\n 'lcsimul_func': random_parameters# ,\n # 'lcsimul_prop': {'mag': (-19.3, 0.1)}\n}\n",
"execution_count": 11,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "model.param_names",
"execution_count": 12,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 12,
"data": {
"text/plain": "['z', 't0', 'amplitude', 's', 'hostebv', 'hostr_v']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "transientprop['lcsimul_func'](redshifts=[0.01, 0.02, 0.03, 0.005], model=model)",
"execution_count": 13,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 13,
"data": {
"text/plain": "{'amplitude': array([1.10223455e-06, 6.49040077e-07, 1.79149521e-07, 5.97228390e-06]),\n 'hostr_v': array([2., 2., 2., 2.]),\n 'hostebv': array([0.00047189, 0.10848363, 0.26401109, 0.00564114]),\n 's': array([1.12073349, 0.6662376 , 1.04175303, 0.8443051 ])}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "tr = simsurvey.get_transient_generator((0.0, 0.05),\n ratefunc=lambda z: 3e-5,\n ra_range=(0,360),\n dec_range=(-30,90),\n mjd_range=mjd_range, #(58178, 58543),\n transientprop=transientprop)",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": "/Users/rbiswas/soft/mypython3/lib/python3.7/site-packages/simsurvey/simultarget.py:641: UserWarning: SFD98 dust map files not found. MW E(B-V) will be set to zero.\n warnings.warn(\"SFD98 dust map files not found. \"\n",
"name": "stderr"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "tr.ntransient",
"execution_count": 15,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 15,
"data": {
"text/plain": "839"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "## Now we provide user supplied parameters"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "tr._derived_properties['simul_parameters']['zcmb'] = [0.01, 0.02, 0.03, 0.005, 0.015, 0.007, 0.012]\nprint(tr._derived_properties['simul_parameters']['zcmb'])\nprint(tr.ntransient)\n# If you provided a different number of redshift then there was before, \n# make sure to create matching-length arrays for RA, Dec etc\nr_, d_ = simsurvey.utils.random.radec(tr.ntransient,\n ra_range=tr.ra_range,\n dec_range=tr.dec_range)\ntr._derived_properties['simul_parameters']['ra'] = r_\ntr._derived_properties['simul_parameters']['dec'] = d_\n\nmjd = np.random.uniform(mjd_range[0], mjd_range[1], len(r_))\ntr._derived_properties['simul_parameters']['mjd'] = mjd\n# MW E(B-V) can just be reset becaus it will be reread from the maps when needed\ntr._reset_mwebv_()\n\n# Now regenerate the lightcurve model parameters\nlc = tr.transient_coverage[\"lightcurve_prop\"]\nparam = lc[\"param_func\"](tr.zcmb, tr.model,\n **lc[\"param_func_kwargs\"])\ntr._derived_properties[\"simul_parameters\"][\"lightcurve\"] = param",
"execution_count": 16,
"outputs": [
{
"output_type": "stream",
"text": "[0.01, 0.02, 0.03, 0.005, 0.015, 0.007, 0.012]\n7\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "tr._derived_properties['simul_parameters']",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "{'zcmb': [0.01, 0.02, 0.03, 0.005, 0.015, 0.007, 0.012],\n 'mjd': array([57554.27941731, 57647.84724408, 57735.48113532, 57622.89937408,\n 57648.61679466, 57632.27902035, 57550.83824948]),\n 'ra': array([143.43288522, 215.27404104, 33.5439331 , 20.17263192,\n 151.35366132, 58.8902822 , 218.41012146]),\n 'dec': array([71.65844246, 67.53425265, -2.87574217, -7.61688718, 56.0857594 ,\n 2.06490795, 19.55507412]),\n 'lightcurve': {'amplitude': array([5.45137631e-07, 2.19420617e-07, 4.46723913e-08, 1.83801743e-06,\n 2.12243437e-07, 8.44310384e-07, 1.24054699e-06]),\n 'hostr_v': array([2., 2., 2., 2., 2., 2., 2.]),\n 'hostebv': array([0.03715887, 0.00647934, 0.10455452, 0.10576463, 0.01179971,\n 0.12394003, 0.08348758]),\n 's': array([1.04311862, 0.93838798, 0.86791909, 1.09220414, 0.93188908,\n 1.11233478, 1.16909086])}}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "mjd_range",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "(57403.16732874228, 57736.41649200727)"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "tr.zcmb",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "[0.01, 0.02, 0.03, 0.005, 0.015, 0.007, 0.012]"
},
"metadata": {}
}
]
},
{
"metadata": {},
"cell_type": "markdown",
"source": "RB: OK, so now we have 7 objects inside the mjd range and we are not doing MW exclusions yet (because of a problem with my dustmaps and not by design)"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "survey = simsurvey.SimulSurvey(generator=tr, plan=plan)\n \nlcs = survey.get_lightcurves(progress_bar=True, notebook=True # If you get an error because of the progress_bar, delete this line.\n)",
"execution_count": 20,
"outputs": [
{
"output_type": "stream",
"text": "Determining field IDs for all objects\n",
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"text/plain": "FloatProgress(value=0.0)",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "abb2259b37d74cc69461f779c104062d"
}
},
"metadata": {}
},
{
"output_type": "stream",
"text": "\nGenerating lightcurves\n",
"name": "stdout"
},
{
"output_type": "display_data",
"data": {
"text/plain": "FloatProgress(value=0.0)",
"application/vnd.jupyter.widget-view+json": {
"version_major": 2,
"version_minor": 0,
"model_id": "d8de039359244f14b1a7f5805ba93184"
}
},
"metadata": {}
},
{
"output_type": "stream",
"text": "\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import pandas as pd",
"execution_count": 21,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "pd.DataFrame(lcs.meta_full)",
"execution_count": 22,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 22,
"data": {
"text/plain": " z t0 amplitude hostr_v hostebv s mwebv \\\n0 0.010 57554.279417 5.451376e-07 2.0 0.037159 1.043119 0 \n1 0.020 57647.847244 2.194206e-07 2.0 0.006479 0.938388 0 \n2 0.015 57648.616795 2.122434e-07 2.0 0.011800 0.931889 0 \n3 0.007 57632.279020 8.443104e-07 2.0 0.123940 1.112335 0 \n4 0.012 57550.838249 1.240547e-06 2.0 0.083488 1.169091 0 \n5 0.030 57735.481135 4.467239e-08 2.0 0.104555 0.867919 0 \n6 0.005 57622.899374 1.838017e-06 2.0 0.105765 1.092204 0 \n\n ra dec mwebv_sfd98 idx_orig \n0 143.432885 71.658442 0 0 \n1 215.274041 67.534253 0 1 \n2 151.353661 56.085759 0 4 \n3 58.890282 2.064908 0 5 \n4 218.410121 19.555074 0 6 \n5 33.543933 -2.875742 0 2 \n6 20.172632 -7.616887 0 3 ",
"text/html": "<div>\n<style scoped>\n .dataframe tbody tr th:only-of-type {\n vertical-align: middle;\n }\n\n .dataframe tbody tr th {\n vertical-align: top;\n }\n\n .dataframe thead th {\n text-align: right;\n }\n</style>\n<table border=\"1\" class=\"dataframe\">\n <thead>\n <tr style=\"text-align: right;\">\n <th></th>\n <th>z</th>\n <th>t0</th>\n <th>amplitude</th>\n <th>hostr_v</th>\n <th>hostebv</th>\n <th>s</th>\n <th>mwebv</th>\n <th>ra</th>\n <th>dec</th>\n <th>mwebv_sfd98</th>\n <th>idx_orig</th>\n </tr>\n </thead>\n <tbody>\n <tr>\n <th>0</th>\n <td>0.010</td>\n <td>57554.279417</td>\n <td>5.451376e-07</td>\n <td>2.0</td>\n <td>0.037159</td>\n <td>1.043119</td>\n <td>0</td>\n <td>143.432885</td>\n <td>71.658442</td>\n <td>0</td>\n <td>0</td>\n </tr>\n <tr>\n <th>1</th>\n <td>0.020</td>\n <td>57647.847244</td>\n <td>2.194206e-07</td>\n <td>2.0</td>\n <td>0.006479</td>\n <td>0.938388</td>\n <td>0</td>\n <td>215.274041</td>\n <td>67.534253</td>\n <td>0</td>\n <td>1</td>\n </tr>\n <tr>\n <th>2</th>\n <td>0.015</td>\n <td>57648.616795</td>\n <td>2.122434e-07</td>\n <td>2.0</td>\n <td>0.011800</td>\n <td>0.931889</td>\n <td>0</td>\n <td>151.353661</td>\n <td>56.085759</td>\n <td>0</td>\n <td>4</td>\n </tr>\n <tr>\n <th>3</th>\n <td>0.007</td>\n <td>57632.279020</td>\n <td>8.443104e-07</td>\n <td>2.0</td>\n <td>0.123940</td>\n <td>1.112335</td>\n <td>0</td>\n <td>58.890282</td>\n <td>2.064908</td>\n <td>0</td>\n <td>5</td>\n </tr>\n <tr>\n <th>4</th>\n <td>0.012</td>\n <td>57550.838249</td>\n <td>1.240547e-06</td>\n <td>2.0</td>\n <td>0.083488</td>\n <td>1.169091</td>\n <td>0</td>\n <td>218.410121</td>\n <td>19.555074</td>\n <td>0</td>\n <td>6</td>\n </tr>\n <tr>\n <th>5</th>\n <td>0.030</td>\n <td>57735.481135</td>\n <td>4.467239e-08</td>\n <td>2.0</td>\n <td>0.104555</td>\n <td>0.867919</td>\n <td>0</td>\n <td>33.543933</td>\n <td>-2.875742</td>\n <td>0</td>\n <td>2</td>\n </tr>\n <tr>\n <th>6</th>\n <td>0.005</td>\n <td>57622.899374</td>\n <td>1.838017e-06</td>\n <td>2.0</td>\n <td>0.105765</td>\n <td>1.092204</td>\n <td>0</td>\n <td>20.172632</td>\n <td>-7.616887</td>\n <td>0</td>\n <td>3</td>\n </tr>\n </tbody>\n</table>\n</div>"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(lcs.meta_full)",
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 23,
"data": {
"text/plain": "7"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "len(lcs.lcs)",
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 24,
"data": {
"text/plain": "5"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "param",
"execution_count": 25,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 25,
"data": {
"text/plain": "{'amplitude': array([5.45137631e-07, 2.19420617e-07, 4.46723913e-08, 1.83801743e-06,\n 2.12243437e-07, 8.44310384e-07, 1.24054699e-06]),\n 'hostr_v': array([2., 2., 2., 2., 2., 2., 2.]),\n 'hostebv': array([0.03715887, 0.00647934, 0.10455452, 0.10576463, 0.01179971,\n 0.12394003, 0.08348758]),\n 's': array([1.04311862, 0.93838798, 0.86791909, 1.09220414, 0.93188908,\n 1.11233478, 1.16909086])}"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "tr.model.param_names",
"execution_count": 26,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 26,
"data": {
"text/plain": "['z', 't0', 'amplitude', 's', 'hostebv', 'hostr_v', 'mwebv', 'mwr_v']"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.7.1",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment