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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

import numpy as np 

from .baseMetric import BaseMetric 

from scipy import stats 

 

__all__ = ['HistogramMetric','AccumulateMetric', 'AccumulateCountMetric', 

'HistogramM5Metric', 'AccumulateM5Metric', 'AccumulateUniformityMetric'] 

 

 

class VectorMetric(BaseMetric): 

""" 

Base for metrics that return a vector 

""" 

def __init__(self, bins=None, binCol='night', col='night', units=None, metricDtype=float, **kwargs): 

super(VectorMetric,self).__init__(col=[col,binCol],units=units,metricDtype=metricDtype,**kwargs) 

self.bins = bins 

self.binCol = binCol 

self.shape = np.size(bins)-1 

 

class HistogramMetric(VectorMetric): 

""" 

A wrapper to stats.binned_statistic 

""" 

def __init__(self, bins=None, binCol='night', col='night', units='Count', statistic='count', 

metricDtype=float, **kwargs): 

self.statistic = statistic 

self.col=col 

super(HistogramMetric,self).__init__(col=col, bins=bins, binCol=binCol, units=units, 

metricDtype=metricDtype,**kwargs) 

 

def run(self, dataSlice, slicePoint=None): 

dataSlice.sort(order=self.binCol) 

result, binEdges,binNumber = stats.binned_statistic(dataSlice[self.binCol], 

dataSlice[self.col], 

bins=self.bins, 

statistic=self.statistic) 

return result 

 

class AccumulateMetric(VectorMetric): 

""" 

Calculate the accumulated stat 

""" 

def __init__(self, col='night', bins=None, binCol='night', function=np.add, 

metricDtype=float, **kwargs): 

self.function = function 

super(AccumulateMetric,self).__init__(col=col,binCol=binCol, bins=bins, 

metricDtype=metricDtype,**kwargs) 

self.col=col 

 

def run(self, dataSlice, slicePoint=None): 

dataSlice.sort(order=self.binCol) 

 

result = self.function.accumulate(dataSlice[self.col]) 

indices = np.searchsorted(dataSlice[self.binCol], self.bins[1:], side='right') 

indices[np.where(indices >= np.size(result))] = np.size(result)-1 

result = result[indices] 

result[np.where(indices == 0)] = self.badval 

return result 

 

class AccumulateCountMetric(AccumulateMetric): 

def run(self, dataSlice, slicePoint=None): 

dataSlice.sort(order=self.binCol) 

toCount = np.ones(dataSlice.size, dtype=int) 

result = self.function.accumulate(toCount) 

indices = np.searchsorted(dataSlice[self.binCol], self.bins[1:], side='right') 

indices[np.where(indices >= np.size(result))] = np.size(result)-1 

result = result[indices] 

result[np.where(indices == 0)] = self.badval 

return result 

 

class HistogramM5Metric(HistogramMetric): 

""" 

Calculate the coadded depth for each bin (e.g., per night). 

""" 

def __init__(self, bins=None, binCol='night', m5Col='fiveSigmaDepth', units='mag', 

metricName='HistogramM5Metric',**kwargs): 

 

super(HistogramM5Metric,self).__init__(col=m5Col,binCol=binCol, bins=bins, 

metricName=metricName, 

units=units,**kwargs) 

self.m5Col=m5Col 

 

def run(self, dataSlice, slicePoint=None): 

dataSlice.sort(order=self.binCol) 

flux = 10.**(.8*dataSlice[self.m5Col]) 

result, binEdges,binNumber = stats.binned_statistic(dataSlice[self.binCol], 

flux, 

bins=self.bins, 

statistic='sum') 

noFlux = np.where(result == 0.) 

result = 1.25*np.log10(result) 

result[noFlux] = self.badval 

return result 

 

class AccumulateM5Metric(AccumulateMetric): 

def __init__(self, bins=None, binCol='night', m5Col='fiveSigmaDepth', 

metricName='AccumulateM5Metric',**kwargs): 

self.m5Col = m5Col 

super(AccumulateM5Metric,self).__init__(bins=bins, binCol=binCol,col=m5Col, 

metricName=metricName,**kwargs) 

 

 

def run(self, dataSlice, slicePoint=None): 

dataSlice.sort(order=self.binCol) 

flux = 10.**(.8*dataSlice[self.m5Col]) 

 

result = np.add.accumulate(flux) 

indices = np.searchsorted(dataSlice[self.binCol], self.bins[1:], side='right') 

indices[np.where(indices >= np.size(result))] = np.size(result)-1 

result = result[indices] 

result = 1.25*np.log10(result) 

result[np.where(indices == 0)] = self.badval 

return result 

 

 

class AccumulateUniformityMetric(AccumulateMetric): 

""" 

Make a 2D version of UniformityMetric 

""" 

def __init__(self, bins=None, binCol='night', expMJDCol='observationStartMJD', 

metricName='AccumulateUniformityMetric',surveyLength=10., 

units='Fraction', **kwargs): 

self.expMJDCol = expMJDCol 

123 ↛ 125line 123 didn't jump to line 125, because the condition on line 123 was never false if bins is None: 

bins = np.arange(0,np.ceil(surveyLength*365.25))-.5 

super(AccumulateUniformityMetric,self).__init__(bins=bins, binCol=binCol,col=expMJDCol, 

metricName=metricName,units=units,**kwargs) 

self.surveyLength = surveyLength 

 

def run(self, dataSlice, slicePoint=None): 

dataSlice.sort(order=self.binCol) 

131 ↛ 132line 131 didn't jump to line 132, because the condition on line 131 was never true if dataSlice.size == 1: 

return np.ones(self.bins.size-1, dtype=float) 

 

visitsPerNight, blah = np.histogram(dataSlice[self.binCol], bins=self.bins) 

visitsPerNight = np.add.accumulate(visitsPerNight) 

expectedPerNight = np.arange(0.,self.bins.size-1)/(self.bins.size-2) * dataSlice.size 

 

D_max = np.abs(visitsPerNight-expectedPerNight) 

D_max = np.maximum.accumulate(D_max) 

result = D_max/expectedPerNight.max() 

return result