-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_run_MMAS.py
164 lines (129 loc) · 4.93 KB
/
first_run_MMAS.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: manuelalautizi
"""
import os
os.chdir('...')
import pandas as pd
import numpy as np
import networkx as nx
import random
import Library_MMAS as MMAS
from importlib import reload
reload(MMAS)
# =============================================================================
# Simulated data
# =============================================================================
# Adjacency matrix simulated network
sim_net = pd.read_csv(".../netSIM.csv")
# Matrix with simulated gene expression, survival time and censor data
sim_gx = pd.read_csv(".../gxSIM.csv")
sim = nx.Graph()
for i in sim_net.columns.tolist():
for j in sim_net.columns.tolist():
if sim_net.loc[int(i), j] > 0.00000:
sim.add_edge(int(i),int(j))
#Parameters
ants = 30
trials = 30
K = 4
# Hyperparameters:
alpha = 1
beta = 1
rho = 0.3
ph_min = 0
ph_max = 1
# =============================================================================
# Assignment heuristic information to edges for the computation
# of best theoretical solution
# =============================================================================
# PCA
# =============================================================================
net_pca = sim.copy()
for u, v, w in net_pca.edges(data=True):
pvals = [MMAS.comp_lrt_pca(sim_gx, fts) for fts in [[u], [v], [u,v]]]
w['weight'] = np.min(pvals[:2]) / pvals[2]
best_theo_pca = MMAS.best_theo(net_pca, sim_gx, K, alg = "pca")
greedy_pca = MMAS.greedy_algorithm(sim_gx, net_pca, K, alg = "pca")
print("Best theo (PCA)", best_theo_pca)
print("Best greedy (PCA)", greedy_pca)
# =============================================================================
# K-Means
# =============================================================================
net_kmeans = sim.copy()
for u, v, w in net_kmeans.edges(data=True):
pvals = [MMAS.comp_lrt_kmeans(sim_gx, fts) for fts in [[u], [v], [u,v]]]
w['weight'] = np.min(pvals[:2]) / pvals[2]
best_theo_kmeans = MMAS.best_theo(net_kmeans, sim_gx, K, alg = "kmeans")
greedy_kmeans = MMAS.greedy_algorithm(sim_gx, net_kmeans, K, alg = "kmeans")
print("Best theo (kmeams)", best_theo_kmeans)
print("Best greedy (kmeans)", greedy_kmeans)
# =============================================================================
# Application
# =============================================================================
pca_time = []
kmeans_time = []
pca_iter = []
kmeans_iter = []
simulations = 10
for i in range(simulations):
sim_G = sim.copy()
print("--------PCA--------")
par_pca = alpha, beta, rho, ph_min, ph_max, sim_gx, sim_G, greedy_pca[1], best_theo_pca, ants, K, trials, i
antRes_pca, time_pca, trials_pca, best_pca = MMAS.MaxMin_AS_sim(par_pca, "pca")
print("-------KMEANS-------")
par_kmeans = alpha, beta, rho, ph_min, ph_max, sim_gx, sim_G, greedy_pca[1], best_theo_kmeans, ants, K, trials, i
antRes_kmeans, time_kmeans, trials_kmeans, best_kmeans = MMAS.MaxMin_AS_sim(par_kmeans, "kmeans")
pca_time.append(time_pca)
pca_iter.append(trials_pca)
# Jaccard
l = [best_pca[0][0][0]]
for i in range(len(best_pca[0])):
l.append(best_pca[0][i][1])
print("JACCARD", MMAS.jaccard(best_theo_pca[0][0], l)*100)
# =============================================================================
# Real Data
# =============================================================================
patients = pd.read_csv("...")
G = pd.read_csv("...")
G.columns = ["A", "B"]
G.head()
# CREATION OF THE GRAPH
tuples = [tuple(G.iloc[i]) for i in range(G.shape[0])]
G = nx.Graph(tuples)
# REMOVE NODES WITHOUT THE GE DATA
Vertices = list(G.nodes())
V = list(set(Vertices).intersection([i for i in map(int, patients.columns.tolist()[3:])])) #3
# i remove from the graph the nodes not in that list (V)
G.remove_nodes_from(list(set(G.nodes())^set(V)))
# remove nodes with degree 0
G.remove_nodes_from(list(nx.isolates(G)))
# =============================================================================
# Application
# =============================================================================
#Parameters
ants = 60
trials = 60
K = 10
# Hyperparameters:
alpha = 1
beta = 1
rho = 0.3
ph_min = 0
ph_max = 1
simulations = 10
for i in range(simulations):
Graph = G.copy()
param = alpha, beta, rho, ph_min, ph_max, patients, Graph, ants, K, trials, i
antRes_pca, time_pca, trials_pca, best_pca = MMAS.MaxMin_AS(param, "pca")
# =============================================================================
# If evaluation on a sample of the real data:
# Extraction subnetwork
# =============================================================================
size = 500
start = random.choice(list(G.nodes()))
nodes = MMAS.extract(G, start, size)
subG = G.subgraph(nodes)
nx.draw(subG)
subPatients = patients[[i for i in map(str, nodes)] + ['os_time', 'os_event']]