Skip to content

Commit

Permalink
update v.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
anthbapt authored Jul 13, 2023
1 parent 5dc7f38 commit a8e9f2c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
4 changes: 2 additions & 2 deletions multixrank/Bipartite.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ def networkx(self) -> networkx.Graph:
dtype = str
edge_attr = ['network_key']
usecols = [0, 1] # two cols like in unweighted
if self.graph_type[1] == '1': # weighted layer
if self.graph_type[1] == '1': # unweighted/weighted layer (0/1)
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
if self.graph_type[0] == '1': # undirected/directed layer (0/1)
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)
Expand Down
4 changes: 2 additions & 2 deletions multixrank/MultiplexLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ def networkx(self) -> networkx.Graph:
# unweighted vs weighted ######################################
edge_attr = ['network_key']
usecols = [0, 1] # two cols like in unweighted
if self.graph_type[1] == '1': # weighted layer
if self.graph_type[1] == '1': # unweighted/weighted layer (0/1)
name_lst = ['source', 'target', 'weight']
dtype_dic['weight'] = numpy.float64
edge_attr = ['network_key', 'weight']
usecols = [0, 1, 2] # three cols in weighted

# undirected vs directed ######################################
networkx_graph_obj = networkx.Graph() # layer file column labels
if self.graph_type[0] == '1': # directed layer
if self.graph_type[0] == '1': # undirected/directed layer (0/1)
networkx_graph_obj = networkx.DiGraph()

multiplex_layer_edge_list_df = pandas.read_csv(
Expand Down
27 changes: 19 additions & 8 deletions multixrank/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import numpy
import os
import copy
import pandas
import pathlib
import shutil
Expand All @@ -27,7 +28,7 @@ class Multixrank(object):
"""Main class to run the random walk with restart in universal multiplex networks"""


def __init__(self, config: str, wdir: str):
def __init__(self, config: str, wdir: str, pr):
"""
Constructs an object for the random walk with restart.
Expand All @@ -44,7 +45,7 @@ def __init__(self, config: str, wdir: str):

config_parser_obj = ConfigParser(config=config, wdir=wdir)
config_parser_obj.parse()

self.pr = pr
self.wdir = os.path.join(os.getcwd(), wdir)

if not os.path.isdir(self.wdir):
Expand Down Expand Up @@ -80,7 +81,6 @@ def __init__(self, config: str, wdir: str):

self.multiplex_layer_count_list = [len(multiplexone_obj.layer_tuple) for multiplexone_obj in multiplexall_obj.multiplex_tuple]

# self.multiplexall_node_2dlist = config_parser_obj.multiplexall_node_list2d
self.multiplexall_node_list2d = [multiplexone_obj.nodes for multiplexone_obj in multiplexall_obj.multiplex_tuple]

# self. N nb of nodes in each multiplex
Expand All @@ -92,8 +92,14 @@ def __init__(self, config: str, wdir: str):
# seed object from config_parser and properties
#
#######################################################################

self.seed_obj = config_parser_obj.seed_obj
if type(self.pr) == type(None):
self.seed_obj = config_parser_obj.seed_obj
else:
N = copy.deepcopy(self.multiplexall_node_count_list)
N.insert(0,0)
L = self.multiplex_layer_count_list
temp = [numpy.repeat((self.pr[numpy.sum(N[:i]):numpy.sum(N[:i+1])]/L[i-1]),L[i-1]) for i in range(1,len(L)+1)]
self.pr = numpy.concatenate(temp)

# 1.3.3 :
def __random_walk_restart(self, prox_vector, transition_matrixcoo, r):
Expand Down Expand Up @@ -144,8 +150,10 @@ def random_walk_rank(self) -> pandas.DataFrame:
transition_matrixcoo = transition_matrix_obj.transition_matrixcoo

# Get initial seed probability distribution
prox_vector, seed_score = self.seed_obj.get_seed_scores(transition=transition_matrixcoo)
# import pdb; pdb.set_trace()
if type(self.pr) == type(None):
prox_vector, seed_score = self.seed_obj.get_seed_scores(transition=transition_matrixcoo)
else:
prox_vector = self.pr
# Run RWR algorithm
rwr_ranking_lst = self.__random_walk_restart(prox_vector, transition_matrixcoo, self.r)
rwr_ranking_df = self.__random_walk_rank_lst_to_df(rwr_result_lst=rwr_ranking_lst)
Expand Down Expand Up @@ -205,7 +213,10 @@ def __random_walk_rank_lst_to_df(self, rwr_result_lst) -> pandas.DataFrame:
layer_lst = [item for subl in
[[layer.key] * len(multiplex.nodes) for layer in multiplex.layer_tuple] for
item in subl]
score = list(rwr_result_lst[i].T[0])
if type(self.pr) == type(None):
score = list(rwr_result_lst[i].T[0])
else:
score = list(rwr_result_lst[i].T)
rwrrestart_df = pandas.concat([rwrrestart_df, pandas.DataFrame(
{'multiplex': multiplex_label_lst, 'node': nodes, 'layer': layer_lst, 'score': score})], axis=0)
return rwrrestart_df
Expand Down

0 comments on commit a8e9f2c

Please sign in to comment.