-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
2,680 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,83 @@ | ||
import networkx | ||
import numpy | ||
import os | ||
import pandas | ||
import sys | ||
|
||
from multixrank.logger_setup import logger | ||
|
||
|
||
class Bipartite: | ||
|
||
"""Multiplex layer""" | ||
|
||
def __init__(self, key, abspath, graph_type, self_loops): | ||
|
||
""" | ||
Args: | ||
abspath: str | ||
existing absolute path | ||
graph_type: str | ||
takes values 00=(unweighted, undirected), 01=(unweighted, directed), | ||
10=(weighted, undirected), 11=(weighted, directed) | ||
""" | ||
|
||
self.key = key | ||
self.abspath = abspath | ||
if not os.path.isfile(abspath): # error if path not exist | ||
logger.error("This path does not exist: {}".format(abspath)) | ||
sys.exit(1) | ||
|
||
if not (graph_type in ['00', '10', '01', '11']): | ||
logger.error('MultiplexLayer multigraph type must take one of these values: 00, 10, 01, 11. ' | ||
'Current value: {}'.format(graph_type)) | ||
sys.exit(1) | ||
self.graph_type = graph_type | ||
self.self_loops = self_loops | ||
|
||
self._networkx = None | ||
|
||
@property | ||
def networkx(self) -> networkx.Graph: | ||
"""Converts layer to multigraph networkx object""" | ||
|
||
if self._networkx is None: | ||
|
||
names = ['col2', 'col1'] # layer file column labels changed | ||
dtype = str | ||
edge_attr = ['network_key'] | ||
usecols = [0, 1] # two cols like in unweighted | ||
if self.graph_type[1] == '1': # weighted layer | ||
names = ['col2', 'col1', 'weight'] # changed | ||
dtype = {'col1': str, 'col2': str, 'weight': numpy.float64} | ||
edge_attr = ['network_key', 'weight'] | ||
usecols = [0, 1, 2] # two cols like in unweighted | ||
|
||
networkx_graph_obj = networkx.Graph() # layer file column labels | ||
if self.graph_type[0] == '1': # directed layer | ||
networkx_graph_obj = networkx.DiGraph() | ||
|
||
multiplex_layer_edge_list_df = pandas.read_csv(self.abspath, sep="\t", header=None, names=names, dtype=dtype, usecols=usecols) | ||
# remove df lines with self-loops, ie source==target | ||
if not self.self_loops: | ||
multiplex_layer_edge_list_df = multiplex_layer_edge_list_df.loc[ | ||
~(multiplex_layer_edge_list_df.col1 == multiplex_layer_edge_list_df.col2)] | ||
multiplex_layer_edge_list_df['network_key'] = self.key | ||
|
||
self._networkx = networkx.from_pandas_edgelist( | ||
df=multiplex_layer_edge_list_df, source='col2', target='col1', | ||
edge_attr=edge_attr, create_using=networkx_graph_obj) # changed | ||
|
||
self._networkx.remove_edges_from(networkx.selfloop_edges(self._networkx)) | ||
|
||
# networkx has no edges | ||
# TODO replace edges with nodes | ||
if len(self._networkx.edges()) == 0: | ||
logger.error( | ||
'The following bipartite graph does not return any edge: {}'.format( | ||
self.key)) | ||
sys.exit(1) | ||
|
||
return self._networkx |
This file contains 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,106 @@ | ||
import networkx | ||
import numpy | ||
import scipy | ||
|
||
|
||
class BipartiteAll: | ||
"""Deals with list of bipartites""" | ||
|
||
def __init__(self, source_target_bipartite_dic, multiplexall): | ||
|
||
self.source_target_bipartite_dic = source_target_bipartite_dic | ||
self.multiplexall = multiplexall | ||
|
||
self.multiplex_layer_count_list2d = [len(multiplexall.layer_tuple) | ||
for multiplexall in | ||
multiplexall.multiplex_tuple] | ||
self.multiplexall_node_list2d = [multiplexall.nodes for | ||
multiplexall in | ||
multiplexall.multiplex_tuple] | ||
self.multiplex_dic = dict() | ||
for multiplexone_obj in self.multiplexall.multiplex_tuple: | ||
self.multiplex_dic[multiplexone_obj.key] = multiplexone_obj | ||
|
||
self._bipartite_matrix = None | ||
self._graph = None # Networkx with all undirected edges in Bipartites | ||
self._digraph = None # Networkx with all directed edges in Bipartites | ||
|
||
@property | ||
def graph(self): | ||
"""Returns graph with all undirected edges in all bipartites""" | ||
|
||
if self._graph is None: | ||
self._graph = networkx.Graph() | ||
for edge_tple in self.source_target_bipartite_dic: | ||
bipartite_obj = self.source_target_bipartite_dic[edge_tple] | ||
if isinstance(bipartite_obj.networkx, networkx.Graph): | ||
edge_data_lst = [(u, v, bipartite_obj.networkx[u][v]) for u, v in bipartite_obj.networkx.edges] | ||
self._graph.add_edges_from(edge_data_lst) | ||
return self._graph | ||
|
||
@property | ||
def digraph(self): | ||
"""Returns graph with all directed edges in all bipartites""" | ||
|
||
if self._digraph is None: | ||
self._digraph = networkx.DiGraph() | ||
for edge_tple in self.source_target_bipartite_dic: | ||
bipartite_obj = self.source_target_bipartite_dic[edge_tple] | ||
if isinstance(bipartite_obj.networkx, networkx.Graph): | ||
edge_data_lst = [(u, v, bipartite_obj.networkx[u][v]) for u, v in bipartite_obj.networkx.edges] | ||
self._digraph.add_edges_from(edge_data_lst) | ||
return self._digraph | ||
|
||
@property | ||
def bipartite_matrix(self): | ||
"""""" | ||
|
||
####################################################################### | ||
# | ||
# Will add B bipartite multigraph matrix to each B_i_j with i!=j | ||
# | ||
####################################################################### | ||
|
||
if self._bipartite_matrix is None: | ||
|
||
multiplexall_supra_adj_matrix_list = [] | ||
for i, multiplex_obj in enumerate(self.multiplexall.multiplex_tuple): | ||
multiplexall_supra_adj_matrix_list.append(multiplex_obj.supra_adj_matrixcoo) | ||
|
||
self._bipartite_matrix = numpy.zeros((len(self.multiplex_layer_count_list2d), len(self.multiplex_layer_count_list2d)), dtype=object) | ||
|
||
for i, multiplexone_obj1 in enumerate(self.multiplexall.multiplex_tuple): | ||
for j, multiplexone_obj2 in enumerate(self.multiplexall.multiplex_tuple): | ||
multiplex_key1 = multiplexone_obj1.key | ||
multiplex_key2 = multiplexone_obj2.key | ||
|
||
if not (multiplex_key1 == multiplex_key2): | ||
if (multiplex_key1, multiplex_key2) in self.source_target_bipartite_dic: | ||
bipartite_layer_obj = self.source_target_bipartite_dic[(multiplex_key1, multiplex_key2)] | ||
bipartite_layer_networkx = bipartite_layer_obj.networkx | ||
bipartite_layer_networkx.remove_edges_from(networkx.selfloop_edges(bipartite_layer_networkx)) | ||
two_multiplex_nodes = multiplexone_obj1.nodes + multiplexone_obj2.nodes | ||
bipartite_layer_networkx_nodes = bipartite_layer_networkx.nodes | ||
new_bipartite_layer_networkx_nodes = two_multiplex_nodes - bipartite_layer_networkx_nodes | ||
bipartite_layer_networkx.add_nodes_from(new_bipartite_layer_networkx_nodes) | ||
B = networkx.to_scipy_sparse_array(bipartite_layer_networkx, nodelist=two_multiplex_nodes, format="csr") | ||
|
||
self._bipartite_matrix[j, i] = B[0:len(self.multiplexall_node_list2d[i]), len(self.multiplexall_node_list2d[i])::].T # changed | ||
if bipartite_layer_networkx.is_directed() == False : # changed | ||
self._bipartite_matrix[i, j] = self._bipartite_matrix[j, i].T # changed | ||
for i in range(len(self.multiplex_layer_count_list2d)): | ||
for j in range(len(self.multiplex_layer_count_list2d)): | ||
if i != j: | ||
if isinstance(self._bipartite_matrix[i, j], int): | ||
row = numpy.shape(multiplexall_supra_adj_matrix_list[i])[0] | ||
col = numpy.shape(multiplexall_supra_adj_matrix_list[j])[1] | ||
self._bipartite_matrix[i, j] = scipy.sparse.coo_matrix((row, col)) | ||
else: | ||
temp = numpy.zeros((self.multiplex_layer_count_list2d[i], | ||
self.multiplex_layer_count_list2d[j]), | ||
dtype=object) | ||
for k in range(self.multiplex_layer_count_list2d[i]): | ||
temp[k] = self._bipartite_matrix[i, j] | ||
self._bipartite_matrix[i, j] = scipy.sparse.bmat(temp, format='coo') | ||
|
||
return self._bipartite_matrix |
Oops, something went wrong.