Skip to content

class_stamp.py

Contain a class to stamp gaussian on a given grid.

Stamp

A class to stamp kernel on a grid on a given position.

Attributes

self.__grid : np.ndarray
The grid on which to apply gaussian.

self.__resolution: np.ndarray
The grid resolution.

self.__grid_origin : np.ndarray
The grid origin. This corresponds to the coordinate in [0, 0, 0] in
the grid.

self.__delta : np.ndarray
The delta of the grid.

self.__kernel_resolution : np.ndarray
The resolution of the kernel.

self.__kernel : Kernel
The kernel.

Source code in src/smiffer/grid/class_stamp.py
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class Stamp:
    """A class to stamp kernel on a grid on a given position.

    Attributes
    ----------
    self.__grid : `np.ndarray`
        The grid on which to apply gaussian.

    self.__resolution: `np.ndarray`
            The grid resolution.

    self.__grid_origin : `np.ndarray`
        The grid origin. This corresponds to the coordinate in [0, 0, 0] in
        the grid.

    self.__delta : `np.ndarray`
        The delta of the grid.

    self.__kernel_resolution : `np.ndarray`
        The resolution of the kernel.

    self.__kernel : `Kernel`
        The kernel.
    """

    def __init__(
        self,
        grid: np.ndarray,
        grid_origin: np.ndarray,
        delta: np.ndarray,
        kernel,
    ):
        """Instantiates a kernel to apply gaussian on a given grid.

        Parameters
        ----------
        grid : `np.ndarray`
            The grid on which to apply gaussian.

        grid_origin : `np.ndarray`
            The grid origin. This corresponds to the coordinate in [0, 0, 0] in
            the grid.

        delta : `np.ndarray`
            The delta of the grid.

        kernel : `np.ndarray`
            The kernel to use.
        """
        self.__grid: np.ndarray = grid
        self.__resolution: np.ndarray = np.array(grid.shape)
        self.__grid_origin: np.ndarray = grid_origin

        self.__delta: np.ndarray = delta

        self.__kernel: np.ndarray = kernel.kernel()
        self.__kernel_resolution: np.ndarray = kernel.kernel_resolution()

    def refresh_orientation(self, kernel: np.ndarray):
        """Refresh the kernel orientation.

        Parameters
        ----------
        kernel : `np.ndarray`
            The new kernel to apply gaussian on the defined grid.
        """
        self.__kernel = kernel

    def stamp_kernel(self, center: np.ndarray, factor: float = None):
        """Stamp a kernel on a given position on the defined grid.

        Parameters
        ----------
        center : `np.ndarray`
            The position where to stamp the kernel.

        factor : `float`, optional
            A multiplication factor to change the kernel's gaussian intensity.
        """
        # Computing the origin on the grid to stamp the kernel on it.
        origin: np.ndarray = center.copy()
        origin -= self.__delta * self.__kernel_resolution / 2
        origin = np.round((origin - self.__grid_origin) / self.__delta).astype(
            int
        )

        grid_index: np.ndarray = np.array(
            [origin, origin + self.__kernel_resolution]
        )

        kernel_index: np.ndarray = np.array(
            [[0, 0, 0], self.__kernel_resolution]
        )

        # Switching between min and max indices.
        for i in [0, 1]:
            # Resizing the grid when the kernel is outside (smaller).
            array_filter: np.ndarray = grid_index[i] < 0
            kernel_index[i][array_filter] = -grid_index[i][array_filter]
            grid_index[i][array_filter] = 0

            # Resizing the grid when the kernel is outisde (bigger).
            array_filter: np.ndarray = grid_index[i] > self.__resolution
            kernel_index[i][array_filter] = (
                self.__resolution[array_filter] - grid_index[i][array_filter]
            )
            grid_index[i][array_filter] = self.__resolution[array_filter]

        # Applying computed indexes.
        grid: np.ndarray = self.__grid[
            grid_index[0][0] : grid_index[1][0],
            grid_index[0][1] : grid_index[1][1],
            grid_index[0][2] : grid_index[1][2],
        ]

        kernel: np.ndarray = self.__kernel[
            kernel_index[0][0] : kernel_index[1][0],
            kernel_index[0][1] : kernel_index[1][1],
            kernel_index[0][2] : kernel_index[1][2],
        ]

        # Stamping the kernel.
        if factor is not None:
            grid += kernel * factor
        else:
            grid += kernel

