-
Notifications
You must be signed in to change notification settings - Fork 79
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
ENH: include Graph.describe() to describe neighbourhood values #717
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aedb687
summary statistics for neigbourhood values
u3ks cb4b1e0
summary statistics for neigbourhood values
u3ks 188f02b
formatting
u3ks 045e792
Apply suggestions from code review
u3ks b634b38
extra tests
u3ks 805e5c2
testing fix
u3ks 3a0ebed
Apply suggestions from code review
u3ks 8f3a300
docstring
u3ks 4cfa943
ndarray test
u3ks 879f3f5
nas equivalence and more filtration tests
u3ks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -24,8 +24,10 @@ | |
from ._spatial_lag import _lag_spatial | ||
from ._triangulation import _delaunay, _gabriel, _relative_neighborhood, _voronoi | ||
from ._utils import ( | ||
_compute_stats, | ||
_evaluate_index, | ||
_neighbor_dict_to_edges, | ||
_percentile_filtration_grouper, | ||
_resolve_islands, | ||
_sparse_to_arrays, | ||
) | ||
|
@@ -1993,6 +1995,69 @@ | |
""" | ||
return self._adjacency.groupby(level=0).agg(func) | ||
|
||
def describe( | ||
self, | ||
y: np.typing.NDArray[np.float_] | pd.Series, | ||
q: tuple[float, float] | None = None, | ||
statistics: list[str] | None = None, | ||
) -> pd.DataFrame: | ||
"""Describe the distribution of ``y`` values within the neighbors of each node. | ||
|
||
Given the graph, computes the descriptive statistics of values within the | ||
neighbourhood of each node. Optionally, the values can be limited to a certain | ||
quantile range before computing the statistics. | ||
|
||
Notes | ||
----- | ||
The index of ``values`` must match the index of the graph. | ||
|
||
Weight values do not affect the calculations, only adjacency does. | ||
|
||
Returns nan for all isolates. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe 'nan' is OK here, but also maybe Probably not a big deal either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed to numpy.nan |
||
|
||
The numba package is used extensively in this function | ||
to accelerate the computation of statistics. | ||
Without numba, these computations may become slow on large data. | ||
|
||
Parameters | ||
---------- | ||
y : NDArray[np.float_] | Series | ||
An 1D array of numeric values to be described. | ||
q : tuple[float, float] | None, optional | ||
Tuple of percentages for the percentiles to compute. | ||
Values must be between 0 and 100 inclusive. When set, values below and above | ||
the percentiles will be discarded before computation of the statistics. | ||
The percentiles are computed for each neighborhood. By default None. | ||
statistics : List[str] | None | ||
u3ks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
A list of stats functions to compute. If None, compute all | ||
available functions - "count", "mean", "median", | ||
"std", "min", "max", "sum", "nunique", "mode". By default None. | ||
|
||
Returns | ||
------- | ||
DataFrame | ||
A DataFrame with descriptive statistics. | ||
""" | ||
|
||
if not isinstance(y, pd.Series): | ||
y = pd.Series(y) | ||
|
||
if q is None: | ||
grouper = y.take(self._adjacency.index.codes[1]).groupby( | ||
self._adjacency.index.codes[0] | ||
) | ||
else: | ||
grouper = _percentile_filtration_grouper(y, self._adjacency.index, q=q) | ||
|
||
stat_ = _compute_stats(grouper, statistics) | ||
|
||
stat_.index = self.unique_ids | ||
if isinstance(stat_, pd.Series): | ||
stat_.name = None | ||
# NA isolates | ||
stat_.loc[self.isolates] = np.nan | ||
return stat_ | ||
|
||
|
||
def _arrange_arrays(heads, tails, weights, ids=None): | ||
""" | ||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should this be
pandas.Grouper
?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 think the pandas.Grouper is another type of object, that is used for filtering columns , i used the name grouper since its used in other functions and the type is groupby since, pandas groupy returns a groupby object