Hide keyboard shortcuts

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

1"""Sets of metrics to look at time between visits/pairs, etc. 

2""" 

3import numpy as np 

4import lsst.sims.maf.metrics as metrics 

5import lsst.sims.maf.slicers as slicers 

6import lsst.sims.maf.plots as plots 

7import lsst.sims.maf.metricBundles as mb 

8from .colMapDict import ColMapDict 

9from .common import standardSummary, filterList, combineMetadata, radecCols 

10 

11__all__ = ['intraNight', 'interNight', 'seasons'] 

12 

13 

14def intraNight(colmap=None, runName='opsim', nside=64, extraSql=None, extraMetadata=None, 

15 slicer=None, display_group='IntraNight', subgroup='Pairs'): 

16 """Generate a set of statistics about the pair/triplet/etc. rate within a night. 

17 

18 Parameters 

19 ---------- 

20 colmap : dict or None, opt 

21 A dictionary with a mapping of column names. Default will use OpsimV4 column names. 

22 runName : str, opt 

23 The name of the simulated survey. Default is "opsim". 

24 nside : int, opt 

25 Nside for the healpix slicer. Default 64. 

26 extraSql : str or None, opt 

27 Additional sql constraint to apply to all metrics. 

28 extraMetadata : str or None, opt 

29 Additional metadata to apply to all results. 

30 slicer : slicer object (None) 

31 Optinally use something other than a HealpixSlicer 

32 

33 Returns 

34 ------- 

35 metricBundleDict 

36 """ 

37 

38 if colmap is None: 

39 colmap = ColMapDict('opsimV4') 

40 

41 metadata = extraMetadata 

42 if extraSql is not None and len(extraSql) > 0: 

43 if metadata is None: 

44 metadata = extraSql 

45 

46 raCol, decCol, degrees, ditherStacker, ditherMeta = radecCols(None, colmap, None) 

47 metadata = combineMetadata(metadata, ditherMeta) 

48 

49 bundleList = [] 

50 standardStats = standardSummary() 

51 subsetPlots = [plots.HealpixSkyMap(), plots.HealpixHistogram()] 

52 

53 if slicer is None: 

54 slicer = slicers.HealpixSlicer(nside=nside, latCol=decCol, lonCol=raCol, latLonDeg=degrees) 

55 

56 # Look for the fraction of visits in gri where there are pairs within dtMin/dtMax. 

57 displayDict = {'group': display_group, 'subgroup': subgroup, 'caption': None, 'order': 0} 

58 if extraSql is not None and len(extraSql) > 0: 

59 sql = '(%s) and (filter="g" or filter="r" or filter="i")' % extraSql 

60 else: 

61 sql = 'filter="g" or filter="r" or filter="i"' 

62 md = 'gri' 

63 if metadata is not None: 

64 md += ' ' + metadata 

65 dtMin = 10.0 

66 dtMax = 60.0 

67 metric = metrics.PairFractionMetric(mjdCol=colmap['mjd'], minGap=dtMin, maxGap=dtMax, 

68 metricName='Fraction of visits in pairs (%.0f-%.0f min)' % (dtMin, 

69 dtMax)) 

70 displayDict['caption'] = 'Fraction of %s visits that have a paired visit' \ 

71 'between %.1f and %.1f minutes away. ' % (md, dtMin, dtMax) 

72 displayDict['caption'] += 'If all visits were in pairs, this fraction would be 1.' 

73 displayDict['order'] += 1 

74 bundle = mb.MetricBundle(metric, slicer, sql, metadata=md, summaryMetrics=standardStats, 

75 plotFuncs=subsetPlots, displayDict=displayDict) 

76 bundleList.append(bundle) 

77 

78 dtMin = 20.0 

79 dtMax = 90.0 

80 metric = metrics.PairFractionMetric(mjdCol=colmap['mjd'], minGap=dtMin, maxGap=dtMax, 

81 metricName='Fraction of visits in pairs (%.0f-%.0f min)' % (dtMin, 

82 dtMax)) 

83 displayDict['caption'] = 'Fraction of %s visits that have a paired visit' \ 

84 'between %.1f and %.1f minutes away. ' % (md, dtMin, dtMax) 

85 displayDict['caption'] += 'If all visits were in pairs, this fraction would be 1.' 

