-
-
Notifications
You must be signed in to change notification settings - Fork 35
Refactor get_all_functions to use inspect.signature instead of inspect.getfullargspe #104
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
Conversation
nx_parallel/tests/test_get_chunks.py
Outdated
| signature = inspect.signature(obj) | ||
| args = [ | ||
| param.name | ||
| for param in signature.parameters.values() | ||
| if param.kind | ||
| in ( | ||
| inspect.Parameter.POSITIONAL_ONLY, | ||
| inspect.Parameter.POSITIONAL_OR_KEYWORD, | ||
| ) | ||
| ] | ||
| kwargs = [ | ||
| param.name | ||
| for param in signature.parameters.values() | ||
| if param.kind == inspect.Parameter.KEYWORD_ONLY | ||
| ] |
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 checked the tests are passing(using pytest nx_parallel). The CI test failure is unrelated!
Maybe we can simplify this code by only returning kwargs instead of both args and kwargs as get_chunks is always a kwarg. Does that sound like a good idea to you?
nx_parallel/tests/test_get_chunks.py
Outdated
| for edge in G.edges: | ||
| assert math.isclose( | ||
| c1.get(edge, 0), c2.get(edge, 0), abs_tol=1e-16 | ||
| ) |
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.
Good catch! But this will now work with edge_betweenness_centrality and not betweenness_centrality because for the later the keys are nodes not edges. But, this seems like it is working because when you apply get on edges it always returns 0 for both c1 and c2. But, that shouldn't happen. So maybe we should use c1.keys instead of G.nodes(or G.edges). LMKWYT.
| - check-list for adding a new function: | ||
| - [ ] Add the parallel implementation(make sure API doesn't break), the file structure should be the same as that in networkx. | ||
| - [ ] add the function to the `BackendInterface` class in [interface.py](./nx_parallel/interface.py) (take care of the `name` parameter in `_dispatchable` (ref. [docs](https://networkx.org/documentation/latest/reference/backends.html))) | ||
| - [ ] Include the `get_chunks` additional parameter. Currently, all algorithms in nx-parallel offer the user to pass their own custom chunks. Unless it is impossible to chunk, please do include this additional parameter. |
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.
Could you consider rebasing this branch -- so that these changes don't appear in the diff? Thanks!
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 agree with you @Schefflera-Arboricola! I've made the changes you suggested. |
Schefflera-Arboricola
left a comment
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.
Thanks @akshitasure12 -- Apologies for the delayed response! Please LMK if you still have the bandwidth to address the following comments. Thanks!
nx_parallel/tests/test_get_chunks.py
Outdated
| kwargs = [ | ||
| param.name | ||
| for param in signature.parameters.values() | ||
| if param.default is not inspect.Parameter.empty | ||
| ] |
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.
this is collecting all the default values of all the kwargs-- not the kwargs. Is that what you intended? The get_functions_with_get_chunks function calls this function-- and it assumes that get_all_functions is providing all the functions keyed to their kwargs. Could you please consider updating the docstrings of these functions to make it more clear to review?
We should also consider adding these helper functions to test/utils or utils maybe and then testing these as well
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.
That was unintentional! I’ll get these changes done asap!
nx_parallel/tests/test_get_chunks.py
Outdated
| assert math.isclose(c1[i], c2[i], abs_tol=1e-16) | ||
| for key in c1.keys(): | ||
| assert math.isclose( | ||
| c1.get(key, 0), c2.get(key, 0), abs_tol=1e-16 |
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 don't think we should use .get here because the keys should be the same in both c1 and c2 and if they are not-- then an error should be raised instead of using 0. LMKWYT.
nx_parallel/tests/test_get_chunks.py
Outdated
| assert math.isclose(c1[i], c2[i], abs_tol=1e-16) | ||
| for key in c1.keys(): | ||
| assert math.isclose( | ||
| c1.get(key, 0), c2.get(key, 0), abs_tol=1e-16 |
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.
same here.
Sure! I’ll be free in about a week and will get back to it. |
…ons_kwargs() to return kwargs
nx_parallel/tests/test_get_chunks.py
Outdated
| if not math.isclose(c1[key], c2[key], abs_tol=1e-16): | ||
| raise ValueError( | ||
| f"Values for key '{key}' differ: {c1[key]} != {c2[key]}" | ||
| ) |
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 explicitly raising error like this here, assert already handles this
nx_parallel/tests/test_get_chunks.py
Outdated
| def get_all_functions(package_name="nx_parallel"): | ||
| """Returns a dict keyed by function names to its arguments. | ||
| def get_all_functions_kwargs(package_name="nx_parallel"): | ||
| """Returns a dict keyed by function names to its positional-or-keyword arguments. |
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 have you explicitly mentioned "positional-or-keyword"? -- it returns all the arguments. also in the following code could you please update the variable names i.e. update kwarg_?
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.
All the functions only have positional or keyword arguments, so there is no need to mention that explicitly. I'll make the changes. Thanks!
Schefflera-Arboricola
left a comment
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.
Thanks @akshitasure12!
CI failures unrelated.
dschult
left a comment
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.
This looks like a good simplification and refactor.
And, of course, it now avoids the problems with getfullargspec.
I approve this PR
This PR addresses issue #94
Changes made :
Replaced
inspect.getfullargspec()withinspect.signature()for reporting the signature of the original function and not that of wrapped.Updated the assertion logic in the
test_get_chunksforchk_dict_valsto iterate over graph edges instead of nodes sincechk_dict_valsconsists of functions that deals with edges between the nodes and not the nodes themselves.