Serialization Check

This notebook checks that the outputs from running sncosmo fitting functions, then writing those outputs to an hdf5 file, match the outputs of reading the same fits from the aforementioned hdf5 file.

It finds thats all output is the same within a good tolerance, except for the param_dict attribute in nest_lc() result, which has been reordered. This reordering may present an issue for some functionality but probably won't be fixed in the near future.


In [72]:
%matplotlib inline

import os
import numpy as np
from numpy.random import normal, uniform
import math
import collections
import pandas as pd
from astropy.table import Table

import triangle
import sncosmo
import simulate_lsst as sl
import lc

In [2]:
lightc = sncosmo.read_lc('sn.dat')
lightc.meta


Out[2]:
OrderedDict([('SNID', 44), ('IAUC', 'NULL'), ('FAKE', 2), ('RA', 209.882553), ('DECL', -12.400555), ('PIXSIZE', 0.2), ('NXPIX', -9), ('NYPIX', -9), ('SNTYPE', 1), ('NOBS', 83), ('PTROBS_MIN', 73), ('PTROBS_MAX', 155), ('MWEBV', 0.0829484), ('MWEBV_ERR', 0.0132717), ('REDSHIFT_HELIO', 1.06465), ('REDSHIFT_HELIO_ERR', 0.0005), ('REDSHIFT_FINAL', 1.06656), ('REDSHIFT_FINAL_ERR', 0.0005), ('HOSTGAL_OBJID', 29144), ('HOSTGAL_PHOTOZ', 1.20071), ('HOSTGAL_PHOTOZ_ERR', 0.05), ('HOSTGAL_SPECZ', 0.0), ('HOSTGAL_SPECZ_ERR', 0.0), ('HOSTGAL_SNSEP', -999.0), ('HOSTGAL_LOGMASS', -9.0), ('HOSTGAL_LOGMASS_ERR', -9.0), ('HOSTGAL_MAG_u', 999.0), ('HOSTGAL_MAG_g', 999.0), ('HOSTGAL_MAG_r', 999.0), ('HOSTGAL_MAG_i', 999.0), ('HOSTGAL_MAG_z', 999.0), ('HOSTGAL_MAG_Y', 999.0), ('HOSTGAL_SB_FLUXCAL_u', -9.0), ('HOSTGAL_SB_FLUXCAL_g', -9.0), ('HOSTGAL_SB_FLUXCAL_r', -9.0), ('HOSTGAL_SB_FLUXCAL_i', -9.0), ('HOSTGAL_SB_FLUXCAL_z', -9.0), ('HOSTGAL_SB_FLUXCAL_Y', -9.0), ('PEAKMJD', 50915.8), ('SEARCH_TYPE', -9), ('SIM_MODEL_NAME', 'SALT2.Guy10_LAMOPEN'), ('SIM_MODEL_INDEX', 6), ('SIM_TYPE_INDEX', 1), ('SIM_TYPE_NAME', 'Ia'), ('SIM_NON1a', 0), ('SIM_LIBID', 2082), ('SIM_SEARCHEFF_MASK', 3), ('SIM_REDSHIFT_HELIO', 1.06371), ('SIM_REDSHIFT_CMB', 1.06562), ('SIM_VPEC', -0.0), ('SIM_DLMU', 44.2709), ('SIM_RA', 209.882553101), ('SIM_DECL', -12.400554657), ('SIM_MWEBV', 0.0821098), ('SIM_PEAKMJD', 50916.5), ('SIM_SALT2x0', 1.695e-06), ('SIM_SALT2x1', -0.708466), ('SIM_SALT2c', 0.0178018), ('SIM_SALT2mB', 25.0621), ('SIM_SALT2alpha', 0.14), ('SIM_SALT2beta', 3.2), ('SIM_PEAKMAG_u', -9.0), ('SIM_PEAKMAG_g', -9.0), ('SIM_PEAKMAG_r', 26.5509), ('SIM_PEAKMAG_i', 25.0618), ('SIM_PEAKMAG_z', 24.534), ('SIM_PEAKMAG_Y', 24.5804), ('SIM_EXPOSURE_u', 1.0), ('SIM_EXPOSURE_g', 1.0), ('SIM_EXPOSURE_r', 1.0), ('SIM_EXPOSURE_i', 1.0), ('SIM_EXPOSURE_z', 1.0), ('SIM_EXPOSURE_Y', 1.0), ('SIM_GALFRAC_u', -9.0), ('SIM_GALFRAC_g', -9.0), ('SIM_GALFRAC_r', -9.0), ('SIM_GALFRAC_i', -9.0), ('SIM_GALFRAC_z', -9.0), ('SIM_GALFRAC_Y', -9.0)])

