geovalidate.StratifiedClassSampler

class geovalidate.StratifiedClassSampler(n_samples=500, quasi_random=None, random_state=None)[source]

Sample n_samples points total, allocated proportionally across classes.

When weights are provided the allocation for each class is proportional to the sum of the weight values within that class. The integer counts are resolved by the largest-remainder (Hamilton) method so the total equals n_samples exactly.

When weights are omitted the sampler falls back to uniform random sampling over the full union of all class geometries and then assigns labels by spatial containment – equivalent to PointSampler.

Parameters:
n_samples : int, default 500

quasi_random : str or None

random_state : int, RandomState instance, or None

Examples

GeoDataFrame path:

pts = StratifiedClassSampler(n_samples=500).sample(
    gdf.geometry, gdf["lc_class"], gdf["area_ha"]
)

Raster path – read bands yourself:

with rasterio.open("landcover.tif") as ds:
    pts = StratifiedClassSampler(n_samples=500).sample(
        ds, ds.read(1), ds.read(2)   # class band, value band
    )
__init__(n_samples=500, quasi_random=None, random_state=None)[source]

Methods

__init__([n_samples, quasi_random, random_state])

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

sample(geometry[, labels, weights])

Generate proportionally allocated class samples.

set_params(**params)

Set the parameters of this estimator.

get_metadata_routing()[source]

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routing – A MetadataRequest encapsulating routing information.

Return type:

MetadataRequest

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:
deep : bool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:

params – Parameter names mapped to their values.

Return type:

dict

sample(geometry, labels=None, weights=None)[source]

Generate proportionally allocated class samples.

Parameters:
geometry : GeoSeries | GeoDataFrame | rasterio.DatasetReader | path-like

labels : array-like of shape (n,) or 2-D ndarray, required

Class label per geometry (GDF) or 2-D integer class array (raster).

weights : array-like of shape (n,) or 2-D ndarray, optional

Per-geometry weight (GDF) or 2-D value array (raster). When None, falls back to uniform random sampling.

Returns:

Columns: geometry, class_label.

Return type:

geopandas.GeoDataFrame

set_params(**params)[source]

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**params : dict

Estimator parameters.

Returns:

self – Estimator instance.

Return type:

estimator instance