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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
# Generated from: Group_1.ipynb
# Converted at: 2026-06-16T09:17:14.566Z
# Next step (optional): refactor into modules & generate tests with RunCell
# Quick start: pip install runcell

# # GROUP 1
# Members: Lamis Alghamdi, Ayushi Uttamani, Annie Chien, Yu-Chen Tung




import warnings
warnings.filterwarnings("ignore")
import streamlit as st
st.set_page_config(
    page_title="Mesozoic Dinosaur Occurrences",
    page_icon="🦕",
    layout="wide",
)
import pandas as pd
import altair as alt
from vega_datasets import data as vega_data
import pycountry_convert as pc

alt.data_transformers.disable_max_rows()

df = pd.read_csv("dinosauria_occurrences.csv", low_memory=False)
meso = df[(df["max_ma"] >= 66) & (df["max_ma"] <= 252)].copy()

# We found out that some of the dinosaurs have mixed diet, so we need to simplify it.
# 


def simplify_diet(d):
    if pd.isna(d): return "unknown"
    d = str(d).lower()
    if "carnivore" in d or "piscivore" in d or "insectivore" in d: return "carnivore"
    if "herbivore" in d: return "herbivore"
    if "omnivore" in d: return "omnivore"
    return "unknown"

meso["diet_simple"] = meso["diet"].apply(simplify_diet)

diet_colors = alt.Scale(
    domain=["carnivore", "herbivore", "omnivore", "unknown"],
    range=["#dc2626", "#16a34a", "#f59e0b", "#94a3b8"],
)



# ### About the researcher dataset
# 
# We wanted to visualize [scientific colonialism in paleontology](https://science.thewire.in/the-sciences/parachute-science-palaeontology-european-colonialism-indian-scientists/) by mapping where the fossil was found and where it is currently kept. The best way to do this is by using the longitude/latitude coordinates of the original location and the museum where it is currently housed. The museum column was only partially filled, and for the fossils where it was filled in, the museum country often matched the original location. So, the data was very biased and not useful for showing scientific colonialism in paleontology.
# 
# We thought about using the [OpenAlex API](https://openalex.org/), which is a bibliographic catalog of scientific papers, authors, and institutions. The `reference_no` was used to look up each author's institutions and their country. We then extracted the first author’s country for each publication and merged it with the dataset we currently have.
# 
# We want to do this because it enables a comparison between the geographic origin of fossil discoveries and the researchers studying them. Through this method, we can analyze global patterns of scientific contribution and identify potential imbalances in paleontological research across regions.


enrich = pd.read_csv("references_enriched.csv")
meso = meso.merge(enrich, on="reference_no", how="left")
meso["cc"] = meso["cc"].replace({"UK": "GB"})
meso["first_author_country"] = meso["first_author_country"].replace({"UK": "GB"})

CONTINENT_MAP = {"AF":"Africa","AS":"Asia","EU":"Europe",
                 "NA":"Americas","SA":"Americas","OC":"Oceania"}

def cc_to_continent(cc):
    try: return CONTINENT_MAP.get(pc.country_alpha2_to_continent_code(cc))
    except: return None

meso["fossil_continent"] = meso["cc"].apply(cc_to_continent)
meso["author_continent"] = meso["first_author_country"].apply(cc_to_continent)

CONT_DOMAIN = ["Europe","Americas","Asia","Africa","Oceania"]
CONT_RANGE  = ["#e8c43d","#3b7ec1","#c95eb1","#3a8a3f","#d94f2c"]
cont_scale = alt.Scale(domain=CONT_DOMAIN, range=CONT_RANGE)

cont_sel = alt.selection_point(fields=["fossil_continent"], name="contSel", empty=True)

# ## The period selector
# 
# At first, we limited the users to choose the period, but we decided to increase the interactivity by allowing the users selecting the time range instead.
# 


bins = list(range(66, 258, 5))
meso["ma_bin"] = pd.cut(meso["max_ma"], bins=bins, right=False).apply(
    lambda x: round((x.left + x.right) / 2, 1) if pd.notna(x) else None
)
hist = (
    meso.groupby("ma_bin", observed=False).size()
        .reset_index(name="count")
        .dropna(subset=["ma_bin"])
        .sort_values("ma_bin")
)
hist["ma_bin"] = hist["ma_bin"].astype(float)

y_max = int(hist["count"].max())
boundary_df = pd.DataFrame({"ma": [201.3, 145.0]})
period_df = pd.DataFrame({
    "ma":     [228.0,      173.0,      105.0],
    "label":  ["Triassic", "Jurassic", "Cretaceous"],
    "colour": ["#000000",  "#000000",  "#000000"],
    "y":      [y_max * 1.06] * 3,
})

brush = alt.selection_interval(encodings=["x"], name="time_brush", empty=True)