In [3]:
model = sncosmo.Model(source='salt2-extended')
params = {'t0': lightc.meta['SIM_PEAKMJD'], 'x0': lightc.meta['SIM_SALT2x0'], 'x1': lightc.meta['SIM_SALT2x1'],
          'c': lightc.meta['SIM_SALT2c'], 'z': lightc.meta['SIM_REDSHIFT_HELIO']}
model.set(**params)
vparams = ['t0', 'x0', 'x1', 'c']

In [13]:
lightc


Out[13]:
<Table masked=False length=83>
MJDFIELDTELESCOPEPHOTFLAGPHOTPROBfluxfluxerrMAGMAGERRPSF_SIG1PSF_SIG2PSF_RATIOSKY_SIGSKY_SIG_TRDNOISEZEROPTZEROPT_ERRGAINSIM_MAGOBSZPZPSYSband
float64int64string32int64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64float64string16string48
50877.2112082LSST00.0-0.2433751.2738199.05.02.910.00.0124.62-9.01.1535.010.0051.099.027.5abLSST_r
50877.2272082LSST00.0-0.5786432.0315299.05.02.870.00.0188.94-9.01.234.940.0051.099.027.5abLSST_i
50877.2382082LSST00.00.6115683.2315799.05.02.910.00.0201.32-9.01.2734.520.0051.099.027.5abLSST_z
50877.2462082LSST00.0-19.073313.914899.05.02.910.00.0171.38-9.01.1232.760.0051.099.027.5abLSST_Y
50881.1912082LSST00.0-0.1395551.2361899.05.01.530.00.0217.66-9.01.1234.950.0051.099.027.5abLSST_r
50881.2072082LSST00.03.203141.4485499.05.01.520.00.0242.71-9.01.1734.890.0051.099.027.5abLSST_i
50881.2192082LSST00.0-0.181621.8680899.05.01.510.00.0250.5-9.01.3534.640.0051.099.027.5abLSST_z
50881.232082LSST00.0-13.27497.2463599.05.01.530.00.0179.39-9.01.1532.820.0051.099.027.5abLSST_Y
50886.2812082LSST00.0-9.0849317.2941128.00.01.970.00.095.02-9.00.6131.460.0051.028.144527.5abLSST_Y
50892.1912082LSST00.0-0.3054651.52099128.00.02.290.00.0183.94-9.01.1234.980.0051.028.534327.5abLSST_r
..................................................................
51012.0122082LSST00.02.724312.3695426.41192.213172.240.00.0195.27-9.01.2734.540.0051.028.027427.5abLSST_z
51012.022082LSST00.0-2.0592610.4875128.00.02.240.00.0169.36-9.01.1232.770.0051.027.469927.5abLSST_Y
51014.9842082LSST00.0-0.2443280.726859128.00.01.820.00.0114.75-9.01.1235.020.0051.030.450327.5abLSST_r
51015.02082LSST00.0-0.6389361.30243128.00.01.820.00.0169.46-9.01.1234.810.0051.028.851527.5abLSST_i
51015.0122082LSST00.00.9127751.9403527.5991100.4011.820.00.0196.84-9.01.2734.540.0051.028.075627.5abLSST_z
51015.022082LSST00.0-0.3374048.64081128.00.01.810.00.0182.5-9.01.1532.830.0051.027.500727.5abLSST_Y
51022.0662082LSST00.03.325295.8941226.1954101.8052.070.00.096.53-9.00.5632.70.0051.028.192927.5abLSST_z
51023.0272082LSST00.0-8.9103217.7638128.00.01.960.00.097.2-9.00.6131.450.0051.027.577527.5abLSST_Y
51028.0432082LSST00.016.502218.828524.4561103.5441.950.00.0101.6-9.00.6131.430.0051.027.626927.5abLSST_Y
51030.0392082LSST00.0-4.5282810.3872128.00.02.210.00.096.05-9.00.4332.150.0051.028.329327.5abLSST_z

