-
Notifications
You must be signed in to change notification settings - Fork 25
refactor: separate statistic computation #411
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
tristan-f-r
wants to merge
18
commits into
main
Choose a base branch
from
lazy-stats
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6ec4f62
refactor: separate statistic computation
tristan-f-r 9987189
fix: correct tuple assumption
tristan-f-r 25eef5e
fix: stably use graph statistic values
tristan-f-r cb373c1
style: fmt
tristan-f-r 47a9e26
Merge branch 'main' into lazy-stats
tristan-f-r 898d568
style: specify zip strict
tristan-f-r c675ece
fix: make undirected for determining number of connected components
tristan-f-r 3c81d05
Merge branch 'main' into lazy-stats
tristan-f-r 1ca730e
feat: snakemake-based summary generation
tristan-f-r d67186d
fix(Snakefile): use parse_output for edgelist parsing
tristan-f-r fd483c3
fix: parse edgelist with rank, embed header skip inside from_edgelist
tristan-f-r fd5046f
style: fmt
tristan-f-r 79cf748
chore: mention statistics_files param
tristan-f-r 339d915
Merge branch 'hash' into lazy-stats
tristan-f-r 85e0ea8
docs: more info on summary & statistics
tristan-f-r 804849a
style: fmt
tristan-f-r cf3c6a0
Merge branch 'hash' into lazy-stats
tristan-f-r 0f7acca
Merge remote-tracking branch 'upstream/main' into lazy-stats
tristan-f-r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| """ | ||
| Graph statistics, used to power summary.py. | ||
tristan-f-r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| We allow for arbitrary computation of any specific statistic on some graph, | ||
| computing more than necessary if we have dependencies. See the top level | ||
| `statistics_computation` dictionary for usage. | ||
|
|
||
| To make the statistics allow directed graph input, they will always take | ||
| in a networkx.DiGraph, which contains even more information, even though | ||
| the underlying graph may be just as easily represented by networkx.Graph. | ||
| """ | ||
|
|
||
| import itertools | ||
| from statistics import median | ||
| from typing import Callable | ||
|
|
||
| import networkx as nx | ||
|
|
||
|
|
||
| def compute_degree(graph: nx.DiGraph) -> tuple[int, float]: | ||
| """ | ||
| Computes the (max, median) degree of a `graph`. | ||
| """ | ||
| # number_of_nodes is a cheap call | ||
| if graph.number_of_nodes() == 0: | ||
| return (0, 0.0) | ||
| else: | ||
| degrees = [deg for _, deg in graph.degree()] | ||
| return max(degrees), median(degrees) | ||
|
|
||
| def compute_on_cc(directed_graph: nx.DiGraph) -> tuple[int, float]: | ||
| # We convert our directed_graph to an undirected graph as networkx (reasonably) does | ||
| # not allow for computing the connected components of a directed graph, but the connected | ||
| # component count still is a useful statistic for us. | ||
| graph: nx.Graph = directed_graph.to_undirected() | ||
tristan-f-r marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| cc = list(nx.connected_components(graph)) | ||
| # Save the max diameter | ||
| # Use diameter only for components with ≥2 nodes (singleton components have diameter 0) | ||
| diameters = [ | ||
| nx.diameter(graph.subgraph(c).copy()) if len(c) > 1 else 0 | ||
| for c in cc | ||
| ] | ||
| max_diameter = max(diameters, default=0) | ||
|
|
||
| # Save the average path lengths | ||
| # Compute average shortest path length only for components with ≥2 nodes (undefined for singletons, set to 0.0) | ||
| avg_path_lengths = [ | ||
| nx.average_shortest_path_length(graph.subgraph(c).copy()) if len(c) > 1 else 0.0 | ||
| for c in cc | ||
| ] | ||
|
|
||
| if len(avg_path_lengths) != 0: | ||
| avg_path_len = sum(avg_path_lengths) / len(avg_path_lengths) | ||
| else: | ||
| avg_path_len = 0.0 | ||
|
|
||
| return max_diameter, avg_path_len | ||
|
|
||
| # The type signature here is meant to be 'an n-tuple has n-outputs.' | ||
| statistics_computation: dict[tuple[str, ...], Callable[[nx.DiGraph], tuple[float | int, ...]]] = { | ||
| ('Number of nodes',): lambda graph : (graph.number_of_nodes(),), | ||
| ('Number of edges',): lambda graph : (graph.number_of_edges(),), | ||
| ('Number of connected components',): lambda graph : (nx.number_connected_components(graph.to_undirected()),), | ||
| ('Density',): lambda graph : (nx.density(graph),), | ||
| ('Max degree', 'Median degree'): compute_degree, | ||
| ('Max diameter', 'Average path length'): compute_on_cc, | ||
| } | ||
|
|
||
| # All of the keys inside statistics_computation, flattened. | ||
| statistics_options: list[str] = list(itertools.chain(*(list(key) for key in statistics_computation.keys()))) | ||
|
|
||
| def from_output_pathway(lines) -> nx.Graph: | ||
| with open(lines, 'r') as f: | ||
| lines = f.readlines()[1:] | ||
|
|
||
| return nx.read_edgelist(lines, data=(('Rank', int), ('Direction', str))) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.