Spaces:
Running
Running
Zekun Wu
commited on
Commit
·
5af5671
1
Parent(s):
3e74664
update
Browse files- pages/2_Evaluation.py +14 -1
- util/plot.py +56 -0
pages/2_Evaluation.py
CHANGED
@@ -1,10 +1,11 @@
|
|
1 |
import os
|
2 |
|
|
|
3 |
import streamlit as st
|
4 |
import pandas as pd
|
5 |
from io import StringIO
|
6 |
from util.evaluation import statistical_tests,calculate_correlations,calculate_divergences
|
7 |
-
from util.plot import create_score_plot,create_rank_plots,create_correlation_heatmaps
|
8 |
import plotly.express as px
|
9 |
|
10 |
|
@@ -53,6 +54,18 @@ def app():
|
|
53 |
|
54 |
st.write('Test Results:', results_df)
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
score_fig = create_score_plot(df)
|
57 |
st.plotly_chart(score_fig)
|
58 |
|
|
|
1 |
import os
|
2 |
|
3 |
+
import numpy as np
|
4 |
import streamlit as st
|
5 |
import pandas as pd
|
6 |
from io import StringIO
|
7 |
from util.evaluation import statistical_tests,calculate_correlations,calculate_divergences
|
8 |
+
from util.plot import create_score_plot,create_rank_plots,create_correlation_heatmaps,create_3d_plot,calculate_distances
|
9 |
import plotly.express as px
|
10 |
|
11 |
|
|
|
54 |
|
55 |
st.write('Test Results:', results_df)
|
56 |
|
57 |
+
fig_3d = create_3d_plot(data)
|
58 |
+
|
59 |
+
st.plotly_chart(fig_3d)
|
60 |
+
|
61 |
+
# Calculate and display average distance
|
62 |
+
point_A = np.array([0, 0, 0])
|
63 |
+
point_B = np.array([10, 10, 10])
|
64 |
+
distances = calculate_distances(data, point_A, point_B)
|
65 |
+
average_distance = distances.mean()
|
66 |
+
st.write(f'Average distance to the ideal line: {average_distance}')
|
67 |
+
|
68 |
+
|
69 |
score_fig = create_score_plot(df)
|
70 |
st.plotly_chart(score_fig)
|
71 |
|
util/plot.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import pandas as pd
|
2 |
import plotly.graph_objs as go
|
3 |
import plotly.express as px
|
@@ -100,3 +101,58 @@ def create_correlation_heatmaps(df):
|
|
100 |
figs[title] = fig
|
101 |
|
102 |
return figs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
import pandas as pd
|
3 |
import plotly.graph_objs as go
|
4 |
import plotly.express as px
|
|
|
101 |
figs[title] = fig
|
102 |
|
103 |
return figs
|
104 |
+
|
105 |
+
|
106 |
+
def point_to_line_distance(point, A, B):
|
107 |
+
"""Calculate the distance from a point to a line defined by two points A and B."""
|
108 |
+
line_vec = B - A
|
109 |
+
point_vec = point - A
|
110 |
+
line_len = np.linalg.norm(line_vec)
|
111 |
+
line_unitvec = line_vec / line_len
|
112 |
+
point_vec_scaled = point_vec / line_len
|
113 |
+
t = np.dot(line_unitvec, point_vec_scaled)
|
114 |
+
nearest = line_vec * t
|
115 |
+
dist = np.linalg.norm(nearest - point_vec)
|
116 |
+
return dist
|
117 |
+
|
118 |
+
|
119 |
+
def calculate_distances(data, point_A, point_B):
|
120 |
+
distances = data.apply(lambda row: point_to_line_distance(
|
121 |
+
np.array([row['Privilege_Avg_Score'], row['Protect_Avg_Score'], row['Neutral_Avg_Score']]),
|
122 |
+
point_A, point_B), axis=1)
|
123 |
+
return distances
|
124 |
+
|
125 |
+
|
126 |
+
def create_3d_plot(data):
|
127 |
+
# Define the ideal line (from point A to point B)
|
128 |
+
point_A = np.array([0, 0, 0])
|
129 |
+
point_B = np.array([10, 10, 10])
|
130 |
+
|
131 |
+
# Calculate distances
|
132 |
+
distances = calculate_distances(data, point_A, point_B)
|
133 |
+
data['Distance_to_Ideal'] = distances
|
134 |
+
|
135 |
+
# Label points that perfectly match the ideal line (distance close to 0)
|
136 |
+
tolerance = 1e-6
|
137 |
+
data['Perfect_Match'] = data['Distance_to_Ideal'].apply(lambda x: 'Yes' if x < tolerance else 'No')
|
138 |
+
|
139 |
+
# Create a 3D scatter plot of the scores
|
140 |
+
fig_3d = px.scatter_3d(data, x='Privilege_Avg_Score', y='Protect_Avg_Score', z='Neutral_Avg_Score',
|
141 |
+
color='Distance_to_Ideal', symbol='Perfect_Match',
|
142 |
+
hover_data={
|
143 |
+
'Occupation': True,
|
144 |
+
'Role': True,
|
145 |
+
'Privilege_Avg_Score': True,
|
146 |
+
'Protect_Avg_Score': True,
|
147 |
+
'Neutral_Avg_Score': True,
|
148 |
+
'Distance_to_Ideal': True,
|
149 |
+
'Perfect_Match': True
|
150 |
+
},
|
151 |
+
title='Occupation and Role Clusters based on Scores with Distance to Ideal Line')
|
152 |
+
|
153 |
+
# Add ideal line where Neutral = Protect = Privilege
|
154 |
+
ideal_line = go.Scatter3d(x=[0, 10], y=[0, 10], z=[0, 10], mode='lines', name='Ideal Line',
|
155 |
+
line=dict(color='green', dash='dash'))
|
156 |
+
fig_3d.add_trace(ideal_line)
|
157 |
+
|
158 |
+
return fig_3d
|