forked from wildlife-dynamics/ecoscope
-
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.
Implement classifications to continuous data (wildlife-dynamics#151)
- Loading branch information
Showing
3 changed files
with
82 additions
and
0 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,39 @@ | ||
import mapclassify | ||
|
||
classification_methods = { | ||
"equal_interval": mapclassify.EqualInterval, | ||
"natural_breaks": mapclassify.NaturalBreaks, | ||
"quantile": mapclassify.Quantiles, | ||
"std_mean": mapclassify.StdMean, | ||
"max_breaks": mapclassify.MaximumBreaks, | ||
"fisher_jenks": mapclassify.FisherJenks, | ||
} | ||
|
||
|
||
# pass in a series and output the series | ||
def apply_classification(x, labels=None, scheme="natural_breaks", **kwargs): | ||
""" | ||
Classifies the data in a GeoDataFrame column using specified classification scheme. | ||
Args: | ||
y : An array containing the data to classify. | ||
labels (str): labels of bins, use bin edges if labels==None. | ||
scheme (str): Classification scheme to use [equal_interval, natural_breaks, quantile, std_mean, max_breaks, | ||
fisher_jenks] | ||
**kwargs: Additional keyword arguments specific to the classification scheme. | ||
Returns: | ||
result: an array of corresponding labels of the input data. | ||
""" | ||
|
||
classifier_class = classification_methods.get(scheme) | ||
|
||
if not classifier_class: | ||
raise ValueError(f"Invalid classification scheme. Choose from: {list(classification_methods.keys())}") | ||
|
||
classifier = classifier_class(x, **kwargs) | ||
if labels is None: | ||
labels = classifier.bins | ||
assert len(labels) == len(classifier.bins) | ||
return [labels[i] for i in classifier.yb] |
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,41 @@ | ||
import pytest | ||
|
||
import ecoscope | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"scheme,kwargs,expected", | ||
[ | ||
("equal_interval", {"k": 2}, [3, 3, 3, 5, 5]), | ||
("quantile", {"k": 2}, [3, 3, 3, 5, 5]), | ||
( | ||
"std_mean", | ||
{"multiples": [-2, -1, 1, 2]}, | ||
[1.4188611699158102, 4.58113883008419, 4.58113883008419, 4.58113883008419, 6.16227766016838], | ||
), | ||
("max_breaks", {"k": 4}, [2.5, 2.5, 3.5, 4.5, 5.0]), | ||
("fisher_jenks", {"k": 5}, [1.0, 2.0, 3.0, 4.0, 5.0]), | ||
], | ||
) | ||
def test_classify_data(scheme, kwargs, expected): | ||
y = [1, 2, 3, 4, 5] | ||
result = ecoscope.analysis.apply_classification(y, scheme=scheme, **kwargs) | ||
assert result == expected, f"Failed on scheme {scheme}" | ||
|
||
|
||
def test_classify_with_labels(): | ||
y = [1, 2, 3, 4, 5] | ||
result = ecoscope.analysis.apply_classification(y, labels=["1", "2"], scheme="equal_interval", k=2) | ||
assert result == ["1", "1", "1", "2", "2"] | ||
|
||
|
||
def test_classify_with_invalid_labels(): | ||
y = [1, 2, 3, 4, 5] | ||
with pytest.raises(AssertionError): | ||
ecoscope.analysis.apply_classification(y, labels=[0], scheme="std_mean") | ||
|
||
|
||
def test_classify_with_invalid_scheme(): | ||
y = [1, 2, 3, 4, 5] | ||
with pytest.raises(ValueError): | ||
ecoscope.analysis.apply_classification(y, scheme="InvalidScheme") |