Spaces:
Runtime error
Runtime error
jcmachicao
commited on
Commit
•
943aa96
1
Parent(s):
803b4f5
Upload app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
|
|
|
|
3 |
|
4 |
c1, c2, = st.columns([7,7])
|
5 |
with c2:
|
@@ -8,60 +10,113 @@ with c1:
|
|
8 |
st.image('figures/giz_logo.png', width=200, caption='Peru')
|
9 |
|
10 |
st.title('Estudio de Encuesta Integridad')
|
11 |
-
option0 = st.selectbox('Formato de Analisis: ', ('Textos', 'Gráficos'))
|
12 |
-
option3 = st.selectbox('Documento: ', ('Todos', 'Encuesta', 'Indicadores'))
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
# Load dataframe
|
15 |
-
file = st.file_uploader('Seleccione un archivo Excel: ')
|
16 |
|
17 |
-
if
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
21 |
df00 = pd.read_excel(file, engine='openpyxl')
|
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 |
-
texto_tot
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
else:
|
61 |
-
option21 = st.selectbox('Tipo de Consulta: ', ('Categoría', 'Pregunta', 'Total'))
|
62 |
-
|
63 |
-
if option21 == 'Total':
|
64 |
-
st.image('figures/discrepancias_heatmap.png', width=1200)
|
65 |
else:
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
|
6 |
c1, c2, = st.columns([7,7])
|
7 |
with c2:
|
|
|
10 |
st.image('figures/giz_logo.png', width=200, caption='Peru')
|
11 |
|
12 |
st.title('Estudio de Encuesta Integridad')
|
13 |
+
option0 = st.selectbox('Formato de Analisis: ', (None, 'Textos', 'Gráficos'))
|
14 |
+
option3 = st.selectbox('Documento: ', (None, 'Todos', 'Encuesta', 'Indicadores'))
|
15 |
+
maprange = {'0 a 50%': 10, '10 a 50%': 20, '20 a 50%': 30, '30 a 50%': 40, '40 a 50%': 50}
|
16 |
+
option2 = st.selectbox('Franja de Discrepancia siendo la máxima [0, 50]', ('0 a 50%', '10 a 50%', '20 a 50%', '30 a 50%', '40 a 50%'))
|
17 |
+
mapn = maprange[option2]
|
18 |
+
st.write('La franja de discrepancia mostrada es entre ', mapn-10, ' y 50%')
|
19 |
|
|
|
|
|
20 |
|
21 |
+
if option0 and option2 and option3 is not None:
|
22 |
+
|
23 |
+
# Load dataframe
|
24 |
+
file = st.file_uploader('Seleccione un archivo Excel: ')
|
25 |
+
|
26 |
+
if file is not None:
|
27 |
+
|
28 |
df00 = pd.read_excel(file, engine='openpyxl')
|
29 |
+
st.write(df00.shape)
|
30 |
+
|
31 |
+
if option0 == 'Textos':
|
32 |
+
|
33 |
+
option1 = st.selectbox('Clasificar resultados por:', ('Pregunta', 'Categoria'))
|
34 |
+
#df = df00[(df00['diff'] > mapn-10) & (df00['diff'] < mapn)]
|
35 |
+
df = df00[(df00['diff'] > mapn-10)]
|
36 |
+
if option3 != 'Todos':
|
37 |
+
df = df[df['documento']==option3]
|
38 |
+
else:
|
39 |
+
df = df
|
40 |
+
|
41 |
+
# Group dataframe by chosen option
|
42 |
+
if option1 == 'Pregunta':
|
43 |
+
grouped_df = df.groupby('pregunta')
|
44 |
+
elif option1 == 'Categoria':
|
45 |
+
grouped_df = df.groupby('cat_sup')
|
46 |
+
|
47 |
+
# Generate text with bullets
|
48 |
+
text = ''
|
49 |
+
|
50 |
+
for name, group in grouped_df:
|
51 |
+
|
52 |
+
cad01 = '\n\n > **'+ option1+ ': '+ name + '**'
|
53 |
+
st.write(cad01)
|
54 |
+
st.write('Cantidad de afirmaciones: ', len(group))
|
55 |
+
texto_tot = ''
|
56 |
+
for index, row in group.iterrows():
|
57 |
+
val0 = str(row['diff'])
|
58 |
+
text = '* En el rubro sobre ' + row.pregunta + ' la categoría ' + row.cat_sup + ' responde ' + \
|
59 |
+
row.respuesta + ' un **' + val0 + '%** más que la categoría ' + row.cat_inf + '\n'
|
60 |
+
texto_tot = texto_tot + '\n' + text
|
61 |
+
st.write(texto_tot)
|
62 |
+
|
63 |
+
# Download link for text file
|
64 |
+
st.download_button(label='Descargar Reporte', data=texto_tot, file_name='reporte.txt', mime='text/plain')
|
65 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
else:
|
67 |
+
|
68 |
+
option21 = st.selectbox('Tipo de Gráfico ', ('Total', 'Parcial') )
|
69 |
+
|
70 |
+
if option21 == 'Total':
|
71 |
+
st.write('Este diagrama muestra las discrepancias promedio de categorías y preguntas, con discrepancias mayores a 15%.')
|
72 |
+
st.write('Permite un panorama general de las discrepancias.')
|
73 |
+
st.image('figures/discrepancias_heatmap.png', width=1200)
|
74 |
+
else:
|
75 |
+
|
76 |
+
df2 = df00[(df00['diff'] > mapn-10)]
|
77 |
+
|
78 |
+
if option3 != 'Todos':
|
79 |
+
df2 = df2[df2['documento']==option3]
|
80 |
+
else:
|
81 |
+
pass
|
82 |
+
|
83 |
+
df_pv = pd.pivot_table(df2, values='diff', index='pid', columns='cat_sup', aggfunc='mean').fillna(0)
|
84 |
+
st.write(df_pv.shape)
|
85 |
+
|
86 |
+
data_pv_s = df_pv[df_pv > 10]
|
87 |
+
row_to_drop = list(data_pv_s.index[data_pv_s.sum(axis=1) == 0])
|
88 |
+
data_pv_s = data_pv_s.drop(row_to_drop, axis=0)
|
89 |
+
col_to_drop = list(data_pv_s.columns[data_pv_s.sum(axis=0) == 0])
|
90 |
+
data_pv_s = data_pv_s.drop(col_to_drop, axis=1)
|
91 |
+
|
92 |
+
fig, ax = plt.subplots(figsize=(40, 20))
|
93 |
+
|
94 |
+
heatmap = ax.imshow(data_pv_s.T, cmap='inferno')
|
95 |
+
|
96 |
+
ax.set_yticks(range(len(data_pv_s.columns)))
|
97 |
+
ax.set_yticklabels(data_pv_s.columns, fontsize=16)
|
98 |
+
|
99 |
+
ax.set_xticks(range(len(data_pv_s.index)))
|
100 |
+
ax.set_xticklabels(data_pv_s.index, rotation=90, fontsize=16)
|
101 |
+
|
102 |
+
for i in range(len(data_pv_s.index)):
|
103 |
+
for j in range(len(data_pv_s.columns)):
|
104 |
+
value = data_pv_s.iloc[i, j]
|
105 |
+
if not np.isnan(value):
|
106 |
+
ax.annotate(int(value), xy=(i, j), horizontalalignment='center',
|
107 |
+
verticalalignment='center', fontsize=18, color='darkgray',
|
108 |
+
fontweight='bold'
|
109 |
+
)
|
110 |
+
|
111 |
+
ax.set_title('Distribución de Discrepancias Máximas en Categorías y Preguntas', fontsize=20)
|
112 |
+
ax.set_ylabel('Categorías')
|
113 |
+
ax.set_xlabel('Preguntas')
|
114 |
+
|
115 |
+
plt.colorbar(heatmap, ax=ax)
|
116 |
+
plt.grid(True)
|
117 |
+
st.pyplot(fig, width=1200)
|
118 |
+
st.write('Para grabar la imagen solo presione botón derecho y guardela como imagen en su servidor.')
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|