-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp_uniformity.py
98 lines (76 loc) · 2.61 KB
/
exp_uniformity.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
import os
import sys
import random
import time
import math
import numpy as np
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt
import u_time
from datetime import datetime
from models.buffed_RSSs import RSS, RSS2
from sampling_util import load_G, gen_all_ksub
def loss_uniform(samples, pi) -> float:
n_samples = np.sum(samples)
freq = samples / n_samples
loss = np.sum(np.abs(freq - pi)) / 2
return float(loss)
if __name__ == "__main__":
# load
data_name = sys.argv[1]
k = int(sys.argv[2])
model_name = sys.argv[3]
mixing_time_ratio = float(sys.argv[4]) if len(sys.argv) > 4 else 1.0
e = float(sys.argv[5]) if len(sys.argv) > 5 else 0.05
generating_ratio = int(sys.argv[6]) if len(sys.argv) > 6 else 100
# load edge file
G = load_G(data_name)
n = len(G)
m = len(G.edges())
all_ksub = gen_all_ksub(G, k)
n_omega = len(all_ksub)
# generate ratio for each subgraph
n_samples = generating_ratio * n_omega
pi = 1 / n_omega
print('arguments;')
print('data set :', data_name)
print("k :", k)
print("model_name :", model_name)
print("mixing_time_ratio:", mixing_time_ratio)
print("e :", e)
print("generating_ratio :", generating_ratio)
print("n=", n, "m=", len(nx.edges(G)), " k=", k)
print("actual number of k-subgraph:", n_omega)
print("n_samples:", n_samples)
model_name = sys.argv[3]
if model_name == "RSS":
sampler = RSS(G, e, preload_k=k, mixing_time_ratio=mixing_time_ratio)
elif model_name == "RSS+" or model_name == "RSS2":
sampler = RSS2(G, e, preload_k=k, mixing_time_ratio=mixing_time_ratio)
else:
raise ValueError("%s is not implemented" % model_name)
print("pi:", pi)
counts = np.zeros(n_omega)
start = time.time()
done = 0
# counting
while done < n_samples:
vs = sampler.uniform_state_sample(k, n_samples=min(int(k * (n_samples - done)), 1000000), only_accepted=True)
for i in vs:
counts[i] += 1
done = int(np.sum(counts))
if done == n_samples:
break
t = time.time() - start
et = t / done * n_samples
loss = loss_uniform(counts, pi)
print('%7d/%d %s estimated: %s loss:%7.5f' % (done, n_samples, u_time.time2str(t), u_time.time2str(et), loss))
spent_time = time.time() - start
print("over all time:" + u_time.time2str(spent_time))
# freq
freq = counts / n_samples
# loss
loss = loss_uniform(counts, pi)
print("loss:", loss)
print("should be smaller than e:", e)