Skip to content

Add config option to disable warning when using cached value - #7497

Merged
jarrodmillman merged 15 commits into
networkx:mainfrom
eriknw:config_warnings
Oct 10, 2024
Merged

jarrodmillman merged 15 commits into
networkx:mainfrom
eriknw:config_warnings

Conversation

@eriknw

@eriknw eriknw commented Jun 13, 2024 •

Copy link
Copy Markdown
Contributor

The warning was updated to show how to use the config setting:

nx.config.warnings.discard("cache")

Here is a way to ignore this warning using warnings module:

import warnings

warnings.filterwarnings('ignore', 'Using cached graph', UserWarning, "networkx.utils.backends")

There are a lot of options for how to spell and codify this behavior. I'm not sure what would be best, so this PR is an initial proposal. It can also generalize for other warnings if desired.

CC @rlratzel

eriknw added 2 commits June 13, 2024 15:07
The warning was updated to show how to use the config setting.
@dschult

dschult commented Jun 26, 2024

Copy link
Copy Markdown
Member

When the first caching warning shows up, the user no longer knows about caching (caching is "on" by default now -- they used to have to be turned on so the user knew they were doing that). So the current message does not start well for its new placement.

Can we change the first sentence to something like:
Converting the input graph to type and caching the converted graph to be used by other functions.

@dschult dschult added Dispatching Related to dispatching and backend support type: Enhancements labels Jun 26, 2024
@dschult

dschult commented Jun 26, 2024

Copy link
Copy Markdown
Member

And now that I said that, I'm not sure that this PR makes caching turned on by default. Is caching turned on by default?

@eriknw

eriknw commented Jun 26, 2024

Copy link
Copy Markdown
Contributor Author

And now that I said that, I'm not sure that this PR makes caching turned on by default. Is caching turned on by default?

#7498 enables the cache by default. I can merge the two PRs to consider them simultaneously.

I'll see about updating the first sentence and will resolve the merge conflict.

@eriknw

eriknw commented Jun 27, 2024

Copy link
Copy Markdown
Contributor Author

When the first caching warning shows up, the user no longer knows about caching (caching is "on" by default now -- they used to have to be turned on so the user knew they were doing that). So the current message does not start well for its new placement.

Can we change the first sentence to something like: Converting the input graph to type and caching the converted graph to be used by other functions.

What's wrong with starting with "Using cached graph..."? It begins with the most important thing: that a cached value is being used (and, hence, that a cache exists).

Your suggested intro doesn't make sense here--we are not actually doing the conversion. Do you want to add a warning message when values are added to the cache? This is something that should be more clear via introspection and/or logging.

There are a few other key points in the warning message that we can consider reorganizing if you'd like:

  • how to enable/disable caching
  • how to disable this warning
  • how to avoid invalidating the cache by manually mutating data
  • how to clear the cache manually

@dschult

dschult commented Jun 29, 2024

Copy link
Copy Markdown
Member

I think I just don't have it straight in my head when this message gets shown. If caching the graph conversions is turned on by default, then I think it is shown the first time a user calls a function that checks the cache (the first time they call a function that could cache a converted graph). That means a user reads about backends somewhere, installs cugraph or another backend and tries it. They get the warning. But they don't know anything about caching yet. As far as they know, they just turned on the backend. And now they are getting a warning about caching being done.

When caching is not turned on by default, then the user has to install the backend, play around a bit, find out about caching and turn it on, and that's when they get the warning message. So they know that caching is a thing when they get the message.

I was just thinking that if the user hasn't heard about caching before, we should introduce it slightly at the start of the warning.

@eriknw

eriknw commented Jul 1, 2024

Copy link
Copy Markdown
Contributor Author

I see, thanks @dschult. I added an opening sentence (that assumes #7498 will go in):

Note: conversions to backend graphs are saved to cache (G.__networkx_cache__ on the original graph) by default.

The warning is shown when a value from the cache will be used instead of doing a conversion.

Comment thread networkx/utils/backends.py Outdated
warning_message = (
f"Using cached graph for {backend_name!r} backend in "
"Note: conversions to backend graphs are saved to cache "
"(`G.__networkx_cache__` on the original graph) by default."

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.

I know this isn't the first time we've documented __networkx_cache__ but do we want to advertise our "private" members? Would it be better to just add a well-named property for formally inspecting things like the state of the cache now and document that? That would give us the freedom to possibly change things related to caching later without breaking docs/users.

OTOH, keeping it a dunder makes it obvious to a user that it's not related to "their graph" (IOW, it's NX bookkeeping and not user data), so I could go either way. I think I'm leaning toward making it a property though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't understand this suggestion, or maybe I just don't agree with it. Dunders aren't private. x.foo is public, x._foo is an implementation detail, x.__foo is private (and mangled), and x.__foo__ is a dunder. Dunder methods are different. They are good for protocols, even if the protocol is simply "here there be caching". I think it is helpful to indicate that the cache exists on the object, which is why I advertise it.

I think introducing e.g. G.cache is out of scope for this PR. Similarly, helper functions for working with the cache is another thing I think we've discussed (not today, but other days) could make sense, and when we have them we can update this message.

In other words, I don't think this feedback is actionable for this PR, but is something we can be aware of and discuss (and perhaps track) elsewhere.

Comment thread networkx/utils/configs.py Outdated
cache_converted_graphs=bool(os.environ.get("NETWORKX_CACHE_CONVERTED_GRAPHS", "")),
warnings={
x.strip()
for x in os.environ.get("NETWORKX_WARNINGS", "cache").split(",")

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.

Is this a new env var? I didn't notice it documented anywhere. Am I interpreting it right that you add a comma-separated list of "things" to ignore warnings for? If so, it seems like the name should be something more like NETWORK_WARNINGS_TO_DISCARD or something like that but better.

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.

Looks like I misinterpreted this and NETWORKX_WARNINGS is a list of things/categories for which warnings should be shown. I'm assuming this is then used to override configuration settings that have certain warnings disabled?

Either way, can you also update the docstring for NetworkXConfig to include this new env var to the list of existing env vars here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the set is positive, not negative. Do you think we should change it to be negative?

The docstring is already updated. Do you mean to update docs somewhere else?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

What about something like NETWORKX_WARNINGS_TO_IGNORE and/or nx.config.warnings_to_ignore? Is this more convenient to use? Would we also want to keep the current nx.config.warnings and to keep warnings and warnings_to_ignore consistent such that warnings ^ warnings_to_ignore == set() and warnings | warnings_to_ignore == all_warnings? When using environment variables, I do think choosing which ones to ignore is more convenient, but from Python I prefer nx.config.warnings.

Alternatively, is there a good name for a boolean config to enable/disable the specific warning about caching (i.e., to not use a more generic nx.config.warning approach)?

@eriknw

eriknw commented Jul 24, 2024

Copy link
Copy Markdown
Contributor Author

I changed the config to nx.config.warnings_to_ignore based on the discussion today in the networkx dispatching call and @rlratzel's comment above.

Comment thread networkx/utils/backends.py Resolved
Co-authored-by: Dan Schult <[email protected]>
@jarrodmillman jarrodmillman added this to the 3.4 milestone Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Dispatching Related to dispatching and backend support type: Enhancements

Development

Successfully merging this pull request may close these issues.

6 participants