Coverage for python/lsst/sims/maf/metrics/tgaps.py : 21%

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
1import numpy as np
2from .baseMetric import BaseMetric
4__all__ = ['TgapsMetric', 'NightgapsMetric', 'NVisitsPerNightMetric', 'MaxGapMetric']
7class TgapsMetric(BaseMetric):
8 """Histogram the times of the gaps between observations.
11 Measure the gaps between observations. By default, only gaps
12 between neighboring visits are computed. If allGaps is set to true, all gaps are
13 computed (i.e., if there are observations at 10, 20, 30 and 40 the default will
14 return a histogram of [10,10,10] while allGaps returns a histogram of [10,10,10,20,20,30])
16 Parameters
17 ----------
18 timesCol : str, opt
19 The column name for the exposure times. Values assumed to be in days.
20 Default observationStartMJD.
21 allGaps : bool, opt
22 Histogram the gaps between all observations (True) or just successive observations (False)?
23 Default is False. If all gaps are used, this metric can become significantly slower.
24 bins : np.ndarray, opt
25 The bins to use for the histogram of time gaps (in days, or same units as timesCol).
26 Default values are bins from 0 to 2 hours, in 5 minute intervals.
28 Returns a histogram at each slice point; these histograms can be combined and plotted using the
29 'SummaryHistogram plotter'.
30 """
32 def __init__(self, timesCol='observationStartMJD', allGaps=False, bins=np.arange(0, 120.0, 5.0)/60./24.,
33 units='days', **kwargs):
34 # Pass the same bins to the plotter.
35 self.bins = bins
36 self.timesCol = timesCol
37 super(TgapsMetric, self).__init__(col=[self.timesCol], metricDtype='object', units=units, **kwargs)
38 self.allGaps = allGaps
40 def run(self, dataSlice, slicePoint=None):
41 if dataSlice.size < 2:
42 return self.badval
43 times = np.sort(dataSlice[self.timesCol])
44 if self.allGaps:
45 allDiffs = []
46 for i in np.arange(1,times.size,1):
47 allDiffs.append((times-np.roll(times,i))[i:])
48 dts = np.concatenate(allDiffs)
49 else:
50 dts = np.diff(times)
51 result, bins = np.histogram(dts, self.bins)
52 return result
55class NightgapsMetric(BaseMetric):
56 """Histogram the number of nights between observations.
59 Measure the gaps between observations. By default, only gaps
60 between neighboring visits are computed. If allGaps is set to true, all gaps are
61 computed (i.e., if there are observations at 10, 20, 30 and 40 the default will
62 histogram [10,10,10] while allGaps histograms [10,10,10,20,20,30])
64 Parameters
65 ----------
66 nightCol : str, opt
67 The column name for the night of each observation.
68 Default 'night'.
69 allGaps : bool, opt
70 Histogram the gaps between all observations (True) or just successive observations (False)?
71 Default is False. If all gaps are used, this metric can become significantly slower.
72 bins : np.ndarray, opt
73 The bins to use for the histogram of time gaps (in days, or same units as timesCol).
74 Default values are bins from 0 to 10 days, in 1 day intervals.
76 Returns a histogram at each slice point; these histograms can be combined and plotted using the
77 'SummaryHistogram plotter'.
78 """
80 def __init__(self, nightCol='night', allGaps=False, bins=np.arange(0, 10, 1),
81 units='nights', **kwargs):
82 # Pass the same bins to the plotter.
83 self.bins = bins
84 self.nightCol = nightCol
85 super(NightgapsMetric, self).__init__(col=[self.nightCol], metricDtype='object',
86 units=units, **kwargs)
87 self.allGaps = allGaps
89 def run(self, dataSlice, slicePoint=None):
90 if dataSlice.size < 2:
91 return self.badval
92 nights = np.sort(np.unique(dataSlice[self.nightCol]))
93 if self.allGaps:
94 allDiffs = []
95 for i in np.arange(1, nights.size,1):
96 allDiffs.append((nights-np.roll(nights,i))[i:])
97 dnights = np.concatenate(allDiffs)
98 else:
99 dnights = np.diff(nights)
100 result, bins = np.histogram(dnights, self.bins)
101 return result
104class NVisitsPerNightMetric(BaseMetric):
105 """Histogram the number of visits in each night.
107 Splits the visits by night, then histograms how many visits occur in each night.
109 Parameters
110 ----------
111 nightCol : str, opt
112 The column name for the night of each observation.
113 Default 'night'.
114 bins : np.ndarray, opt
115 The bins to use for the histogram of time gaps (in days, or same units as timesCol).
116 Default values are bins from 0 to 5 visits, in steps of 1.
118 Returns a histogram at each slice point; these histograms can be combined and plotted using the
119 'SummaryHistogram plotter'.
120 """
122 def __init__(self, nightCol='night', bins=np.arange(0, 10, 1), units='#', **kwargs):
123 # Pass the same bins to the plotter.
124 self.bins = bins
125 self.nightCol = nightCol
126 super(NVisitsPerNightMetric, self).__init__(col=[self.nightCol], metricDtype='object',
127 units=units, **kwargs)
129 def run(self, dataSlice, slicePoint=None):
130 n, counts = np.unique(dataSlice[self.nightCol], return_counts=True)
131 result, bins = np.histogram(counts, self.bins)
132 return result
135class MaxGapMetric(BaseMetric):
136 """Find the maximum gap in observations. Useful for making sure there is an image within the last year that would
137 make a good template image.
138 """
140 def __init__(self, mjdCol='observationStartMJD', **kwargs):
141 self.mjdCol = mjdCol
142 units = 'Days'
143 super(MaxGapMetric, self).__init__(col=[self.mjdCol], units=units, **kwargs)
145 def run(self, dataSlice, slicePoint=None):
146 gaps = np.diff(np.sort(dataSlice[self.mjdCol]))
147 if np.size(gaps) > 0:
148 result = np.max(gaps)
149 else:
150 result = self.badval
151 return result