-
Notifications
You must be signed in to change notification settings - Fork 17
Bipartite rewiring network with Guilherme #30
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
aleurbi
wants to merge
6
commits into
netsiphd:main
Choose a base branch
from
aleurbi:main
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
6 commits
Select commit
Hold shift + click to select a range
3194568
bipartite network rewiring
aleurbi 8839064
Merge branch 'netsiphd:main' into main
aleurbi 63bc26a
corrections suggested fixed
aleurbi 4acadb6
Merge branch 'main' of https://github.com/aleurbi/netrw into main
aleurbi 82ee0c8
Merge branch 'netsiphd:main' into main
aleurbi 546f298
bug fixed
aleurbi 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| from .base import BaseRewirer | ||
|
|
||
| import copy | ||
| import warnings | ||
|
|
||
| import numpy as np | ||
|
|
||
| import networkx as nx | ||
| from networkx.algorithms import bipartite | ||
|
|
||
| import random as rand | ||
|
|
||
|
|
||
| class RewireBipartite(BaseRewirer): | ||
| """ | ||
|
|
||
| The algorithm keep the bipartite structure, randomply choose the edges and swap them. | ||
| It stops according to an analitically foung max iter which allow to reach a wanded accuracy, | ||
| defined with the Jaccard Distance between network. | ||
|
|
||
| Ref: Andrea Gobbi, Francesco Iorio, Kevin J. Dawson, David C. Wedge, David Tamborero, Ludmil B. Alexandrov, Nuria Lopez-Bigas, Mathew J. Garnett, Giuseppe Jurman, Julio Saez-Rodriguez, Fast randomization of large genomic datasets while preserving alteration counts, Bioinformatics, Volume 30, Issue 17, 1 September 2014, Pages i617–i623, https://doi.org/10.1093/bioinformatics/btu474 | ||
|
|
||
| """ | ||
|
|
||
| def rewire(self, G): | ||
| return G | ||
|
|
||
| # Flag that checks if the program performed a single step rewire | ||
| step_rewire_flag = False | ||
| bipartite_flag = True | ||
|
|
||
| def max_iter(self, e, t, accuracy): | ||
| return int(np.ceil((e * (1 - e / t)) * np.log((1 - e / t) / accuracy) / 2.0)) | ||
|
|
||
| # Single step: Stochastic | ||
| # This method may return the same graph | ||
| def step_rewire(self, G, verbose = False): | ||
| self.step_rewire_flag = False | ||
|
|
||
| #check if still bipartite | ||
| if bipartite.is_bipartite(G) == False: | ||
|
|
||
| if verbose == True: | ||
| warnings.warn( | ||
| "This algorithm is designed for bipartite graphs.", | ||
| SyntaxWarning, | ||
| ) | ||
|
|
||
| self.bipartite_flag = False | ||
|
|
||
| return G | ||
|
|
||
| edges = list(G.edges()) | ||
| e = len(edges) | ||
|
|
||
| rand1 = rand.randint(0, e - 1) | ||
|
|
||
| while True: | ||
| rand2 = rand.randint(0, e - 1) | ||
|
|
||
| if rand1 != rand2: | ||
| break | ||
|
|
||
| a = edges[rand1][0] | ||
| c = edges[rand2][0] | ||
| b = edges[rand1][1] | ||
| d = edges[rand2][1] | ||
|
|
||
| if ( | ||
| a != c | ||
| and d != b | ||
| and G.has_edge(a, d) == False | ||
| and G.has_edge(c, b) == False | ||
| ): | ||
| G.remove_edge(a, b) | ||
| G.remove_edge(c, d) | ||
|
|
||
| G.add_edge(a, d) | ||
| G.add_edge(c, b) | ||
|
|
||
| self.step_rewire_flag = True | ||
|
|
||
| return G | ||
|
|
||
| def full_rewire(self, G, accuracy, timesteps=0, copy_graph=False, verbose=False): | ||
| # Test if the input is a bipartite graph | ||
| if bipartite.is_bipartite(G) == False: | ||
|
|
||
| if verbose == True: | ||
| warnings.warn( | ||
| "This algorithm is designed for bipartite graphs.", | ||
| SyntaxWarning, | ||
| ) | ||
|
|
||
| self.bipartite_flag = False | ||
|
|
||
| return G | ||
|
|
||
| # Copy the graph | ||
| if copy_graph == True: | ||
| G = copy.deepcopy(G) | ||
|
|
||
| # set the max_iter variable | ||
| edges = list(G.edges()) | ||
| e = len(edges) | ||
|
|
||
| X, Y = bipartite.sets(G) | ||
| nc = len(X) | ||
| nr = len(Y) | ||
| t = nc * nr | ||
|
|
||
| if timesteps == 0: | ||
| N = self.max_iter(e, t, accuracy) | ||
|
|
||
| else: | ||
| N = timesteps | ||
|
|
||
| if verbose == True: | ||
| warnings.warn( | ||
| "Warning: timesteps can different from the theoretical bound.", | ||
| SyntaxWarning, | ||
| ) | ||
|
|
||
| for n in range(N): | ||
| G = self.step_rewire(G) | ||
|
|
||
| # Guarantees that n is the number of succesfull rewirings | ||
| if self.step_rewire_flag == False: | ||
| n = n - 1 | ||
|
|
||
| return G | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a check to see whether the input graph is, in fact, bipartite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the step_rewire method