nanopyx.core.analysis.estimate_shift

 1import numpy as np
 2from scipy.optimize import minimize
 3
 4from ..transform._interpolation import cr_interpolate
 5
 6
 7class GetMaxOptimizer(object):
 8    """
 9    Class GetMaxOptimizer, used to extract the maximum value from a cross correlation matrix with subpixel precision.
10    """
11
12    def __init__(self, slice_ccm) -> None:
13        """
14        Creates an instance of GetMaxOptimizer.
15        :param slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel
16        precision.
17        """
18        self.slice_ccm = slice_ccm
19
20    def get_interpolated_px_value(self, coords):
21        """
22        Method to be used for calculating the interpolated values of cross correlation matrices.
23        :param coords: tuple of coordinates.
24        :return: float; value of cross correlation matrix at given coordinates.
25        For minimizer reasons -> negatives values become positive and positive become negative.
26        """
27        return -cr_interpolate(self.slice_ccm, coords[0], coords[1])
28
29    def get_max(self):
30        """
31        Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.
32        :return: tuple; coordinates of maximum value of ccm with subpixel precision
33        """
34        y_max, x_max = np.unravel_index(self.slice_ccm.argmax(), self.slice_ccm.shape)
35        minimizer = minimize(
36            self.get_interpolated_px_value, (y_max, x_max), method="Nelder-Mead", options={"maxiter": 1000}
37        )
38        return minimizer.x
class GetMaxOptimizer:
 8class GetMaxOptimizer(object):
 9    """
10    Class GetMaxOptimizer, used to extract the maximum value from a cross correlation matrix with subpixel precision.
11    """
12
13    def __init__(self, slice_ccm) -> None:
14        """
15        Creates an instance of GetMaxOptimizer.
16        :param slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel
17        precision.
18        """
19        self.slice_ccm = slice_ccm
20
21    def get_interpolated_px_value(self, coords):
22        """
23        Method to be used for calculating the interpolated values of cross correlation matrices.
24        :param coords: tuple of coordinates.
25        :return: float; value of cross correlation matrix at given coordinates.
26        For minimizer reasons -> negatives values become positive and positive become negative.
27        """
28        return -cr_interpolate(self.slice_ccm, coords[0], coords[1])
29
30    def get_max(self):
31        """
32        Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.
33        :return: tuple; coordinates of maximum value of ccm with subpixel precision
34        """
35        y_max, x_max = np.unravel_index(self.slice_ccm.argmax(), self.slice_ccm.shape)
36        minimizer = minimize(
37            self.get_interpolated_px_value, (y_max, x_max), method="Nelder-Mead", options={"maxiter": 1000}
38        )
39        return minimizer.x

Class GetMaxOptimizer, used to extract the maximum value from a cross correlation matrix with subpixel precision.

GetMaxOptimizer(slice_ccm)
13    def __init__(self, slice_ccm) -> None:
14        """
15        Creates an instance of GetMaxOptimizer.
16        :param slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel
17        precision.
18        """
19        self.slice_ccm = slice_ccm

Creates an instance of GetMaxOptimizer.

Parameters
  • slice_ccm: numpy array with shape (y, x); ccm from which to extract the maximum value with subpixel precision.
slice_ccm
def get_interpolated_px_value(self, coords):
21    def get_interpolated_px_value(self, coords):
22        """
23        Method to be used for calculating the interpolated values of cross correlation matrices.
24        :param coords: tuple of coordinates.
25        :return: float; value of cross correlation matrix at given coordinates.
26        For minimizer reasons -> negatives values become positive and positive become negative.
27        """
28        return -cr_interpolate(self.slice_ccm, coords[0], coords[1])

Method to be used for calculating the interpolated values of cross correlation matrices.

Parameters
  • coords: tuple of coordinates.
Returns

float; value of cross correlation matrix at given coordinates. For minimizer reasons -> negatives values become positive and positive become negative.

def get_max(self):
30    def get_max(self):
31        """
32        Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.
33        :return: tuple; coordinates of maximum value of ccm with subpixel precision
34        """
35        y_max, x_max = np.unravel_index(self.slice_ccm.argmax(), self.slice_ccm.shape)
36        minimizer = minimize(
37            self.get_interpolated_px_value, (y_max, x_max), method="Nelder-Mead", options={"maxiter": 1000}
38        )
39        return minimizer.x

Method used to calculate the maximum value and corresponding coordinates of a ccm. Uses a minimizer approach.

Returns

tuple; coordinates of maximum value of ccm with subpixel precision