Coverage for python/lsst/sims/catUtils/supernovae/snUniversalRules.py : 24%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1from __future__ import absolute_import
2from builtins import object
3import numpy as np
5__all__ = ['SNUniverse']
7class SNUniverse(object):
8 """
9 Mixin Class for `lsst.sims.catalogs.measures.instances.InstanceCatalog`
10 providing methods for distributing SN model parameters
11 conditioned on the parameters of the hostgalaxy from catsim Catalogs.
12 In particular, this is meant to be used to extend another mixin
13 `lsst.sims.catUtils.mixins.sncat.py`
15 The base class must have the following attributes:
17 numobjs : integer, number of objects in the catalog.
18 badvalues : a value to be reported when calculations fail, typically
19 `np.nan`
20 suppressHighzSN : Bool, if true drop any SN with redshift higher than
21 a threshold `self.maxz`
22 maxz : float, threshold redshift value, so that SN with z > self.maxz are
23 dropped if self.suppressHighzSN is True
24 midSurveyTime : float, MJD at the middle of the survey
25 mjdobs : float, MJD of the date of observation of the instance catalog
26 maxTimeSNVisible : float, units of days, Time beyond which SN is assumed
27 to be invisible. (see definition of suppressDimSN)
28 suppressDimSN : bool, if true drop observations of SN with peak at t0, if
29 abs(mjdobs - t0) > self.maxTimeVisible
31 """
33 @property
34 def snFrequency(self):
35 """
36 Frequency with which SN occur in the host galaxy in units of days^{-1}
37 """
38 if not hasattr(self, '_snFrequency'):
39 hundredyear = 100. * 365.
40 self._snFrequency = 1.0 / hundredyear
41 return self._snFrequency
43 @snFrequency.setter
44 def snFrequency(self, value):
45 """
46 set the value of snFrequency
48 Parameters
49 ----------
50 value : float, mandatory
51 value to which to set snFrequency to (units of 1/days)
52 """
53 self._snFrequency = value
54 return self._snFrequency
56 @property
57 def midSurveyTime(self):
58 '''
59 The time at the middle of the survey: ie. at the 5 year period.
62 .. note: Changing this should not change the statistical
63 properties of the survey, but will change the exact SN we find.
64 '''
65 if not hasattr(self, '_midSurveyTime'):
66 midSurveyTime_default = 61406.5
67 self._midSurveyTime = midSurveyTime_default
68 return self._midSurveyTime
70 @midSurveyTime.setter
71 def midSurveyTime(self, mymidSurveyTime):
72 '''
73 set the value of `self.midSurveyTime` of the catalog
75 Parameters
76 ----------
77 mymidSurveyTime : float, mandatory
78 Value to set the midSurveyTime attribute to
79 '''
80 # if suppressDimSN is None:
81 # self._suppressDimSN = True
82 # else:
83 self._midSurveyTime = mymidSurveyTime
84 return self._midSurveyTime
88 def SNCoordinatesFromHost(self, hostra, hostdec, hostz):
89 '''
90 Distribution of SN coordinates and velocities given a set of host
91 coordinates and velocities.
93 Parameters
94 ----------
95 hostra : ra of host
96 hostdec : dec of host
97 hostz : redshift of host
98 '''
99 numhosts = self.numobjs
101 sndec = hostdec
102 snra = hostra
103 snz = hostz
105 snvra = np.zeros(numhosts)
106 snvdec = np.zeros(numhosts)
107 snvr = np.zeros(numhosts)
109 if self.suppressHighzSN:
110 snz = np.where(snz > self.maxz, np.nan, snz)
112 return snra, sndec, snz , snvra, snvdec, snvr
114 def SNparamDistFromHost(self, hostz, hostid, hostmu):
115 """
116 Distribution of SN model parameters given their hosts
118 Parameters
119 ----------
120 hostz : float, mandatory
121 redshift of host
122 hostid : int, mandatory
123 ID of host
124 hostmu : float, mandatory
125 distance modulus of host in 'magnitudes'
126 """
127 vals = np.zeros(shape=(self.numobjs, 4))
129 for i, v in enumerate(vals):
130 vals[i, :] = self.drawSNParams(hostid[i], hostmu[i])
132 return vals
134 def getSN_rng(self, hostid):
135 hostid = hostid % 4294967295
136 rng = np.random.RandomState(hostid)
137 return rng
139 def drawSNParams(self, hostid, hostmu):
140 """
141 return the SALT2 parameters c, x1, x0, t0 for a SN with a particular
142 hostid/seed, and distance modulus value in Mpc
144 Parameters
145 ----------
146 hostid: int, mandatory
148 hostmu: float, mandatory
149 """
150 rng = self.getSN_rng(hostid)
151 t0val = self.drawFromT0Dist(rng)
152 if t0val is self.badvalues:
153 return [self.badvalues]*4
154 else:
155 cval = self.drawFromcDist(rng)
156 x1val = self.drawFromx1Dist(rng)
157 x0val = self.drawFromX0Dist(rng, x1val, cval, hostmu=hostmu)
158 return [cval, x1val, x0val, t0val]
164 def drawFromx1Dist(self, rng, **hostParams):
165 """
166 rng is an instantiation of np.random.RandomState
167 """
168 return rng.normal(0. , 1.0)
170 def drawFromcDist(self, rng, **hostParams):
171 """
172 rng is an instantiation of np.random.RandomState
173 """
174 return rng.normal(0. , 0.1)
176 def drawFromX0Dist(self, rng, x1val, cval, hostmu, **hostParams):
177 """
178 rng is an instantiation of np.random.RandomState
179 """
180 from . import snObject
182 # First draw an absolute BessellB magnitude for SN
183 mabs = rng.normal(-19.3, 0.3)
184 mag = mabs + hostmu
186 sn = snObject.SNObject()
187 sn.set(x1=x1val, c=cval)
188 sn.source.set_peakmag(mag, band='bessellb', magsys='ab')
189 x0val = sn.get('x0')
191 return x0val
193 def drawFromT0Dist(self, rng, **hostParams):
194 '''
195 Distribution function of the time of peak of SN
197 rng is an instantiation of np.random.RandomState
198 '''
200 # Will not use hostid for now, but this is there so that
201 # later on one could obtain z, hostmass etc. This is useful to obtain
202 # z and host dependent SN Frequency
203 hundredyear = 1.0 / self.snFrequency
204 t0val = rng.uniform(-hundredyear / 2.0 + self.midSurveyTime,
205 hundredyear / 2.0 + self.midSurveyTime)
207 if self.suppressDimSN:
209 # SN far away from peak will have zero flux, but writing zero flux
210 # objects to catalogs will mean each instance catalog will have
211 # too many objects. So, this variable will always stay True (except
212 # for testing purposes), while the variable that will be changed
213 # is maxTimeSNVisible
215 if np.abs(t0val - self.mjdobs) > self.maxTimeSNVisible:
216 t0val = self.badvalues
217 return t0val