In [15]:
fits_run = lc.LC(model, lightc, vparams)
fits_readfromfile = lc.LC(model, lightc, vparams)

In [16]:
fits_run.fitOut


running chi^2 fit
Out[16]:
(       errors: OrderedDict([('t0', 0.869705464632716), ('x0', 6.352280833289825e-08), ('x1', 0.5247654629885447), ('c', 0.048283506458290146)])
   parameters: array([  1.06371000e+00,   5.09155364e+04,   1.19022208e-06,
        -1.37443229e+00,  -2.74520718e-02])
      success: True
         ndof: 79
   covariance: array([[  7.56396247e-01,  -1.47415535e-08,   9.27513342e-02,
          8.97227286e-03],
       [ -1.47415535e-08,   4.03514718e-15,  -1.57647791e-08,
          8.09694619e-10],
       [  9.27513342e-02,  -1.57647791e-08,   2.79008873e-01,
          1.00106837e-02],
       [  8.97227286e-03,   8.09694619e-10,   1.00106837e-02,
          2.35188405e-03]])
 vparam_names: ['t0', 'x0', 'x1', 'c']
        chisq: 94.67428083189289
  param_names: ['z', 't0', 'x0', 'x1', 'c']
      message: 'Minimization exited successfully.'
        ncall: 98,
 <sncosmo.models.Model at 0x1099cbd50>)

In [18]:
fits_run.mcmcOut


running MCMC fit
Out[18]:
(             vparam_names: ['t0', 'x0', 'x1', 'c']
                   errors: OrderedDict([('t0', 0.96641660746480229), ('x0', 6.1798662228628924e-08), ('x1', 0.57363086895194149), ('c', 0.045134673089685648)])
                  samples: array([[  5.09169383e+04,   1.16404787e-06,  -7.29881838e-01,
          6.09524278e-02],
       [  5.09169383e+04,   1.16404787e-06,  -7.29881838e-01,
          6.09524278e-02],
       [  5.09168300e+04,   1.19136635e-06,  -1.05085648e+00,
          4.73520685e-02],
       ..., 
       [  5.09158371e+04,   1.18020699e-06,  -6.30904921e-01,
         -4.02638873e-03],
       [  5.09159178e+04,   1.17023413e-06,  -5.17086614e-01,
         -6.33549077e-03],
       [  5.09159520e+04,   1.17144264e-06,  -5.76688271e-01,
         -7.85788275e-03]])
               parameters: array([  1.06371000e+00,   5.09156437e+04,   1.19222989e-06,
        -1.24938000e+00,  -1.18653719e-02])
              param_names: ['z', 't0', 'x0', 'x1', 'c']
 mean_acceptance_fraction: 0.58489999999999998
               covariance: array([[  9.33961059e-01,  -1.98132324e-08,   1.27355486e-01,
          8.32272228e-03],
       [ -1.98132324e-08,   3.81907465e-15,  -1.74468062e-08,
          4.14616059e-10],
       [  1.27355486e-01,  -1.74468062e-08,   3.29052374e-01,
          1.05596100e-02],
       [  8.32272228e-03,   4.14616059e-10,   1.05596100e-02,
          2.03713871e-03]]),
 <sncosmo.models.Model at 0x109a287d0>)

In [19]:
fits_run.nestOut


running nest fit
 iter=  1721 logz=-59.785532calls=5612 time=375.322s
