-
Notifications
You must be signed in to change notification settings - Fork 56
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 reached calculations #575
Conversation
Timings:
|
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.
First round
momepy/functional/_diversity.py
Outdated
try: | ||
from numba import njit | ||
except (ModuleNotFoundError, ImportError): | ||
warnings.warn( | ||
"The numba package is used extensively in this function to accelerate the" | ||
" computation of statistics but it is not installed or cannot be imported." | ||
" Without numba, these computations may become slow on large data.", | ||
UserWarning, | ||
stacklevel=2, | ||
) | ||
from libpysal.common import jit as njit |
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.
The warning should be printed only if a user calls a function that actually uses numba. That is why it was originally inside describe
. I would still keep the warning inside if possible.
momepy/functional/_diversity.py
Outdated
|
||
|
||
def describe_reached( | ||
y, y_group, result_index=None, graph=None, q=None, include_mode=False |
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 probably call y_group
graph_index
following what we did in street_alignment
momepy/momepy/functional/_distribution.py
Lines 318 to 322 in 36b13f8
def street_alignment( | |
building_orientation: Series, | |
street_orientation: Series, | |
street_index: Series, | |
) -> Series: |
it should be clear from the name that this contains ID's aligned with y
that link to the index in graph
.
momepy/functional/_diversity.py
Outdated
y, y_group, result_index=None, graph=None, q=None, include_mode=False | ||
) -> DataFrame: | ||
""" | ||
Calculates statistics of ``y`` objects reached on a street network. |
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.
Not only street network. Graph or IDs can come from anywhere. You can easily use enclosure ID here instead. The function is pretty generic.
momepy/functional/_diversity.py
Outdated
""" | ||
Calculates statistics of ``y`` objects reached on a street network. | ||
Requires a ``y_group`` that links the ``y`` objects to streets (or ``graph``) | ||
assigned beforehand (e.g. using :py:func:`momepy.get_network_id`). |
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.
assigned beforehand (e.g. using :py:func:`momepy.get_network_id`). | |
assigned beforehand (e.g. using :py:func:`momepy.get_nearest_street`). |
that is the new one
momepy/functional/_diversity.py
Outdated
return stat_ | ||
if isinstance(y, np.ndarray): | ||
y = pd.Series(y, name="obs_index") | ||
elif isinstance(y, 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.
elif isinstance(y, Series): | |
if isinstance(y, Series): |
to treat the series created above, no?
momepy/functional/_diversity.py
Outdated
) | ||
result.loc[stats.index.values] = stats.values | ||
result.columns = stats.columns | ||
result = result.fillna(0) |
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.
No need for this if you are filling the results full of zeros, no?
@@ -11,7 +12,7 @@ | |||
GPD_013 = Version(gpd.__version__) >= Version("0.13") | |||
|
|||
|
|||
class TestDistribution: | |||
class TestDiscribe: |
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.
class TestDiscribe: | |
class TestDescribe: |
typo
# not using assert_result since the method | ||
# is returning an aggregation, indexed based on nID |
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.
Why not? We use it in describe
without issues. I'd prefer to use it as it checks the index.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #575 +/- ##
=======================================
+ Coverage 97.4% 97.8% +0.4%
=======================================
Files 26 37 +11
Lines 4328 5299 +971
=======================================
+ Hits 4214 5183 +969
- Misses 114 116 +2
|
ci/envs/310-oldest.yaml
Outdated
@@ -10,7 +10,7 @@ dependencies: | |||
- networkx=2.7 | |||
- numpy=1.22 | |||
- packaging | |||
- pandas>=1.4.0,!=1.5.0,<2 | |||
- pandas=2.0 |
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.
Can you revert this since we're checking for 2.1.0 anyway?
momepy/functional/_diversity.py
Outdated
throw_numba_warning = False | ||
try: | ||
from numba import njit | ||
except (ModuleNotFoundError, ImportError): | ||
throw_numba_warning = True | ||
from libpysal.common import jit as njit |
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.
throw_numba_warning = False | |
try: | |
from numba import njit | |
except (ModuleNotFoundError, ImportError): | |
throw_numba_warning = True | |
from libpysal.common import jit as njit | |
try: | |
from numba import njit | |
HAS_NUMBA = True | |
except (ModuleNotFoundError, ImportError): | |
HAS_NUMBA = False | |
from libpysal.common import jit as njit |
Just a minor nit to keep this consistent across PySAL.
momepy/functional/_diversity.py
Outdated
results = [ | ||
values.shape[0], | ||
np.mean(values), | ||
np.median(values), | ||
np.std(values), | ||
np.min(values), | ||
np.max(values), | ||
np.sum(values), |
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.
Needs to work properly with missing values.
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 updated the code, I think we can just get rid of the nans beforehand and compute everything on the non_na values, what do you think?
momepy/functional/_diversity.py
Outdated
Parameters | ||
---------- | ||
grouper : pandas.GroupBy | ||
Groupby Object which specifies the aggregations to be performed |
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.
Groupby Object which specifies the aggregations to be performed | |
Groupby Object which specifies the aggregations to be performed. |
momepy/functional/_diversity.py
Outdated
""" | ||
|
||
if Version(pd.__version__) <= Version("2.1.0"): | ||
raise NotImplementedError("Please update to a newer version of pandas.") |
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.
Is this a proper usage of a NotImplementedError
?
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 be ImportError
I think
momepy/functional/_diversity.py
Outdated
|
||
if (result_index is None) and (graph is None): | ||
raise ValueError( | ||
"One of result_index or graph has to be specified, but not both." |
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.
Let's declare this ValueError
so it can be used here and below. Or at least declare the message to re-use.
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.
Few notes, mostly on the documentation side.
momepy/functional/_diversity.py
Outdated
""" | ||
Calculates statistics of ``y`` objects reached on a neighbourhood graph. | ||
Requires a ``graph_index`` that links the ``y`` objects to ``graph`` or streets |
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.
It do not have to be streets, the graph can be done based on anything. The key point is that it is a Graph based on another object than the one on which we have y
. We can include link to streets as an example.
The statistics calculated are count, sum, mean, median, std. | ||
Optionally, mode can be calculated, or the statistics can be calculated in | ||
quantiles ``q``. |
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.
Can you move this to the top and have the implementation details come after this? You first want to know what it does and then how to use it.
Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net>
a new functional version of the mm.Reached function. It calculates several statistics at once similar to mm.describe(