Spaces:
Sleeping
Sleeping
Create visualizations.py
Browse files- src/visualizations.py +75 -0
src/visualizations.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def generate_wordcloud(papers):
|
| 2 |
+
text = " ".join([p.get("abstract", "") for p in papers if p.get("abstract")])
|
| 3 |
+
if not text:
|
| 4 |
+
st.info("No abstracts to build a word cloud.")
|
| 5 |
+
return
|
| 6 |
+
|
| 7 |
+
custom_stopwords = set(STOPWORDS).union(stop_words)
|
| 8 |
+
wc = WordCloud(
|
| 9 |
+
width=800,
|
| 10 |
+
height=400,
|
| 11 |
+
stopwords=custom_stopwords,
|
| 12 |
+
background_color="white",
|
| 13 |
+
colormap="viridis"
|
| 14 |
+
).generate(text)
|
| 15 |
+
|
| 16 |
+
fig, ax = plt.subplots()
|
| 17 |
+
ax.imshow(wc, interpolation="bilinear")
|
| 18 |
+
ax.axis("off")
|
| 19 |
+
st.pyplot(fig)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# ========== CO-AUTHOR GRAPH ==========
|
| 23 |
+
def build_coauthor_graph(papers):
|
| 24 |
+
G = nx.Graph()
|
| 25 |
+
|
| 26 |
+
for p in papers:
|
| 27 |
+
authors = p.get("authors", ["Unknown"])
|
| 28 |
+
# normalize
|
| 29 |
+
authors = [a.strip() for a in authors if a and isinstance(a, str)]
|
| 30 |
+
# filter obvious affiliation tokens
|
| 31 |
+
authors = [a for a in authors if not looks_like_affiliation(a) and a.lower() != "unknown"]
|
| 32 |
+
|
| 33 |
+
# add nodes
|
| 34 |
+
for a in authors:
|
| 35 |
+
G.add_node(a)
|
| 36 |
+
|
| 37 |
+
# add edges (with weight)
|
| 38 |
+
for i in range(len(authors)):
|
| 39 |
+
for j in range(i + 1, len(authors)):
|
| 40 |
+
u, v = authors[i], authors[j]
|
| 41 |
+
if G.has_edge(u, v):
|
| 42 |
+
G[u][v]['weight'] += 1
|
| 43 |
+
else:
|
| 44 |
+
G.add_edge(u, v, weight=1)
|
| 45 |
+
|
| 46 |
+
if G.number_of_nodes() == 0:
|
| 47 |
+
st.info("No author data available to build co-author graph.")
|
| 48 |
+
return
|
| 49 |
+
|
| 50 |
+
pos = nx.spring_layout(G, seed=42)
|
| 51 |
+
edge_x, edge_y = [], []
|
| 52 |
+
for u, v, data in G.edges(data=True):
|
| 53 |
+
x0, y0 = pos[u]
|
| 54 |
+
x1, y1 = pos[v]
|
| 55 |
+
edge_x += [x0, x1, None]
|
| 56 |
+
edge_y += [y0, y1, None]
|
| 57 |
+
|
| 58 |
+
node_x, node_y, node_text = [], [], []
|
| 59 |
+
for n in G.nodes():
|
| 60 |
+
x, y = pos[n]
|
| 61 |
+
node_x.append(x); node_y.append(y); node_text.append(n)
|
| 62 |
+
|
| 63 |
+
fig = go.Figure()
|
| 64 |
+
fig.add_trace(go.Scatter(
|
| 65 |
+
x=edge_x, y=edge_y, mode='lines',
|
| 66 |
+
line=dict(width=0.5, color='#888'),
|
| 67 |
+
hoverinfo='none'
|
| 68 |
+
))
|
| 69 |
+
fig.add_trace(go.Scatter(
|
| 70 |
+
x=node_x, y=node_y, mode='markers+text',
|
| 71 |
+
text=node_text, textposition="top center",
|
| 72 |
+
marker=dict(size=12, color='skyblue')
|
| 73 |
+
))
|
| 74 |
+
st.plotly_chart(fig, use_container_width=True)
|
| 75 |
+
|