Density estimation

detroit.contour_density()

Constructs a new density estimator with the default settings.

class detroit.contour.density.Density

Constructs a new density estimator with the default settings.

__call__(data: list[T]) list[MultiPolygonGeoJSON]

Estimates the density contours for the given array of data, returning an array of GeoJSON MultiPolygon geometry objects.

Parameters:

data (list[T]) – Data input

Returns:

List of GeoJSON MultiPolygon geometry objects

Return type:

list[GeoJSON]

contours(data: list[T]) Contour

Return a Contour callable class that can be used to compute an arbitrary contour on the given data without needing to recompute the underlying grid. The returned contour class also exposes a Contour.max value which represents the maximum density of the grid.

Parameters:

data (list[T]) – Data input

Returns:

Callable class

Return type:

Contour

x(x: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float] | float) Density

Sets the x coordinate accessor and returns itself.

Parameters:

x (Accessor[T, float] | float) – X accessor or constant value

Returns:

Itself

Return type:

Density

y(y: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float] | float) Density

Sets the y coordinate accessor and returns itself.

Parameters:

y (Accessor[T, float] | float) – Y accessor or constant value

Returns:

Itself

Return type:

Density

set_weight(weight: Callable[[T], float] | Callable[[T, int], float] | Callable[[T, int, list[Element]], float] | float) Density

Sets the weight accessor and returns itself.

Parameters:

x (Accessor[T, float] | float) – Weight accessor or constant value

Returns:

Itself

Return type:

Density

set_size(size: tuple[float, float]) Density

Sets the size of the density estimator to the specified bounds and returns itself. The size is specified as an array [width, height] where width is the maximum x-value and height is the maximum y-value.

Parameters:

size (tuple[float, float]) – Size of the density estimator; default value [960, 500]

Returns:

Itself

Return type:

Density

set_cell_size(cell_size: float) Density

Sets the size of individual cells in the underlying bin grid of the positive integer and returns itself.

Parameters:

cell_size (float) – Cell size; default value 4

Returns:

Itself

Return type:

Density

set_thresholds(thresholds: Callable[[list[float], ...], float | list[float]] | list[float] | float) Density

Sets the threshold function to the contour generator and returns itself.

Thresholds are defined as an array of values \([x_0, x_1, ...]\). The first generated density contour corresponds to the area where the estimated density is greater than or equal to \(x_0\); the second contour corresponds to the area where the estimated density is greater than or equal to \(x_1\), and so on. Thus, there is exactly one generated MultiPolygon geometry object for each specified threshold value; the threshold value is exposed as geometry["value"]. The first value \(x_0\) should typically be greater than zero.

Parameters:

thresholds (Callable[[list[float], ...], float | list[float]] | list[float] | float) – Threshold function or array or constant value

Returns:

Itself

Return type:

Density

set_bandwidth(bandwidth: float) Density

Sets the bandwidth (the standard deviation) of the Gaussian kernel and returns itself.

Parameters:

bandwidth (float) – Bandwidth value

Returns:

Itself

Return type:

Density

class detroit.contour.density.Contour(values: list[float], contours: Contours, pow4k: float, transform: Callable[[MultiPolygonGeoJSON], MultiPolygonGeoJSON])

Class used to compute an arbitrary contour without needing to recompute the underlying grid.

Parameters:
  • values (list[float]) – Computed grid values

  • contours (Contours) – Contours class

  • pow4k (float) – Computed constant value

  • transform (Callable[[GeoJSON], GeoJSON]) – Transform function

__call__(value: float) MultiPolygonGeoJSON

Compute an arbitrary contour without needing to recompute the underlying grid.

Parameters:

value (float) – Threshold value

Returns:

GeoJSON MultiPolygon geometry object

Return type:

GeoJSON

max() float

Computes the maximum density of the grid.

Returns:

Maximum density of the grid

Return type:

float