Out[19]:
(        niter: 1722
       errors: OrderedDict([('t0', 0.92234910684464666), ('x0', 6.3086027646714359e-08), ('x1', 0.55882972699104072), ('c', 0.05059971608094839)])
   param_dict: OrderedDict([('z', 1.0637099999999999), ('t0', 50915.586386077928), ('x0', 1.1846365119066584e-06), ('x1', -1.2725691849184311), ('c', -0.016387962565982316)])
   parameters: array([  1.06371000e+00,   5.09155864e+04,   1.18463651e-06,
        -1.27256918e+00,  -1.63879626e-02])
       bounds: {'c': (-0.3, 0.3), 'x1': (-3.0, 3.0), 'x0': (0.0, 1.4249497311936984e-05), 't0': (50774.025500000011, 51071.31319999999)}
            h: 10.409304125663482
     logprior: array([ -4.61016602,  -4.62016602,  -4.63016602, ..., -21.82517019,
       -21.82517019, -21.82517019])
         logz: -59.777783297675832
         ndof: 79
        ncall: 5612
 vparam_names: ['t0', 'x0', 'x1', 'c']
      weights: array([  0.00000000e+00,   0.00000000e+00,   0.00000000e+00, ...,
         7.53874994e-05,   7.41112576e-05,   7.44992683e-05])
      samples: array([[  5.09068339e+04,   1.40227088e-05,  -8.25662223e-01,
         -2.49403336e-01],
       [  5.09251962e+04,   1.13631238e-05,  -1.45712075e+00,
         -2.01863503e-01],
       [  5.09288517e+04,   1.04907047e-05,   3.52519891e-01,
         -4.76533692e-02],
       ..., 
       [  5.09154322e+04,   1.21978393e-06,  -1.47553988e+00,
         -2.09912769e-02],
       [  5.09159262e+04,   1.19225771e-06,  -1.42583909e+00,
         -2.10510009e-02],
       [  5.09155912e+04,   1.18366107e-06,  -1.40916650e+00,
         -1.52150577e-02]])
         time: 375.3223900794983
      logzerr: 0.3226345320275479
   covariance: array([[  8.50727875e-01,  -1.46016862e-08,   1.35466840e-01,
          1.31806008e-02],
       [ -1.46016862e-08,   3.97984688e-15,  -1.90053393e-08,
          6.80506024e-10],
       [  1.35466840e-01,  -1.90053393e-08,   3.12290664e-01,
          1.08423019e-02],
       [  1.31806008e-02,   6.80506024e-10,   1.08423019e-02,
          2.56033127e-03]])
         logl: array([ -5.61571061e+04,  -4.85330366e+04,  -3.72343526e+04, ...,
        -4.74454822e+01,  -4.74625562e+01,  -4.74573344e+01]),
 <sncosmo.models.Model at 0x1099cbf50>)

In [20]:
fits_run.writeFits('serial_test.hdf5', 'sn')

In [21]:
fits_readfromfile.fitOut, fits_readfromfile.mcmcOut, fits_readfromfile.nestOut = fits_readfromfile.readFits('serial_test.hdf5', 'sn')

In [22]:
fits_readfromfile.plotLC()


Out[22]:

fit_lc() comparison


In [49]:
fits_run.fitRes


Out[49]:
       errors: OrderedDict([('t0', 0.869705464632716), ('x0', 6.352280833289825e-08), ('x1', 0.5247654629885447), ('c', 0.048283506458290146)])
   parameters: array([  1.06371000e+00,   5.09155364e+04,   1.19022208e-06,
        -1.37443229e+00,  -2.74520718e-02])
      success: True
         ndof: 79
   covariance: array([[  7.56396247e-01,  -1.47415535e-08,   9.27513342e-02,
          8.97227286e-03],
       [ -1.47415535e-08,   4.03514718e-15,  -1.57647791e-08,
          8.09694619e-10],
       [  9.27513342e-02,  -1.57647791e-08,   2.79008873e-01,
          1.00106837e-02],
       [  8.97227286e-03,   8.09694619e-10,   1.00106837e-02,
          2.35188405e-03]])
 vparam_names: ['t0', 'x0', 'x1', 'c']
        chisq: 94.67428083189289
  param_names: ['z', 't0', 'x0', 'x1', 'c']
      message: 'Minimization exited successfully.'
        ncall: 98

In [48]:
fits_run.fitRes.errors == fits_readfromfile.fitRes.errors


Out[48]:
True

In [50]:
np.allclose(fits_run.fitRes.parameters, fits_readfromfile.fitRes.parameters)


Out[50]:
True

In [73]:
np.isclose(fits_run.fitRes.ndof, fits_readfromfile.fitRes.ndof)


Out[73]:
True

In [51]:
np.allclose(fits_run.fitRes.covariance, fits_readfromfile.fitRes.covariance)


