-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_timeline.py
executable file
·76 lines (56 loc) · 1.95 KB
/
cluster_timeline.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
#!/usr/bin/env python
"""
usage: cluster_timeline.py [-h] <evolution_dictionary>
Calculate Louvain clusters on a graph, given as an edge list
positional arguments:
<network> A file with the specification of the network as an edge list
optional arguments:
-h, --help show this help message and exit
"""
import os
import csv
import json
import argparse
import logging
########## logging
# create logger with 'spam_application'
logger = logging.getLogger(__file__)
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('louvain_clusters.log')
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(fh)
logger.addHandler(ch)
##########
def get_args():
description=('Create a timeline with clusters')
parser = argparse.ArgumentParser(description=description)
parser.add_argument('evodict', metavar='<evolution_dictionary>',
help='A file with the dictionary and the clusters')
args = parser.parse_args()
return args
def main():
args = get_args()
logger.info('Start')
evodict = dict()
with open(args.evodict, 'r') as evodict_file:
evodict = json.load(evodict_file)
evo_basename = os.path.basename(args.evodict).split('.')[0]
evo_path = os.path.join('data', '{}.timeline.data'.format(evo_basename))
with open(evo_path, 'w+') as evo_file:
writer = csv.writer(evo_file, delimiter='\t')
for date, compare_dict in evodict.items():
for k, v in compare_dict.items():
writer.writerow((date, v))
# import ipdb; ipdb.set_trace()
logger.info('Done!')
if __name__ == '__main__':
main()