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#!/usr/bin/env python 

2# encoding: utf-8 

3""" 

4*definition of polynomial functions needed throughout code* 

5 

6:Author: 

7 David Young 

8 

9:Date Created: 

10 September 10, 2020 

11""" 

12################# GLOBAL IMPORTS #################### 

13from builtins import object 

14import sys 

15import os 

16os.environ['TERM'] = 'vt100' 

17from fundamentals import tools 

18import math 

19import numpy as np 

20 

21 

22class chebyshev_order_wavelength_polynomials(): 

23 """*the chebyshev polynomial fits for the single pinhole frames; to be iteratively fitted to minimise errors* 

24 

25 **Key Arguments:** 

26 - ``log`` -- logger 

27 - ``order_deg`` -- degree of the order polynomial components 

28 - ``wavelength_deg`` -- degree of wavelength polynomial components 

29 - ``slit_deg`` -- degree of the slit polynomial components 

30 

31 **Usage:** 

32 

33 ```python 

34 from soxspipe.commonutils.polynomials import chebyshev_order_wavelength_polynomials 

35 poly = chebyshev_order_wavelength_polynomials( 

36 log=self.log, order_deg=order_deg, wavelength_deg=wavelength_deg, slit_deg=slit_deg).poly 

37 ``` 

38 """ 

39 

40 def __init__( 

41 self, 

42 log, 

43 order_deg, 

44 wavelength_deg, 

45 slit_deg 

46 ): 

47 self.log = log 

48 self.order_deg = order_deg 

49 self.wavelength_deg = wavelength_deg 

50 self.slit_deg = slit_deg 

51 

52 return None 

53 

54 def poly(self, lineList, *coeff): 

55 """the polynomial definition 

56 

57 **Key Arguments:** 

58 - ``lineList`` -- a pandas dataframe containing wavelengths, orders and slit positions 

59 - ``*coeff`` -- a list of the initial coefficients 

60 

61 **Return:** 

62 - ``lhsVals`` -- the left-hand-side vals of the fitted polynomials 

63 """ 

64 self.log.debug('starting the ``poly`` method') 

65 

66 # UNPACK TUPLE INPUT 

67 order_deg = self.order_deg 

68 wavelength_deg = self.wavelength_deg 

69 slit_deg = self.slit_deg 

70 

71 # lhsVals = np.sum([v * c for v, c in zip([lineList["Order"].values**i * lineList["Wavelength"].values**j * lineList["slit_position"].values**k for i in range(0, order_deg + 1) 

72 # for j in range(0, wavelength_deg + 1) for k in range(0, slit_deg + 

73 # 1)], coeff)], axis=0) 

74 

75 # THE LIST COMPREHENSION ABOVE DID NOT SPEED UP THE NEST LOOPS BELOW - 

76 # KEEPING LOOPS! 

77 n_coeff = 0 

78 lhsVals = np.zeros(len(lineList.index)) 

79 for i in range(0, order_deg + 1): 

80 for j in range(0, wavelength_deg + 1): 

81 for k in range(0, slit_deg + 1): 

82 lhsVals += coeff[n_coeff] * lineList["Order"].values**i * \ 

83 lineList["Wavelength"].values**j * \ 

84 lineList["slit_position"].values**k 

85 n_coeff += 1 

86 

87 self.log.debug('completed the ``poly`` method') 

88 

89 return lhsVals 

90 

91 

92class chebyshev_xy_polynomial(): 

93 """*the chebyshev polynomial fits for the pinhole flat frame order tracing; to be iteratively fitted to minimise errors* 

94 

95 **Key Arguments:** 

96 - ``log`` -- logger 

97 - ``deg`` -- degree of the polynomial components 

98 

99 **Usage:** 

100 

101 ```python 

102 from soxspipe.commonutils.polynomials import chebyshev_xy_polynomial 

103 poly = chebyshev_xy_polynomial( 

104 log=self.log, deg=deg).poly 

105 ``` 

106 """ 

107 

108 def __init__( 

109 self, 

110 log, 

111 deg 

112 ): 

113 self.log = log 

114 self.deg = deg 

115 

116 return None 

117 

118 def poly(self, yarray, *coeff): 

119 """the polynomial definition 

120 

121 **Key Arguments:** 

122 - ``yarray`` -- the y coordinates 

123 - ``*coeff`` -- a list of the initial coefficients 

124 

125 **Return:** 

126 - ``xvals`` -- the x values of the fitted polynomial 

127 """ 

128 self.log.info('starting the ``poly`` method') 

129 

130 xvals = [] 

131 

132 # POLYNOMIALS SUMS 

133 for y in yarray: 

134 n_coeff = 0 

135 val = 0 

136 for i in range(0, self.deg + 1): 

137 val += coeff[n_coeff] * \ 

138 math.pow(y, i) 

139 n_coeff += 1 

140 xvals.append(val) 

141 

142 self.log.info('completed the ``poly`` method') 

143 

144 return xvals