Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ The default chunking in nx-parallel is done by slicing the list of nodes (or edg
- The algorithm that you are considering to add to nx-parallel should be in the main networkx repository and it should have the `_dispatchable` decorator. If not, you can consider adding a sequential implementation in networkx first.
- 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.

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!

- [ ] add the function to the `ALGORITHMS` list in [interface.py](./nx_parallel/interface.py). Take care of the `name` parameter in `_dispatchable` for the algorithms with same name but different implementations. The `name` parameter is used distinguish such algorithms in a single namespace. (ref. [docs](https://networkx.org/documentation/latest/reference/backends.html)))
- [ ] update the `__init__.py` files accordingly
- [ ] docstring following the above format
- [ ] run the [timing script](./timing/timing_individual_function.py) to get the performance heatmap
- [ ] add additional test(if any)
- [ ] add benchmark(s) for the new function(ref. the README in benchmarks folder for more details)
- [ ] add additional test, if needed. The smoke tests for the additional parameter `get_chunks` are done [here](https://github.com/networkx/nx-parallel/blob/main/nx_parallel/tests/test_get_chunks.py) together for all the algorithms.
- [ ] run the [timing script](./timing/timing_individual_function.py) to get the performance heatmap (ref. [Issue#51](https://github.com/networkx/nx-parallel/issues/51))
- [ ] add benchmark(s) for the new function(ref. the README in `benchmarks` folder for more details)

Happy contributing! 🎉
32 changes: 20 additions & 12 deletions nx_parallel/tests/test_get_chunks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ def get_all_functions(package_name="nx_parallel"):
the function's keyword arguments and positional arguments.
"""
package = importlib.import_module(package_name)
functions = {}
all_funcs_kwargs = {}

for name, obj in inspect.getmembers(package, inspect.isfunction):
if not name.startswith("_"):
args, kwargs = inspect.getfullargspec(obj)[:2]
functions[name] = {"args": args, "kwargs": kwargs}

return functions
signature = inspect.signature(obj)
kwargs = [
param.name
for param in signature.parameters.values()
if param.default is not inspect.Parameter.empty
]
all_funcs_kwargs[name] = kwargs
return all_funcs_kwargs


def get_functions_with_get_chunks():
"""Returns a list of function names with the `get_chunks` kwarg."""
all_funcs = get_all_functions()
all_funcs_kwargs = get_all_functions()
get_chunks_funcs = []
for func in all_funcs:
if "get_chunks" in all_funcs[func]["args"]:
for func in all_funcs_kwargs:
if "get_chunks" in all_funcs_kwargs[func]:
get_chunks_funcs.append(func)
return get_chunks_funcs

Expand Down Expand Up @@ -76,14 +80,18 @@ def random_chunking(nodes):
if isinstance(c1, types.GeneratorType):
c1, c2 = dict(c1), dict(c2)
if func in chk_dict_vals:
for i in range(len(G.nodes)):
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
)
else:
assert c1 == c2
else:
if func in chk_dict_vals:
for i in range(len(G.nodes)):
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
)
else:
if isinstance(c1, float):
assert math.isclose(c1, c2, abs_tol=1e-16)
Expand Down
Loading