File size: 14,982 Bytes
7f9a652
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from evallib import fmt
from common import long_features

DS = 'Lilambd/world-hazard'
REVISION = 'hf-main@2026-11-03 (data as of 2026-11-01)'
LICENSE = 'CC BY 4.0'
STR = {'data/admin1.csv': ['a1', 'iso2', 'iso_3166_2', 'hasc'], 'data/station_warming.csv': ['station', 'fips2']}
KEYS = {
    'data/admin1.csv': ['a1', 'name', 'country', 'iso_3166_2'],
    'data/admin1_quake.csv': ['a1', 'name', 'country'],
    'data/admin1_warming.csv': ['a1', 'name', 'country'],
    'data/country_warming.csv': ['country'],
    'data/country_quake.csv': ['method', 'country'],
    'data/country_event.csv': ['kind', 'country'],
    'data/decade_quake.csv': ['decade'],
    'data/band_warming.csv': ['band'],
    'data/station_warming.csv': ['station', 'name'],
    'data/events.csv': ['kind', 'id', 'year', 'country'],
    'data/joint_exposure.csv': ['a1', 'name', 'country'],
    'data/features_country.csv': ['iso3', 'feature'],
    'data/year_anomaly.csv': ['year', 'band'],
}
SH = 'World Hazard Ledger'