x_enc = alt.X("ma_bin:Q", scale=alt.Scale(domain=[252, 66]),
               axis=alt.Axis(title="Million Years Ago (Ma)", tickCount=10,
                             grid=True, gridDash=[4, 2]))
y_enc = alt.Y("count:Q", scale=alt.Scale(domain=[0, y_max * 1.14]),
               axis=alt.Axis(title="Number of Occurrences", grid=True, gridDash=[4, 2]))

area_base = (alt.Chart(hist)
    .mark_area(interpolate="monotone", color="#86efac", opacity=0.30,
               stroke="#16a34a", strokeWidth=1.5)
    .encode(x=x_enc, y=y_enc))

area_sel = (alt.Chart(hist)
    .mark_area(interpolate="monotone", color="#4ade80", opacity=0.65,
               stroke="#16a34a", strokeWidth=2.5)
    .encode(x=x_enc, y=y_enc)
    .transform_filter(brush))

rules = (alt.Chart(boundary_df)
    .mark_rule(color="#94a3b8", strokeDash=[5, 3], strokeWidth=1.3, opacity=0.8)
    .encode(x=alt.X("ma:Q", scale=alt.Scale(domain=[252, 66]))))

period_text = (alt.Chart(period_df)
    .mark_text(fontSize=13, fontWeight="bold", align="center", font="Helvetica")
    .encode(
        x=alt.X("ma:Q", scale=alt.Scale(domain=[252, 66])),
        y=alt.Y("y:Q", scale=alt.Scale(domain=[0, y_max * 1.14])),
        text=alt.Text("label:N"),
        color=alt.Color("colour:N", scale=None),
    ))

brush_chart = (
    alt.layer(area_base, area_sel, rules, period_text)
    .properties(width=600, height=180,
        title=alt.TitleParams(
            "Fossil Diversity Over Time",
            subtitle="Drag horizontally to filter all panels below  ·  5 Ma bins",
            fontSize=18, font="Helvetica", fontWeight="bold", color="#000000",
            subtitleFont="Helvetica", subtitleFontSize=12,
            subtitleColor="#000000", anchor="start", offset=12,
        ))
    .add_params(brush)
)

# ## The Continent filter
# 
# We used the country_converter library to map countries to their continents. Users can select the continent(s) they want.
# 


cont_df = pd.DataFrame({"fossil_continent": CONT_DOMAIN})

cont_buttons = (
    alt.Chart(cont_df).mark_rect(cornerRadius=5)
    .encode(
        x=alt.X("fossil_continent:N",
                axis=alt.Axis(labelAngle=0, title=None, ticks=False,
                              domain=False, labelFontSize=12,
                              labelColor="#000000", labelFont="Helvetica",
                              labelFontWeight="bold")),
        color=alt.condition(
            cont_sel,
            alt.Color("fossil_continent:N", scale=cont_scale, legend=None),
            alt.value("#e5e7eb")),
        tooltip=alt.Tooltip("fossil_continent:N", title="Click to filter"),
    )
    .add_params(cont_sel)
    .properties(
        width=600, height=80,
        title=alt.TitleParams(
            "Choosing a Continent",
            subtitle="Click on a continent to choose it  ·  hold SHIFT for multiple  ·  click again to clear",
            fontSize=18, font="Helvetica", fontWeight="bold", color="#000000",
            subtitleFont="Helvetica", subtitleFontSize=12, subtitleFontWeight="bold",
            subtitleColor="#000000", anchor="start", offset=10,
        )
    )
)

# ## The map
# 
# Maps are important for this dataset. The dots represent different fossils, and the colors represent different diets.


#: :generated:
#: :model: Claude Opus 4.7
#: :prompt: Render a world map with country outlines and graticule using vega_datasets world_110m, and overlay fossil dig-site dots from `meso` colored by simplified diet. The dots should react to the time brush and continent pill selection.
#: :changes: 
#: :response:
countries = alt.topo_feature(vega_data.world_110m.url, "countries")

base_world = alt.Chart(countries).mark_geoshape(
    fill="#f1f5f9", stroke="#94a3b8", strokeWidth=0.4
).project(type="equirectangular")

graticule = alt.Chart({"graticule": {"step": [30, 30]}}).mark_geoshape(
    fill=None, stroke="#cbd5e1", strokeWidth=0.3
).project(type="equirectangular")

paleo_points = (
    alt.Chart(meso)
    .mark_circle(size=14, opacity=0.55, stroke="white", strokeWidth=0.3)
    .encode(
        longitude="lng:Q",
        latitude="lat:Q",
        color=alt.Color("diet_simple:N", scale=diet_colors, legend=None),
        tooltip=["accepted_name:N", "diet_simple:N", "max_ma:Q", "ma_bin:Q", "cc:N"],
    )
    .project(type="equirectangular")
    .transform_filter(brush)
    .transform_filter(cont_sel)
)

