draw_ert.py 2.64 KB
Newer Older
1
2
3
4
#encoding: utf-8
from os import system, remove, path
import matplotlib.pyplot as plt
import numpy as np
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
5
import json
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

########################################################################
# Interface
########################################################################

if __name__=="__main__":
	import argparse

	can = argparse.ArgumentParser()

	can.add_argument("-N", "--nb-run", metavar="NB", default=100, type=int,
			help="Number of runs")

	can.add_argument("-v", "--for-value", metavar="NB", type=int,
			help="Print ERT with probabilities to obtain this value")

Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
22
23
24
	can.add_argument("-save", "--save-proba", action='store_true', default=False,
            help="Disable graphical output and save proba")

25
26
27
28
29
30
31
32
33
34
35
	arg = can.parse_known_args()
	the = arg[0]

	# Minimum checks.
	assert(0 < the.nb_run)

	if path.isfile('run.csv'):
		remove('run.csv')

	for i in range(the.nb_run):
		print("\rRun {}".format(i), end = '')
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
36
		system("python3 snp.py -o run -no -s " + str(np.random.randint(0, 10*the.nb_run)) + " "+" ".join(arg[1]))
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	print("\r")

	# Data extraction
	data = []
	attempt = {}
	current_best = 0
	temps_max = 0
	value_max = 0
	with open('run.csv', 'r') as fd:
		for line in fd:
			line = line.split(' ; ')
			line[0] = int(line[0])
			line[1] = float(line[1])

			if line[0] > temps_max:
				temps_max = line[0]

			if line[1] > value_max:
				value_max = line[1]

			if line[0] == 1:
				if len(attempt) > 0:
					data.append(attempt)
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
60
				current_best = 0
61
				attempt = {}
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
62
			if line[1] > current_best:
63
64
65
66
67
68
69
70
71
72
				attempt[line[0]] = line[1]
				current_best = line[1]

		data.append(attempt)

	def probability(data,time,value):
		num = 0
		for attempt in data:
			good = False
			for t,v in attempt.items():
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
73
				if t <= time and v >= value:
74
75
76
77
78
79
80
81
82
					good = True
					break
			if good:
				num += 1
		return num/len(data)

	probability = np.vectorize(probability, excluded=[0])

	# Plot
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
83
84
85
86
	if the.save_proba:
		with open('proba.json', 'w') as outfile:
			json.dump(data, outfile)
	elif the.for_value is not None:
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
87
88
89
90
91
92
		P =  probability(data,range(1,temps_max+1),the.for_value)
		plt.bar(range(1,temps_max+1), P)
		plt.title("ERT for target {}".format(the.for_value))
		plt.xlabel('run')
		plt.ylabel('probability')
		plt.ylim(0,1)
93
94
		plt.show()
	else:
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
95
96
		temps = range(0,temps_max+2)
		values = range(0,int(value_max+2))
97
98
99
		X, Y = np.meshgrid(temps, values)

		Z = probability(data,X,Y)
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
100
101
102
		print(Z)

		"""
103
104
105
		fig, ax = plt.subplots()
		cs = ax.contourf(X, Y, Z, 20)
		cbar = fig.colorbar(cs)
Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
106
107
108
109
110
111
112
113
		"""

		proba = np.linspace(0.0, 1.0, 11)
		cs = plt.contourf(X, Y, Z, levels=proba)
		cbar = plt.colorbar(cs)
		cbar.set_label('probability')
		plt.xlabel('run')
		plt.ylabel('target')
114
115
		plt.show()

Grégoire Grzeczkowicz's avatar
Grégoire Grzeczkowicz committed
116
	#remove('run.csv')