-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Functional diversity #581
Functional diversity #581
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #581 +/- ##
=======================================
+ Coverage 97.4% 97.9% +0.6%
=======================================
Files 26 37 +11
Lines 4328 5807 +1479
=======================================
+ Hits 4214 5687 +1473
- Misses 114 120 +6
|
momepy/functional/_diversity.py
Outdated
from libpysal.graph import Graph | ||
from numpy.typing import NDArray | ||
from packaging.version import Version | ||
from pandas import DataFrame, Series | ||
|
||
import momepy as mm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to load just the functions you need. Any maybe check if we can njit them :)
momepy/functional/_diversity.py
Outdated
@@ -176,6 +188,322 @@ def describe( | |||
return _compute_stats(grouper, q, include_mode) | |||
|
|||
|
|||
def values_range( | |||
data: DataFrame | Series, graph: Graph, rng: tuple | list = (0, 100), **kwargs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data: DataFrame | Series, graph: Graph, rng: tuple | list = (0, 100), **kwargs | |
y: DataFrame | Series | NDArray[np.float_], graph: Graph, q: tuple | list = (0, 100), **kwargs |
rng is now used by numpy for random-number-generator. Let's switch as we did in describe
. And let's call the input array y
as we do in desribe.
momepy/functional/_diversity.py
Outdated
data: DataFrame | Series, graph: Graph, rng: tuple | list = (0, 100), **kwargs | ||
): | ||
"""Calculates the range of values within neighbours defined in ``graph``. | ||
Uses ``scipy.stats.iqr`` under the hood. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the note if scipy iqr to avoid the confusion of keywords as they are still using rng.
momepy/functional/_diversity.py
Outdated
""" | ||
|
||
def _apply_range(values): | ||
return sp.stats.iqr(values, rng=rng, **kwargs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be actually easier to use numpy.percentile directly like we have in describe and pass the kwargs there for the sake of consistency. you can also njit that.
momepy/functional/_diversity.py
Outdated
|
||
Parameters | ||
---------- | ||
data : DataFrame | Series |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data : DataFrame | Series | |
y : DataFrame | Series | NDArray[np.float_] |
momepy/functional/_diversity.py
Outdated
def unique(data: DataFrame | Series, graph: Graph, dropna: bool = True): | ||
"""Calculates the number of unique values within neighbours defined in ``graph``. | ||
|
||
.. math:: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. math:: |
"min": 3789.0228732928035, | ||
"max": 34510.77694161156, | ||
} | ||
print(np.mean(full_sw)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
print(np.mean(full_sw)) |
quan_sw, quan_sw_expected, self.df_tessellation, check_names=False | ||
) | ||
|
||
with pytest.raises(ValueError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with pytest.raises(ValueError): | |
with pytest.raises(ValueError, match=""): |
check against the error to ensure we hit the correct one
assert_result(cat, cat_expected, self.df_tessellation, check_names=False) | ||
|
||
def test_gini(self): | ||
with pytest.raises(ValueError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with pytest.raises(ValueError): | |
with pytest.raises(ValueError, match=""): |
same here. add expected error message
assert_result(limit, limit_expected, self.df_tessellation, check_names=False) | ||
|
||
def test_shannon(self): | ||
with pytest.raises(ValueError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aso ensure correct typing. mypy is failing
Functional implementation of the distribution description functions. Code is same as OO version, with the difference that for loops are replaced by calls to libpysal.Graph.apply