86 displayDict['order'] += 1 

87 bundle = mb.MetricBundle(metric, slicer, sql, metadata=md, summaryMetrics=standardStats, 

88 plotFuncs=subsetPlots, displayDict=displayDict) 

89 bundleList.append(bundle) 

90 

91 # Look at the fraction of visits which have another visit within dtMax, gri. 

92 dtMax = 60.0 

93 metric = metrics.NRevisitsMetric(mjdCol=colmap['mjd'], dT=dtMax, normed=True, 

94 metricName='Fraction of visits with a revisit < %.0f min' % dtMax) 

95 displayDict['caption'] = 'Fraction of %s visits that have another visit ' \ 

96 'within %.1f min. ' % (md, dtMax) 

97 displayDict['caption'] += 'If all visits were in pairs (only), this fraction would be 0.5.' 

98 displayDict['order'] += 1 

99 bundle = mb.MetricBundle(metric, slicer, sql, metadata=md, summaryMetrics=standardStats, 

100 plotFuncs=subsetPlots, displayDict=displayDict) 

101 bundleList.append(bundle) 

102 

103 # Intranight gap map, all filters. Returns value in hours. 

104 metric = metrics.IntraNightGapsMetric(metricName='Median Intra-Night Gap', mjdCol=colmap['mjd'], 

105 reduceFunc=np.median) 

106 displayDict['caption'] = 'Median gap between consecutive visits within a night, all bands' 

107 if metadata is None or len(metadata) == 0: 

108 displayDict['caption'] += ', all proposals.' 

109 else: 

110 displayDict['caption'] += ', %s.' % metadata 

111 displayDict['order'] += 1 

112 plotDict = {'percentileClip': 95} 

113 bundle = mb.MetricBundle(metric, slicer, extraSql, metadata=metadata, displayDict=displayDict, 

114 plotFuncs=subsetPlots, plotDict=plotDict, 

115 summaryMetrics=standardStats) 

116 bundleList.append(bundle) 

117 

118 # Histogram the number of visits per night. 

119 countbins = np.arange(0, 10, 1) 

120 metric = metrics.NVisitsPerNightMetric(nightCol=colmap['night'], bins=countbins, 

121 metricName="NVisitsPerNight") 

122 plotDict = {'bins': countbins, 'xlabel': 'Number of visits each night'} 

123 displayDict['caption'] = 'Histogram of the number of visits in each night, per point on the sky' 

124 if metadata is None or len(metadata) == 0: 

125 displayDict['caption'] += ', all proposals.' 

126 else: 

127 displayDict['caption'] += ', %s.' % metadata 

128 displayDict['order'] = 0 

129 plotFunc = plots.SummaryHistogram() 

130 bundle = mb.MetricBundle(metric, slicer, extraSql, plotDict=plotDict, 

131 displayDict=displayDict, metadata=metadata, plotFuncs=[plotFunc]) 

132 bundleList.append(bundle) 

133 

134 # Histogram of the time between revisits (all filters) within two hours. 

135 binMin = 0 

136 binMax = 120. 

137 binsize = 5. 

138 bins_metric = np.arange(binMin / 60.0 / 24.0, (binMax + binsize) / 60. / 24., binsize / 60. / 24.) 

139 bins_plot = bins_metric * 24.0 * 60.0 

140 metric = metrics.TgapsMetric(bins=bins_metric, timesCol=colmap['mjd'], metricName='DeltaT Histogram') 

141 plotDict = {'bins': bins_plot, 'xlabel': 'dT (minutes)'} 

142 displayDict['caption'] = 'Histogram of the time between consecutive visits to a given point ' \ 

143 'on the sky, considering visits between %.1f and %.1f minutes' % (binMin, 

144 binMax) 

145 if metadata is None or len(metadata) == 0: 

146 displayDict['caption'] += ', all proposals.' 

147 else: 

148 displayDict['caption'] += ', %s.' % metadata 

149 displayDict['order'] += 1 

150 plotFunc = plots.SummaryHistogram() 

151 bundle = mb.MetricBundle(metric, slicer, extraSql, plotDict=plotDict, 

152 displayDict=displayDict, metadata=metadata, plotFuncs=[plotFunc]) 

153 bundleList.append(bundle) 

154 

