-
Notifications
You must be signed in to change notification settings - Fork 829
Optimize inverse index mapping in groups.py (Fixes Issue #3387) #5252
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
Changes from 1 commit
d07e964
463a1ec
7724ba3
d62eecd
fa8ad6c
9ae78cb
fd97d19
f56d246
fcb4931
e22709e
97f112c
50ac687
eede57e
e1df729
111da56
fc3f507
c1c39a3
e697b56
fd67b0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,7 +37,7 @@ from cython.operator cimport dereference as deref | |
|
|
||
| cnp.import_array() | ||
|
|
||
| __all__ = ['unique_int_1d', 'make_whole', 'find_fragments', | ||
| __all__ = ['unique_int_1d', 'inverse_int_index', 'make_whole', 'find_fragments', | ||
| '_sarrus_det_single', '_sarrus_det_multiple'] | ||
|
|
||
| cdef extern from "calc_distances.h": | ||
|
|
@@ -91,6 +91,42 @@ def unique_int_1d(cnp.intp_t[:] values): | |
|
|
||
| return np.array(result) | ||
|
|
||
| @cython.boundscheck(False) | ||
| @cython.wraparound(False) | ||
| def inverse_int_index(cnp.intp_t[:] values, | ||
| cnp.intp_t[:] unique_vals): | ||
| """ | ||
| Construct inverse index map such that: | ||
|
|
||
| unique_vals[mask] == values | ||
|
|
||
| Parameters | ||
| ---------- | ||
| values : numpy.ndarray | ||
| 1D array of integers. | ||
| unique_vals : numpy.ndarray | ||
| 1D array of unique integers (unsorted). | ||
|
|
||
| Returns | ||
| ------- | ||
| numpy.ndarray | ||
| Integer mask mapping values -> index in unique_vals. | ||
|
Member
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. add a .. versionadded:: 2.11.0Make sure that there are TWO blank lines separating this line from the rest.
Contributor
Author
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. I've added a |
||
| """ | ||
|
|
||
| cdef Py_ssize_t n = values.shape[0] | ||
| cdef Py_ssize_t m = unique_vals.shape[0] | ||
| cdef Py_ssize_t i | ||
|
|
||
| cdef dict lookup = {} | ||
| cdef cnp.intp_t[:] mask = np.empty(n, dtype=np.intp) | ||
|
|
||
| for i in range(m): | ||
| lookup[unique_vals[i]] = i | ||
|
|
||
| for i in range(n): | ||
| mask[i] = lookup[values[i]] | ||
|
|
||
| return np.array(mask) | ||
|
|
||
| @cython.boundscheck(False) | ||
| def _in2d(cnp.intp_t[:, :] arr1, cnp.intp_t[:, :] arr2): | ||
|
|
@@ -515,4 +551,4 @@ def find_fragments(atoms, bondlist): | |
| # Add fragment to output | ||
| frags.append(np.asarray(this_frag)) | ||
|
|
||
| return frags | ||
| return frags | ||
|
orbeckst marked this conversation as resolved.
|
||
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.
maskis not defined — do you need to say "Construct inverse indexmasksuch that ..."? Properly explain what this code does.Add a simple example in the docs below (look at https://userguide.mdanalysis.org/stable/contributing_code.html#working-with-the-code-documentation for how to structure doc strings using the numpy docstring standard)
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've now added a proper docstring for this function which includes the description of the function, the parameters, the return value and an example.