-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping_nodes.py
80 lines (57 loc) · 2.08 KB
/
mapping_nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 19 13:20:41 2016
@author: madhurima
"""
import pandas as pd
def mapnodes_weight(file):
''' This function maps the node ids (either integers
or strings) which may or may not be continous to integers
starting from 0. Input file is in edgelist format with
first column as the source, second column the target
and the third one the edge weight.
'''
fn = open(file, 'r')
lines = fn.readlines()
l1 = []
l2 = []
l3 = []
for line in lines:
l = line.split()
l1.append(l[0])
l2.append(l[1])
l3.append(l[2])
c = {}
c['s'] = l1
c['t'] = l2
c['w'] = l3
cf = pd.DataFrame(c)
cf[['s', 't']] = pd.Series(cf[['s', 't']].stack().factorize()[0], index = cf[['s', 't']].stack().index).unstack()
##this will change only the 's' and 't' column keeping the third column unchanged
cf.to_csv('foo.txt', sep = '\t', header = False, index = False, encoding = 'utf-8')
## put the path along with the filename, if required. This will form the file 'foo.txt' in the same directory as file.txt.
fn.close()
def mapnodes(file):
''' This function maps the node ids (either integers
or strings) which may or may not be continous to integers
starting from 0. Input file is in edgelist format with
first column as the source, second column the target.
'''
fn = open(file, 'r')
lines = fn.readlines()
l1 = []
l2 = []
for line in lines:
l = line.split()
l1.append(l[0])
l2.append(l[1])
c = {}
c['s'] = l1
c['t'] = l2
cf = pd.DataFrame(c)
cf = pd.Series(cf.stack().factorize()[0], index = cf.stack().index).unstack()
cf.to_csv('foo.txt', sep = '\t', header = False, index = False, encoding = 'utf-8')
## put the path along with the filename, if required. This will form the file 'foo.txt' in the same directory as file.txt.
fn.close()
mapnodes('file.txt')
mapnodes_weight('file.txt')