Skip to content
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

Merged
merged 8 commits into from
Jun 12, 2024
Merged

Functional diversity #581

merged 8 commits into from
Jun 12, 2024

Conversation

u3ks
Copy link
Collaborator

@u3ks u3ks commented May 16, 2024

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

Copy link

codecov bot commented May 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.9%. Comparing base (4037c70) to head (ef230f4).
Report is 84 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           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     
Files with missing lines Coverage Δ
momepy/functional/_diversity.py 98.3% <100.0%> (ø)
momepy/functional/tests/test_diversity.py 100.0% <100.0%> (ø)

from libpysal.graph import Graph
from numpy.typing import NDArray
from packaging.version import Version
from pandas import DataFrame, Series

import momepy as mm
Copy link
Member

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 :)

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

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.
Copy link
Member

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.

"""

def _apply_range(values):
return sp.stats.iqr(values, rng=rng, **kwargs)
Copy link
Member

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.


Parameters
----------
data : DataFrame | Series
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data : DataFrame | Series
y : DataFrame | Series | NDArray[np.float_]

def unique(data: DataFrame | Series, graph: Graph, dropna: bool = True):
"""Calculates the number of unique values within neighbours defined in ``graph``.

.. math::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.. math::

"min": 3789.0228732928035,
"max": 34510.77694161156,
}
print(np.mean(full_sw))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
print(np.mean(full_sw))

quan_sw, quan_sw_expected, self.df_tessellation, check_names=False
)

with pytest.raises(ValueError):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

Copy link
Member

@martinfleis martinfleis left a 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

@u3ks u3ks requested a review from martinfleis June 11, 2024 15:56
@martinfleis martinfleis merged commit 86a7dc9 into pysal:main Jun 12, 2024
14 checks passed
@martinfleis martinfleis added enhancement New feature or request and removed refactor labels Jun 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants