-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_v3.py
More file actions
1208 lines (1043 loc) · 47.3 KB
/
Copy pathapp_v3.py
File metadata and controls
1208 lines (1043 loc) · 47.3 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from shiny import App, ui, render, reactive
from src.optimized_data_manager import OptimizedDataManager
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
import matplotlib.pyplot as plt
import json
import ast # For safely evaluating string representations of lists
import base64
import io
from wordcloud import WordCloud
import tempfile
import os
import networkx as nx
from pathlib import Path
import random
# --- Load the optimized data manager ---
print("Initializing Math Research Compass with optimized database...")
data_manager = OptimizedDataManager()
print("Database connection established successfully!")
# Create a mapping dictionary for math categories to their descriptions
math_category_labels = {
"math.AC": "math.AC - Commutative Algebra",
"math.AG": "math.AG - Algebraic Geometry",
"math.AP": "math.AP - Analysis of PDEs",
"math.AT": "math.AT - Algebraic Topology",
"math.CA": "math.CA - Classical Analysis and ODEs",
"math.CO": "math.CO - Combinatorics",
"math.CT": "math.CT - Category Theory",
"math.CV": "math.CV - Complex Variables",
"math.DG": "math.DG - Differential Geometry",
"math.DS": "math.DS - Dynamical Systems",
"math.FA": "math.FA - Functional Analysis",
"math.GM": "math.GM - General Mathematics",
"math.GN": "math.GN - General Topology",
"math.GR": "math.GR - Group Theory",
"math.GT": "math.GT - Geometric Topology",
"math.HO": "math.HO - History and Overview",
"math.IT": "math.IT - Information Theory",
"math.KT": "math.KT - K-Theory and Homology",
"math.LO": "math.LO - Logic",
"math.MG": "math.MG - Metric Geometry",
"math.MP": "math.MP - Mathematical Physics",
"math.NA": "math.NA - Numerical Analysis",
"math.NT": "math.NT - Number Theory",
"math.OA": "math.OA - Operator Algebras",
"math.OC": "math.OC - Optimization and Control",
"math.PR": "math.PR - Probability",
"math.QA": "math.QA - Quantum Algebra",
"math.RA": "math.RA - Rings and Algebras",
"math.RT": "math.RT - Representation Theory",
"math.SG": "math.SG - Symplectic Geometry",
"math.ST": "math.ST - Statistics Theory"
}
# Get available categories from the database
print("Loading category choices...")
dropdown_choices = data_manager.get_category_choices()
print(f"Found {len(dropdown_choices)} categories")
def load_collaboration_network(topic_id):
"""Load collaboration network from pre-computed files if they exist."""
try:
# Look for network files in results/collaboration_analysis/network_graphs/
network_dir = Path("results/collaboration_analysis/network_graphs")
network_file = network_dir / f"topic_{topic_id}_network.json"
if network_file.exists():
with open(network_file, 'r') as f:
network_data = json.load(f)
return network_data
else:
return None
except Exception as e:
print(f"Error loading network for topic {topic_id}: {e}")
return None
def create_collaboration_network_plot(network_data):
"""Create Plotly network visualization from network data."""
if not network_data or 'nodes' not in network_data or 'edges' not in network_data:
return None
nodes = network_data['nodes']
edges = network_data['edges']
# Create edge traces
edge_x = []
edge_y = []
for edge in edges:
x0, y0 = edge['x0'], edge['y0']
x1, y1 = edge['x1'], edge['y1']
edge_x.extend([x0, x1, None])
edge_y.extend([y0, y1, None])
edge_trace = go.Scatter(
x=edge_x, y=edge_y,
line=dict(width=0.5, color='#888'),
hoverinfo='none',
mode='lines'
)
# Create node traces
node_x = [node['x'] for node in nodes]
node_y = [node['y'] for node in nodes]
node_text = [node['name'] for node in nodes]
node_size = [node.get('size', 5) for node in nodes]
node_trace = go.Scatter(
x=node_x, y=node_y,
mode='markers',
hoverinfo='text',
text=node_text,
marker=dict(
showscale=True,
colorscale='YlOrRd',
reversescale=True,
color=node_size,
size=[max(5, min(20, s)) for s in node_size], # Scale sizes between 5-20
colorbar=dict(
thickness=15,
len=0.5,
x=1.02,
title="Papers"
),
line=dict(width=0.5, color='DarkSlateGrey')
)
)
# Create figure
fig = go.Figure(data=[edge_trace, node_trace],
layout=go.Layout(
title='Collaboration Network',
title_font_size=16,
showlegend=False,
hovermode='closest',
margin=dict(b=20,l=5,r=5,t=40),
annotations=[ dict(
text="Node size = papers published<br>Hover for author names",
showarrow=False,
xref="paper", yref="paper",
x=0.005, y=-0.002, xanchor='left', yanchor='bottom',
font=dict(color="grey", size=12)
)],
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
plot_bgcolor='white'
))
return fig
# Create the Shiny app with a tab layout
app_ui = ui.page_navbar(
ui.nav_panel(
"Overview",
ui.tags.head(
ui.tags.style("""
.value-box { text-align: center; }
.value-box .value { font-size: 2rem; }
.project-description {
text-align: center;
max-width: 800px;
margin: 0 auto 30px auto;
line-height: 1.6;
color: #555;
}
.collab-metric {
background: #f8f9fa;
border-left: 4px solid #007bff;
padding: 10px;
margin: 5px 0;
border-radius: 3px;
}
.network-container {
border: 1px solid #ddd;
border-radius: 5px;
padding: 10px;
margin: 10px 0;
}
""")
),
ui.panel_title(
None,
"Math Research Compass"
),
ui.div(
ui.h1("Math Research Compass", class_="text-center"),
ui.h4("Created by Brian Hepler", class_="text-center"),
style="margin-top: 5px; margin-bottom: 20px;"
),
# Project description
ui.div(
ui.p("""
Math Research Compass analyzes ArXiv preprints to identify trending research topics across mathematical subfields from the past 5 years.
This interactive dashboard visualizes topic modeling results from thousands of recent mathematics papers,
helping researchers and students discover emerging areas and popular research directions.
The analysis uses advanced natural language processing to cluster semantically related papers and identify coherent research themes.
Check out the Overview page for a high-level view of topics in each mathematics ArXiv category, or visit the Topic Explorer tab for a deeper-dive into each topic.
"""),
class_="project-description"
),
# Links to GitHub and Personal Website
ui.div(
ui.p([
"View the full documentation on ",
ui.a("GitHub", href="https://github.com/brian-hepler-phd/MathResearchCompass", target="_blank"),
" | Visit the creator's website at ",
ui.a("bhepler.com", href="https://bhepler.com", target="_blank")
], style="text-align: center; margin-bottom: 20px;"),
),
# Add a category dropdown at the top
ui.row(
ui.column(
6,
ui.input_select(
"category",
"Filter by Primary Math Category:",
choices=dropdown_choices,
selected="All Math Categories",
width="100%"
),
offset=3
)
),
# Add explanation of primary category
ui.row(
ui.column(
8,
ui.p("""
Topics are shown based on their primary category - the category that appears most frequently
as the main category across all papers in that topic.
""", style="text-align: center; font-style: italic; color: #666;"),
offset=2
)
),
ui.hr(),
# Summary statistics cards
ui.layout_columns(
ui.value_box(
title=ui.output_text("papers_title"),
value=ui.output_text("total_papers"),
showcase=ui.tags.i(class_="fa-solid fa-file-lines"),
theme="primary",
),
ui.value_box(
title=ui.output_text("topics_title"),
value=ui.output_text("total_topics"),
showcase=ui.tags.i(class_="fa-solid fa-diagram-project"),
theme="primary",
),
col_widths=(6, 6),
),
# Top topics bar chart
ui.card(
ui.card_header(ui.output_text("plot_title")),
ui.output_ui("top_topics_plot"),
),
# Collaboration Analysis Section for Overview
ui.output_ui("overview_collaboration_section"),
),
# Topic Explorer tab
ui.nav_panel(
"Topic Explorer",
ui.panel_title(
ui.h1("Topic Explorer", class_="text-center"),
),
# Category and Topic selection dropdowns
ui.row(
ui.column(
4,
ui.input_select(
"explorer_category",
"Filter by Primary Math Category:",
choices=dropdown_choices,
selected="All Math Categories",
width="100%"
),
offset=2
),
ui.column(
4,
ui.output_ui("topic_dropdown_container")
)
),
# Topic header (shown conditionally when a topic is selected)
ui.output_ui("topic_header"),
# Collaboration Metrics Row
ui.output_ui("collaboration_metrics_section"),
# Existing plots row
ui.row(
# left column - Top Authors
ui.column(
6,
ui.card(
ui.card_header("Top Contributing Authors"),
ui.output_ui("explorer_top_authors_list")
)
),
# right column - Category distribution
ui.column(
6,
ui.card(
ui.card_header("Where Can You Find This Topic?"),
ui.output_ui("explorer_category_dist_plot")
)
)
),
# Network Visualization Row
ui.output_ui("collaboration_network_section"),
# Summary card with details about the selected topic
ui.output_ui("representative_articles")
),
id="navbar",
navbar_options=ui.navbar_options(position="static-top"),
)
def server(input, output, session):
# --- OVERVIEW PAGE FUNCTIONALITY ---
# Reactive filtered dataframe based on primary category - now using optimized database
@reactive.Calc
def filtered_data():
return data_manager.get_topics_by_category(input.category())
# Dynamic titles
@output
@render.text
def papers_title():
if input.category() == "All Math Categories":
return "Total Math Papers"
else:
# Extract just the full name part for display
if " - " in input.category():
category_name = input.category().split(" - ")[1]
return f"Papers in {category_name}"
else:
return f"Papers in {input.category()}"
@output
@render.text
def topics_title():
if input.category() == "All Math Categories":
return "Math Topics Discovered"
else:
# Extract just the full name part for display
if " - " in input.category():
category_name = input.category().split(" - ")[1]
return f"Topics in {category_name}"
else:
return f"Topics in {input.category()}"
@output
@render.text
def plot_title():
if input.category() == "All Math Categories":
return "Top Math Research Topics"
else:
# Extract just the full name part for display
if " - " in input.category():
category_name = input.category().split(" - ")[1]
return f"Top Research Topics in {category_name}"
else:
return f"Top Research Topics in {input.category()}"
# Calculate stats based on filtered data - now using optimized queries
@reactive.Calc
def stats():
filtered_df = filtered_data()
if filtered_df.empty:
return {"papers": 0, "topics": 0}
total_papers = filtered_df['count'].sum()
total_topics = len(filtered_df) # Use len() instead of nunique() since each row is a unique topic
return {"papers": total_papers, "topics": total_topics}
# Output the statistics
@output
@render.text
def total_papers():
return f"{stats()['papers']:,}" # Add comma formatting for readability
@output
@render.text
def total_topics():
return f"{stats()['topics']:,}" # Add comma formatting for readability
@output
@render.ui
def top_topics_plot():
# Get filtered data from optimized database
filtered_df = filtered_data()
# Check if we have data to display
if filtered_df.empty:
return ui.p("No topics found for the selected category.")
# Get top 10 topics by count (or all if fewer than 10)
top_n = min(10, len(filtered_df))
top_topics = filtered_df.sort_values('count', ascending=False).head(top_n)
# Create the bar chart using descriptive_label column
fig = px.bar(
top_topics,
y='descriptive_label',
x='count',
orientation='h',
labels={'count': 'Number of Papers', 'descriptive_label': 'Topic'},
color='count',
color_continuous_scale="viridis",
hover_data=['primary_category'] # Show primary category on hover
)
# Update layout for better appearance
fig.update_layout(
yaxis={'categoryorder': 'total ascending'},
coloraxis_showscale=False,
margin=dict(l=20, r=20, t=50, b=20),
height=500
)
# Return a ui.tags.iframe with the Plotly figure
return ui.tags.iframe(
srcDoc=fig.to_html(include_plotlyjs='cdn'),
style="width:100%; height:500px; border:none;"
)
# NEW: Overview Collaboration Section
@output
@render.ui
def overview_collaboration_section():
# Get collaboration insights for the selected category
try:
collaboration_insights = data_manager.get_collaboration_insights(input.category())
if not collaboration_insights or not collaboration_insights.get('top_collaborative_topics'):
return ui.div()
return ui.div(
ui.h3("Collaboration Network Analysis", class_="text-center", style="margin-top: 30px; margin-bottom: 20px;"),
ui.row(
# Most Collaborative Topics
ui.column(
6,
ui.card(
ui.card_header("Most Collaborative Topics"),
ui.output_ui("most_collaborative_topics_plot")
)
),
# Cross-Topic Collaborators
ui.column(
6,
ui.card(
ui.card_header("Top Cross-Topic Collaborators"),
ui.output_ui("cross_topic_collaborators_list")
)
)
),
style="margin-top: 20px;"
)
except Exception as e:
# If collaboration features aren't available, return empty div
print(f"Collaboration features not available: {e}")
return ui.div()
@output
@render.ui
def most_collaborative_topics_plot():
try:
collaboration_insights = data_manager.get_collaboration_insights(input.category())
top_collab_topics = collaboration_insights.get('top_collaborative_topics', [])
if not top_collab_topics:
return ui.p("No collaboration data available for this category.")
# Prepare data for plotting
collab_df = pd.DataFrame(top_collab_topics)
# Limit to top 8 for readability
collab_df = collab_df.head(8)
# Create horizontal bar chart
fig = px.bar(
collab_df,
y='descriptive_label',
x='collaboration_rate',
orientation='h',
labels={'collaboration_rate': 'Collaboration Rate', 'descriptive_label': 'Topic'},
color='collaboration_rate',
color_continuous_scale="viridis",
hover_data=['network_density']
)
fig.update_layout(
yaxis={'categoryorder': 'total ascending'},
coloraxis_showscale=False,
margin=dict(l=20, r=20, t=20, b=20),
height=400
)
# Format x-axis as percentage
fig.update_xaxes(tickformat='.0%')
return ui.tags.iframe(
srcDoc=fig.to_html(include_plotlyjs='cdn'),
style="width:100%; height:420px; border:none;"
)
except Exception as e:
return ui.p("Collaboration data not available.")
@output
@render.ui
def topic_comparison_section():
"""Show how the current category's topics compare to overall patterns."""
try:
# Get comparison insights from database
category = input.category()
comparison_data = data_manager.get_topic_comparison_insights(category)
if not comparison_data:
return ui.div()
return ui.div(
ui.h3("Topic Network Patterns", class_="text-center", style="margin-top: 30px;"),
ui.row(
ui.column(
6,
ui.card(
ui.card_header("Network Topology Distribution"),
ui.output_ui("network_topology_scatter")
)
),
ui.column(
6,
ui.card(
ui.card_header("Collaboration Patterns by Topic Size"),
ui.output_ui("collaboration_patterns_plot")
)
)
)
)
except:
return ui.div()
@output
@render.ui
def network_topology_scatter():
"""Scatter plot showing centralization vs modularity colored by topic size."""
comparison_data = data_manager.get_topic_comparison_insights(input.category())
if not comparison_data:
return ui.p("No comparison data available.")
df = pd.DataFrame(comparison_data['topics'])
fig = px.scatter(
df,
x='degree_centralization',
y='modularity',
size='total_papers',
color='small_world',
hover_data=['topic_id', 'descriptive_label'],
labels={
'degree_centralization': 'Network Centralization',
'modularity': 'Community Modularity',
'small_world': 'Small-World Network'
},
title="Network Structure Landscape"
)
# Add quadrant labels
fig.add_annotation(x=0.05, y=0.9, text="Isolated<br>Communities", showarrow=False, opacity=0.5)
fig.add_annotation(x=0.2, y=0.9, text="Hub-dominated<br>Communities", showarrow=False, opacity=0.5)
fig.add_annotation(x=0.05, y=0.3, text="Integrated<br>Egalitarian", showarrow=False, opacity=0.5)
fig.add_annotation(x=0.2, y=0.3, text="Integrated<br>Hierarchical", showarrow=False, opacity=0.5)
return ui.tags.iframe(
srcDoc=fig.to_html(include_plotlyjs='cdn'),
style="width:100%; height:400px; border:none;"
)
@output
@render.ui
def cross_topic_collaborators_list():
try:
collaboration_insights = data_manager.get_collaboration_insights(input.category())
cross_topic_collaborators = collaboration_insights.get('cross_topic_collaborators', [])
if not cross_topic_collaborators:
return ui.p("No cross-topic collaboration data available.")
# Simple filtering: the data_manager.get_collaboration_insights already handles filtering
# Just use the data as returned by the database query
selected_category = input.category()
collab_items = []
max_collabs = cross_topic_collaborators[0]['cross_topic_collaborations'] if cross_topic_collaborators else 1
for idx, author_info in enumerate(cross_topic_collaborators[:8]):
author_name = author_info['author_name']
cross_topic_count = author_info['cross_topic_collaborations']
total_papers = author_info['total_papers']
num_topics = author_info['num_topics']
primary_topic = author_info.get('primary_topic', 'Unknown')
# Format author name
formatted_name = author_name
if ',' in author_name and len(author_name.split(',')) == 2:
last, first = author_name.split(',', 1)
formatted_name = f"{first.strip()} {last.strip()}"
percentage = (cross_topic_count / max_collabs) * 100 if max_collabs > 0 else 0
# Get primary topic name for display
primary_topic_name = f"Topic {primary_topic}"
try:
topic_details = data_manager.get_topic_details(primary_topic)
if topic_details and topic_details.get('info'):
topic_label = topic_details['info'].get('descriptive_label', f"Topic {primary_topic}")
if selected_category == "All Math Categories":
primary_topic_name = f"Topic {primary_topic}: {topic_label[:50]}{'...' if len(topic_label) > 50 else ''}"
else:
primary_topic_name = f"{topic_label[:50]}{'...' if len(topic_label) > 50 else ''}"
except:
pass
collab_items.append(
ui.div(
ui.div(
f"{idx+1}. {formatted_name}",
style="display: inline-block; width: 60%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; vertical-align: middle; font-weight: bold;"
),
ui.div(
f"{cross_topic_count} cross-topic collaborations",
style="display: inline-block; width: 35%; text-align: right; vertical-align: middle; font-size: 0.9em;"
),
ui.div(
f"Primary area: {primary_topic_name}",
style="font-size: 0.75em; color: #666; margin-top: 2px;"
),
ui.div(
f"{total_papers} total papers across {num_topics} topics",
style="font-size: 0.8em; color: #666; margin-top: 2px;"
),
ui.div( # Progress bar
style=f"background-color: #17a2b8; height: 4px; width: {percentage}%; margin-top: 5px;"
),
style="margin-bottom: 12px; border-bottom: 1px solid #eee; padding-bottom: 8px;"
)
)
# Update the description based on category selection
if selected_category == "All Math Categories":
description = "Researchers collaborating across multiple mathematical topics:"
else:
category_name = selected_category.split(" - ")[1] if " - " in selected_category else selected_category
description = f"Top cross-topic collaborators in {category_name}:"
return ui.div(
ui.p(description,
style="font-style: italic; margin-bottom: 15px;"),
ui.div(*collab_items, style="margin-top: 5px;"),
style="padding: 0 10px;"
)
except Exception as e:
return ui.p("Cross-topic collaboration data not available.")
# --- TOPIC EXPLORER PAGE FUNCTIONALITY ---
# Filtered data for explorer page - now using optimized database
@reactive.Calc
def filtered_explorer_data():
selected_category = input.explorer_category()
return data_manager.get_topics_by_category(selected_category)
# Get topic choices for the selected category
@reactive.Calc
def get_topic_choices():
filtered_df = filtered_explorer_data()
if filtered_df.empty:
return []
# Create a list of (topic_id, descriptive_label) tuples for the dropdown
topic_choices = [
(str(row['topic_id']), f"Topic {row['topic_id']}: {row['descriptive_label']}")
for _, row in filtered_df.iterrows()
]
# Sort by topic ID
topic_choices.sort(key=lambda x: int(x[0]))
return topic_choices
# Dynamic topic dropdown
@output
@render.ui
def topic_dropdown_container():
topic_choices = get_topic_choices()
if not topic_choices:
return ui.p("No topics available for this category.")
# Create a dropdown with the topic choices
return ui.input_select(
"selected_topic",
"Select Topic:",
choices=dict(topic_choices),
width="100%"
)
# Get the selected topic data - now using optimized database
@reactive.Calc
def selected_topic_data():
if not hasattr(input, 'selected_topic') or not input.selected_topic():
return None
topic_id = int(input.selected_topic())
return data_manager.get_topic_details(topic_id)
# Topic header with dynamic title
@output
@render.ui
def topic_header():
topic_data = selected_topic_data()
if topic_data is None:
return ui.div() # Empty div if no topic selected
topic_info = topic_data['info']
return ui.div(
ui.h2(f"Topic {topic_info['topic_id']}: {topic_info['descriptive_label']}",
class_="text-center",
style="margin-top: 30px; margin-bottom: 20px;"
),
ui.hr()
)
# NEW: Collaboration Metrics Section
@output
@render.ui
def collaboration_metrics_section():
topic_data = selected_topic_data()
if topic_data is None:
return ui.div()
try:
collaboration_metrics = topic_data.get('collaboration_metrics')
enhanced_metrics = topic_data.get('enhanced_metrics') # NEW: Get enhanced metrics
if not collaboration_metrics:
return ui.div()
# Get team size distribution
topic_id = topic_data['info']['topic_id']
team_size_dist = data_manager.get_topic_team_size_distribution(topic_id)
# NEW: Extract enhanced collaboration data
advanced_metrics = enhanced_metrics.get('advanced', {}) if enhanced_metrics else {}
degree_analysis = enhanced_metrics.get('degree_analysis', {}) if enhanced_metrics else {}
# Create collaboration metrics cards with enhanced features
return ui.div(
ui.h4("Collaboration Network Analysis", class_="text-center", style="margin-top: 20px; margin-bottom: 15px;"),
# Row 1: Basic collaboration metrics
ui.row(
ui.column(
4,
ui.div(
ui.h5("Collaboration Rate", style="margin-bottom: 5px; color: #007bff;"),
ui.h3(f"{collaboration_metrics['collaboration_rate']:.1%}", style="margin: 0; font-weight: bold;"),
ui.tags.small("of papers have multiple authors", style="color: #666;"),
class_="collab-metric"
)
),
ui.column(
4,
ui.div(
ui.h5("Repeat Collaborations", style="margin-bottom: 5px; color: #007bff;"),
ui.h3(f"{collaboration_metrics.get('repeat_collaboration_rate', 0):.1%}", style="margin: 0; font-weight: bold;"),
ui.tags.small("of partnerships are recurring", style="color: #666;"),
class_="collab-metric"
)
),
ui.column(
4,
ui.div(
ui.h5("Research Communities", style="margin-bottom: 5px; color: #007bff;"),
ui.h3(f"{collaboration_metrics['num_components']}", style="margin: 0; font-weight: bold;"),
ui.tags.small("connected research groups", style="color: #666;"),
class_="collab-metric"
)
)
),
# NEW: Row 2: Enhanced network topology metrics
ui.row(
ui.column(
3,
ui.div(
ui.h5("Centralization", style="margin-bottom: 5px; color: #28a745;"),
ui.h3(f"{advanced_metrics.get('degree_centralization', 0):.3f}", style="margin: 0; font-weight: bold;"),
ui.tags.small("network hierarchy level", style="color: #666;"),
class_="collab-metric"
)
),
ui.column(
3,
ui.div(
ui.h5("Modularity", style="margin-bottom: 5px; color: #28a745;"),
ui.h3(f"{advanced_metrics.get('modularity', 0):.3f}", style="margin: 0; font-weight: bold;"),
ui.tags.small("community strength", style="color: #666;"),
class_="collab-metric"
)
),
ui.column(
3,
ui.div(
ui.h5("Small-World", style="margin-bottom: 5px; color: #28a745;"),
ui.h3("✓" if advanced_metrics.get('is_small_world', False) else "✗",
style="margin: 0; font-weight: bold; color: #28a745;" if advanced_metrics.get('is_small_world', False) else "margin: 0; font-weight: bold; color: #dc3545;"),
ui.tags.small("high clustering + short paths", style="color: #666;"),
class_="collab-metric"
)
),
ui.column(
3,
ui.div(
ui.h5("Power-Law", style="margin-bottom: 5px; color: #28a745;"),
ui.h3("✓" if degree_analysis.get('power_law_good_fit', False) else "✗",
style="margin: 0; font-weight: bold; color: #28a745;" if degree_analysis.get('power_law_good_fit', False) else "margin: 0; font-weight: bold; color: #dc3545;"),
ui.tags.small("scale-free network", style="color: #666;"),
class_="collab-metric"
)
)
),
# Add team size distribution and enhanced insights
ui.output_ui("team_size_distribution_plot") if team_size_dist else ui.div(),
ui.output_ui("enhanced_network_insights"), # NEW: Add enhanced insights
style="margin-bottom: 20px;"
)
except Exception as e:
return ui.div()
# NEW: Collaboration Network Visualization Section
@output
@render.ui
def collaboration_network_section():
topic_data = selected_topic_data()
if topic_data is None:
return ui.div()
try:
collaboration_metrics = topic_data.get('collaboration_metrics')
if not collaboration_metrics or collaboration_metrics.get('num_authors', 0) < 3:
return ui.div()
topic_id = topic_data['info']['topic_id']
top_authors_list = topic_data.get('top_authors', [])
# Try to load pre-computed network, otherwise generate sample
network_data = load_collaboration_network(topic_id)
if network_data is None:
# Generate sample network based on collaboration metrics with real author names
network_data = generate_sample_network(topic_id, collaboration_metrics, top_authors_list)
if network_data is None:
return ui.div()
# Create network visualization
network_fig = create_collaboration_network_plot(network_data)
if network_fig is None:
return ui.div()
return ui.div(
ui.row(
ui.column(
12,
ui.card(
ui.card_header("Collaboration Network - Largest Connected Component"),
ui.div(
ui.tags.iframe(
srcDoc=network_fig.to_html(include_plotlyjs='cdn'),
style="width:100%; height:500px; border:none;"
),
class_="network-container"
),
ui.div(
ui.p([
ui.strong("Network Insights: "),
f"This visualization shows the largest connected component of researchers collaborating in this topic. ",
f"Node size represents the number of papers published by each author. ",
f"The network has {collaboration_metrics['num_authors']} total authors with ",
f"{collaboration_metrics['network_density']:.3f} connectivity density."
], style="margin-top: 10px; font-size: 0.9em; color: #666;")
)
)
)
),
style="margin-bottom: 20px;"
)
except Exception as e:
return ui.div()
@output
@render.ui
def enhanced_network_insights():
topic_data = selected_topic_data()
if topic_data is None or 'enhanced_metrics' not in topic_data:
return ui.div()
enhanced = topic_data['enhanced_metrics']
advanced = enhanced.get('advanced', {})
# Determine network type based on metrics
insights = []
# Centralization insight
centralization = advanced.get('degree_centralization', 0)
if centralization > 0.15:
insights.append("🎯 **Hub-dominated**: A few key researchers drive collaborations")
elif centralization < 0.05:
insights.append("🤝 **Egalitarian**: Collaborations are evenly distributed")
# Modularity insight
modularity = advanced.get('modularity', 0)
if modularity > 0.8:
insights.append("🏘️ **Highly modular**: Distinct research subcommunities exist")
elif modularity < 0.4:
insights.append("🌐 **Well-integrated**: Strong cross-community collaboration")
# Robustness insight
robustness_ratio = advanced.get('robustness_ratio', 1)
if robustness_ratio < 0.1:
insights.append("⚠️ **Fragile**: Network depends heavily on key individuals")
elif robustness_ratio > 0.5:
insights.append("💪 **Resilient**: Network can withstand researcher departures")
# Core-periphery insight
coreness = advanced.get('coreness', 0)
if coreness > 0.5:
insights.append("🎪 **Core-periphery**: Clear insider/outsider structure")
if not insights:
return ui.div()
return ui.div(
ui.h5("Network Characteristics", style="margin-top: 20px; margin-bottom: 10px;"),
ui.div(
*[ui.p(insight, style="margin: 5px 0; padding: 5px 10px; background: #f0f8ff; border-radius: 3px;")
for insight in insights],
style="font-size: 0.9em;"
),
style="margin-top: 15px;"
)
@output
@render.ui
def research_communities_section():
"""Display detected research communities within the topic."""
topic_data = selected_topic_data()
if not topic_data:
return ui.div()
enhanced = topic_data.get('enhanced_metrics', {})
communities = enhanced.get('communities', {})
if not communities or communities.get('num_communities', 0) < 2:
return ui.div()
return ui.div(
ui.h5(f"Research Subcommunities ({communities['num_communities']} detected)",
style="margin-top: 20px;"),