|
18 | 18 | from refnx._lib import flatten |
19 | 19 |
|
20 | 20 |
|
| 21 | +def circular_distance(angle1, angle2, period=2*np.pi): |
| 22 | + """ |
| 23 | + Calculates the circular distance between two angles. |
| 24 | +
|
| 25 | + Args: |
| 26 | + angle1 (float or np.ndarray): The first angle(s) in radians. |
| 27 | + angle2 (float or np.ndarray): The second angle(s) in radians. |
| 28 | + period (float): The period of the circular domain (e.g., 2*np.pi for full circle). |
| 29 | +
|
| 30 | + Returns: |
| 31 | + float or np.ndarray: The shortest circular distance between the angles. |
| 32 | + """ |
| 33 | + diff = np.abs(angle1 - angle2) |
| 34 | + return np.minimum(diff, period - diff) |
| 35 | + |
| 36 | + |
21 | 37 | class ObjectiveSE(BaseObjective): |
22 | 38 | """ |
23 | 39 | Objective function for using with curvefitters such as |
@@ -179,7 +195,7 @@ def residuals(self, pvals=None): |
179 | 195 | wavelength, aoi, psi_d, delta_d = self.data.data |
180 | 196 | wavelength_aoi = np.c_[wavelength, aoi] |
181 | 197 | psi, delta = self.model(wavelength_aoi) |
182 | | - delta_err = (delta - delta_d + 180) % 360 - 180 |
| 198 | + delta_err = circular_distance(delta, delta_d, period=360) |
183 | 199 | return np.r_[psi - psi_d, delta_err] |
184 | 200 |
|
185 | 201 | def chisqr(self, pvals=None): |
|
0 commit comments