Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,68 @@
|
|
1 |
import pandas as pd
|
2 |
import panel as pn
|
3 |
import numpy as np
|
|
|
4 |
from bokeh.models.widgets.tables import NumberFormatter, BooleanFormatter
|
5 |
|
6 |
-
|
|
|
7 |
|
8 |
|
9 |
-
|
|
|
10 |
|
11 |
-
df = pd.read_pickle(f"cossims_plural_slicewidth=1.pkl")
|
12 |
-
if "boson" in df.index:
|
13 |
-
df = df.drop("boson")
|
14 |
-
if "bosons" in df.index:
|
15 |
-
df = df.drop("bosons")
|
16 |
-
if "boson_s" in df.index:
|
17 |
-
df = df.drop("boson_s")
|
18 |
|
19 |
-
|
20 |
-
df = df.loc[:, 1948:] #1948
|
21 |
-
#df = df.fillna(0).round(3)
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
df
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
|
|
|
|
|
|
|
|
27 |
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
#
|
34 |
-
|
35 |
-
header_filters={'token': {'type': 'input', 'func': 'like', 'placeholder': 'search'}} # Für Suche, etc.
|
36 |
|
37 |
-
### style
|
38 |
|
39 |
-
|
40 |
-
page_size = 50 # Number of rows
|
41 |
-
page = 1 # Start on page x
|
42 |
-
#configuration={"rowHeight" : 10*24}
|
43 |
-
#widths = {'index' : 40} or {'index': '5%'}
|
44 |
|
45 |
-
#frozen_rows = list
|
46 |
-
#frozen_columns = list
|
47 |
|
|
|
48 |
|
49 |
-
|
50 |
df,
|
51 |
|
52 |
### functionality
|
53 |
|
54 |
-
formatters=
|
55 |
-
header_filters =
|
56 |
-
selectable=
|
57 |
|
58 |
### style
|
59 |
|
60 |
-
theme =
|
61 |
-
page_size =
|
62 |
-
page =
|
63 |
frozen_columns = {"token" : "left", "sum" : "right"}, # Must give width, otherwise doesn't work!
|
64 |
width=1800,
|
65 |
-
height=950,
|
66 |
-
#frozen_rows = [1],
|
67 |
-
#widths
|
68 |
-
#configuration = configuration,
|
69 |
-
|
70 |
|
71 |
|
72 |
### other
|
@@ -76,13 +72,66 @@ tabs = pn.widgets.Tabulator(
|
|
76 |
)
|
77 |
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
ACCENT = "teal"
|
80 |
|
81 |
pn.template.FastListTemplate(
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
87 |
accent=ACCENT,
|
88 |
).servable()
|
|
|
1 |
import pandas as pd
|
2 |
import panel as pn
|
3 |
import numpy as np
|
4 |
+
import hvplot.pandas
|
5 |
from bokeh.models.widgets.tables import NumberFormatter, BooleanFormatter
|
6 |
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
import seaborn as sns
|
9 |
|
10 |
|
11 |
+
sns.set(style='whitegrid', context='notebook')
|
12 |
+
#plt.rcParams["font.family"] = "Latin Modern Sans"
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
### Data
|
|
|
|
|
16 |
|
17 |
+
def load_data(sel_token):
|
18 |
+
if sel_token == "virtual":
|
19 |
+
df = pd.read_pickle("server_data/virtual_fulltexts_cossim_all_slice=1.pkl")
|
20 |
+
df = df.drop("virtual")
|
21 |
+
elif sel_token == "boson":
|
22 |
+
df = pd.read_pickle("server_data/cossims_plural_slicewidth=1.pkl")
|
23 |
+
elif sel_token == "intermediate":
|
24 |
+
df = pd.read_pickle("server_data/intermediate_fulltexts_cossim_all_slice=1.pkl")
|
25 |
+
df = df.drop("intermediate")
|
26 |
|
27 |
+
topn = 1000
|
28 |
+
df["sum"] = df.sum(axis=1)
|
29 |
+
df = df.astype("float32")
|
30 |
+
df = df.round(3)
|
31 |
|
32 |
+
df = df.sort_values("sum", ascending=False)
|
33 |
+
df = df.head(topn)
|
34 |
+
df.index.name = "token"
|
35 |
+
return df
|
36 |
|
37 |
+
sel_token = pn.widgets.Select(
|
38 |
+
name="Select dataset",
|
39 |
+
value="virtual",
|
40 |
+
options=["virtual", "intermediate", "boson"],
|
41 |
+
#description="Select the base token ",
|
42 |
+
)
|
|
|
43 |
|
|
|
44 |
|
45 |
+
df = pn.rx(load_data)(sel_token=sel_token)
|
|
|
|
|
|
|
|
|
46 |
|
|
|
|
|
47 |
|
48 |
+
### Table
|
49 |
|
50 |
+
table = pn.widgets.Tabulator(
|
51 |
df,
|
52 |
|
53 |
### functionality
|
54 |
|
55 |
+
#formatters= {col : NumberFormatter(format='0.000') for col in df.columns}, #tabulator_formatters,
|
56 |
+
header_filters = {'token': {'type': 'input', 'func': 'like', 'placeholder': 'search'}},
|
57 |
+
selectable='checkbox',
|
58 |
|
59 |
### style
|
60 |
|
61 |
+
theme = "modern", # 'default', 'site', 'simple', 'midnight', 'modern', 'bootstrap', 'bootstrap4', 'materialize', 'bulma', 'semantic-ui', or 'fast'
|
62 |
+
page_size = 8,
|
63 |
+
page = 1,
|
64 |
frozen_columns = {"token" : "left", "sum" : "right"}, # Must give width, otherwise doesn't work!
|
65 |
width=1800,
|
|
|
|
|
|
|
|
|
|
|
66 |
|
67 |
|
68 |
### other
|
|
|
72 |
)
|
73 |
|
74 |
|
75 |
+
### Plot
|
76 |
+
|
77 |
+
def make_fig():
|
78 |
+
fig, ax = plt.subplots(figsize=(12,4))
|
79 |
+
df_temp = load_data(sel_token.value)
|
80 |
+
if len(table.selection) > 0:
|
81 |
+
for i in table.selection:
|
82 |
+
df_temp.iloc[i][:-1].plot(ax=ax, label=df_temp.iloc[i].name, lw=2.2, marker=".")
|
83 |
+
#else:
|
84 |
+
#df.loc["particle"][:-1].plot(ax=ax, label="particle", lw=2.2, marker=".")
|
85 |
+
#plt.hist(np.random.random(10))
|
86 |
+
plt.ylabel("Cosine Similarity", fontsize=12)
|
87 |
+
plt.xlim()
|
88 |
+
plt.legend()
|
89 |
+
plt.close()
|
90 |
+
|
91 |
+
return fig
|
92 |
+
|
93 |
+
def plot_data(event):
|
94 |
+
|
95 |
+
# selected rows as indices in table.selection
|
96 |
+
#token = df.iloc[table.selection[0]].name
|
97 |
+
#values = df.iloc[table.selection[0]][:-1]
|
98 |
+
canvas.loading = True
|
99 |
+
fig = make_fig()
|
100 |
+
canvas.object = fig
|
101 |
+
canvas.loading = False
|
102 |
+
|
103 |
+
button = pn.widgets.Button(
|
104 |
+
name='Plot',
|
105 |
+
button_type='primary',
|
106 |
+
align="center",
|
107 |
+
width=100,
|
108 |
+
icon="snowman",
|
109 |
+
)
|
110 |
+
button.on_click(plot_data)
|
111 |
+
|
112 |
+
|
113 |
+
canvas = pn.pane.Matplotlib(
|
114 |
+
make_fig(),
|
115 |
+
format="svg",
|
116 |
+
#width=1000,
|
117 |
+
sizing_mode='stretch_width',
|
118 |
+
height=400,
|
119 |
+
tight=True)
|
120 |
+
|
121 |
+
|
122 |
+
### Serve
|
123 |
+
|
124 |
+
|
125 |
ACCENT = "teal"
|
126 |
|
127 |
pn.template.FastListTemplate(
|
128 |
+
title="Cosine Similarity for selected tokens",
|
129 |
+
sidebar=[],
|
130 |
+
main=[pn.Column(
|
131 |
+
pn.Row(sel_token),
|
132 |
+
table,
|
133 |
+
button,
|
134 |
+
canvas)],
|
135 |
+
main_layout=None,
|
136 |
accent=ACCENT,
|
137 |
).servable()
|