Skip to content

elapid.stats

Utilities for calculating zonal stats and other transformations

RasterStat

Utility class to iterate over and apply reductions to multiband arrays

Source code in elapid/stats.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
class RasterStat:
    """Utility class to iterate over and apply reductions to multiband arrays"""

    def __init__(self, name: str, method: Callable, dtype: str = None, **kwargs):
        """Create a RasterStat object

        Args:
            name: the label to prepend to the output column
            method: function to reduce a 2d ndarray to an (nbands,) shape array
            dtype: the output array data type
            **kwargs: additional arguments to pass to `method`
        """
        self.name = name
        self.method = method
        self.dtype = dtype
        self.kwargs = kwargs

    def format(self, x: np.ndarray) -> np.ndarray:
        """Format the array data into an array of shape [nbands, n_valid_pixels]

        Args:
            x: ndarray of shape (nbands, nrows, ncols) or (nbands, n_valid_pixels)

        Returns:
            2d ndarray
        """
        if x.ndim == 3:
            bands, rows, cols = x.shape
            x = x.reshape((bands, rows * cols))
        return x

    def reduce(self, x: np.ndarray) -> np.ndarray:
        """Reduce an array using the objects `method` function

        Args:
            x: ndarray of shape (nbands, n_valid_pixels)

        Returns:
            ndarray of shape (nbands,)
        """
        return self.method(self.format(x), **self.kwargs)

__init__(name, method, dtype=None, **kwargs)

Create a RasterStat object

Parameters:

Name Type Description Default
name str

the label to prepend to the output column

required
method Callable

function to reduce a 2d ndarray to an (nbands,) shape array

required
dtype str

the output array data type

None
**kwargs

additional arguments to pass to method

{}
Source code in elapid/stats.py
14
15
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, name: str, method: Callable, dtype: str = None, **kwargs):
    """Create a RasterStat object

    Args:
        name: the label to prepend to the output column
        method: function to reduce a 2d ndarray to an (nbands,) shape array
        dtype: the output array data type
        **kwargs: additional arguments to pass to `method`
    """
    self.name = name
    self.method = method
    self.dtype = dtype
    self.kwargs = kwargs

format(x)

Format the array data into an array of shape [nbands, n_valid_pixels]

Parameters:

Name Type Description Default
x np.ndarray

ndarray of shape (nbands, nrows, ncols) or (nbands, n_valid_pixels)

required

Returns:

Type Description
np.ndarray

2d ndarray

Source code in elapid/stats.py
28
29
30
31
32
33
34
35
36
37
38
39
40
def format(self, x: np.ndarray) -> np.ndarray:
    """Format the array data into an array of shape [nbands, n_valid_pixels]

    Args:
        x: ndarray of shape (nbands, nrows, ncols) or (nbands, n_valid_pixels)

    Returns:
        2d ndarray
    """
    if x.ndim == 3:
        bands, rows, cols = x.shape
        x = x.reshape((bands, rows * cols))
    return x

reduce(x)

Reduce an array using the objects method function

Parameters:

Name Type Description Default
x np.ndarray

ndarray of shape (nbands, n_valid_pixels)

required

Returns:

Type Description
np.ndarray

ndarray of shape (nbands,)

Source code in elapid/stats.py
42
43
44
45
46
47
48
49
50
51
def reduce(self, x: np.ndarray) -> np.ndarray:
    """Reduce an array using the objects `method` function

    Args:
        x: ndarray of shape (nbands, n_valid_pixels)

    Returns:
        ndarray of shape (nbands,)
    """
    return self.method(self.format(x), **self.kwargs)

get_raster_stats_methods(mean=True, min=False, max=False, count=False, sum=False, stdv=False, skew=False, kurtosis=False, mode=False, percentiles=[], all=False)

Return RasterStat configs for the requested stats calculations

Source code in elapid/stats.py
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
def get_raster_stats_methods(
    mean: bool = True,
    min: bool = False,
    max: bool = False,
    count: bool = False,
    sum: bool = False,
    stdv: bool = False,
    skew: bool = False,
    kurtosis: bool = False,
    mode: bool = False,
    percentiles: list = [],
    all: bool = False,
) -> List[RasterStat]:
    """Return RasterStat configs for the requested stats calculations"""
    methods = []

    if mean or all:
        methods.append(RasterStat(name="mean", method=raster_mean, dtype="float32"))

    if min or all:
        methods.append(RasterStat(name="min", method=raster_min))

    if max or all:
        methods.append(RasterStat(name="max", method=raster_max))

    if count or all:
        methods.append(RasterStat(name="count", method=raster_count, dtype="int16"))

    if sum or all:
        methods.append(RasterStat(name="sum", method=raster_sum))

    if stdv or all:
        methods.append(RasterStat(name="stdv", method=raster_stdv))

    if skew or all:
        methods.append(RasterStat(name="skew", method=raster_skew))

    if kurtosis or all:
        methods.append(RasterStat(name="kurt", method=raster_kurtosis))

    if mode or all:
        methods.append(RasterStat(name="mode", method=raster_mode))

    if len(percentiles) > 0:
        for percentile in percentiles:
            methods.append(RasterStat(name=f"{percentile}pct", method=raster_percentile, pctile=percentile))

    return methods

normalize_sample_probabilities(values)

Compute scaled probability scores for an array of samples.

These normalized probabilities sum to 1.0 and are designed to be used as the p parameter for determining sample probabilities in np.random.choice

Parameters:

Name Type Description Default
values ArrayLike

numeric array to be treated as sample probabilities. probabilities will be scaled linearly based on the input range. setting values = np.array([2, 1, 1]) returns (0.5, 0.25, 0.25)

required

Returns:

Type Description
np.ndarray

sample probabiility scores

Source code in elapid/stats.py
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
def normalize_sample_probabilities(values: ArrayLike) -> np.ndarray:
    """Compute scaled probability scores for an array of samples.

    These normalized probabilities sum to 1.0 and are designed to be used
        as the `p` parameter for determining sample probabilities in
        np.random.choice

    Args:
        values: numeric array to be treated as sample probabilities.
            probabilities will be scaled linearly based on the input range.
            setting `values = np.array([2, 1, 1])` returns (0.5, 0.25, 0.25)

    Returns:
        sample probabiility scores
    """
    return values / np.linalg.norm(values, ord=1)