Ticket #307 (reviewing enhancement)

Opened 4 years ago

Last modified 4 years ago

Add progress callback option for long running methods

Reported by: midgleyf@… Owned by: aric
Priority: normal Milestone: networkx-future
Component: networkx Version: devel
Severity: normal Keywords:
Cc:

Description

As discussed previously it would be nice to have progress/status reported while long operations ran. As a bonus the operations could be cancelable.

I propose the following callback mechanism:

  1. NetworkX methods that could run for a long time add a progress_callback parameter with a default value of None. For example:
    def closeness_centrality(G,v=None,weighted_edges=False, progress_callback=None):
        ...
  1. Callers of the methods can provide a callback function such as the following:
    def print_progress(message = None, fraction_complete = None)
        # Just dump the progress to stdout.
        print (message or "Calculating...") + ( "" if fraction_complete is None else " (%.1f%%)" % (fraction_complete * 100.0))
        return True

The message parameter would be a human-readable string for display to the user. The fraction_complete parameter would be a float between 0.0 and 1.0 or None (indicating an unknown amount of progress).

The callback function would also return a boolean to indicate whether or not the method should continue.

  1. The NetworkX methods should periodically call the callback with whatever progress is available. If the callback returns False then they should cease calculation and make no changes to the graph, etc. For example, closeness_centrality could add the following to its main loop:
    for n in G:
        if callable(progress_callback):
            if not progress_callback(message = "Calculating closeness centrality...", fraction_complete = float(len(closeness_centrality)) / len(G)):
                return {}

I have used this mechanism for a few methods on a locally modified version of NetworkX and it works well.

Remaining questions:

  1. Which methods need this?
  1. How much will it impact performance? Will some methods need to throttle their feedback, e.g. update progress at most x times per second?

Attachments

centrality.py (23.6 KB) - added by midgleyf 4 years ago.
Here's a version of centrality.py (based on 1.0.1 release) which adds progress callbacks for some of the methods.

Change History

Changed 4 years ago by midgleyf

Here's a version of centrality.py (based on 1.0.1 release) which adds progress callbacks for some of the methods.

comment:1 Changed 4 years ago by aric

  • Status changed from new to reviewing
Note: See TracTickets for help on using tickets.