adollbo commited on
Commit
1794e4f
·
1 Parent(s): 985f1c7

added functions like change colour button and data processing

Browse files
Files changed (1) hide show
  1. utils.py +79 -0
utils.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit.components.v1 as components
2
+
3
+ def ChangeButtonColour(widget_label, font_color, background_color='transparent'):
4
+ htmlstr = f"""
5
+ <script>
6
+ var elements = window.parent.document.querySelectorAll('button');
7
+ for (var i = 0; i < elements.length; ++i) {{
8
+ if (elements[i].innerText == '{widget_label}') {{
9
+ elements[i].style.color ='{font_color}';
10
+ elements[i].style.background = '{background_color}'
11
+ }}
12
+ }}
13
+ </script>
14
+ """
15
+ components.html(f"{htmlstr}", height=0, width=0)
16
+
17
+
18
+ def first_five_posneg_indices(response_body):
19
+ values = response_body["explanations"][0]["shap_values"]
20
+ # Separate positive and negative values, keep indice as corresponds to key
21
+ positive_dict = {index: val for index, val in enumerate(values) if val > 0}
22
+ negative_dict = {index: val for index, val in enumerate(values) if val < 0}
23
+
24
+ # Sort dictionaries based on the magnitude of values
25
+ sorted_positive_indices = [index for index, _ in sorted(positive_dict.items(), key=lambda item: abs(item[1]), reverse=True)]
26
+ sorted_negative_indices = [index for index, _ in sorted(negative_dict.items(), key=lambda item: abs(item[1]), reverse=True)]
27
+
28
+ return [sorted_positive_indices[:5], sorted_negative_indices[:5]]
29
+
30
+ def get_texts(posneg_indices, feature_texts):
31
+ positive_texts = [feature_texts[x] for x in posneg_indices[0]]
32
+ negative_texts = [feature_texts[x] for x in posneg_indices[1]]
33
+ return positive_texts, negative_texts
34
+
35
+
36
+ def get_input_values(posneg_indices, data_instance):
37
+ values = data_instance["instances"][0]
38
+ outputs = []
39
+ for lst in posneg_indices:
40
+ vals = []
41
+ for idx in lst:
42
+ if idx in range(7,12):
43
+ val = str(bool(values[idx])).capitalize()
44
+ else:
45
+ val = values[idx]
46
+ vals.append(val)
47
+ outputs.append(vals)
48
+ return outputs[0], outputs[1]
49
+
50
+ response = {
51
+ "predictions": [
52
+ True
53
+ ],
54
+ "explanations": [
55
+ {
56
+ "shap_values": [
57
+ -0.020634920634920784,
58
+ -0.053968253968253166,
59
+ -0.0015873015873012486,
60
+ 0,
61
+ 0.04603174603174587,
62
+ -0.12063492063492065,
63
+ 0.8365079365079348,
64
+ -0.16349206349206302,
65
+ 0.12222222222222279,
66
+ -0.04444444444444462,
67
+ -0.02444444444444462,
68
+ 0.03603174603174587,
69
+ ],
70
+ "expected_value": 0.4
71
+ }
72
+ ]
73
+ }
74
+
75
+ feature_texts = {0: "Day out of 30", 1: "Type of transaction: ", 2: "Amount transferred: ", 3: "Initial balance of sender: ", 4: "New balance of sender: ",
76
+ 5: "Initial balance of recipient: ", 6: "New balance of recipient: ", 7: "Sender's balance was exactly credited: ",
77
+ 8: "Receiver's balance was exactly credited: ", 9: "Transaction over 450.000: ", 10: "Frequent receiver of transactions: ", 11: "Receiver is merchant: ", 12: "Sender ID: ", 13: "Receiver ID: "}
78
+
79
+ example_input = {"instances":[[1,"PAYMENT",9839.64,170136,160296.36,0,0,1,1,0,0,1,84,2424]]}