def build(B):
    s = B.store
    q = s['data/admin1_quake.csv']
    # unique (name,country)
    dup = q.duplicated(['name', 'country'], keep=False)
    qu = q[~dup]
    for _, r in qu.loc[B.sample(qu.index, 45)].iterrows():
        place = f"{r['name']} ({r['country']})"
        c = B.rng.choice(['m6', 'm7', 'm6_per_1000km2', 'm6_in'])
        text = {
            'm6': f'In the {SH}, how many M6+ earthquakes from 1900 to 2026 are assigned to {place}?',
            'm7': f'In the {SH}, how many M7+ earthquakes from 1900 to 2026 are assigned to {place}?',
            'm6_per_1000km2': f'In the {SH}, how many M6+ earthquakes (1900-2026) per 1,000 km² does {place} have?',
            'm6_in': f'In the {SH}, how many of the M6+ earthquakes assigned to {place} had their epicentre inside the region (column m6_in)?',
        }[c]
        B.add('lookup', text, {'op': 'lookup', 'file': 'data/admin1_quake.csv', 'where': [['name', '==', r['name']], ['country', '==', r['country']]], 'col': c},
              tol={'abs': 0} if c != 'm6_per_1000km2' else {'rel': 0.01}, skills=['table_lookup'])
    cs = q.groupby('country').size()
    for c in B.sample(cs[cs >= 4].index, 22):
        col = B.rng.choice(['m6', 'm7', 'm6_per_1000km2'])
        word = {'m6': 'the most M6+ earthquakes', 'm7': 'the most M7+ earthquakes', 'm6_per_1000km2': 'the highest density of M6+ earthquakes per 1,000 km²'}[col]
        B.add('rank', f'In the {SH}, which admin-1 region of {c} has {word} since 1900?',
              {'op': 'argmax', 'file': 'data/admin1_quake.csv', 'where': [['country', '==', c]], 'col': col, 'return': 'name'},
              difficulty=2, skills=['ranking', 'group_filter'])
    B.add('rank', f'In the {SH}, list the 5 admin-1 regions with the most M7+ earthquakes since 1900, most first.',
          {'op': 'topk', 'k': 5, 'file': 'data/admin1_quake.csv', 'col': 'm7', 'return': 'name'}, difficulty=3, skills=['ranking', 'full_scan'], answer_format='JSON list of region names')
    for _ in range(18):
        a, b = qu.sample(2, random_state=B.rng.randint(0, 1 << 30)).to_dict('records')
        B.add('compare', f"In the {SH}, which region had more M6+ earthquakes since 1900: {a['name']} ({a['country']}) or {b['name']} ({b['country']})?",
              {'op': 'compare', 'labels': [a['name'], b['name']], 'parts': [
                  {'op': 'lookup', 'file': 'data/admin1_quake.csv', 'where': [['a1', '==', a['a1']]], 'col': 'm6'},
                  {'op': 'lookup', 'file': 'data/admin1_quake.csv', 'where': [['a1', '==', b['a1']]], 'col': 'm6'}]},
              difficulty=2, skills=['comparison'])
    for c in B.sample(cs[cs >= 3].index, 12):
        B.add('aggregate', f'In the {SH}, what is the total number of M7+ earthquakes summed over all admin-1 regions of {c} listed in admin1_quake?',
              {'op': 'sum', 'file': 'data/admin1_quake.csv', 'where': [['country', '==', c]], 'col': 'm7'}, tol={'abs': 0}, difficulty=2, skills=['aggregation'])
        B.add('count', f'In the {SH}, how many admin-1 regions of {c} have at least one M7+ earthquake since 1900?',
              {'op': 'count', 'file': 'data/admin1_quake.csv', 'where': [['country', '==', c], ['m7', '>=', 1]]}, difficulty=2, skills=['filter', 'count'])

    w = s['data/admin1_warming.csv']
    wu = w[~w.duplicated(['name', 'country'], keep=False)]
    for _, r in wu.loc[B.sample(wu.index, 35)].iterrows():
        col = B.rng.choice(['mean', 'mean', 'n'])
        t = (f"In the {SH}, by how many °C did the weather stations of {r['name']} ({r['country']}) warm on average (1991-2020 normal minus 1961-1990 normal)?"
             if col == 'mean' else f"In the {SH}, how many weather stations with both normals are counted for {r['name']} ({r['country']})?")
        B.add('lookup', t, {'op': 'lookup', 'file': 'data/admin1_warming.csv', 'where': [['a1', '==', r['a1']]], 'col': col},
              unit='°C' if col == 'mean' else None, tol={'abs': 0.002} if col == 'mean' else {'abs': 0}, skills=['table_lookup'])
    wc = w.groupby('country').size()
    for c in B.sample(wc[wc >= 4].index, 15):
        B.add('rank', f'In the {SH}, which admin-1 region of {c} shows the largest mean station warming?',
              {'op': 'argmax', 'file': 'data/admin1_warming.csv', 'where': [['country', '==', c]], 'col': 'mean', 'return': 'name'}, difficulty=2, skills=['ranking', 'group_filter'])
    # absent warming: regions in admin1 with no station record
    a1 = s['data/admin1.csv']
    have = set(w['a1'])
    miss = a1[~a1['a1'].isin(have) & ~a1.duplicated(['name', 'country'], keep=False)]
    for _, r in miss.loc[B.sample(miss.index, 14)].iterrows():
        B.add('unanswerable', f"In the {SH}, what is the mean station warming (°C) recorded for {r['name']} ({r['country']})? If the ledger has no station record for it, answer null.",
              {'op': 'absent', 'file': 'data/admin1_warming.csv', 'where': [['a1', '==', r['a1']]]}, difficulty=2, skills=['abstention'],
              note='The README states a region missing from admin1_warming has no station record, which is not the same as no warming.')

    cw = s['data/country_warming.csv']
    for _, r in cw.loc[B.sample(cw.index, 15)].iterrows():
        B.add('lookup', f"In the {SH}, what is the mean station warming of {r['country']} in °C?",
              {'op': 'lookup', 'file': 'data/country_warming.csv', 'where': [['country', '==', r['country']]], 'col': 'mean'}, unit='°C', tol={'abs': 0.002}, skills=['table_lookup'])
    for _ in range(10):
        a, b = cw.sample(2, random_state=B.rng.randint(0, 1 << 30))['country']
        B.add('compare', f'In the {SH}, which country warmed more on average (station mean): {a} or {b}?',
              {'op': 'compare', 'labels': [a, b], 'parts': [
                  {'op': 'lookup', 'file': 'data/country_warming.csv', 'where': [['country', '==', a]], 'col': 'mean'},
                  {'op': 'lookup', 'file': 'data/country_warming.csv', 'where': [['country', '==', b]], 'col': 'mean'}]}, difficulty=2, skills=['comparison'])
    B.add('rank', f'In the {SH}, which country has the highest mean station warming among countries with at least 10 stations?',
          {'op': 'argmax', 'file': 'data/country_warming.csv', 'where': [['n', '>=', 10]], 'col': 'mean', 'return': 'country'}, difficulty=3, skills=['ranking', 'filter'])

    ce = s['data/country_event.csv']
    kinds = sorted(ce['kind'].unique())
    for _, r in ce.loc[B.sample(ce.index, 25)].iterrows():
        col = B.rng.choice(['n', 'deaths'])
        kind = r['kind'].replace('sig_', 'significant ')
        t = (f"In the {SH}, how many {kind} events are recorded for {r['country']}?" if col == 'n' else f"In the {SH}, how many deaths from {kind} events are recorded for {r['country']}?")
        B.add('lookup', t, {'op': 'lookup', 'file': 'data/country_event.csv', 'where': [['kind', '==', r['kind']], ['country', '==', r['country']]], 'col': col}, tol={'abs': 0}, skills=['table_lookup'])
    for k in kinds:
        kind = k.replace('sig_', 'significant ')
        B.add('rank', f'In the {SH}, which country has the most recorded deaths from {kind} events?',
              {'op': 'argmax', 'file': 'data/country_event.csv', 'where': [['kind', '==', k]], 'col': 'deaths', 'return': 'country'}, difficulty=2, skills=['ranking'])
        B.add('rank', f'In the {SH}, list the 3 countries with the most {kind} events, most first.',
              {'op': 'topk', 'k': 3, 'file': 'data/country_event.csv', 'where': [['kind', '==', k]], 'col': 'n', 'return': 'country'}, difficulty=2, skills=['ranking'], answer_format='JSON list of country names as written in the ledger')
        B.add('aggregate', f'In the {SH}, what is the total number of deaths from {kind} events summed over all countries?',
              {'op': 'sum', 'file': 'data/country_event.csv', 'where': [['kind', '==', k]], 'col': 'deaths'}, tol={'abs': 0}, difficulty=2, skills=['aggregation'])

    for _, r in s['data/decade_quake.csv'].iterrows():
        col = B.rng.choice(['m6', 'm7', 'm8'])
        B.add('lookup', f"In the {SH}, how many {col.upper()}+ earthquakes are counted in the decade starting {r['decade']}?",
              {'op': 'lookup', 'file': 'data/decade_quake.csv', 'where': [['decade', '==', int(r['decade'])]], 'col': col}, tol={'abs': 0}, skills=['table_lookup'])
    B.add('rank', f'In the {SH}, which decade has the most M8+ earthquakes?', {'op': 'argmax', 'file': 'data/decade_quake.csv', 'col': 'm8', 'return': 'decade'}, difficulty=2, skills=['ranking'])
    for _, r in s['data/band_warming.csv'].iterrows():
        B.add('lookup', f"In the {SH}, what is the mean station warming (°C) in the latitude band {r['band']}?",
              {'op': 'lookup', 'file': 'data/band_warming.csv', 'where': [['band', '==', r['band']]], 'col': 'mean'}, unit='°C', tol={'abs': 0.002}, skills=['table_lookup'])

    st = s['data/station_warming.csv']
    stu = st[~st.duplicated(['name'], keep=False)]
    for _, r in stu.loc[B.sample(stu.index, 20)].iterrows():
        B.add('lookup', f"In the {SH}, how much did GHCN-M station {r['station']} ({r['name']}) warm between the 1961-1990 and 1991-2020 normals, in °C?",
              {'op': 'lookup', 'file': 'data/station_warming.csv', 'where': [['station', '==', r['station']]], 'col': 'delta'}, unit='°C', tol={'abs': 0.002}, skills=['table_lookup', 'id_lookup'])
    fc = st.groupby('fips2').size()
    for f in B.sample(fc[fc >= 15].index, 10):
        B.add('count', f'In the {SH} station table, how many stations with FIPS country code {f} cooled (delta below 0)?',
              {'op': 'count', 'file': 'data/station_warming.csv', 'where': [['fips2', '==', f], ['delta', '<', 0]]}, difficulty=2, skills=['filter', 'count'])
        B.add('aggregate', f'In the {SH} station table, what is the mean warming (delta, °C) over all stations with FIPS country code {f}?',
              {'op': 'mean', 'file': 'data/station_warming.csv', 'where': [['fips2', '==', f]], 'col': 'delta'}, unit='°C', tol={'abs': 0.005}, difficulty=2, skills=['aggregation'])
    B.add('count', f'In the {SH}, how many weather stations got cooler (delta below 0) between the two normals?',
          {'op': 'count', 'file': 'data/station_warming.csv', 'where': [['delta', '<', 0]]}, difficulty=2, skills=['filter', 'count', 'full_scan'])

    ev = s['data/events.csv']
    eq = ev[ev.kind == 'sig_earthquake']
    ec = eq.groupby('country').size()
    for c in B.sample(ec[ec >= 10].index, 14):
        y = B.rng.choice([1900, 1950, 2000])
        B.add('count', f'In the {SH} events table, how many significant earthquakes are recorded for {c} from {y} onward?',
              {'op': 'count', 'file': 'data/events.csv', 'where': [['kind', '==', 'sig_earthquake'], ['country', '==', c], ['year', '>=', y]]}, difficulty=2, skills=['filter', 'count', 'temporal'])
        B.add('rank', f'In the {SH} events table, in which year did the deadliest significant earthquake recorded for {c} occur (by deaths)?',
              {'op': 'argmax', 'file': 'data/events.csv', 'where': [['kind', '==', 'sig_earthquake'], ['country', '==', c]], 'col': 'deaths', 'return': 'year'}, difficulty=3, skills=['ranking', 'group_filter'])

    je = s['data/joint_exposure.csv']
    B.add('count', f'In the {SH}, how many regions are in the top quarter for both M6 density and warming (joint_exposure)?', {'op': 'count', 'file': 'data/joint_exposure.csv'}, skills=['count'])
    B.add('rank', f'In the {SH}, which joint-exposure region (top quarter for both shaking and warming) has the largest population?',
          {'op': 'argmax', 'file': 'data/joint_exposure.csv', 'col': 'population', 'return': 'name'}, difficulty=2, skills=['ranking'])
    B.add('rank', f'In the {SH}, which joint-exposure region warmed the most?', {'op': 'argmax', 'file': 'data/joint_exposure.csv', 'col': 'warming', 'return': 'name'}, difficulty=2, skills=['ranking'])
    jc = je.groupby('country').size()
    for c in jc[jc >= 2].index:
        B.add('count', f'In the {SH}, how many joint-exposure regions are in {c}?', {'op': 'count', 'file': 'data/joint_exposure.csv', 'where': [['country', '==', c]]}, skills=['filter', 'count'])

    # multi-hop: Japanese name / ISO 3166-2 -> a1 -> quake / warming
    aq = a1[a1['a1'].isin(set(q['a1'])) & a1['name_ja'].notna() & ~a1.duplicated(['name_ja'], keep=False)]
    for _, r in aq.loc[B.sample(aq.index, 14)].iterrows():
        B.add('multi_hop', f"In the {SH}, the region whose Japanese name is 「{r['name_ja']}」 has how many M6+ earthquakes since 1900?",
              {'op': 'chain', 'steps': [
                  {'op': 'lookup', 'file': 'data/admin1.csv', 'where': [['name_ja', '==', r['name_ja']]], 'col': 'a1', 'as': 'a1'},
                  {'op': 'lookup', 'file': 'data/admin1_quake.csv', 'where': [['a1', '==', '$a1']], 'col': 'm6'}]},
              tol={'abs': 0}, difficulty=3, skills=['join', 'multilingual_entity'])
    aw = a1[a1['a1'].isin(set(w['a1'])) & a1['iso_3166_2'].notna() & ~a1.duplicated(['iso_3166_2'], keep=False) & ~a1['iso_3166_2'].astype(str).str.contains('~')]
    for _, r in aw.loc[B.sample(aw.index, 14)].iterrows():
        B.add('multi_hop', f"In the {SH}, what is the mean station warming (°C) of the region with ISO 3166-2 code {r['iso_3166_2']}?",
              {'op': 'chain', 'steps': [
                  {'op': 'lookup', 'file': 'data/admin1.csv', 'where': [['iso_3166_2', '==', r['iso_3166_2']]], 'col': 'a1', 'as': 'a1'},
                  {'op': 'lookup', 'file': 'data/admin1_warming.csv', 'where': [['a1', '==', '$a1']], 'col': 'mean'}]},
              unit='°C', tol={'abs': 0.002}, difficulty=3, skills=['join', 'code_resolution'])

    long_features(B, 'data/features_country.csv', n_lookup=30, n_rank=4, n_cmp=8, n_count=4, n_absent=8, ds_short=SH)