jacopoteneggi commited on
Commit
c3af76c
·
verified ·
1 Parent(s): f6eb5e3
Files changed (1) hide show
  1. app_lib/viz.py +64 -2
app_lib/viz.py CHANGED
@@ -1,8 +1,67 @@
 
1
  import pandas as pd
2
  import plotly.express as px
 
3
  import streamlit as st
4
 
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def _viz_wealth(results):
7
  wealth = results["wealth"]
8
  concepts = results["concepts"]
@@ -27,6 +86,7 @@ def _viz_wealth(results):
27
  annotation_position="bottom right",
28
  )
29
  fig.update_yaxes(range=[0, 1.5 * 1 / significance_level])
 
30
  st.plotly_chart(fig, use_container_width=True)
31
 
32
 
@@ -40,14 +100,16 @@ def viz_results():
40
 
41
  with rank_tab:
42
  st.subheader("Rank of Semantic Importance")
43
-
44
  with st.expander("Details"):
45
  st.write(
46
  "This tab shows the rank of semantic importance of the concepts for the predictions of the model on the image. Concepts are sorted by increasing rejection time, where a shorter rejection time indicates higher importance."
47
  )
 
 
 
 
48
  with wealth_tab:
49
  st.subheader("Wealth Process of Testing Procedures")
50
-
51
  with st.expander("Details"):
52
  st.write(
53
  "This tab shows the average wealth process of the testing procedures for random draws of conditioning subsets with the same cardinality. The black dashed line represents the rejection threshold."
 
1
+ import numpy as np
2
  import pandas as pd
3
  import plotly.express as px
4
+ import plotly.graph_objects as go
5
  import streamlit as st
6
 
7
 
8
+ def _viz_rank(results):
9
+ rejected = results["rejected"]
10
+ tau = results["tau"]
11
+ concepts = results["concepts"]
12
+ significance_level = results["significance_level"]
13
+
14
+ rejected_mu = rejected.mean(axis=0)
15
+ tau_mu = tau.mean(axis=0)
16
+
17
+ sorted_idx = np.argsort(tau_mu)[::-1]
18
+ sorted_tau = tau_mu[sorted_idx]
19
+ sorted_rejected = rejected_mu[sorted_idx]
20
+ sorted_concepts = [concepts[idx] for idx in sorted_idx]
21
+
22
+ rank_df = []
23
+ for concept, tau, rejected in zip(sorted_concepts, sorted_tau, sorted_rejected):
24
+ rank_df.append({"concept": concept, "tau": tau, "rejected": rejected})
25
+ rank_df = pd.DataFrame(rank_df)
26
+
27
+ fig = go.Figure()
28
+
29
+ fig.add_trace(
30
+ go.Scatter(
31
+ x=rank_df["rejected"],
32
+ y=rank_df["concept"],
33
+ marker=dict(size=8),
34
+ line=dict(color="#1f78b4", dash="dash"),
35
+ name="Rejection rate",
36
+ )
37
+ )
38
+ fig.add_trace(
39
+ go.Bar(
40
+ x=rank_df["tau"],
41
+ y=rank_df["concept"],
42
+ orientation="h",
43
+ marker=dict(color="#a6cee3"),
44
+ name="Normalized rejection time",
45
+ )
46
+ )
47
+ fig.add_shape(
48
+ type="line",
49
+ yref="paper",
50
+ line=dict(color="black", dash="dash"),
51
+ x0=significance_level,
52
+ x1=significance_level,
53
+ y0=0,
54
+ y1=1,
55
+ name="significance level",
56
+ showlegend=True,
57
+ )
58
+ fig.update_layout(yaxis_title="Rank of importance", xaxis_title="")
59
+
60
+ _, centercol, _ = st.columns([1, 4, 1])
61
+ with centercol:
62
+ st.plotly_chart(fig, use_container_width=True)
63
+
64
+
65
  def _viz_wealth(results):
66
  wealth = results["wealth"]
67
  concepts = results["concepts"]
 
86
  annotation_position="bottom right",
87
  )
88
  fig.update_yaxes(range=[0, 1.5 * 1 / significance_level])
89
+ # fig.update_layout(legend=dict(orientation="h", x=0, y=1.2))
90
  st.plotly_chart(fig, use_container_width=True)
91
 
92
 
 
100
 
101
  with rank_tab:
102
  st.subheader("Rank of Semantic Importance")
 
103
  with st.expander("Details"):
104
  st.write(
105
  "This tab shows the rank of semantic importance of the concepts for the predictions of the model on the image. Concepts are sorted by increasing rejection time, where a shorter rejection time indicates higher importance."
106
  )
107
+
108
+ if results is not None:
109
+ _viz_rank(results)
110
+
111
  with wealth_tab:
112
  st.subheader("Wealth Process of Testing Procedures")
 
113
  with st.expander("Details"):
114
  st.write(
115
  "This tab shows the average wealth process of the testing procedures for random draws of conditioning subsets with the same cardinality. The black dashed line represents the rejection threshold."