-
Notifications
You must be signed in to change notification settings - Fork 17
randomize weights: added checking eighted network #32
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
lfo-po
wants to merge
5
commits into
netsiphd:main
Choose a base branch
from
lfo-po: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
5 commits
Select commit
Hold shift + click to select a range
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,93 @@ | ||
| from .base import BaseRewirer | ||
| import copy | ||
| import itertools as it | ||
| import random | ||
| import networkx as nx | ||
| import numpy as np | ||
|
|
||
|
|
||
| class RandomizedWeightCM_swap(BaseRewirer): | ||
| """ | ||
| Swap weights of a weighted network without rewiring edges. | ||
|
|
||
| - rewire_step: Swap weights bewteen two randomly chosen edges | ||
| - rewire: Over the list of edges, permutate the list of associated weigths | ||
|
|
||
| 4th method is from Ghavasieh, A.; De Domenico, M. | ||
| "Multiscale Information Propagation in Emergent Functional Networks". | ||
| Entropy 2021, 23, 1369. https://doi.org/10.3390/e23101369 | ||
| """ | ||
|
|
||
| def edge_pair_random_choice(self, G): | ||
| e_list = list(G.edges(data=True)) | ||
| e_1 = random.choice(e_list) | ||
| e_list.remove(e_1) | ||
| e_2 = random.choice(e_list) | ||
|
|
||
| return e_1, e_2 | ||
|
|
||
| def step_rewire(self, G, copy_graph=True): | ||
| if copy_graph: | ||
| G = copy.deepcopy(G) | ||
|
|
||
| e_1, e_2 = self.edge_pair_random_choice(G) | ||
|
|
||
| w_1 = e_1[2]["weight"] | ||
| w_2 = e_2[2]["weight"] | ||
|
|
||
| G.edges[e_1[0], e_1[1]]["weight"] = w_2 | ||
| G.edges[e_2[0], e_2[1]]["weight"] = w_1 | ||
|
|
||
| return G | ||
|
|
||
| def full_rewire(self, G, copy_graph=True): | ||
| if copy_graph: | ||
| G = copy.deepcopy(G) | ||
|
|
||
| e_list = list(G.edges()) | ||
| w_list = [x[2]["weight"] for x in list(G.edges(data=True))] | ||
|
|
||
| w_list = np.random.permutation(w_list) | ||
| nx.set_edge_attributes(G, dict(zip(e_list, w_list)), "weight") | ||
|
|
||
| return G | ||
|
|
||
|
|
||
| class RandomizedWeightCM_redistribution(BaseRewirer): | ||
| """ | ||
| Redistribute weights of a weighted network without rewiring edges. | ||
|
|
||
| - rewire_step: the total sum of weight of a randomly chosen pair of links is randomly re-distributed over this two links | ||
| - rewire: The total sum of weights of all links in the netwrok is randomly distributed over the links | ||
| """ | ||
|
|
||
| def step_rewire(self, G, copy_graph=True): | ||
| if copy_graph: | ||
| G = copy.deepcopy(G) | ||
|
|
||
| e_1, e_2 = self.edge_pair_random_choice(G) | ||
|
|
||
| a_1 = random.random() | ||
|
|
||
| w_sum = e_1[2]["weight"] + e_2[2]["weight"] | ||
|
|
||
| G.edges[e_1[0], e_1[1]]["weight"] = a_1 * w_sum | ||
| G.edges[e_2[0], e_2[1]]["weight"] = (1 - a_1) * w_sum | ||
|
|
||
| return G | ||
|
|
||
| def full_rewire(self, G, copy_graph=True): | ||
| if copy_graph: | ||
| G = copy.deepcopy(G) | ||
|
|
||
| alphas = np.random.rand(len(G.edges())) | ||
| alphas = alphas / np.sum(alphas) | ||
|
|
||
| w = [x[2]["weight"] for x in list(G.edges(data=True))] | ||
| w_sum = np.sum(w) | ||
| aw = alphas * w_sum | ||
|
|
||
| e_list = list(G.edges()) | ||
| nx.set_edge_attributes(G, dict(zip(e_list, aw)), "weight") | ||
|
|
||
| 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.
For the
step_rewirefunctions here and below, can you defaultcopy_graph=Falseinstead? Then forfull_rewire, we're defaulting to True