-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- switched from gdf to ndarray for point iteration - removed rasterio as dep - added kernels - finished docstrings to do: - tests - CICD - README
- Loading branch information
1 parent
911bb5e
commit f085da6
Showing
7 changed files
with
357 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## Roadmap | ||
------- | ||
- Add more kernels. | ||
- Implement other methods of distance measurement, e.g. haversine, manhattan. | ||
- Investigate possible alternatives to iterating over points. | ||
- Enable use of single radius and weight values without filling array of the same length as the points GeoDataFrame/GeoSeries. Results in marginal speed up but the current approach may become an issue with large point datasets. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,118 @@ | ||
"""Kernels for density estimation. | ||
All kernels accept the following parameters: | ||
Parameters | ||
---------- | ||
distance : int | float | ||
Euclidean distance value for a given point from the reference point. | ||
radius : int | float | ||
Search window radius for the reference point. | ||
weight : int | float | ||
Value with which the KDE value for a point will be weighted. | ||
And provide the following return: | ||
Returns | ||
------- | ||
value : float | ||
Specified kernel's density estimation value. | ||
""" | ||
|
||
from math import pi | ||
|
||
import numpy as np | ||
|
||
|
||
VALID_KERNELS = [ | ||
"epanechnikov", | ||
"quartic", | ||
"triweight", | ||
] | ||
|
||
|
||
@np.vectorize | ||
def quartic_raw( | ||
distance: int | float, | ||
radius: int | float, | ||
weight: int | float, | ||
) -> float: | ||
"""Raw Quartic kernel.""" | ||
if distance: | ||
value = weight * pow(1 - pow(distance / radius, 2), 2) | ||
else: | ||
value = 0.0 | ||
return value | ||
|
||
|
||
@np.vectorize | ||
def quartic_scaled( | ||
distance: int | float, | ||
radius: int | float, | ||
weight: int | float, | ||
) -> float: | ||
"""Scaled Quartic kernel.""" | ||
if distance: | ||
norm_const = 116 / (5 * pi * pow(radius, 2)) | ||
value = weight * (norm_const * (15 / 16) * pow(1 - pow(distance / radius, 2), 2)) | ||
else: | ||
value = 0.0 | ||
return value | ||
|
||
|
||
@np.vectorize | ||
def epanechnikov_raw( | ||
distance: int | float, | ||
radius: int | float, | ||
weight: int | float, | ||
) -> float: | ||
"""Raw Epanechnikov kernel.""" | ||
if distance: | ||
value = weight * (1 - pow(distance / radius, 2)) | ||
else: | ||
value = 0.0 | ||
return value | ||
|
||
|
||
@np.vectorize | ||
def epanechnikov_scaled( | ||
distance: int | float, | ||
radius: int | float, | ||
weight: int | float, | ||
) -> float: | ||
"""Scaled Epanechnikov kernel.""" | ||
if distance: | ||
norm_const = 8 / (3 * pi * pow(radius, 2)) | ||
value = weight * (norm_const * (3 / 4) * (1 - pow(distance / radius, 2))) | ||
else: | ||
value = 0.0 | ||
return value | ||
|
||
|
||
@np.vectorize | ||
def triweight_raw( | ||
distance: int | float, | ||
radius: int | float, | ||
weight: int | float, | ||
) -> float: | ||
"""Raw triweight kernel.""" | ||
if distance: | ||
value = weight * pow(1 - pow(distance / radius, 2), 3) | ||
else: | ||
value = 0.0 | ||
return value | ||
|
||
|
||
@np.vectorize | ||
def quartic(distance: int | float, radius: int | float, weight: int | float) -> float: | ||
"""Quartic kernel. | ||
Parameters | ||
---------- | ||
distance : int | float | ||
radius : int | float | ||
weight : int | float | ||
Returns | ||
------- | ||
value : float | ||
""" | ||
def triweight_scaled( | ||
distance: int | float, | ||
radius: int | float, | ||
weight: int | float, | ||
) -> float: | ||
"""Scaled triweight kernel.""" | ||
if distance: | ||
value = weight * ((1 - ((distance / radius) ** 2)) ** 2) | ||
norm_const = 128 / (35 * pi * pow(radius, 2)) | ||
value = weight * (norm_const * (35 / 32) * pow(1 - pow(distance / radius, 2), 3)) | ||
else: | ||
value = 0.0 | ||
return value |
Oops, something went wrong.