Skip to content

MAINT: used add_edges_from instead of initialising nx.Graph() with edges in cycles.py - #7402

Closed
Schefflera-Arboricola wants to merge 5 commits into
networkx:mainfrom
Schefflera-Arboricola:cycles_grp_creation
Closed

Schefflera-Arboricola wants to merge 5 commits into
networkx:mainfrom
Schefflera-Arboricola:cycles_grp_creation

Conversation

@Schefflera-Arboricola

Copy link
Copy Markdown
Member

fixes #7401

I've fixed instances in cycles.py but there probably would be other such instances of initialisations of Graph with edges

@Schefflera-Arboricola
Schefflera-Arboricola marked this pull request as draft April 9, 2024 17:51
@Schefflera-Arboricola
Schefflera-Arboricola marked this pull request as ready for review April 9, 2024 18:04
@Schefflera-Arboricola Schefflera-Arboricola changed the title used add_edges_from instead of initialising with edges MAINT: used add_edges_from instead of initialising nx.Graph() with edges in cycles.py Apr 9, 2024

@rossbar rossbar left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's worth taking a look at to_networkx_graph itself to see if that can't be made not to warn. The Graph(<edgelist>) pattern is extremely common, and converting all of those to use .add_edges_from explicitly is likely a lot of churn!

@Schefflera-Arboricola

Copy link
Copy Markdown
Member Author

IMO it's worth taking a look at to_networkx_graph itself to see if that can't be made not to warn. The Graph(<edgelist>) pattern is extremely common, and converting all of those to use .add_edges_from explicitly is likely a lot of churn!

I initially did think of modifying to_networkx_graph by moving the check for the list, tuples, generators, etc. on the top but there was a note in the comments(which you added in PR #4136) telling specifically to keep it at the end.

Another thought was to remove all the warnings and just have a pass in the except ImportError block and it made sense(to me) because if someone doesn't have pandas installed then they probably wouldn't have been able to make a pd.DataFrame object in the first place, so then our warning seems unnecessary. And, removing all warning messages wouldn't over-populate the output but I'm not sure if this would impact the execution time(like in the case in issue #7401). LMK what you think.

Thanks!

@albertferras-vrf

albertferras-vrf commented Apr 10, 2024 •

Copy link
Copy Markdown

Is there a reason why the import is done on every call to to_network_graph instead of a try import/except at module level? Is that intentional? It seems like this is done to accept installing these packages after an interpreter is already open with networkx already imported (but numpy/pandas/.. not installed at that time).

Maybe there are better ways to check if the graph data is a numpy/pandas/scipy object without having to try importing the module.

@rossbar

rossbar commented Apr 10, 2024

Copy link
Copy Markdown
Contributor

by moving the check for the list, tuples, generators, etc. on the top but there was a note in the comments(which you added in PR #4136) telling specifically to keep it at the end.

Yes, I don't think the entire conditional can be moved as-is, however it might be worth trying adding an additional condition before the attempted imports specifically for edge lists.

Is there a reason why the import is done on every call to to_network_graph instead of a try import/except at module level? Is that intentional?

Yes, this is intentional - numpy, scipy, and pandas are soft dependencies and everything is lazy-loaded, so only functions that actually use these libraries attempt to import them.

@rossbar

rossbar commented Apr 10, 2024

Copy link
Copy Markdown
Contributor

FWIW this definitely extends beyond just simple_cycles. To test:

  1. Setup a development environment with only networkx+test dependencies installed, e.g.
python -m venv nx-only
source nx-only/bin/activate
pip install -r requirements/test.txt -e .
  1. Run the test suite, elevating all warnings to errors:
pytest -Werror --doctest-modules --pyargs networkx

This gives ~500 errors/failures, most of which are related to the ImportWarnings from pandas.

So - there are a couple options here; replacing the warnings with pass will cut down on verbosity and should alleviate any performance concerns given the inherent caching for module lookup (should be validated with the benchmark suite). Alternatively, the logic of to_networkx_graph can be re-organized to handle Python built-ins (i.e. lists, dicts, and NX-native objects like graphs and edge views) first.

@dschult

dschult commented Apr 11, 2024

Copy link
Copy Markdown
Member

Has anyone tried just removing the import and the isinstance check? The call to e.g. from_pandas_* should raise an exception -- I suspect due to a missing attribute most of the time. The questions is whether that messes up the input object.

The type-based dispatching here didn't used to be the mechanism used. And if we don't use it we do get rid of the import being tried upon each construction. BUT is that really the problem here?

The reason for using e.g import pandas as a way to check if the input is of pandas type is that importing a package that isn't installed is pretty fast. It shouldn't matter even if you do it 1000 times. I suspect that in the case where 90% of the time is spent creating the warnings that is primarily due to the fact that the test is so small. The warnings are not taking very long on an absolute scale, and if you were doing a bigger task you wouldn't notice the time they take. But I haven't actually checked it -- that's just my intuition.

So, I will ask a perhaps silly question @albertferras-vrf : Is the problem that the failed imports take too long, or that the warnings pollute the log?

To stop the pollution of the logs, can we set a warnings filter so they don't go into the log?
Does that solve your issues? Or is it too slow?

And, there is, of course, a way to write the code so the import is tried at the module level with different versions of the function being defined based on what can be imported. But I suspect that makes the code pretty ugly. So, let's identify the problem first. Is it the speed or the logging pollution that is the issue here? Or are there two issues? :)

@albertferras-vrf

Copy link
Copy Markdown

So, I will ask a perhaps silly question @albertferras-vrf : Is the problem that the failed imports take too long, or that the warnings pollute the log?

The main reason I created the issue #7401 was due to all the warnings that appeared in the logs.
However, since I saw that the to_networkx_graph attempted to import numpy/pandas on each call I also made a benchmark out of curiosity to see how much time is spent on importing and saw that it took ~90% of the total execution time (0.871 of 0.958s for 1000 construction calls). Since that looks odd, I also reported it.
So yes, 2 separate issues

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

simple_cycles attempts to import multiple libraries on every call

4 participants