Note
Go to the end to download the full example code or to run this example in your browser via Binder.
Evolving narratives in a temporal hypergraph#
The synthetic news hypergraph has three periods. Snapshot turnover, persistence of theme combinations, vertex activity over time and the frequent itemsets per period show how the narratives change — the questions that the hypergraph analysis of climate-change news coverage asks.
import matplotlib.pyplot as plt
import pandas as pd
import hatvis as hv
from hatvis.plotting import plot_activity
T = hv.datasets.make_news_transactions(150, random_state=0)
# [snippet:temporal]
print(hv.temporal.turnover(T))
print(hv.temporal.persistence(T).head(5).to_string())
top = {t: hv.frequent_itemsets(S, min_support=0.08, min_size=2).head(3) for t, S in T.snapshots().items()}
# [/snippet:temporal]
for t, fi in top.items():
print(t, [sorted(s) for s in fi["itemset"]])
share = pd.crosstab(T.edge_data["time"], T.edge_data["narrative"].str.contains("finance"), normalize="index")
print("share of articles with the finance narrative:", share[True].round(3).to_dict())
from to new persisting dropped jaccard
0 2021 2022 94 29 86 0.138756
1 2022 2023 90 26 97 0.122066
members size occurrences first last
0 (AID, DEVELOPING, DROUGHT, FINANCE, FLOOD, HEATWAVE, LOSS_DAMAGE, WILDFIRE) 8 3 2021 2023
1 (AGREEMENT, AID, DEVELOPING, FINANCE, LOSS_DAMAGE, NEGOTIATION, SUMMIT) 7 3 2021 2023
2 (DROUGHT, FLOOD, HEATWAVE, WILDFIRE) 4 3 2021 2023
3 (HEALTH, MIGRATION, PROTEST, YOUTH) 4 3 2021 2023
4 (CLIMATE, EMISSIONS, IPCC, TEMPERATURE) 4 3 2021 2023
2021 [['EMISSIONS', 'TEMPERATURE'], ['COAL', 'OIL_GAS'], ['AGREEMENT', 'PLEDGE']]
2022 [['CLIMATE', 'EMISSIONS'], ['EMISSIONS', 'IPCC'], ['EMISSIONS', 'TEMPERATURE']]
2023 [['DEVELOPING', 'LOSS_DAMAGE'], ['AID', 'LOSS_DAMAGE'], ['DEVELOPING', 'FINANCE']]
share of articles with the finance narrative: {'2021': 0.1, '2022': 0.187, '2023': 0.34}
fig, ax = plt.subplots(figsize=(7, 6))
plot_activity(T, ax=ax, title="theme activity per period")
plt.tight_layout()

Total running time of the script: (0 minutes 0.312 seconds)