Skip to content

class_rnds.py

Contains a class with strategy to trim a grid based on RNDS.

StrategyRndsTrimming

Bases: StrategyTrimming

A class to define strategies to trim a grid based on RNDS.

Inheritance

This class is the child of StrategyTrimming. Check this one for other
attributes and methods definitions.

Source code in src/smiffer/grid/trimming_strategy/class_rnds.py
 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
class StrategyRndsTrimming(StrategyTrimming):
    """A class to define strategies to trim a grid based on RNDS.

    Inheritance
    -----------
    This class is the child of `StrategyTrimming`. Check this one for other
    **attributes** and **methods** definitions.
    """

    def trim_box(self, grid_object):
        """Trim a grid mask based on RNDS trimming method.

        Parameters
        ----------
        grid_object : `Grid`
            The grid object to access all attributes.
        """
        # Setting max distance RNDS trimming.
        max_dist: float = grid_object.yaml["trimming_distance_rnds_maximum"]

        if max_dist is None:
            max_dist = np.inf

        # Compute all combinaison between [0, 1, -1].
        direction: np.ndarray = np.array(
            np.meshgrid([0, 1, -1], [0, 1, -1], [0, 1, -1])
        ).T.reshape(-1, 3)

        direction = np.delete(direction, [0, 0, 0], axis=0)

        if not grid_object.yaml["other_neighbour_system"]:
            direction = direction[~np.any(np.isin(direction, 0), axis=1)]

        low_mesh: np.ndarray = (
            grid_object.resolution / 2
            - grid_object.yaml["other_cog_cube_radius"]
        )

        high_mesh: np.ndarray = (
            grid_object.resolution / 2
            + grid_object.yaml["other_cog_cube_radius"]
            + 1
        )

        # Compute all combinaison between all coordinates.
        iterator: np.ndarray = (
            np.array(
                np.meshgrid(
                    np.arange(low_mesh[0], high_mesh[0]),
                    np.arange(low_mesh[1], high_mesh[1]),
                    np.arange(low_mesh[2], high_mesh[2]),
                )
            )
            .T.reshape(-1, 3)
            .astype(int)
        )

        # Filling with 0 based on the iterator.
        search_dist: np.ndarray = np.full(grid_object.resolution, np.inf)
        search_dist[tuple(iterator.T)] = 0

        cog_cube: np.ndarray = np.unique(iterator, axis=0)
        # Clean this huge numpy array.
        del iterator

        visited: np.ndarray = np.zeros(grid_object.resolution, dtype=bool)

        # While there is neighbors to visit.
        while cog_cube.shape[0] != 0:
            node = cog_cube[0]

            # Deleting the first items in cog_cub np.array.
            cog_cube = cog_cube[1:]

            visited[tuple(node)] = True

            neighbour = direction + node

            # Masking data that are not included between 0 and resolution.
            mask: np.ndarray = np.all(
                (np.array([0, 0, 0]) <= neighbour)
                & (neighbour < grid_object.resolution),
                axis=1,
            )

            # Filter neighbors.
            neighbour: tuple = tuple(neighbour[mask].T)

            search_dist[neighbour] = np.min(
                (
                    [search_dist[tuple(node)] + 1]
                    * search_dist[neighbour].shape[0],
                    search_dist[neighbour],
                ),
                axis=0,
            )

            # Skipping data not responding to these criteria:
            # 1. Checking that every value are strictly greater than
            #    self.max_dist, else skip.
            # 2. Checking that value have not been visited.
            # 3. Checking that the mask value are set to False.
            #
            # If any of this criteria are not respected, the return value is
            # False, so it is not added into the cog_cube np.array.
            mask: np.ndarray = (
                (search_dist[neighbour] > max_dist)
                | visited[neighbour]
                | grid_object.mask[neighbour]
            )

            # Appending selected data.
            cog_cube = np.vstack((cog_cube, np.array(neighbour).T[~mask]))
            visited[tuple(np.array(neighbour).T[~mask].T)] = True

        # Masking data.
        grid_object.mask[~visited] = True

trim_box(grid_object)

Trim a grid mask based on RNDS trimming method.

Parameters

grid_object : Grid
The grid object to access all attributes.

Source code in src/smiffer/grid/trimming_strategy/class_rnds.py
 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
def trim_box(self, grid_object):
    """Trim a grid mask based on RNDS trimming method.

    Parameters
    ----------
    grid_object : `Grid`
        The grid object to access all attributes.
    """
    # Setting max distance RNDS trimming.
    max_dist: float = grid_object.yaml["trimming_distance_rnds_maximum"]

    if max_dist is None:
        max_dist = np.inf

    # Compute all combinaison between [0, 1, -1].
    direction: np.ndarray = np.array(
        np.meshgrid([0, 1, -1], [0, 1, -1], [0, 1, -1])
    ).T.reshape(-1, 3)

    direction = np.delete(direction, [0, 0, 0], axis=0)

    if not grid_object.yaml["other_neighbour_system"]:
        direction = direction[~np.any(np.isin(direction, 0), axis=1)]

    low_mesh: np.ndarray = (
        grid_object.resolution / 2
        - grid_object.yaml["other_cog_cube_radius"]
    )

    high_mesh: np.ndarray = (
        grid_object.resolution / 2
        + grid_object.yaml["other_cog_cube_radius"]
        + 1
    )

    # Compute all combinaison between all coordinates.
    iterator: np.ndarray = (
        np.array(
            np.meshgrid(
                np.arange(low_mesh[0], high_mesh[0]),
                np.arange(low_mesh[1], high_mesh[1]),
                np.arange(low_mesh[2], high_mesh[2]),
            )
        )
        .T.reshape(-1, 3)
        .astype(int)
    )

    # Filling with 0 based on the iterator.
    search_dist: np.ndarray = np.full(grid_object.resolution, np.inf)
    search_dist[tuple(iterator.T)] = 0

    cog_cube: np.ndarray = np.unique(iterator, axis=0)
    # Clean this huge numpy array.
    del iterator

    visited: np.ndarray = np.zeros(grid_object.resolution, dtype=bool)

    # While there is neighbors to visit.
    while cog_cube.shape[0] != 0:
        node = cog_cube[0]

        # Deleting the first items in cog_cub np.array.
        cog_cube = cog_cube[1:]

        visited[tuple(node)] = True

        neighbour = direction + node

        # Masking data that are not included between 0 and resolution.
        mask: np.ndarray = np.all(
            (np.array([0, 0, 0]) <= neighbour)
            & (neighbour < grid_object.resolution),
            axis=1,
        )

        # Filter neighbors.
        neighbour: tuple = tuple(neighbour[mask].T)

        search_dist[neighbour] = np.min(
            (
                [search_dist[tuple(node)] + 1]
                * search_dist[neighbour].shape[0],
                search_dist[neighbour],
            ),
            axis=0,
        )

        # Skipping data not responding to these criteria:
        # 1. Checking that every value are strictly greater than
        #    self.max_dist, else skip.
        # 2. Checking that value have not been visited.
        # 3. Checking that the mask value are set to False.
        #
        # If any of this criteria are not respected, the return value is
        # False, so it is not added into the cog_cube np.array.
        mask: np.ndarray = (
            (search_dist[neighbour] > max_dist)
            | visited[neighbour]
            | grid_object.mask[neighbour]
        )

        # Appending selected data.
        cog_cube = np.vstack((cog_cube, np.array(neighbour).T[~mask]))
        visited[tuple(np.array(neighbour).T[~mask].T)] = True

    # Masking data.
    grid_object.mask[~visited] = True