File size: 4,347 Bytes
2891210
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import ast
import os
from copy import deepcopy

import dhg
import gradio as gr
import matplotlib.pyplot as plt
import pandas as pd
from dhg.visualization.structure.defaults import (default_hypergraph_strength,
                                                  default_hypergraph_style,
                                                  default_size)
from dhg.visualization.structure.layout import force_layout
from dhg.visualization.structure.utils import draw_circle_edge, draw_vertex
from huggingface_hub import hf_hub_download


def draw_hypergraph(
    hg: "dhg.Hypergraph",
    e_style="circle",
    v_label=None,
    v_size=1.0,
    v_color="r",
    v_line_width=1.0,
    e_color="gray",
    e_fill_color="whitesmoke",
    e_line_width=1.0,
    font_size=1.0,
    font_family="sans-serif",
    push_v_strength=1.0,
    push_e_strength=1.0,
    pull_e_strength=1.0,
    pull_center_strength=1.0,
):
    fig, ax = plt.subplots(figsize=(6, 6))

    num_v, e_list = hg.num_v, deepcopy(hg.e[0])
    # default configures
    v_color, e_color, e_fill_color = default_hypergraph_style(
        hg.num_v, hg.num_e, v_color, e_color, e_fill_color
    )
    v_size, v_line_width, e_line_width, font_size = default_size(
        num_v, e_list, v_size, v_line_width, e_line_width
    )
    (
        push_v_strength,
        push_e_strength,
        pull_e_strength,
        pull_center_strength,
    ) = default_hypergraph_strength(
        num_v,
        e_list,
        push_v_strength,
        push_e_strength,
        pull_e_strength,
        pull_center_strength,
    )
    # layout
    v_coor = force_layout(
        num_v,
        e_list,
        push_v_strength,
        push_e_strength,
        pull_e_strength,
        pull_center_strength,
    )
    draw_circle_edge(
        ax,
        v_coor,
        v_size,
        e_list,
        e_color,
        e_fill_color,
        e_line_width,
    )

    draw_vertex(
        ax,
        v_coor,
        v_label,
        font_size,
        font_family,
        v_size,
        v_color,
        v_line_width,
    )

    plt.xlim((0, 1.0))
    plt.ylim((0, 1.0))
    plt.axis("off")
    fig.tight_layout()

    return fig


def plot_dataset(dataset_choice: str, sampling_choice: str, split_choice: str):
    os.makedirs("artifacts", exist_ok=True)
    hf_hub_download(
        filename=f"processed/{sampling_choice}/{split_choice}_df.csv",
        local_dir="./artifacts/",
        repo_id=f"SauravMaheshkar/{dataset_choice}",
        repo_type="dataset",
    )

    df = pd.read_csv(f"artifacts/processed/{sampling_choice}/{split_choice}_df.csv")

    num_vertices = len(df)
    edge_list = df["nodes"].values.tolist()
    edge_list = [ast.literal_eval(edges) for edges in edge_list]

    hypergraph = dhg.Hypergraph(num_vertices, edge_list)

    fig = draw_hypergraph(hypergraph)
    return fig


with gr.Blocks() as demo:

    with gr.Row():
        dataset_choices = gr.Dropdown(
            choices=[
                "email-Eu",
                "email-Enron",
                "NDC-classes",
                "tags-math-sx",
                "email-Eu-25",
                "NDC-substances",
                "congress-bills",
                "tags-ask-ubuntu",
                "email-Enron-25",
                "NDC-classes-25",
                "threads-ask-ubuntu",
                "contact-high-school",
                "NDC-substances-25",
                "congress-bills-25",
                "contact-primary-school",
            ],
            value="email-Enron-25",
            label="Please choose a dataset",
            interactive=True,
        )

        sampling_choice = gr.Dropdown(
            choices=[
                "transductive",
                "inductive",
            ],
            value="inductive",
            label="Choose sampling type",
            interactive=True,
        )

        split_choice = gr.Dropdown(
            choices=[
                "train",
                "valid",
                "test",
            ],
            value="test",
            label="Choose split",
            interactive=True,
        )

    output_plot = gr.Plot(label="Hypergraph plot")

    btn = gr.Button("Visualise")
    btn.click(
        fn=plot_dataset,
        inputs=[dataset_choices, sampling_choice, split_choice],
        outputs=output_plot,
    )

demo.launch()