__init__(grid, grid_origin, delta, kernel)

Instantiates a kernel to apply gaussian on a given grid.

Parameters

grid : np.ndarray
The grid on which to apply gaussian.

np.ndarray

The grid origin. This corresponds to the coordinate in [0, 0, 0] in
the grid.

np.ndarray

The delta of the grid.

np.ndarray

The kernel to use.

Source code in src/smiffer/grid/class_stamp.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def __init__(
    self,
    grid: np.ndarray,
    grid_origin: np.ndarray,
    delta: np.ndarray,
    kernel,
):
    """Instantiates a kernel to apply gaussian on a given grid.

    Parameters
    ----------
    grid : `np.ndarray`
        The grid on which to apply gaussian.

    grid_origin : `np.ndarray`
        The grid origin. This corresponds to the coordinate in [0, 0, 0] in
        the grid.

    delta : `np.ndarray`
        The delta of the grid.

    kernel : `np.ndarray`
        The kernel to use.
    """
    self.__grid: np.ndarray = grid
    self.__resolution: np.ndarray = np.array(grid.shape)
    self.__grid_origin: np.ndarray = grid_origin

    self.__delta: np.ndarray = delta

    self.__kernel: np.ndarray = kernel.kernel()
    self.__kernel_resolution: np.ndarray = kernel.kernel_resolution()

refresh_orientation(kernel)

Refresh the kernel orientation.

Parameters

kernel : np.ndarray
The new kernel to apply gaussian on the defined grid.

Source code in src/smiffer/grid/class_stamp.py
69
70
71
72
73
74
75
76
77
def refresh_orientation(self, kernel: np.ndarray):
    """Refresh the kernel orientation.

    Parameters
    ----------
    kernel : `np.ndarray`
        The new kernel to apply gaussian on the defined grid.
    """
    self.__kernel = kernel

stamp_kernel(center, factor=None)

Stamp a kernel on a given position on the defined grid.

Parameters

center : np.ndarray
The position where to stamp the kernel.

float, optional

A multiplication factor to change the kernel's gaussian intensity.

Source code in src/smiffer/grid/class_stamp.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def stamp_kernel(self, center: np.ndarray, factor: float = None):
    """Stamp a kernel on a given position on the defined grid.

    Parameters
    ----------
    center : `np.ndarray`
        The position where to stamp the kernel.

    factor : `float`, optional
        A multiplication factor to change the kernel's gaussian intensity.
    """
    # Computing the origin on the grid to stamp the kernel on it.
    origin: np.ndarray = center.copy()
    origin -= self.__delta * self.__kernel_resolution / 2
    origin = np.round((origin - self.__grid_origin) / self.__delta).astype(
        int
    )

    grid_index: np.ndarray = np.array(
        [origin, origin + self.__kernel_resolution]
    )

    kernel_index: np.ndarray = np.array(
        [[0, 0, 0], self.__kernel_resolution]
    )

    # Switching between min and max indices.
    for i in [0, 1]:
        # Resizing the grid when the kernel is outside (smaller).
        array_filter: np.ndarray = grid_index[i] < 0
        kernel_index[i][array_filter] = -grid_index[i][array_filter]
        grid_index[i][array_filter] = 0

        # Resizing the grid when the kernel is outisde (bigger).
        array_filter: np.ndarray = grid_index[i] > self.__resolution
        kernel_index[i][array_filter] = (
            self.__resolution[array_filter] - grid_index[i][array_filter]
        )
        grid_index[i][array_filter] = self.__resolution[array_filter]

    # Applying computed indexes.
    grid: np.ndarray = self.__grid[
        grid_index[0][0] : grid_index[1][0],
        grid_index[0][1] : grid_index[1][1],
        grid_index[0][2] : grid_index[1][2],
    ]

    kernel: np.ndarray = self.__kernel[
        kernel_index[0][0] : kernel_index[1][0],
        kernel_index[0][1] : kernel_index[1][1],
        kernel_index[0][2] : kernel_index[1][2],
    ]

    # Stamping the kernel.
    if factor is not None:
        grid += kernel * factor
    else:
        grid += kernel