File size: 3,312 Bytes
2a15960
 
c4727bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from src.utils.segregate import Segregator
from src.dto.dto import ExplanationDto


UPPER_COLOR = "#D4EFDF"  # green
MID_COLOR = "#FBFBB8BF"  # amber
LOW_COLOR = "black"


class Visualizer:
    def __init__(self, show_mid_features: bool = True, show_low_features: bool = True):
        self.__show_mid_features = show_mid_features
        self.__show_low_features = show_low_features

    def visualize(
        self,
        segregator: Segregator,
        explanations: ExplanationDto,
        output_from_explanations: str,
        avoid_exp_label: bool = False,
    ) -> str:
        highlighted_text = output_from_explanations

        pos_features, mid_features, low_features = segregator.segregate(
            explanations=explanations
        )

        if not self.__show_mid_features:
            mid_features = []

        if not self.__show_low_features:
            low_features = []

        for explanation in explanations.explanations:
            score = round(explanation.score, 2)

            if explanation.feature in pos_features:
                token_str = (
                    '<span title="'
                    + str(score)
                    + '"style="font-weight:bold;background-color:'
                    + UPPER_COLOR
                    + '">'
                    + explanation.feature
                    + "</span>"
                )
            elif explanation.feature in mid_features:
                token_str = (
                    '<span title="'
                    + str(score)
                    + '"style="font-weight:bold;background-color:'
                    + MID_COLOR
                    + '">'
                    + explanation.feature
                    + "</span>"
                )
            else:
                token_str = (
                    '<span title="'
                    + str(score)
                    + '"style="color:'
                    + LOW_COLOR
                    + '">'
                    + explanation.feature
                    + "</span>"
                )

            highlighted_text = highlighted_text.replace(explanation.feature, token_str)

        if avoid_exp_label:
            vis = "<p>" + highlighted_text + "</p>"
        else:
            vis = "<p><b>Explanations:</b><br>" + highlighted_text + "</p>"
        vis = vis.replace("\n", "<br>")

        legend = "<p align='right'"

        legend += (
            '<span title="' + '"style="color:' + LOW_COLOR + '">' + "💡" + "</span>"
        )

        legend += "&emsp;"

        legend += (
            '<span title="'
            + '"style="color:'
            + LOW_COLOR
            + '">'
            + "not important"
            + "</span>"
        )

        legend += "&emsp;⇢&emsp;"

        legend += (
            '<span title="'
            + '"style="font-weight:bold;background-color:'
            + MID_COLOR
            + '">'
            + " important "
            + "</span>"
        )

        legend += "&emsp;⇢&emsp;"

        legend += (
            '<span title="'
            + '"style="font-weight:bold;background-color:'
            + UPPER_COLOR
            + '">'
            + " very important "
            + "</span>"
        )

        legend += "</p>"

        html_str = legend + vis

        return html_str