Out[51]:
True

In [74]:
np.isclose(fits_run.fitRes.chisq, fits_readfromfile.fitRes.chisq)


Out[74]:
True

In [75]:
np.isclose(fits_run.fitRes.ncall, fits_readfromfile.fitRes.ncall)


Out[75]:
True

mcmc_lc() comparison


In [52]:
fits_run.mcmcRes


Out[52]:
             vparam_names: ['t0', 'x0', 'x1', 'c']
                   errors: OrderedDict([('t0', 0.96641660746480229), ('x0', 6.1798662228628924e-08), ('x1', 0.57363086895194149), ('c', 0.045134673089685648)])
                  samples: array([[  5.09169383e+04,   1.16404787e-06,  -7.29881838e-01,
          6.09524278e-02],
       [  5.09169383e+04,   1.16404787e-06,  -7.29881838e-01,
          6.09524278e-02],
       [  5.09168300e+04,   1.19136635e-06,  -1.05085648e+00,
          4.73520685e-02],
       ..., 
       [  5.09158371e+04,   1.18020699e-06,  -6.30904921e-01,
         -4.02638873e-03],
       [  5.09159178e+04,   1.17023413e-06,  -5.17086614e-01,
         -6.33549077e-03],
       [  5.09159520e+04,   1.17144264e-06,  -5.76688271e-01,
         -7.85788275e-03]])
               parameters: array([  1.06371000e+00,   5.09156437e+04,   1.19222989e-06,
        -1.24938000e+00,  -1.18653719e-02])
              param_names: ['z', 't0', 'x0', 'x1', 'c']
 mean_acceptance_fraction: 0.58489999999999998
               covariance: array([[  9.33961059e-01,  -1.98132324e-08,   1.27355486e-01,
          8.32272228e-03],
       [ -1.98132324e-08,   3.81907465e-15,  -1.74468062e-08,
          4.14616059e-10],
       [  1.27355486e-01,  -1.74468062e-08,   3.29052374e-01,
          1.05596100e-02],
       [  8.32272228e-03,   4.14616059e-10,   1.05596100e-02,
          2.03713871e-03]])

In [53]:
fits_run.mcmcRes.errors == fits_readfromfile.mcmcRes.errors


Out[53]:
True

In [56]:
np.allclose(fits_run.mcmcRes.samples, fits_readfromfile.mcmcRes.samples)


Out[56]:
True

In [57]:
np.allclose(fits_run.mcmcRes.parameters, fits_readfromfile.mcmcRes.parameters)


Out[57]:
True

In [77]:
np.isclose(fits_run.mcmcRes.mean_acceptance_fraction,
           fits_readfromfile.mcmcRes.mean_acceptance_fraction)


Out[77]:
True

In [58]:
np.allclose(fits_run.mcmcRes.covariance, fits_readfromfile.mcmcRes.covariance)


Out[58]:
True

nest_lc() comparison


In [59]:
fits_run.nestRes


