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

142

import importlib 

import os 

import numpy as np 

import healpy as hp 

import warnings 

 

__all__ = ['optimalBins', 'percentileClipping', 

'gnomonic_project_toxy', 'radec2pix'] 

 

 

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

""" 

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

 

Parameters 

---------- 

datain : numpy.ndarray or numpy.ma.MaskedArray 

The data for which we want to set the binsize. 

binmin : float 

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

binmax : float 

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

nbinMax : int 

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

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

nbinMin : int 

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

 

Returns 

------- 

int 

The number of bins. 

""" 

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

35 ↛ 36line 35 didn't jump to line 36, because the condition on line 35 was never true if hasattr(datain, 'compressed'): 

data = datain.compressed() 

else: 

data = datain 

# Check that any good data values remain. 

40 ↛ 41line 40 didn't jump to line 41, because the condition on line 40 was never true if data.size == 0: 

nbins = nbinMax 

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

# Else proceed. 

else: 

if binmin is None: 

binmin = np.nanmin(data) 

if binmax is None: 

binmax = np.nanmax(data) 

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

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

51 ↛ 52line 51 didn't jump to line 52, because the condition on line 51 was never true if np.size(data[cond]) == 0: 

nbins = nbinMax 

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

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

else: 

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

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

nbins = (binmax - binmin) / binwidth 

59 ↛ 60line 59 didn't jump to line 60, because the condition on line 59 was never true if nbins > nbinMax: 

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

nbins = nbinMax 

62 ↛ 63line 62 didn't jump to line 63, because the condition on line 62 was never true if nbins < nbinMin: 

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

nbins = nbinMin 

65 ↛ 66line 65 didn't jump to line 66, because the condition on line 65 was never true if np.isnan(nbins): 

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

nbins = nbinMax 

return int(nbins) 

 

 

def percentileClipping(data, percentile=95.): 

""" 

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

discarding data more than 'percentile' from the median. 

This is useful for determining useful data ranges for plots. 

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

 

Parameters 

---------- 

data : numpy.ndarray 

The data to clip. 

percentile : float 

Retain values within percentile of the median. 

 

Returns 

------- 

float, float 

The minimum and maximum values of the clipped data. 

""" 

lower_percentile = (100 - percentile) / 2.0 

upper_percentile = 100 - lower_percentile 

min_value = np.percentile(data, lower_percentile) 

max_value = np.percentile(data, upper_percentile) 

return min_value, max_value 

 

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

""" 

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

 

Parameters 

---------- 

RA1 : numpy.ndarray 

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

Dec1 : numpy.ndarray 

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

RAcen: float 

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

Deccen : float 

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

 

Returns 

------- 

numpy.ndarray, numpy.ndarray 

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

""" 

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

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

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

return x, y 

 

 

def radec2pix(nside, ra, dec): 

""" 

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

 

Parameters 

---------- 

nside : int 

The nside value of the healpix grid. 

ra : numpy.ndarray 

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

dec : numpy.ndarray 

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

 

Returns 

------- 

numpy.ndarray 

The healpix ids. 

""" 

lat = np.pi/2. - dec 

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

return hpid