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

1import numpy as np 

2import healpy as hp 

3import warnings 

4 

5__all__ = ['optimalBins', 'percentileClipping', 

6 'gnomonic_project_toxy', 'radec2pix'] 

7 

8 

9def optimalBins(datain, binmin=None, binmax=None, nbinMax=200, nbinMin=1): 

10 """ 

11 Set an 'optimal' number of bins using the Freedman-Diaconis rule. 

12 

13 Parameters 

14 ---------- 

15 datain : numpy.ndarray or numpy.ma.MaskedArray 

16 The data for which we want to set the binsize. 

17 binmin : float 

18 The minimum bin value to consider (if None, uses minimum data value). 

19 binmax : float 

20 The maximum bin value to consider (if None, uses maximum data value). 

21 nbinMax : int 

22 The maximum number of bins to create. Sometimes the 'optimal binsize' implies 

23 an unreasonably large number of bins, if the data distribution is unusual. 

24 nbinMin : int 

25 The minimum number of bins to create. Default is 1. 

26 

27 Returns 

28 ------- 

29 int 

30 The number of bins. 

31 """ 

32 # if it's a masked array, only use unmasked values 

33 if hasattr(datain, 'compressed'): 

34 data = datain.compressed() 

35 else: 

36 data = datain 

37 # Check that any good data values remain. 

38 if data.size == 0: 

39 nbins = nbinMax 

40 warnings.warn('No unmasked data available for calculating optimal bin size: returning %i bins' %(nbins)) 

41 # Else proceed. 

42 else: 

43 if binmin is None: 

44 binmin = np.nanmin(data) 

45 if binmax is None: 

46 binmax = np.nanmax(data) 

47 cond = np.where((data >= binmin) & (data <= binmax))[0] 

48 # Check if any data points remain within binmin/binmax. 

49 if np.size(data[cond]) == 0: 

50 nbins = nbinMax 

51 warnings.warn('No data available for calculating optimal bin size within range of %f, %f' 

52 %(binmin, binmax) + ': returning %i bins' %(nbins)) 

53 else: 

54 iqr = np.percentile(data[cond], 75) - np.percentile(data[cond], 25) 

55 binwidth = 2 * iqr * (np.size(data[cond])**(-1./3.)) 

56 nbins = (binmax - binmin) / binwidth 

57 if nbins > nbinMax: 

58 warnings.warn('Optimal bin calculation tried to make %.0f bins, returning %i'%(nbins, nbinMax)) 

59 nbins = nbinMax 

60 if nbins < nbinMin: 

61 warnings.warn('Optimal bin calculation tried to make %.0f bins, returning %i'%(nbins, nbinMin)) 

62 nbins = nbinMin 

63 if np.isnan(nbins): 

64 warnings.warn('Optimal bin calculation calculated NaN: returning %i' %(nbinMax)) 

65 nbins = nbinMax 

66 return int(nbins) 

67 

68 

69def percentileClipping(data, percentile=95.): 

70 """ 

71 Calculate the minimum and maximum values of a distribution of points, after 

72 discarding data more than 'percentile' from the median. 

73 This is useful for determining useful data ranges for plots. 

74 Note that 'percentile' percent of the data is retained. 

75 

76 Parameters 

77 ---------- 

78 data : numpy.ndarray 

79 The data to clip. 

80 percentile : float 

81 Retain values within percentile of the median. 

82 

83 Returns 

84 ------- 

85 float, float 

86 The minimum and maximum values of the clipped data. 

87 """ 

88 lower_percentile = (100 - percentile) / 2.0 

89 upper_percentile = 100 - lower_percentile 

90 min_value = np.percentile(data, lower_percentile) 

91 max_value = np.percentile(data, upper_percentile) 

92 return min_value, max_value 

93 

94def gnomonic_project_toxy(RA1, Dec1, RAcen, Deccen): 

95 """ 

96 Calculate the x/y values of RA1/Dec1 in a gnomonic projection with center at RAcen/Deccen. 

97 

98 Parameters 

99 ---------- 

100 RA1 : numpy.ndarray 

101 RA values of the data to be projected, in radians. 

102 Dec1 : numpy.ndarray 

103 Dec values of the data to be projected, in radians. 

104 RAcen: float 

105 RA value of the center of the projection, in radians. 

106 Deccen : float 

107 Dec value of the center of the projection, in radians. 

108 

109 Returns 

110 ------- 

111 numpy.ndarray, numpy.ndarray 

112 The x/y values of the projected RA1/Dec1 positions. 

113 """ 

114 cosc = np.sin(Deccen) * np.sin(Dec1) + np.cos(Deccen) * np.cos(Dec1) * np.cos(RA1-RAcen) 

115 x = np.cos(Dec1) * np.sin(RA1-RAcen) / cosc 

116 y = (np.cos(Deccen)*np.sin(Dec1) - np.sin(Deccen)*np.cos(Dec1)*np.cos(RA1-RAcen)) / cosc 

117 return x, y 

118 

119 

120def radec2pix(nside, ra, dec): 

121 """ 

122 Calculate the nearest healpixel ID of an RA/Dec array, assuming nside. 

123 

124 Parameters 

125 ---------- 

126 nside : int 

127 The nside value of the healpix grid. 

128 ra : numpy.ndarray 

129 The RA values to be converted to healpix ids, in radians. 

130 dec : numpy.ndarray 

131 The Dec values to be converted to healpix ids, in radians. 

132 

133 Returns 

134 ------- 

135 numpy.ndarray 

136 The healpix ids. 

137 """ 

138 lat = np.pi/2. - dec 

139 hpid = hp.ang2pix(nside, lat, ra ) 

140 return hpid