-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap_collab.py
More file actions
186 lines (158 loc) · 5.52 KB
/
map_collab.py
File metadata and controls
186 lines (158 loc) · 5.52 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
import pycountry
import pandas as pd
import folium
import numpy as np
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter
from math import radians, degrees, sin, cos, atan2, sqrt, asin
from pycountry_convert import country_alpha2_to_continent_code
from tqdm import tqdm
# Function to get continent from country
def get_continent(country_name):
try:
country = pycountry.countries.lookup(country_name)
code = country.alpha_2
continent_map = {
"AF": "Africa",
"AS": "Asia",
"EU": "Europe",
"NA": "North America",
"SA": "South America",
"OC": "Oceania",
}
cont_code = country_alpha2_to_continent_code(code)
return continent_map.get(cont_code, "Other")
except:
return "Other"
def crosses_dateline(lon1, lon2):
"""Check if the shortest path between two longitudes crosses the dateline."""
# Normalize longitudes to [-180, 180]
lon1 = ((lon1 + 180) % 360) - 180
lon2 = ((lon2 + 180) % 360) - 180
# Calculate the difference
diff = abs(lon1 - lon2)
# The shortest path crosses the dateline if the difference is > 180
# This means going "the other way" around the globe is shorter
return diff > 180
def interpolate_arc(lat1, lon1, lat2, lon2, num_points=50):
"""Interpolates great-circle arc points between two coordinates."""
# Convert to radians
lat1, lon1, lat2, lon2 = map(radians, [lat1, lon1, lat2, lon2])
# Compute angle between points
delta = 2 * asin(
sqrt(
sin((lat2 - lat1) / 2) ** 2
+ cos(lat1) * cos(lat2) * sin((lon2 - lon1) / 2) ** 2
)
)
if delta == 0:
return [(degrees(lat1), degrees(lon1))] * num_points
# Interpolate along the arc
points = []
for i in np.linspace(0, 1, num_points):
A = sin((1 - i) * delta) / sin(delta)
B = sin(i * delta) / sin(delta)
x = A * cos(lat1) * cos(lon1) + B * cos(lat2) * cos(lon2)
y = A * cos(lat1) * sin(lon1) + B * cos(lat2) * sin(lon2)
z = A * sin(lat1) + B * sin(lat2)
lat = atan2(z, sqrt(x**2 + y**2))
lon = atan2(y, x)
points.append(
(degrees(lat), ((degrees(lon) + 540) % 360) - 180)
) # Ensures lon stays between -180 and 180
return points
def main():
# Load the SciVal export
df = pd.read_csv("collab.csv")
# Setup geocoder
geolocator = Nominatim(user_agent="scival-map")
geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
# Initialize map and data holders
locations = {}
continent_colors = {
"Europe": "blue",
"Asia": "red",
"North America": "green",
"South America": "orange",
"Africa": "purple",
"Oceania": "cyan",
"Other": "gray",
}
# Geocode institutions
print("Geocoding institutions...")
for idx, row in tqdm(df.iterrows(), total=len(df)):
inst = row["Institution"]
country = row["Country/Region"]
try:
location = geocode(f"{inst}, {country}")
if location:
lat, lon = location.latitude, location.longitude
continent = get_continent(country)
locations[inst] = {
"lat": lat,
"lon": lon,
"country": country,
"continent": continent,
"publications": row["Co-authored publications"],
}
except Exception as e:
print(f"Failed to geocode {inst}: {e}")
# Create map centered roughly on home location
home_location = [-27.4975, 153.0137] # University of Queensland, St Lucia
m = folium.Map(
location=home_location,
zoom_start=2,
max_bounds=True,
world_copy_jump=False,
no_wrap=True,
)
# Add your node in the center (as the main author)
folium.CircleMarker(
location=home_location,
radius=10,
popup="You",
color="black",
fill=True,
fill_opacity=0.9,
).add_to(m)
# Add collaborators and lines
for inst, data in locations.items():
lat, lon = data["lat"], data["lon"]
pub_count = data["publications"]
continent = data["continent"]
color = continent_colors.get(continent, "gray")
# Node
folium.CircleMarker(
location=(lat, lon),
radius=3 + pub_count / 3,
popup=f"{inst} ({data['country']}) - {pub_count} papers",
color=color,
fill=True,
fill_opacity=0.7,
).add_to(m)
# Edge (line to your node)
if crosses_dateline(home_location[1], lon):
# Draw straight line instead of arc if crossing dateline
folium.PolyLine(
locations=[home_location, (lat, lon)],
weight=1 + pub_count / 5,
color="black",
opacity=0.5,
).add_to(m)
else:
# Draw curved arc
arc_points = interpolate_arc(
home_location[0], home_location[1], lat, lon, num_points=50
)
folium.PolyLine(
locations=arc_points,
weight=1 + pub_count / 5,
color="black",
opacity=0.5,
).add_to(m)
# Save the map
m.save("scival_collaboration_map.html")
print("Map saved as 'scival_collaboration_map.html'")
if __name__ == "__main__":
# Display the map in a Jupyter Notebook
main()