File size: 3,274 Bytes
62fa030
df99924
 
827ec23
df99924
62fa030
827ec23
 
62fa030
 
827ec23
 
62fa030
 
827ec23
62fa030
827ec23
 
 
 
 
 
 
 
 
62fa030
827ec23
 
 
 
62fa030
827ec23
 
 
 
62fa030
827ec23
 
 
 
 
 
df99924
 
827ec23
df99924
62fa030
827ec23
62fa030
827ec23
df99924
62fa030
df99924
 
827ec23
 
 
df99924
 
 
827ec23
 
 
df99924
 
 
 
 
 
 
62fa030
 
 
df99924
827ec23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df99924
 
62fa030
827ec23
 
 
 
 
 
 
 
62fa030
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pandas as pd
import panel as pn
import numpy as np
import hvplot.pandas
from bokeh.models.widgets.tables import NumberFormatter, BooleanFormatter

import matplotlib.pyplot as plt
import seaborn as sns


sns.set(style='whitegrid', context='notebook')
#plt.rcParams["font.family"] = "Latin Modern Sans"


### Data

def load_data(sel_token):
    if sel_token == "virtual":
        df = pd.read_pickle("server_data/virtual_fulltexts_cossim_all_slice=1.pkl")
        df = df.drop("virtual")
    elif sel_token == "boson":
        df = pd.read_pickle("server_data/cossims_plural_slicewidth=1.pkl")
    elif sel_token == "intermediate":
        df = pd.read_pickle("server_data/intermediate_fulltexts_cossim_all_slice=1.pkl")
        df = df.drop("intermediate")

    topn = 1000
    df["sum"] = df.sum(axis=1)
    df = df.astype("float32")
    df = df.round(3)

    df = df.sort_values("sum", ascending=False)
    df = df.head(topn)
    df.index.name = "token"
    return df

sel_token = pn.widgets.Select(
    name="Select dataset",
    value="virtual",
    options=["virtual", "intermediate", "boson"],
    #description="Select the base token ",
)


df = pn.rx(load_data)(sel_token=sel_token)


### Table

table = pn.widgets.Tabulator(
    df,

    ### functionality

    #formatters= {col : NumberFormatter(format='0.000') for col in df.columns}, #tabulator_formatters,
    header_filters = {'token': {'type': 'input', 'func': 'like', 'placeholder': 'search'}},
    selectable='checkbox',

    ### style

    theme = "modern", # 'default', 'site', 'simple', 'midnight', 'modern', 'bootstrap', 'bootstrap4', 'materialize', 'bulma', 'semantic-ui', or 'fast'
    page_size = 8,
    page = 1,
    frozen_columns = {"token" : "left", "sum" : "right"}, # Must give width, otherwise doesn't work!
    width=1800,
    

    ### other

    disabled = True # Whether the cells are editable

)


### Plot

def make_fig():
    fig, ax = plt.subplots(figsize=(12,4))
    df_temp = load_data(sel_token.value)
    if len(table.selection) > 0:
        for i in table.selection:
            df_temp.iloc[i][:-1].plot(ax=ax, label=df_temp.iloc[i].name, lw=2.2, marker=".")
    #else:
        #df.loc["particle"][:-1].plot(ax=ax, label="particle", lw=2.2, marker=".")
        #plt.hist(np.random.random(10))
    plt.ylabel("Cosine Similarity", fontsize=12)
    plt.xlim()
    plt.legend()
    plt.close()

    return fig

def plot_data(event):

    # selected rows as indices in table.selection
    #token = df.iloc[table.selection[0]].name
    #values = df.iloc[table.selection[0]][:-1]
    canvas.loading = True
    fig = make_fig()
    canvas.object = fig
    canvas.loading = False

button = pn.widgets.Button(
    name='Plot', 
    button_type='primary', 
    align="center",
    width=100,
    icon="snowman",
)
button.on_click(plot_data)


canvas = pn.pane.Matplotlib(
    make_fig(),
    format="svg", 
    #width=1000,
    sizing_mode='stretch_width',
    height=400,
    tight=True)


### Serve


ACCENT = "teal"

pn.template.FastListTemplate(
    title="Cosine Similarity for selected tokens",
    sidebar=[],
    main=[pn.Column(
            pn.Row(sel_token),
            table, 
            button, 
            canvas)],
    main_layout=None,
    accent=ACCENT,
).servable()