Coverage for soxspipe/commonutils/polynomials.py : 100%

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*
6:Author:
7 David Young
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
22class chebyshev_order_wavelength_polynomials():
23 """*the chebyshev polynomial fits for the single pinhole frames; to be iteratively fitted to minimise errors*
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
31 **Usage:**
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 """
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
52 return None
54 def poly(self, lineList, *coeff):
55 """the polynomial definition
57 **Key Arguments:**
58 - ``lineList`` -- a pandas dataframe containing wavelengths, orders and slit positions
59 - ``*coeff`` -- a list of the initial coefficients
61 **Return:**
62 - ``lhsVals`` -- the left-hand-side vals of the fitted polynomials
63 """
64 self.log.debug('starting the ``poly`` method')
66 # UNPACK TUPLE INPUT
67 order_deg = self.order_deg
68 wavelength_deg = self.wavelength_deg
69 slit_deg = self.slit_deg
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)
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
87 self.log.debug('completed the ``poly`` method')
89 return lhsVals
92class chebyshev_xy_polynomial():
93 """*the chebyshev polynomial fits for the pinhole flat frame order tracing; to be iteratively fitted to minimise errors*
95 **Key Arguments:**
96 - ``log`` -- logger
97 - ``deg`` -- degree of the polynomial components
99 **Usage:**
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 """
108 def __init__(
109 self,
110 log,
111 deg
112 ):
113 self.log = log
114 self.deg = deg
116 return None
118 def poly(self, yarray, *coeff):
119 """the polynomial definition
121 **Key Arguments:**
122 - ``yarray`` -- the y coordinates
123 - ``*coeff`` -- a list of the initial coefficients
125 **Return:**
126 - ``xvals`` -- the x values of the fitted polynomial
127 """
128 self.log.info('starting the ``poly`` method')
130 xvals = []
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)
142 self.log.info('completed the ``poly`` method')
144 return xvals