paleo_map = (
    (base_world + graticule + paleo_points)
    .resolve_scale(color="independent")
    .properties(
        width=600, height=340,
        title=alt.TitleParams(
            "Where They're Found",
            subtitle="Modern dig sites · colored by diet",
            fontSize=18, font="Helvetica", fontWeight="bold", color="#000000",
            subtitleFont="Helvetica", subtitleFontSize=12,
            subtitleColor="#000000", anchor="start", offset=12,
        )
    )
)


# ## KPIs


#: :generated:
#: :model: Claude Opus 4.7
#: :prompt: Build a 2x2 KPI panel for the dashboard with four metrics: distinct family count, total fossil occurrences, percentage of foreign-led papers (first-author country differs from fossil country), and percentage of rare species (accepted_name appearing fewer than RARITY_THRESHOLD times). Each KPI should respond to the time brush and continent selection.
#: :changes: Improved the style.
RARITY_THRESHOLD = 2
KPI_W, KPI_H = 200, 75

def kpi_title(text, subtitle):
    return alt.TitleParams(
        text,
        subtitle=subtitle,
        fontSize=14, font="Helvetica", fontWeight="bold", color="#000000",
        subtitleFont="Helvetica", subtitleFontSize=10, subtitleFontWeight="bold",
        subtitleColor="#000000", anchor="middle",
    )

# Families
kpi_families = (
    alt.Chart(meso.dropna(subset=["family"]).query("family != 'NO_FAMILY_SPECIFIED'"))
    .transform_filter(brush)
    .transform_filter(cont_sel)
    .transform_aggregate(n="distinct(family)")
    .mark_text(fontSize=36, fontWeight="bold", color="#16a34a", font="Helvetica",
               align="center", baseline="middle", dy=2)
    .encode(text=alt.Text("n:Q", format=",.0f"))
    .properties(width=KPI_W, height=KPI_H,
                title=kpi_title("Families", "Unique taxonomic families"))
)

# Occurrences
kpi_occurrences = (
    alt.Chart(meso)
    .transform_filter(brush)
    .transform_filter(cont_sel)
    .transform_aggregate(n="count()")
    .mark_text(fontSize=36, fontWeight="bold", color="#3b7ec1", font="Helvetica",
               align="center", baseline="middle", dy=2)
    .encode(text=alt.Text("n:Q", format=",.0f"))
    .properties(width=KPI_W, height=KPI_H,
                title=kpi_title("Occurrences", "Total fossil records"))
)

# Foreign-led
meso_with_authors = meso.dropna(subset=["cc","first_author_country"]).copy()
meso_with_authors["foreign_led"] = (
    meso_with_authors["cc"] != meso_with_authors["first_author_country"]
).astype(int)
kpi_foreign = (
    alt.Chart(meso_with_authors)
    .transform_filter(brush)
    .transform_filter(cont_sel)
    .transform_aggregate(pct="mean(foreign_led)")
    .transform_calculate(pct_label='format(datum.pct * 100, ".0f") + "%"')
    .mark_text(fontSize=36, fontWeight="bold", color="#dc2626", font="Helvetica",
               align="center", baseline="middle", dy=2)
    .encode(text=alt.Text("pct_label:N"))
    .properties(width=KPI_W, height=KPI_H,
                title=kpi_title("Foreign-Led", "% papers with non-local first author"))
)

# Rarity
name_counts = meso["accepted_name"].value_counts()
rare_names = set(name_counts[name_counts <= RARITY_THRESHOLD].index)
meso["is_rare"] = meso["accepted_name"].isin(rare_names).astype(int)
kpi_rarity = (
    alt.Chart(meso)
    .transform_filter(brush)
    .transform_filter(cont_sel)
    .transform_aggregate(pct="mean(is_rare)")
    .transform_calculate(pct_label='format(datum.pct * 100, ".0f") + "%"')
    .mark_text(fontSize=36, fontWeight="bold", color="#f59e0b", font="Helvetica",
               align="center", baseline="middle", dy=2)
    .encode(text=alt.Text("pct_label:N"))
    .properties(width=KPI_W, height=KPI_H,
                title=kpi_title("Rare Species", f"% with ≤{RARITY_THRESHOLD} mentions"))
)

kpi_panel = alt.vconcat(
    alt.hconcat(kpi_families, kpi_occurrences, spacing=8),
    alt.hconcat(kpi_foreign,  kpi_rarity,      spacing=8),
    spacing=8,
)
#: :end:




# ## The diet
# Creating a chart that illustrates the dietary breakdown of known dinosaur genera
# 


