sgt.core module¶
Core tools for scattering geometry calculations.
- sgt.core.make_rotation_matrix(alpha_deg: float, beta_deg: float, gamma_deg: float) ndarray [source]¶
Makes a rotation matrix that defines orientation of the detector.
The rotation matrix is calculated based on the given Euler angles. The Euler angles here is the x-y-x type, that is,
The initial axes \((x, y, z)\) are rotated by alpha_deg around the \(x\) axis.
The resultant axes \((x', y', z')\) are rotated by beta_deg around the \(y'\) axis.
The resultant axes \((x'', y'', z'')\) are rotated by gamma_deg around the \(x''\) axis.
- Parameters:
alpha_deg – Rotation around the \(x\) axis, in degrees.
beta_deg – Rotation around the \(y'\) axis, in degrees.
gamma_deg – Rotation around the \(x''\) axis, in degrees.
- Returns:
A 3x3 numpy array.
Example
>>> R = make_rotation_matrix(10.0, 10.0, 10.0) >>> vec = np.array([1.0, 2.0, 3.0]) >>> rotated = np.matmul(R, vec)
- sgt.core.make_pixel_coords_in_detector_system(hor_px_num: int, ver_px_num: int, px_width: float, px_height: float, center_coord_hor: float, center_coord_ver: float) Tuple[ndarray, ndarray] [source]¶
Makes the matrices of coordinates of each pixel on the 2D detector coordinate system.
The detector coordinate system is a 2D Cartesian coordinate system whose origin is at the image center (= where the direct beam hits the detector plane). Two axes, denoted as u and v, are defined to be parallel to the horizontal and vertical edges of the detector, respectively.
- Parameters:
hor_px_num – Number of pixels along the horizontal axis.
ver_px_num – Number of pixels along the vertical axis.
px_width – Size of a single pixel along the horizontal axis.
px_height – Size of a single pixel along the horizontal axis.
center_coord_hor – Horizontal coordinate of the image center measured from the center of pixel at index [0,0].
center_coord_ver – Vertical coordinate of the image center.
- Returns:
2D numpy arrays of the horizontal and vertical coordinates.
Example
>>> u, v = make_pixel_coords_in_detector_system(1475, 1679, 0.172, 0.172, 132.35, 134.39)
- sgt.core.make_default_mask(hor_px_num: int, ver_px_num: int) ndarray [source]¶
Makes a default mask array.
A mask array is a 2D array of the same shape as the scattering image but of numpy.uint8 type. Pixels to be masked are assigned with 1 and unmasked pixels are assigned with zero.
- Parameters:
hor_px_num – Number of pixels along the horizontal edge.
ver_px_num – Number of pixels along the horizontal edge.
- Returns:
A 2D numpy array of the dtype numpy.uint8.
- sgt.core.make_default_array(hor_px_num: int, ver_px_num: int) ndarray [source]¶
Makes a default float array.
- Parameters:
hor_px_num – Number of pixels along the horizontal edge.
ver_px_num – Number of pixels along the horizontal edge.
- Returns:
A 2D numpy array of the float type.
- sgt.core.make_basis_vectors_on_detector_in_lab_system(rotation_matrix: ndarray) Tuple[ndarray, ndarray, ndarray] [source]¶
Makes the basis vectors of the detector coordinate system, expressed in the lab coordinate system.
For the definition of the detector coordinate system, refer to
make_pixel_coords_in_detector_system()
. The basis vectors of the detector coordinate system is basically the basis vectors of the lab coordinate system being rotated by the rotation matrix that defines the detector orientation. The input rotation matrix can be created bymake_rotation_matrix()
. (but actually can be any SO(3) matrix)- Parameters:
rotation_matrix – A 3x3 numpy array representing the detector orientation.
- Returns:
Three numpy arrays a, b, and n representing the basis vectors. a and b are the basis vector along the horizontal and vertical edge of the detector, respectively, and n is the one perpendicular to the detector plane.
- sgt.core.make_pixel_coords_in_lab_system(xcoords_det: ndarray, ycoords_det: ndarray, a: ndarray, b: ndarray, n: ndarray, sdd: float) Tuple[ndarray, ndarray, ndarray] [source]¶
- sgt.core.calc_shortest_dist_to_detector(a: ndarray, b: ndarray, n: ndarray, sdd: float) float [source]¶
Computes the shortest distance from the sample to the detector plane.
Let P be the point on the detector such that OP is the shortest distance between the origin and the detector plane. The vector OP must be perpendicular to the detector plane, so the vector \(\vec{\mathrm{OP}}\) is proportional to the detector plane normal vector \(\vec{n}\). That is,
\[\vec{\mathrm{OP}} = \mathrm{OP} \vec{n}\]The vector OP can also be expressed as
\[\vec{\mathrm{OP}} = u\vec{a} + v\vec{b} + L\vec{e}_z\]where \((u, v)\) is the coordinate of point P on the detector coordinate system and vector \(\vec{e}_z\) is the z basis vector. \(L\) is the sample-to-detector distance.
Equating the two expressions,
\[k\vec{n} = u\vec{a} + v\vec{b} + L\vec{e}_z\]which reads
\[\begin{split}u a_x + v b_x - \mathrm{OP} n_x &= 0 \\ u a_y + v b_y - \mathrm{OP} n_y &= 0 \\ u a_z + v b_z - \mathrm{OP} n_z &= -L\end{split}\]By defining a matrix
\[\begin{split}\mathbf{M} = \begin{pmatrix} a_x & b_x & -n_x \\ a_y & b_y & -n_y \\ a_z & b_z & -n_z \\ \end{pmatrix}\end{split}\]the equations are simplified to
\[\begin{split}\mathbf{M} \vec{s} &= -L \vec{e}_z \\ \vec{s} &= -L \mathbf{M}^{-1}\vec{e}_z\end{split}\]where \(\vec{s} = (u, v, \mathrm{OP})\). This method computes \(\vec{s}\) using the above equation and returns its third component, \(\mathrm{OP}\).
- Parameters:
a – basis vector of the detector coordinate system along the horizontal edge of the detector.
b – basis vector of the detector coordinate system along the vertical edge of the detector.
n – basis vector of the detector coordinate system along the plane normal of the detector.
sdd – sample-to-detector distance.
- Returns:
The distance in the float value.
Note
The input vectors should be expressed in the lab coordinate system. Use
make_basis_vectors_on_detector_in_lab_system()
to generate the basis vectors.
- sgt.core.make_solid_angle_coverage_correction_factors(x: ndarray, y: ndarray, z: ndarray, shortest_dist_to_detector: float) ndarray [source]¶
Makes an array with the correction factor for solid angle coverage of each pixel.
Based on Equation 28 in Pauw J Phys.: Condens. Matter 25, 383201. DOI: 10.1088/0953-8984/25/38/383201.
- Parameters:
x – x coordinates of pixels in the lab system.
y – y coordinates of pixels in the lab system.
z – z coordinates of pixels in the lab system.
shortest_dist_to_detector – shortest distance from the sample to the detector plane. see
calc_shortest_dist_to_detector()
.
- Returns:
A 2D array of correction factors for each pixel. The correction factor is normalized at the beam center. The correction can be done by multiplying this array to the intensity array.
- sgt.core.make_q(x: ndarray, y: ndarray, z: ndarray, wavelength: float) Tuple[ndarray, ndarray, ndarray] [source]¶
Computes q vector.
By definition,
\[\vec{q} = \dfrac{2 \pi}{\lambda}(\vec{e}_\mathrm{s} - \vec{e}_\mathrm{i})\]where \(\lambda\) is the wavelength, \(\vec{e}_\mathrm{s}\) is the basis vector along the scattered ray, and \(\vec{e}_\mathrm{i}\) is the basis vector along the incident ray.
Here, \(\vec{e}_\mathrm{i}\) is fixed to (0, 0, 1). Since the sample is placed at the origin,
\[\vec{e}_\mathrm{s} = \dfrac{\vec{r}}{|\vec{r}|}\]where \(\vec{r}\) is the coordinate of the pixel in the lab system.
- Parameters:
x – x coordinates of pixels in the lab system.
y – y coordinates of pixels in the lab system.
z – z coordinates of pixels in the lab system.
wavelength – wavelength of the incident beam.
- Returns:
Three 2D arrays representing x, y, and z components of the q vector.
- sgt.core.calc_polar_map(qx: ndarray, qy: ndarray, qz: ndarray, mask: ndarray, qmin: float, qmax: float, N_q: int, N_azi: int) Tuple[ndarray, ndarray, ndarray, ndarray, ndarray] [source]¶
Calculates mapping to the polar coordinate system.
- Parameters:
qx – x component of q vector.
qy – y component of q vector.
qz – z component of q vector.
mask – mask array.
qmin – lower boundary of q.
qmax – upper boundary of q.
N_q – number of bins along the q axis.
N_azi – number of bins along the azimuthal axis. 360 deg is divided into N_azi sections.
- Returns:
Five numpy arrays, map_q, map_azi, density, ax_q, and ax_azi.