155 # Set the runName for all bundles and return the bundleDict. 

156 for b in bundleList: 

157 b.setRunName(runName) 

158 plotBundles = None 

159 return mb.makeBundlesDictFromList(bundleList), plotBundles 

160 

161 

162def interNight(colmap=None, runName='opsim', nside=64, extraSql=None, extraMetadata=None, 

163 slicer=None, display_group='InterNight', subgroup='Night gaps'): 

164 """Generate a set of statistics about the spacing between nights with observations. 

165 

166 Parameters 

167 ---------- 

168 colmap : dict or None, opt 

169 A dictionary with a mapping of column names. Default will use OpsimV4 column names. 

170 runName : str, opt 

171 The name of the simulated survey. Default is "opsim". 

172 nside : int, opt 

173 Nside for the healpix slicer. Default 64. 

174 extraSql : str or None, opt 

175 Additional sql constraint to apply to all metrics. 

176 extraMetadata : str or None, opt 

177 Additional metadata to use for all outputs. 

178 slicer : slicer object (None) 

179 Optinally use something other than a HealpixSlicer 

180 

181 Returns 

182 ------- 

183 metricBundleDict 

184 """ 

185 

186 if colmap is None: 

187 colmap = ColMapDict('opsimV4') 

188 

189 bundleList = [] 

190 

191 # Set up basic all and per filter sql constraints. 

192 raCol, decCol, degrees, ditherStacker, ditherMeta = radecCols(None, colmap, None) 

193 metadata = combineMetadata(extraMetadata, ditherMeta) 

194 filterlist, colors, orders, sqls, metadata = filterList(all=True, 

195 extraSql=extraSql, 

196 extraMetadata=metadata) 

197 

198 if slicer is None: 

199 slicer = slicers.HealpixSlicer(nside=nside, latCol=decCol, lonCol=raCol, latLonDeg=degrees) 

200 

201 displayDict = {'group': display_group, 'subgroup': subgroup, 'caption': None, 'order': 0} 

202 

203 # Histogram of the number of nights between visits. 

204 bins = np.arange(1, 20.5, 1) 

205 metric = metrics.NightgapsMetric(bins=bins, nightCol=colmap['night'], metricName='DeltaNight Histogram') 

206 plotDict = {'bins': bins, 'xlabel': 'dT (nights)'} 

207 displayDict['caption'] = 'Histogram of the number of nights between consecutive visits to a ' \ 

208 'given point on the sky, considering separations between %d and %d' \ 

209 % (bins.min(), bins.max()) 

210 if metadata['all'] is None or len(metadata['all']) == 0: 

211 displayDict['caption'] += ', all proposals.' 

212 else: 

213 displayDict['caption'] += ', %s.' % metadata['all'] 

214 plotFunc = plots.SummaryHistogram() 

215 bundle = mb.MetricBundle(metric, slicer, sqls['all'], plotDict=plotDict, 

216 displayDict=displayDict, metadata=metadata['all'], plotFuncs=[plotFunc]) 

217 bundleList.append(bundle) 

218 

219 standardStats = standardSummary() 

220 subsetPlots = [plots.HealpixSkyMap(), plots.HealpixHistogram()] 

221 

222 # Median inter-night gap (each and all filters) 

223 metric = metrics.InterNightGapsMetric(metricName='Median Inter-Night Gap', mjdCol=colmap['mjd'], 

224 reduceFunc=np.median) 

225 for f in filterlist: 

226 displayDict['caption'] = 'Median gap between nights with observations, %s.' % metadata[f] 

227 displayDict['order'] = orders[f] 

228 plotDict = {'color': colors[f], 'percentileClip': 95.} 

229 bundle = mb.MetricBundle(metric, slicer, sqls[f], metadata=metadata[f], 

230 displayDict=displayDict, 

231 plotFuncs=subsetPlots, plotDict=plotDict, 

232 summaryMetrics=standardStats) 

233 bundleList.append(bundle) 

234 

235 # Maximum inter-night gap (in each and all filters). 

236 metric = metrics.InterNightGapsMetric(metricName='Max Inter-Night Gap', mjdCol=colmap['mjd'], 

237 reduceFunc=np.max) 

238 for f in filterlist: 

239 displayDict['caption'] = 'Maximum gap between nights with observations, %s.' % metadata[f] 