Out[59]:
        niter: 1722
       errors: OrderedDict([('t0', 0.92234910684464666), ('x0', 6.3086027646714359e-08), ('x1', 0.55882972699104072), ('c', 0.05059971608094839)])
   param_dict: OrderedDict([('z', 1.0637099999999999), ('t0', 50915.586386077928), ('x0', 1.1846365119066584e-06), ('x1', -1.2725691849184311), ('c', -0.016387962565982316)])
   parameters: array([  1.06371000e+00,   5.09155864e+04,   1.18463651e-06,
        -1.27256918e+00,  -1.63879626e-02])
       bounds: {'c': (-0.3, 0.3), 'x1': (-3.0, 3.0), 'x0': (0.0, 1.4249497311936984e-05), 't0': (50774.025500000011, 51071.31319999999)}
            h: 10.409304125663482
     logprior: array([ -4.61016602,  -4.62016602,  -4.63016602, ..., -21.82517019,
       -21.82517019, -21.82517019])
         logz: -59.777783297675832
         ndof: 79
        ncall: 5612
 vparam_names: ['t0', 'x0', 'x1', 'c']
      weights: array([  0.00000000e+00,   0.00000000e+00,   0.00000000e+00, ...,
         7.53874994e-05,   7.41112576e-05,   7.44992683e-05])
      samples: array([[  5.09068339e+04,   1.40227088e-05,  -8.25662223e-01,
         -2.49403336e-01],
       [  5.09251962e+04,   1.13631238e-05,  -1.45712075e+00,
         -2.01863503e-01],
       [  5.09288517e+04,   1.04907047e-05,   3.52519891e-01,
         -4.76533692e-02],
       ..., 
       [  5.09154322e+04,   1.21978393e-06,  -1.47553988e+00,
         -2.09912769e-02],
       [  5.09159262e+04,   1.19225771e-06,  -1.42583909e+00,
         -2.10510009e-02],
       [  5.09155912e+04,   1.18366107e-06,  -1.40916650e+00,
         -1.52150577e-02]])
         time: 375.3223900794983
      logzerr: 0.3226345320275479
   covariance: array([[  8.50727875e-01,  -1.46016862e-08,   1.35466840e-01,
          1.31806008e-02],
       [ -1.46016862e-08,   3.97984688e-15,  -1.90053393e-08,
          6.80506024e-10],
       [  1.35466840e-01,  -1.90053393e-08,   3.12290664e-01,
          1.08423019e-02],
       [  1.31806008e-02,   6.80506024e-10,   1.08423019e-02,
          2.56033127e-03]])
         logl: array([ -5.61571061e+04,  -4.85330366e+04,  -3.72343526e+04, ...,
        -4.74454822e+01,  -4.74625562e+01,  -4.74573344e+01])

In [78]:
np.isclose(fits_run.nestRes.niter, fits_readfromfile.nestRes.niter)


Out[78]:
True

In [60]:
fits_run.nestRes.errors == fits_readfromfile.nestRes.errors


Out[60]:
True

In [61]:
fits_run.nestRes.param_dict == fits_readfromfile.nestRes.param_dict


Out[61]:
False

In [71]:
fits_run.nestRes.param_dict, fits_readfromfile.nestRes.param_dict


Out[71]:
(OrderedDict([('z', 1.0637099999999999), ('t0', 50915.586386077928), ('x0', 1.1846365119066584e-06), ('x1', -1.2725691849184311), ('c', -0.016387962565982316)]),
 OrderedDict([('t0', 50915.586386077928), ('x0', 1.1846365119066584e-06), ('x1', -1.2725691849184311), ('c', -0.016387962565982316), ('z', 1.0637099999999999)]))

In [63]:
np.allclose(fits_run.nestRes.parameters, fits_readfromfile.nestRes.parameters)


Out[63]:
True

In [64]:
fits_run.nestRes.bounds == fits_readfromfile.nestRes.bounds


Out[64]:
True

In [79]:
np.isclose(fits_run.nestRes.h, fits_readfromfile.nestRes.h)


Out[79]:
True

In [65]:
np.allclose(fits_run.nestRes.logprior, fits_readfromfile.nestRes.logprior)


Out[65]:
True

In [80]:
np.isclose(fits_run.nestRes.logz, fits_readfromfile.nestRes.logz)


Out[80]:
True

In [81]:
np.isclose(fits_run.nestRes.ndof, fits_readfromfile.nestRes.ndof)


Out[81]:
True

In [82]:
np.isclose(fits_run.nestRes.ncall, fits_readfromfile.nestRes.ncall)


Out[82]:
True

In [66]:
np.allclose(fits_run.nestRes.weights, fits_readfromfile.nestRes.weights)


Out[66]:
True

In [67]:
np.allclose(fits_run.nestRes.samples, fits_readfromfile.nestRes.samples)


Out[67]:
True

In [83]:
np.isclose(fits_run.nestRes.time, fits_readfromfile.nestRes.time)


Out[83]:
True

In [84]:
np.isclose(fits_run.nestRes.logzerr, fits_readfromfile.nestRes.logzerr)


Out[84]:
True

In [68]:
np.allclose(fits_run.nestRes.covariance, fits_readfromfile.nestRes.covariance)


Out[68]:
True

In [69]:
np.allclose(fits_run.nestRes.logl, fits_readfromfile.nestRes.logl)


Out[69]:
True

In [ ]: