-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfomapClustersImages.py
56 lines (48 loc) · 2.18 KB
/
infomapClustersImages.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
import sys
import numpy as np
import os
import pandas as pd
import random
from PIL import Image
def generate_picture(infomapDir):
row = 130
col= 172
files = [f for f in os.listdir(os.path.join(infomapDir)) if os.path.isfile(os.path.join(infomapDir, f))]
if not os.path.exists(os.path.join(infomapDir, 'img')):
print('Creating folder ' + os.path.join(infomapDir, 'img'))
os.mkdir(os.path.join(infomapDir, 'img'))
for f in files:
# if community file and that timestamp
if f.endswith(".clu"):
#create matrix
image = np.zeros((row,col,3), 'uint8')
df = pd.read_csv(os.path.join(infomapDir, f), sep=' ', header=None, skiprows = 2)
df.sort_values(by=1,inplace=True) #order by community
#randomly choose colors for first cluster
colorR = random.sample(range(0,256),1)[0]
colorG = random.sample(range(0,256),1)[0]
colorB = random.sample(range(0,256),1)[0]
oldCommId = 1 #first comm always one
for riga in df.iterrows():
pixelId = int(riga[1][0])
commId = int(riga[1][1])
if commId != oldCommId: #select new colors for the new community
colorR = random.sample(range(0,256),1)[0]
colorG = random.sample(range(0,256),1)[0]
colorB = random.sample(range(0,256),1)[0]
pixelI = pixelId // col
pixelJ = pixelId % col
image[pixelI,pixelJ,:]=[colorR,colorG,colorB]
img = Image.fromarray(image,'RGB')
timestamp = int(f.split('-t')[1].split('.clu')[0])
img.save(os.path.join(infomapDir, 'img', str(timestamp)) + '.tif')
##################################################
def startme():
#folder = 'C:/Users/Umberto/Desktop'
folder = sys.argv[1]
print('Looking for resultInfomap in '+folder)
for dir in os.listdir(folder):
if dir == 'resultInfomap':
print(dir)
generate_picture(os.path.join(folder,dir))
###################################