-
Notifications
You must be signed in to change notification settings - Fork 14
/
simulate_analyze.py
118 lines (84 loc) · 2.19 KB
/
simulate_analyze.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
import glob
import numpy
import re
import sys
import os
from matplotlib import pyplot
def get_ccdf(x):
xs = numpy.array(x)
xs.sort()
N = float(len(xs))
P = numpy.arange(N)/N
return xs, P
def get_gamma0(campaign_glob, Pfa=0.1):
path = campaign_glob.replace("*", "off")
gammaN = numpy.loadtxt(path)
gammaN, Pd = get_ccdf(gammaN)
gamma0 = numpy.interp(1. - Pfa, Pd, gammaN)
return gamma0
def iterate_campaign(path):
for fn in glob.glob(path):
g = re.search("_m([0-9_]+)dbm\.dat$", fn)
if g:
Pg = -float(g.group(1).replace('_', '.'))
gamma = numpy.loadtxt(fn)
yield Pg, gamma
def get_campaign_g(path, gamma0):
Pg = []
Pd = []
for Pg0, gamma in iterate_campaign(path):
Pg.append(Pg0)
Pd.append(numpy.mean(gamma > gamma0))
Pg = numpy.array(Pg)
Pd = numpy.array(Pd)
Pga = Pg.argsort()
Pd = Pd[Pga]
Pg = Pg[Pga]
return Pg, Pd
def get_campaign(path, gamma0):
Pg, Pd = get_campaign_g(path, gamma0)
Pin = Pg
return Pin, Pd
def get_pinmin(campaign_glob, gamma0, Pdmin, figdir):
Pin, Pd = get_campaign(campaign_glob, gamma0)
figname = os.path.basename(campaign_glob).replace("_*.dat", ".png")
figpath = os.path.join(figdir, figname)
pyplot.figure()
pyplot.plot(Pin, Pd)
pyplot.xlabel("Pin")
pyplot.ylabel("Pd")
pyplot.axis([None, None, 0, 1])
pyplot.title(os.path.basename(campaign_glob))
pyplot.grid()
pyplot.savefig(figpath)
pyplot.close()
Pinmin = numpy.interp(Pdmin, Pd, Pin, left=0, right=0)
return Pinmin
def process_campaign(campaign_glob, fout, figdir):
gamma0 = get_gamma0(campaign_glob)
Pinmin = get_pinmin(campaign_glob, gamma0, .9, figdir)
fout.write("%s\t%f\n" % (campaign_glob, Pinmin))
def main():
try:
indir = sys.argv[1]
except IndexError:
print "USAGE: %s dir_to_analyze" % (sys.argv[0],)
return
outdir = os.path.join(indir, "ana")
figdir = os.path.join(indir, "fig")
try:
os.mkdir(outdir)
except OSError:
pass
try:
os.mkdir(figdir)
except OSError:
pass
fout = open("%s/pinmin.dat" % (outdir,), "w")
for path in glob.glob("%s/dat/*_off.dat" % (indir,)):
campaign_glob = path.replace("_off.", "_*.")
print campaign_glob
process_campaign(campaign_glob, fout, figdir)
fout.close()
if __name__ == '__main__':
main()