1010RADIUS_MM = 1
1111N_POINTS_ON_DIAMETER = 300
1212TTH_GRID = np .arange (1 , 180.1 , 0.1 )
13- # Round down the last element if it's slightly above 180 due to floating point precision
13+ # Round down the last element if it's slightly above 180.00
14+ # due to floating point precision
1415TTH_GRID [- 1 ] = 180.00
1516CVE_METHODS = ["brute_force" , "polynomial_interpolation" ]
1617
1718# Pre-computed datasets for polynomial interpolation (fast calculation)
1819MUD_LIST = [0.5 , 1 , 2 , 3 , 4 , 5 , 6 ]
1920CWD = Path (__file__ ).parent .resolve ()
2021MULS = np .loadtxt (CWD / "data" / "inverse_cve.xy" )
21- COEFFICIENT_LIST = np .array (pd .read_csv (CWD / "data" / "coefficient_list.csv" , header = None ))
22- INTERPOLATION_FUNCTIONS = [interp1d (MUD_LIST , coefficients , kind = "quadratic" ) for coefficients in COEFFICIENT_LIST ]
22+ COEFFICIENT_LIST = np .array (
23+ pd .read_csv (CWD / "data" / "coefficient_list.csv" , header = None )
24+ )
25+ INTERPOLATION_FUNCTIONS = [
26+ interp1d (MUD_LIST , coeffs , kind = "quadratic" ) for coeffs in COEFFICIENT_LIST
27+ ]
2328
2429
2530class Gridded_circle :
26- def __init__ (self , radius = 1 , n_points_on_diameter = N_POINTS_ON_DIAMETER , mu = None ):
31+ def __init__ (
32+ self , radius = 1 , n_points_on_diameter = N_POINTS_ON_DIAMETER , mu = None
33+ ):
2734 self .radius = radius
2835 self .npoints = n_points_on_diameter
2936 self .mu = mu
@@ -32,17 +39,23 @@ def __init__(self, radius=1, n_points_on_diameter=N_POINTS_ON_DIAMETER, mu=None)
3239 self ._get_grid_points ()
3340
3441 def _get_grid_points (self ):
35- """Given a radius and a grid size, return a grid of points to uniformly sample that circle."""
42+ """Given a radius and a grid size,
43+ return a grid of points to uniformly sample that circle."""
3644 xs = np .linspace (- self .radius , self .radius , self .npoints )
3745 ys = np .linspace (- self .radius , self .radius , self .npoints )
38- self .grid = {(x , y ) for x in xs for y in ys if x ** 2 + y ** 2 <= self .radius ** 2 }
46+ self .grid = {
47+ (x , y ) for x in xs for y in ys if x ** 2 + y ** 2 <= self .radius ** 2
48+ }
3949 self .total_points_in_grid = len (self .grid )
4050
4151 def _get_entry_exit_coordinates (self , coordinate , angle ):
42- """Get the coordinates where the beam enters and leaves the circle for a given angle and grid point.
52+ """Get the coordinates where the beam enters and leaves the circle
53+ for a given angle and grid point.
4354
4455 It is calculated in the following way:
45- For the entry coordinate, the y-component will be the y of the grid point and the x-component will be minus
56+ For the entry coordinate,
57+ the y-component will be the y of the grid point
58+ and the x-component will be minus
4659 the value of x on the circle at the height of this y.
4760
4861 For the exit coordinate:
@@ -53,7 +66,8 @@ def _get_entry_exit_coordinates(self, coordinate, angle):
5366 x^2 + (ax+b)^2 = r^2
5467 => x^2 + a^2x^2 + 2abx + b^2 - r^2 = 0
5568 => (1+a^2) x^2 + 2abx + (b^2 - r^2) = 0
56- to find x_exit we find the roots of these equations and pick the root that is above y-grid
69+ to find x_exit we find the roots of these equations
70+ and pick the root that is above y-grid
5771 then we get y_exit from y_exit = a*x_exit + b.
5872
5973 Parameters
@@ -67,8 +81,10 @@ def _get_entry_exit_coordinates(self, coordinate, angle):
6781 Returns
6882 -------
6983 (entry_point, exit_point): tuple of floats
70- (1) The coordinate of the entry point and (2) of the exit point of a beam entering horizontally
71- impinging on a coordinate point that lies in the circle and then exiting at some angle, angle.
84+ (1) The coordinate of the entry point and
85+ (2) of the exit point of a beam entering horizontally
86+ impinging on a coordinate point that lies in the circle
87+ and then exiting at some angle, angle.
7288 """
7389 epsilon = 1e-7 # precision close to 90
7490 angle = math .radians (angle )
@@ -80,7 +96,9 @@ def _get_entry_exit_coordinates(self, coordinate, angle):
8096 if not math .isclose (angle , math .pi / 2 , abs_tol = epsilon ):
8197 b = ygrid - xgrid * math .tan (angle )
8298 a = math .tan (angle )
83- xexit_root1 , xexit_root2 = np .roots ((1 + a ** 2 , 2 * a * b , b ** 2 - self .radius ** 2 ))
99+ xexit_root1 , xexit_root2 = np .roots (
100+ (1 + a ** 2 , 2 * a * b , b ** 2 - self .radius ** 2 )
101+ )
84102 yexit_root1 = a * xexit_root1 + b
85103 yexit_root2 = a * xexit_root2 + b
86104 if yexit_root2 >= yexit_root1 : # We pick the point above
@@ -93,8 +111,9 @@ def _get_entry_exit_coordinates(self, coordinate, angle):
93111 return entry_point , exit_point
94112
95113 def _get_path_length (self , grid_point , angle ):
96- """Return the path length of a horizontal line entering the circle at the
97- same height to the grid point then exiting at angle.
114+ """Return the path length of
115+ a horizontal line entering the circle at the same height
116+ to the grid point then exiting at angle.
98117
99118 Parameters
100119 ----------
@@ -107,10 +126,12 @@ def _get_path_length(self, grid_point, angle):
107126 Returns
108127 -------
109128 (total distance, primary distance, secondary distance): tuple of floats
110- The tuple containing three floats, which are the total distance, entry distance and exit distance.
129+ The tuple containing three floats,
130+ which are the total distance, entry distance and exit distance.
111131 """
112132
113- # move angle a tad above zero if it is zero to avoid it having the wrong sign due to some rounding error
133+ # move angle a tad above zero if it is zero
134+ # to avoid it having the wrong sign due to some rounding error
114135 angle_delta = 0.000001
115136 if angle == float (0 ):
116137 angle = angle + angle_delta
@@ -121,14 +142,17 @@ def _get_path_length(self, grid_point, angle):
121142 return total_distance , primary_distance , secondary_distance
122143
123144 def set_distances_at_angle (self , angle ):
124- """Given an angle, set the distances from the grid points to the entry and exit coordinates.
145+ """Given an angle, set the distances from the grid points
146+ to the entry and exit coordinates.
125147
126148 Parameters
127149 ----------
128150 angle : float
129151 The angle of the output beam in degrees.
130152 """
131- self .primary_distances , self .secondary_distances , self .distances = [], [], []
153+ self .primary_distances = []
154+ self .secondary_distances = []
155+ self .distances = []
132156 for coord in self .grid :
133157 distance , primary , secondary = self ._get_path_length (coord , angle )
134158 self .distances .append (distance )
@@ -152,7 +176,8 @@ def set_muls_at_angle(self, angle):
152176
153177
154178def _cve_brute_force (input_pattern , mud ):
155- """Compute cve for the given mud on a global grid using the brute-force method.
179+ """Compute cve for the given mud on a global grid
180+ using the brute-force method.
156181 Assume mu=mud/2, given that the same mu*D yields the same cve and D/2=1.
157182 """
158183 mu_sample_invmm = mud / 2
@@ -185,13 +210,22 @@ def _cve_polynomial_interpolation(input_pattern, mud):
185210 """
186211 if mud > 6 or mud < 0.5 :
187212 raise ValueError (
188- f"mu*D is out of the acceptable range (0.5 to 6) for polynomial interpolation. "
189- f"Please rerun with a value within this range or specifying another method from { * CVE_METHODS , } ."
213+ f"mu*D is out of the acceptable range (0.5 to 6) "
214+ f"for polynomial interpolation. "
215+ f"Please rerun with a value within this range "
216+ f"or specifying another method from { * CVE_METHODS , } ."
190217 )
191218 coeff_a , coeff_b , coeff_c , coeff_d , coeff_e = [
192- interpolation_function (mud ) for interpolation_function in INTERPOLATION_FUNCTIONS
219+ interpolation_function (mud )
220+ for interpolation_function in INTERPOLATION_FUNCTIONS
193221 ]
194- muls = np .array (coeff_a * MULS ** 4 + coeff_b * MULS ** 3 + coeff_c * MULS ** 2 + coeff_d * MULS + coeff_e )
222+ muls = np .array (
223+ coeff_a * MULS ** 4
224+ + coeff_b * MULS ** 3
225+ + coeff_c * MULS ** 2
226+ + coeff_d * MULS
227+ + coeff_e
228+ )
195229 cve = 1 / muls
196230
197231 cve_do = DiffractionObject (
@@ -213,21 +247,30 @@ def _cve_method(method):
213247 "polynomial_interpolation" : _cve_polynomial_interpolation ,
214248 }
215249 if method not in CVE_METHODS :
216- raise ValueError (f"Unknown method: { method } . Allowed methods are { * CVE_METHODS , } ." )
250+ raise ValueError (
251+ f"Unknown method: { method } . "
252+ f"Allowed methods are { * CVE_METHODS , } ."
253+ )
217254 return methods [method ]
218255
219256
220- def compute_cve (input_pattern , mud , method = "polynomial_interpolation" , xtype = "tth" ):
221- f"""Compute and interpolate the cve for the given input diffraction data and mu*D using the selected method.
257+ def compute_cve (
258+ input_pattern , mud , method = "polynomial_interpolation" , xtype = "tth"
259+ ):
260+ f"""Compute and interpolate the cve
261+ for the given input diffraction data and mu*D
262+ using the selected method.
222263
223264 Parameters
224265 ----------
225266 input_pattern : DiffractionObject
226267 The input diffraction object to which the cve will be applied.
227268 mud : float
228- The mu*D value of the diffraction object, where D is the diameter of the circle.
269+ The mu*D value of the diffraction object,
270+ where D is the diameter of the circle.
229271 xtype : str
230- The quantity on the independent variable axis, allowed values are { * XQUANTITIES , } .
272+ The quantity on the independent variable axis,
273+ allowed values are { * XQUANTITIES , } .
231274 method : str
232275 The method used to calculate cve, must be one of { * CVE_METHODS , } .
233276
@@ -255,7 +298,8 @@ def compute_cve(input_pattern, mud, method="polynomial_interpolation", xtype="tt
255298
256299
257300def apply_corr (input_pattern , absorption_correction ):
258- """Apply absorption correction to the given diffraction object with the correction diffraction object.
301+ """Apply absorption correction to the given diffraction object
302+ with the correction diffraction object.
259303
260304 Parameters
261305 ----------
@@ -267,7 +311,8 @@ def apply_corr(input_pattern, absorption_correction):
267311 Returns
268312 -------
269313 corrected_pattern: DiffractionObject
270- The corrected diffraction object with the correction applied through multiplication.
314+ The corrected diffraction object
315+ with the correction applied through multiplication.
271316 """
272317 corrected_pattern = input_pattern * absorption_correction
273318 return corrected_pattern
0 commit comments