draw_ert.py 2.29 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
	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 = '')
		system("python3 snp.py -o run -no "+" ".join(arg[1]))
	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:
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
		temps = np.linspace(0,temps_max,100)
		P =  probability(data,temps,the.for_value)
		plt.plot(temps,P)
		plt.show()
	else:
		temps = np.linspace(0,temps_max,100)
		values = np.linspace(0,value_max,100)
		X, Y = np.meshgrid(temps, values)

		Z = probability(data,X,Y)
		fig, ax = plt.subplots()
		cs = ax.contourf(X, Y, Z, 20)
		cbar = fig.colorbar(cs)
		plt.show()

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