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

import numpy as np 

from matplotlib import colors 

import matplotlib.pyplot as plt 

from .plotHandler import BasePlotter 

from .perceptual_rainbow import makePRCmap 

 

__all__ = ['TwoDMap', 'VisitPairsHist'] 

 

perceptual_rainbow = makePRCmap() 

 

class TwoDMap(BasePlotter): 

def __init__(self): 

self.plotType = 'TwoD' 

self.objectPlotter = False 

self.defaultPlotDict = {'title': None, 'xlabel': None, 'ylabel': None, 'label': None, 

'logScale': False, 'cbarFormat': None, 'cbarTitle': 'Count', 

'cmap': perceptual_rainbow, 

'percentileClip': None, 'colorMin': None, 'colorMax': None, 

'zp': None, 'normVal': None, 

'cbar_edge': True, 'nTicks': None, 'aspect': 'auto', 

'xextent': None, 'origin': None} 

 

def __call__(self, metricValue, slicer, userPlotDict, fignum=None): 

""" 

Parameters 

---------- 

metricValue : numpy.ma.MaskedArray 

slicer : lsst.sims.maf.slicers.BaseSpatialSlicer 

(any spatial slicer) 

userPlotDict: dict 

Dictionary of plot parameters set by user (overrides default values). 

fignum : int 

Matplotlib figure number to use (default = None, starts new figure). 

 

Returns 

------- 

int 

Matplotlib figure number used to create the plot. 

""" 

if 'Healpix' in slicer.slicerName: 

self.defaultPlotDict['ylabel'] = 'Healpix ID' 

elif 'Opsim' in slicer.slicerName: 

self.defaultPlotDict['ylabel'] = 'Field ID' 

self.defaultPlotDict['origin'] = 'lower' 

elif 'User' in slicer.slicerName: 

self.defaultPlotDict['ylabel'] = 'User Field ID' 

 

plotDict = {} 

plotDict.update(self.defaultPlotDict) 

# Don't clobber with None 

for key in userPlotDict: 

if userPlotDict[key] is not None: 

plotDict[key] = userPlotDict[key] 

 

if plotDict['xextent'] is None: 

plotDict['xextent'] = [0, metricValue[0, :].size] 

 

if plotDict['logScale']: 

norm = colors.LogNorm() 

else: 

norm = None 

 

# Mask out values below the color minimum so they show up as white 

if plotDict['colorMin'] is not None: 

lowVals = np.where(metricValue.data < plotDict['colorMin']) 

metricValue.mask[lowVals] = True 

 

figure = plt.figure(fignum) 

ax = figure.add_subplot(111) 

yextent = slicer.spatialExtent 

xextent = plotDict['xextent'] 

extent = [] 

extent.extend(xextent) 

extent.extend(yextent) 

image = ax.imshow(metricValue, vmin=plotDict['colorMin'], vmax=plotDict['colorMax'], 

aspect=plotDict['aspect'], cmap=plotDict['cmap'], norm=norm, 

extent=extent, 

interpolation='none', origin=plotDict['origin']) 

cb = plt.colorbar(image) 

 

ax.set_xlabel(plotDict['xlabel']) 

ax.set_ylabel(plotDict['ylabel']) 

ax.set_title(plotDict['title']) 

cb.set_label(plotDict['cbarTitle']) 

 

# Fix white space on pdf's 

if plotDict['cbar_edge']: 

cb.solids.set_edgecolor("face") 

return figure.number 

 

class VisitPairsHist(BasePlotter): 

""" 

Given an opsim2dSlicer, figure out what fraction of observations are in singles, pairs, triples, etc. 

""" 

def __init__(self): 

self.plotType = 'TwoD' 

self.objectPlotter = False 

self.defaultPlotDict = {'title': None, 'xlabel': 'N visits per night per field', 

'ylabel': 'N Visits', 'label': None, 'color': 'b', 

'xlim': [0, 20], 'ylim': None} 

 

def __call__(self, metricValue, slicer, userPlotDict, fignum=None): 

""" 

Parameters 

---------- 

metricValue : numpy.ma.MaskedArray 

slicer : lsst.sims.maf.slicers.TwoDSlicer 

userPlotDict: dict 

Dictionary of plot parameters set by user (overrides default values). 

fignum : int 

Matplotlib figure number to use (default = None, starts new figure). 

 

Returns 

------- 

int 

Matplotlib figure number used to create the plot. 

""" 

plotDict = {} 

plotDict.update(self.defaultPlotDict) 

# Don't clobber with None 

for key in userPlotDict: 

if userPlotDict[key] is not None: 

plotDict[key] = userPlotDict[key] 

 

maxVal = metricValue.max() 

bins = np.arange(0.5, maxVal + 0.5, 1) 

 

vals, bins = np.histogram(metricValue, bins) 

xvals = (bins[:-1] + bins[1:]) / 2. 

 

figure = plt.figure(fignum) 

ax = figure.add_subplot(111) 

ax.bar(xvals, vals * xvals, color=plotDict['color'], label=plotDict['label']) 

ax.set_xlabel(plotDict['xlabel']) 

ax.set_ylabel(plotDict['ylabel']) 

ax.set_title(plotDict['title']) 

ax.set_xlim(plotDict['xlim']) 

ax.set_ylim(plotDict['ylim']) 

 

return figure.number