240 displayDict['order'] = orders[f] 

241 plotDict = {'color': colors[f], 'percentileClip': 95., 'binsize': 5} 

242 bundle = mb.MetricBundle(metric, slicer, sqls[f], metadata=metadata[f], displayDict=displayDict, 

243 plotFuncs=subsetPlots, plotDict=plotDict, summaryMetrics=standardStats) 

244 bundleList.append(bundle) 

245 

246 # Set the runName for all bundles and return the bundleDict. 

247 for b in bundleList: 

248 b.setRunName(runName) 

249 plotBundles = None 

250 return mb.makeBundlesDictFromList(bundleList), plotBundles 

251 

252 

253def seasons(colmap=None, runName='opsim', nside=64, extraSql=None, extraMetadata=None): 

254 """Generate a set of statistics about the length and number of seasons. 

255 

256 Parameters 

257 ---------- 

258 colmap : dict or None, opt 

259 A dictionary with a mapping of column names. Default will use OpsimV4 column names. 

260 runName : str, opt 

261 The name of the simulated survey. Default is "opsim". 

262 nside : int, opt 

263 Nside for the healpix slicer. Default 64. 

264 extraSql : str or None, opt 

265 Additional sql constraint to apply to all metrics. 

266 extraMetadata : str or None, opt 

267 Additional metadata to use for all outputs. 

268 

269 Returns 

270 ------- 

271 metricBundleDict 

272 """ 

273 

274 if colmap is None: 

275 colmap = ColMapDict('opsimV4') 

276 

277 bundleList = [] 

278 

279 # Set up basic all and per filter sql constraints. 

280 raCol, decCol, degrees, ditherStacker, ditherMeta = radecCols(None, colmap, None) 

281 metadata = combineMetadata(extraMetadata, ditherMeta) 

282 filterlist, colors, orders, sqls, metadata = filterList(all=True, 

283 extraSql=extraSql, 

284 extraMetadata=metadata) 

285 

286 slicer = slicers.HealpixSlicer(nside=nside, latCol=decCol, lonCol=raCol, latLonDeg=degrees) 

287 

288 displayDict = {'group': 'IntraSeason', 'subgroup': 'Season length', 'caption': None, 'order': 0} 

289 

290 standardStats = standardSummary() 

291 subsetPlots = [plots.HealpixSkyMap(), plots.HealpixHistogram()] 

292 

293 metric = metrics.SeasonLengthMetric(metricName='Median Season Length', mjdCol=colmap['mjd'], 

294 reduceFunc=np.median) 

295 for f in filterlist: 

296 displayDict['caption'] = 'Median season length, %s.' % metadata[f] 

297 displayDict['order'] = orders[f] 

298 maxS = 250 

299 if f == 'all': 

300 minS = 90 

301 else: 

302 minS = 30 

303 plotDict = {'color': colors[f], 'colorMin': minS, 'colorMax': maxS, 'xMin': minS, 'xMax': maxS} 

304 bundle = mb.MetricBundle(metric, slicer, sqls[f], metadata=metadata[f], 

305 displayDict=displayDict, 

306 plotFuncs=subsetPlots, plotDict=plotDict, 

307 summaryMetrics=standardStats) 

308 bundleList.append(bundle) 

309 

310 # Number of seasons 

311 metric = metrics.CampaignLengthMetric(metricName='NSeasons', mjdCol=colmap['mjd'], 

312 expTimeCol=colmap['exptime'], minExpTime=15) 

313 displayDict['caption'] = 'Number of seasons, any filter.' 

314 displayDict['order'] = 0 

315 plotDict = {'color': 'k', 'colorMin': 0, 'colorMax': 11, 'xMin': 0, 'xMax': 11} 

316 bundle = mb.MetricBundle(metric, slicer, sqls['all'], metadata=metadata['all'], 

317 displayDict=displayDict, 

318 plotFuncs=subsetPlots, plotDict=plotDict, 

319 summaryMetrics=standardStats) 

320 bundleList.append(bundle) 

321 

322 # Set the runName for all bundles and return the bundleDict. 

323 for b in bundleList: 

324 b.setRunName(runName) 

325 plotBundles = None 

326 return mb.makeBundlesDictFromList(bundleList), plotBundles