diet_bars = (
    alt.Chart(meso)
    .mark_bar(stroke="white", strokeWidth=1)
    .encode(
        x=alt.X("count():Q", stack="normalize",
                axis=alt.Axis(title="Share", format="%")),
        y=alt.Y("dummy:N", axis=None, title=None),
        color=alt.Color("diet_simple:N", scale=diet_colors,
                        legend=alt.Legend(
                            title="Diet", orient="bottom", direction="horizontal",
                            labelColor="#000000", titleColor="#000000",
                        )),
        tooltip=["diet_simple:N", "count():Q"],
    )
    .transform_calculate(dummy="'all'")
    .transform_filter(brush)
    .transform_filter(cont_sel)
    .properties(
        width=420, height=60,
        title=alt.TitleParams(
            "Diet Ratio",
            subtitle="Share within current selection",
            fontSize=18, font="Helvetica", fontWeight="bold", color="#000000",
            subtitleFont="Helvetica", subtitleFontSize=12,
            subtitleColor="#000000", anchor="start", offset=12,
        )
    )
)

# ## Authors (Researchers)
# 
# Plot the researchers' countries and where the fossil was found.


df_r = meso.dropna(subset=["fossil_continent","author_continent"])

bubble_matrix = (
    alt.Chart(df_r).mark_rect(stroke="white", strokeWidth=1)
    .encode(
        x=alt.X("fossil_continent:N", sort=CONT_DOMAIN,
                title="Fossil location",
                axis=alt.Axis(labelAngle=-30, labelFontSize=11, labelFont="Helvetica",
                              labelColor="#000000", titleColor="#000000",
                              titleFontSize=12, titleFont="Helvetica")),
        y=alt.Y("author_continent:N", sort=CONT_DOMAIN,
                title="Researcher",
                axis=alt.Axis(labelFontSize=11, labelFont="Helvetica",
                              labelColor="#000000", titleColor="#000000",
                              titleFontSize=12, titleFont="Helvetica")),
        color=alt.Color("count():Q",
                        scale=alt.Scale(scheme="greens"),
                        legend=alt.Legend(title="Occurrences", orient="right",
                                          labelColor="#000000", titleColor="#000000",
                                          labelFont="Helvetica", titleFont="Helvetica")),
        opacity=alt.condition(cont_sel, alt.value(1.0), alt.value(0.25)),
        tooltip=[
            alt.Tooltip("fossil_continent:N", title="Fossil continent"),
            alt.Tooltip("author_continent:N", title="Researcher"),
            alt.Tooltip("count():Q", title="Occurrences"),
        ],
    )
    .transform_filter(brush)
    .properties(
        width=420, height=240,
        title=alt.TitleParams(
            "Who Studies Whose Fossils?",
            subtitle="Researcher origin × fossil location",
            fontSize=18, font="Helvetica", fontWeight="bold", color="#000000",
            subtitleFont="Helvetica", subtitleFontSize=12,
            subtitleColor="#000000", anchor="start", offset=12,
        )
    )
)

# ### The dashboard


left_col  = alt.vconcat(brush_chart, cont_buttons, paleo_map, spacing=20)
right_col = alt.vconcat(kpi_panel, diet_bars, bubble_matrix, spacing=20)

dashboard = (
    alt.hconcat(left_col, right_col, spacing=24)
    .resolve_scale(color="independent")
    .properties(
        title=alt.TitleParams(
            "Mesozoic Dinosaur Occurrences",
            subtitle="Drag the time chart to filter  ·  Click a continent pill to filter map, KPIs, and matrix",
            fontSize=22, font="Helvetica", fontWeight="bold", color="#000000",
            subtitleFont="Helvetica", subtitleFontSize=13, subtitleFontWeight="bold",
            subtitleColor="#000000", anchor="start", offset=12,
        )
    )
    .configure(
        background="white",
        padding={"left":40,"right":40,"top":30,"bottom":30},
    )
    .configure_view(strokeWidth=0)
    .configure_axis(
        labelFontSize=12, labelColor="#000000", labelFont="Helvetica",
        titleFontSize=12, titleColor="#000000", titleFont="Helvetica",
        titleFontWeight="bold",
        domainColor="#000000", tickColor="#000000",
        gridColor="#e5e7eb",
    )
    .configure_legend(
        labelFontSize=12, titleFontSize=12,
        labelColor="#000000", titleColor="#000000",
        labelFont="Helvetica", titleFont="Helvetica",
        titleFontWeight="bold", padding=10,
    )
)
import warnings
warnings.filterwarnings("ignore")
st.title("Mesozoic Dinosaur Occurrences")
st.markdown(
    "Interactive exploration of dinosaur fossil occurrences, diet, geography, and researcher origins."
)
st.altair_chart(
    dashboard,
    use_container_width=True
)