-
Notifications
You must be signed in to change notification settings - Fork 4
/
reset_explainers.py
163 lines (132 loc) · 5.45 KB
/
reset_explainers.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from typing import Iterable
import jinja2
import pandas as pd
from src.explainer import valid_explainers
from src.io import detail, load_results
from src.utils import root
# todo(s) from Tim <3
# header row in specific color
# one empty line before each test
# merge redudent txt
# another font in the table
# fix \n in src
# remove underscore (importance_symmetric) and put human readable info
# todo add paper.csv , link from the website to the csv in github
EXPLAINERS_HTML_PATH = root + "/docs/explainers/" # todo move to src.utils
import re
_urlfinderregex = re.compile(r'http([^<\.\s]+\.[^<\.\s]*)+[^<\.\s]{2,}')
def linkify(text, maxlinklength=256):
def replacewithlink(matchobj):
url = matchobj.group(0)
text = str(url)
if text.startswith('http://'):
text = text.replace('http://', '', 1)
elif text.startswith('https://'):
text = text.replace('https://', '', 1)
if text.startswith('www.'):
text = text.replace('www.', '', 1)
if len(text) > maxlinklength:
halflength = maxlinklength / 2
text = text[0:halflength] + '...' + text[len(text) - halflength:]
return '<a class="comurl" href="' + url + '" target="_blank" rel="nofollow">' + text + '<img class="imglink" src="./images/linkout.png"></a>'
if text != None and text != '':
return _urlfinderregex.sub(replacewithlink, text)
else:
return ''
def explainer_to_html(explainer_df_with_results):
""" clean the df and write it to an html page"""
df = explainer_df_with_results.fillna('')
df = df.applymap(lambda x: '\n '.join(x) if isinstance(x, Iterable) and not isinstance(x, str) else x)
# df['value'] = df['value'].apply(lambda x: linkify(x) if isinstance(x, str) else x)
# os.chdir(file_dirname)
# Generate HTML from template.
template = jinja2.Template("""<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<title>""" + df.loc[0, 'value'] + """</title>
<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" type="text/css">
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.topnav {
overflow: hidden;
background-color: #333;
}
.topnav a {
float: left;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
.topnav a:hover {
background-color: #ddd;
color: black;
}
.topnav a.active {
background-color: #04AA6D;
color: white;
}
</style>
</head>
<body>
<div style="display: flex; width: 100%; height: 50px; flex-direction: column; background-color: gray; overflow: hidden;">
<iframe src="../header.html" style="flex-grow: 1; border: none; margin: 0; padding: 0;" ></iframe>
</div>
{{ dataframe }}
</body>
<script defer type="text/javascript">
let myTable = new simpleDatatables.DataTable("#myTable");
</script>
<style type="text/css">
.imglink {
width: 18px;
padding-left: 3px;
}
</style>
</html>"""
)
table_html = df.to_html(table_id="myTable")
table_html = linkify(table_html)
output_html = template.render(dataframe=table_html)
output_html = output_html.replace(r'\n', ' <br> ')
# Write generated HTML to file.
file = EXPLAINERS_HTML_PATH + df.loc[0, 'value'] + ".htm"
print(file)
with open(file, "w", encoding="utf-8") as file_obj:
file_obj.write(output_html)
if __name__ == "__main__":
result_df = load_results()
result_df_detailed = detail(result_df)
for explainer_class in valid_explainers:
print(explainer_class.name)
# explainer = explainer_class()
explainer_df = pd.DataFrame(explainer_class.to_pandas())
explainer_df.columns = ['value']
explainer_df['test'] = explainer_df.index
explainer_df['sub_test_category'] = None
explainer_df['sub_test'] = None
cols = ['test', 'sub_test_category', 'sub_test', explainer_class.name]
result_s = result_df_detailed[cols]
cols = ['test', 'sub_test_category', 'sub_test', 'value']
result_s.columns = cols
explainer_df_with_results = pd.concat([
explainer_df,
result_s,
], axis=0, ).reset_index()[cols]
# print(explainer_df_with_results)
explainer_df_with_results['remarks'] = None
# todo also add columns results from original paper , and in remarks columns why results differ
explainer_to_html(explainer_df_with_results)
for explainer_class in valid_explainers:
# 'https://karim-53.github.io/Compare-xAI/explainers/'
url = explainer_class.name + '.htm'
print(f'<a href="{url}">{explainer_class.name}</a><br>')
print('End')