-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_mod_assignments.py
52 lines (45 loc) · 1.9 KB
/
get_mod_assignments.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
# -*- coding: utf-8 -*-
"""
Created on Wed May 13 13:46:38 2015
@author: andric
"""
import os
import numpy as np
import pandas as pd
import networkx as nx
if __name__ == '__main__':
dat_dir = os.environ['pnd']
modularity_dir = dat_dir+'/modularity'
graph_dir = dat_dir+'/graphs'
dat_file = 'pandit_data.csv'
df = pd.read_csv(os.path.join(dat_dir, dat_file))
ctnames = pd.Series(df.columns[1:])
subj_list = ['pandit', 'ctrl']
cols = ['nodes', 'coms']
dfcols = ['region', 'community']
for subjid in subj_list:
for thresh_dens in np.arange(.1, .51, .1):
q_name = '%s.dens_%s.Qval' % (subjid, thresh_dens)
qscores = np.loadtxt(os.path.join(modularity_dir, q_name))
maxiter = qscores.argmax()
t_name = '%s.dens_%s.trees' % (subjid, thresh_dens)
trees = np.loadtxt(os.path.join(modularity_dir, t_name))
g_name = '%s.dens_%s.edgelist.gz' % (subjid, thresh_dens)
g = nx.read_edgelist(os.path.join(graph_dir, g_name), nodetype=int)
tmp_mx = np.column_stack((np.arange(148), trees[:, maxiter]))
max_tree = pd.DataFrame(tmp_mx, columns=cols)
max_tree = max_tree.iloc[g.nodes(), :]
# max_tree = trees[:, maxiter]
nnames = []
modid = []
for com in set(max_tree.loc[:, 'coms']):
ns = max_tree.loc[:, 'nodes'][max_tree.loc[:, 'coms'] == com]
list_nodes = map(int, ns)
nnames.append(ctnames[list_nodes])
modid.append(np.repeat(com, len(list_nodes)))
nnames = list(np.concatenate(nnames))
modid = list(np.concatenate(modid))
newd = pd.DataFrame(zip(nnames, modid), columns=dfcols)
mod_list_name = '%s.inclusionlist.dens_%s.csv' % \
(subjid, thresh_dens)
newd.to_csv(os.path.join(dat_dir, mod_list_name))