@@ -26,13 +26,13 @@ class PolynomialCoefficients(BaseModel):
2626 }
2727 labels = {'x':'ra', 'y':'dec'}
2828 """
29- coeffs : dict [str ,float ]
29+
30+ coeffs : dict [str , float ]
3031 labels : dict [str , str ]
3132 unit : AstroPydanticUnit = u .deg
3233 poly_order : int
3334
3435
35-
3636class PolynomialPointingModel (PointingModelProtocol ):
3737 model_type : Literal ["polynomial" ] = "polynomial"
3838
@@ -55,22 +55,23 @@ def _poly_keys(self):
5555 keys .append (f"x^{ i } y^{ j } " )
5656 return keys
5757
58- def build_model (self ,
59- measured_positions : SkyCoord ,
60- expected_positions : SkyCoord ,
61- weights : tuple [list [float ], list [float ]] | list [float ] | None = None
62- ):
58+ def build_model (
59+ self ,
60+ measured_positions : SkyCoord ,
61+ expected_positions : SkyCoord ,
62+ weights : tuple [list [float ], list [float ]] | list [float ] | None = None ,
63+ ):
6364 """
6465 Calculate and set the polynomial coefficients for the pointing model
6566 using the measured and expected positions.
6667
67- weights can be provided as a tuple of (ra_weights, dec_weights)
68+ weights can be provided as a tuple of (ra_weights, dec_weights)
6869 or a single list that applies to both.
69-
70+
7071
7172 Raises
7273 ------
73- ValueError
74+ ValueError
7475 If no positions are provided for model calculation.
7576 ValueError
7677 If the lengths of weights do not match the number of positions.
@@ -83,7 +84,7 @@ def build_model(self,
8384 n = len (ra_offsets )
8485 if n == 0 :
8586 raise ValueError ("No positions provided for model calculation." )
86-
87+
8788 # Unpack weights into ra_weights, dec_weights
8889 if isinstance (weights , tuple ):
8990 ra_weights , dec_weights = weights
@@ -102,8 +103,12 @@ def build_model(self,
102103
103104 ra_weights = np .asarray (ra_weights )
104105 dec_weights = np .asarray (dec_weights )
105- assert len (ra_weights ) == n , "Length of ra_weights must match number of positions"
106- assert len (dec_weights ) == n , "Length of dec_weights must match number of positions"
106+ assert len (ra_weights ) == n , (
107+ "Length of ra_weights must match number of positions"
108+ )
109+ assert len (dec_weights ) == n , (
110+ "Length of dec_weights must match number of positions"
111+ )
107112
108113 ras = measured_positions .ra .to_value (u .deg )
109114 decs = measured_positions .dec .to_value (u .deg )
@@ -125,25 +130,34 @@ def build_model(self,
125130 Aw = A_dec * w_dec [:, None ]
126131 yw = y_dec * w_dec
127132 coeffs_dec , * _ = np .linalg .lstsq (Aw , yw , rcond = None )
128-
129- ra_coeff_dict = {key : coeff for key , coeff in zip (self ._poly_keys (), coeffs_ra )}
130- dec_coeff_dict = {key : coeff for key , coeff in zip (self ._poly_keys (), coeffs_dec )}
131-
132- self .ra_model_coefficients = PolynomialCoefficients (coeffs = ra_coeff_dict , labels = {'x' :'ra' , 'y' :'dec' }, unit = u .deg , poly_order = self .poly_order )
133- self .dec_model_coefficients = PolynomialCoefficients (coeffs = dec_coeff_dict , labels = {'x' :'ra' , 'y' :'dec' }, unit = u .deg , poly_order = self .poly_order )
134-
135133
136- def model_fn (
137- self , x : u .Quantity , y : u .Quantity , coeffs : np .ndarray
138- ) -> u .Quantity :
134+ ra_coeff_dict = {key : coeff for key , coeff in zip (self ._poly_keys (), coeffs_ra )}
135+ dec_coeff_dict = {
136+ key : coeff for key , coeff in zip (self ._poly_keys (), coeffs_dec )
137+ }
138+
139+ self .ra_model_coefficients = PolynomialCoefficients (
140+ coeffs = ra_coeff_dict ,
141+ labels = {"x" : "ra" , "y" : "dec" },
142+ unit = u .deg ,
143+ poly_order = self .poly_order ,
144+ )
145+ self .dec_model_coefficients = PolynomialCoefficients (
146+ coeffs = dec_coeff_dict ,
147+ labels = {"x" : "ra" , "y" : "dec" },
148+ unit = u .deg ,
149+ poly_order = self .poly_order ,
150+ )
151+
152+ def model_fn (self , x : u .Quantity , y : u .Quantity , coeffs : np .ndarray ) -> u .Quantity :
139153 x = np .atleast_1d (x .to_value (u .deg ))
140154 y = np .atleast_1d (y .to_value (u .deg ))
141155 T = self ._poly_terms (x , y )
142156 return (T @ coeffs ) * u .deg
143-
157+
144158 def extract_coefficients (self ) -> tuple [np .ndarray , np .ndarray ]:
145159 """
146- Extract the coefficients from the PolynomialCoefficients dataclasss and
160+ Extract the coefficients from the PolynomialCoefficients dataclasss and
147161 return them as arrays in the correct order for the model function.
148162
149163 Raises
@@ -153,26 +167,26 @@ def extract_coefficients(self) -> tuple[np.ndarray, np.ndarray]:
153167 """
154168 if self .ra_model_coefficients is None or self .dec_model_coefficients is None :
155169 raise ValueError ("Model coefficients have not been calculated yet." )
156-
170+
157171 ra_coeff_array = np .zeros (len (self .ra_model_coefficients .coeffs ))
158172 for i , key in enumerate (self ._poly_keys ()):
159173 ra_coeff_array [i ] = self .ra_model_coefficients .coeffs .get (key , 0 )
160-
174+
161175 dec_coeff_array = np .zeros (len (self .dec_model_coefficients .coeffs ))
162176 for i , key in enumerate (self ._poly_keys ()):
163177 dec_coeff_array [i ] = self .dec_model_coefficients .coeffs .get (key , 0 )
164-
178+
165179 return ra_coeff_array , dec_coeff_array
166-
180+
167181 def predict (self , pos : SkyCoord ) -> SkyCoord :
168- racoeffs ,deccoeffs = self .extract_coefficients ()
182+ racoeffs , deccoeffs = self .extract_coefficients ()
169183 ra_offset = self .model_fn (pos .ra , pos .dec , racoeffs )
170184 dec_offset = self .model_fn (pos .ra , pos .dec , deccoeffs )
171185 ra = pos .ra - ra_offset
172186 dec = pos .dec - dec_offset
173187
174188 return SkyCoord (ra = ra , dec = dec , frame = pos .frame )
175-
189+
176190 def calculate_statistics (self , positions : SkyCoord ):
177191 new_positions = self .predict (positions )
178192 ra_residuals = (new_positions .ra - positions .ra ).to (u .arcsec )
@@ -186,4 +200,4 @@ def calculate_statistics(self, positions: SkyCoord):
186200 mean_dec_offset = mean_dec ,
187201 stddev_ra_offset = std_ra ,
188202 stddev_dec_offset = std_dec ,
189- )
203+ )
0 commit comments