geovalidate.ConstantClassSampler

class geovalidate.ConstantClassSampler(n_per_class=100, quasi_random=None, random_state=None)[source]

Sample exactly n_per_class points from each class.

Accepts a geopandas.GeoSeries / geopandas.GeoDataFrame paired with a labels vector, or a rasterio dataset paired with a 2-D numpy array of class labels read from the desired band.

Parameters:
n_per_class : int, default 100

Exact number of points to generate per class.

quasi_random : str or None

random_state : int, RandomState instance, or None

Examples

GeoDataFrame path:

pts = ConstantClassSampler(n_per_class=50).sample(
    gdf.geometry, gdf["lc_class"]
)

Raster path – read the band yourself, pass it as labels:

with rasterio.open("landcover.tif") as ds:
    pts = ConstantClassSampler(n_per_class=50).sample(ds, ds.read(1))
__init__(n_per_class=100, quasi_random=None, random_state=None)[source]

Methods

__init__([n_per_class, quasi_random, ...])

get_metadata_routing()

Get metadata routing of this object.

get_params([deep])

Get parameters for this estimator.

sample(geometry[, labels])

Generate balanced 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)[source]

Generate balanced class samples.

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

Spatial source. For the raster path a DatasetReader (or file path) is used for its transform / CRS / nodata metadata only – no band is read from it here.

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

Class label for each geometry (GDF path) or a 2-D numpy array of integer class labels with shape (nrows, ncols) (raster path, e.g. ds.read(1)).

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