/
ToyExampleEcoindex.py
74 lines (59 loc) · 1.97 KB
/
ToyExampleEcoindex.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
#
# Toy example fo EcoIndex computation: sampling 5 lines of
# the url_4ecoindex_dataset.csv dataset, compute the Ecoindx
# scores for the urls, and draw a figure.
#
import subprocess
import sys
import random
Nb = 5
x = []; y = []
with open(r"url_4ecoindex_dataset.csv", 'r') as fp:
NbLines = len(fp.readlines())
print('Total lines:', NbLines) # 8
l = random.sample(range(0, NbLines), Nb)
with open(r"url_4ecoindex_dataset.csv", 'r') as fp:
for i, line in enumerate(fp):
if i in l:
# process line
s = line.split(';')[0]
#s = 'http://www.google.com'
# create two files to hold the output and errors, respectively
with open('out.txt','w+') as fout:
with open('err.txt','w+') as ferr:
out=subprocess.call(["python3",'test_ecoindex.py',s],stdout=fout,stderr=ferr)
# reset file to read from it
fout.seek(0)
# save output (if any) in variable
output=fout.read()
# reset file to read from it
ferr.seek(0)
# save errors (if any) in variable
errors = ferr.read()
if output:
url = output.split(';')[0].strip()
score = float('%.2f' % float(output.split(';')[4].strip()))
print("Resultat execution commande : ",url,score)
#print("Errors :",errors)
#x.append('\n'.join(url))
x.append(url)
y.append(score)
print('x coordinates:',x)
print('y coordinates:',y)
import matplotlib.pyplot as plt
#plt.figure(figsize=(9, 3))
plt.figure()
plt.xticks(rotation = 45)
#plt.subplot(131)
plt.bar(x, y)
#plt.subplot(132)
#plt.scatter(x, y)
#plt.subplot(133)
#plt.plot(x, y)
plt.suptitle('EcoIndex Toy Example')
ax = plt.gca()
plt.draw()
for tick in ax.get_xticklabels():
tick.set_rotation(-45)
plt.tight_layout()
plt.show()