File size: 11,173 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
from common import long_features

DS = 'Lilambd/world-earth'
REVISION = 'hf-main@2026-11-03'
LICENSE = 'CC BY 4.0'
SH = 'World Earth Surface Ledger'
A1 = ['a1']
STR = {f'data/{f}.csv': ['a1'] for f in ['admin1', 'forest_base', 'forest_loss_year', 'lc_group', 'lc_transition', 'ghsl', 'soil']}
STR['data/admin1.csv'] = ['a1', 'iso_3166_2', 'hasc']
KEYS = {
    'data/admin1.csv': ['a1', 'name', 'country'],
    'data/forest_base.csv': ['a1', 'thr'],
    'data/forest_loss_year.csv': ['a1', 'thr', 'year'],
    'data/lc_group.csv': ['a1', 'year', 'grp'],
    'data/lc_transition.csv': ['a1', 'g2000', 'g2020'],
    'data/ghsl.csv': ['a1', 'year'],
    'data/soil.csv': ['a1', 'prop', 'depth'],
    'data/features_country.csv': ['iso3', 'feature'],
}
GRP = {'tree': 'tree cover', 'cropland': 'cropland', 'built': 'built-up land', 'water': 'surface water', 'desert': 'desert', 'semi_arid': 'semi-arid land',
       'dense_short_veg': 'dense short vegetation', 'wet_tree': 'wetland trees', 'snow_ice': 'snow and ice', 'wet_short_veg': 'wet short vegetation', 'wet_sparse': 'wet sparse vegetation'}
SOIL = {'phh2o': ('soil pH (in water)', 'pH'), 'clay': ('clay content', '%'), 'sand': ('sand content', '%'), 'soc': ('soil organic carbon', 'g/kg'), 'nitrogen': ('soil nitrogen', 'g/kg'), 'bdod': ('bulk density', 'kg/dm3')}


def build(B):
    s = B.store
    ad = s['data/admin1.csv']
    ad = ad[~ad.duplicated(['name', 'country'], keep=False) & ad['name'].notna()]
    lab = {r.a1: f'{r.name} ({r.country})' for r in ad.itertuples()}
    country_of = {r.a1: r.country for r in ad.itertuples()}
    fb = s['data/forest_base.csv']
    fb30 = fb[(fb.thr == 30) & fb.a1.isin(lab)]
    for r in fb30.loc[B.sample(fb30[fb30.tc2000_km2 > 50].index, 30)].itertuples():
        c = B.rng.choice(['loss_km2', 'tc2000_km2'])
        t = (f'In the {SH}, how many km² of forest (canopy ≥ 30%) did {lab[r.a1]} lose in total from 2001 to 2025?' if c == 'loss_km2'
             else f'In the {SH}, how many km² of tree cover (canopy ≥ 30%) did {lab[r.a1]} have in 2000?')
        B.add('lookup', t, {'op': 'lookup', 'file': 'data/forest_base.csv', 'where': [['a1', '==', r.a1], ['thr', '==', 30]], 'col': c}, unit='km²', skills=['table_lookup'])
    for r in fb30.loc[B.sample(fb30[fb30.tc2000_km2 > 500].index, 15)].itertuples():
        B.add('derived', f'In the {SH}, what share of its year-2000 tree cover (canopy ≥ 30%) did {lab[r.a1]} lose from 2001 to 2025? Give a fraction between 0 and 1.',
              {'op': 'ratio', 'parts': [
                  {'op': 'lookup', 'file': 'data/forest_base.csv', 'where': [['a1', '==', r.a1], ['thr', '==', 30]], 'col': 'loss_km2'},
                  {'op': 'lookup', 'file': 'data/forest_base.csv', 'where': [['a1', '==', r.a1], ['thr', '==', 30]], 'col': 'tc2000_km2'}]},
              tol={'rel': 0.02}, difficulty=2, skills=['arithmetic', 'table_lookup'])
    fb30c = fb30.assign(country=fb30.a1.map(country_of))
    cnt = fb30c.groupby('country').size()
    for c in B.sample(cnt[cnt >= 5].index, 20):
        ids = sorted(fb30c[fb30c.country == c].a1)
        B.add('rank', f'In the {SH}, which admin-1 region of {c} lost the most forest (canopy ≥ 30%, km², 2001-2025)? Answer with its a1 id.',
              {'op': 'argmax', 'file': 'data/forest_base.csv', 'where': [['a1', 'in', ids], ['thr', '==', 30]], 'col': 'loss_km2', 'return': 'a1'},
              difficulty=3, skills=['ranking', 'join', 'group_filter'], note='Region list for the country comes from data/admin1.csv.')
    B.add('rank', f'In the {SH}, list the 5 admin-1 ids with the largest total forest loss at canopy threshold 30%, largest first (exclude the unassigned id _none).',
          {'op': 'topk', 'k': 5, 'file': 'data/forest_base.csv', 'where': [['thr', '==', 30], ['a1', '!=', '_none']], 'col': 'loss_km2', 'return': 'a1'}, difficulty=3, skills=['ranking', 'full_scan'], answer_format='JSON list of a1 ids')
    for _ in range(14):
        a, b = B.sample(list(fb30.a1), 2)
        B.add('compare', f'In the {SH}, which region lost more forest area (canopy ≥ 30%, 2001-2025): {lab[a]} or {lab[b]}? Answer with the region name as given.',
              {'op': 'compare', 'labels': [lab[a], lab[b]], 'parts': [
                  {'op': 'lookup', 'file': 'data/forest_base.csv', 'where': [['a1', '==', a], ['thr', '==', 30]], 'col': 'loss_km2'},
                  {'op': 'lookup', 'file': 'data/forest_base.csv', 'where': [['a1', '==', b], ['thr', '==', 30]], 'col': 'loss_km2'}]}, difficulty=2, skills=['comparison'])
    fly = s['data/forest_loss_year.csv']
    big = list(fb30[fb30.loss_km2 > 200].a1)
    for a in B.sample(big, 22):
        y = B.rng.randint(2001, 2025)
        B.add('lookup', f'In the {SH}, how many km² of forest (canopy ≥ 30%) did {lab[a]} lose in {y}?',
              {'op': 'lookup', 'file': 'data/forest_loss_year.csv', 'where': [['a1', '==', a], ['thr', '==', 30], ['year', '==', y]], 'col': 'loss_km2'}, unit='km²', skills=['table_lookup', 'temporal'])
    for a in B.sample(big, 15):
        B.add('rank', f'In the {SH}, in which year between 2001 and 2025 did {lab[a]} lose the most forest (canopy ≥ 30%)?',
              {'op': 'argmax', 'file': 'data/forest_loss_year.csv', 'where': [['a1', '==', a], ['thr', '==', 30]], 'col': 'loss_km2', 'return': 'year'}, difficulty=2, skills=['ranking', 'temporal'])
    for a in B.sample(big, 10):
        B.add('aggregate', f'In the {SH}, how many km² of forest (canopy ≥ 30%) did {lab[a]} lose in 2016 to 2020 combined?',
              {'op': 'sum', 'file': 'data/forest_loss_year.csv', 'where': [['a1', '==', a], ['thr', '==', 30], ['year', '>=', 2016], ['year', '<=', 2020]], 'col': 'loss_km2'}, unit='km²', difficulty=2, skills=['aggregation', 'temporal'])
    for a in B.sample(big, 10):
        y = B.rng.choice([2027, 2028, 2030])
        B.add('unanswerable', f'In the {SH}, how many km² of forest did {lab[a]} lose in {y}? If the ledger has no such row, answer null.',
              {'op': 'absent', 'file': 'data/forest_loss_year.csv', 'where': [['a1', '==', a], ['year', '==', y]]}, difficulty=1, skills=['abstention', 'temporal'],
              note='Loss years run 2001-2025; a grounded system must not extrapolate.')

    lg = s['data/lc_group.csv']
    lgr = lg[lg.a1.isin(lab)]
    pairs = lgr[['a1', 'grp']].drop_duplicates()
    pairs = pairs[pairs.grp.isin(GRP)]
    for r in pairs.loc[B.sample(pairs.index, 25)].itertuples():
        y = B.rng.choice([2000, 2005, 2010, 2015, 2020])
        B.add('lookup', f'In the {SH}, how many km² of {GRP[r.grp]} (GLCLU group {r.grp}) did {lab[r.a1]} have in {y}?',
              {'op': 'lookup', 'file': 'data/lc_group.csv', 'where': [['a1', '==', r.a1], ['year', '==', y], ['grp', '==', r.grp]], 'col': 'km2'}, unit='km²', skills=['table_lookup'])
    for r in pairs[pairs.grp.isin(['cropland', 'built', 'tree'])].pipe(lambda d: d.loc[B.sample(d.index, 18)]).itertuples():
        B.add('temporal', f'In the {SH}, by how many km² did {GRP[r.grp]} in {lab[r.a1]} change from 2000 to 2020 (2020 minus 2000)?',
              {'op': 'diff', 'parts': [
                  {'op': 'lookup', 'file': 'data/lc_group.csv', 'where': [['a1', '==', r.a1], ['year', '==', 2020], ['grp', '==', r.grp]], 'col': 'km2'},
                  {'op': 'lookup', 'file': 'data/lc_group.csv', 'where': [['a1', '==', r.a1], ['year', '==', 2000], ['grp', '==', r.grp]], 'col': 'km2'}]},
              unit='km²', tol={'rel': 0.02}, difficulty=2, skills=['arithmetic', 'temporal'])
    lt = s['data/lc_transition.csv']
    ltr = lt[lt.a1.isin(lab) & (lt.g2000 != lt.g2020) & lt.g2000.isin(GRP) & lt.g2020.isin(GRP) & (lt.km2 > 5)]
    for r in ltr.loc[B.sample(ltr.index, 15)].itertuples():
        B.add('lookup', f'In the {SH}, how many km² of land in {lab[r.a1]} went from {GRP[r.g2000]} in 2000 to {GRP[r.g2020]} in 2020?',
              {'op': 'lookup', 'file': 'data/lc_transition.csv', 'where': [['a1', '==', r.a1], ['g2000', '==', r.g2000], ['g2020', '==', r.g2020]], 'col': 'km2'}, unit='km²', skills=['table_lookup'])
    gh = s['data/ghsl.csv']
    ghr = gh[gh.a1.isin(lab)]
    for a in B.sample(sorted(set(ghr.a1)), 20):
        y = B.rng.choice([1975, 1990, 2000, 2020])
        col = B.rng.choice(['built_km2', 'pop'])
        t = (f'In the {SH} (GHSL), how many km² were built up in {lab[a]} in {y}?' if col == 'built_km2' else f'In the {SH} (GHSL), what was the population of {lab[a]} in {y}?')
        B.add('lookup', t, {'op': 'lookup', 'file': 'data/ghsl.csv', 'where': [['a1', '==', a], ['year', '==', y]], 'col': col}, unit='km²' if col == 'built_km2' else 'persons', skills=['table_lookup', 'temporal'])
    for a in B.sample(sorted(set(ghr.a1)), 10):
        B.add('derived', f'In the {SH} (GHSL), by what factor did the built-up area of {lab[a]} grow from 1975 to 2020 (built 2020 divided by built 1975)?',
              {'op': 'ratio', 'parts': [
                  {'op': 'lookup', 'file': 'data/ghsl.csv', 'where': [['a1', '==', a], ['year', '==', 2020]], 'col': 'built_km2'},
                  {'op': 'lookup', 'file': 'data/ghsl.csv', 'where': [['a1', '==', a], ['year', '==', 1975]], 'col': 'built_km2'}]}, tol={'rel': 0.02}, difficulty=2, skills=['arithmetic', 'temporal'])
    so = s['data/soil.csv']
    sor = so[so.a1.isin(lab) & so.prop.isin(SOIL) & (so.depth == '0-5cm')]
    for r in sor.loc[B.sample(sor.index, 20)].itertuples():
        nm, u = SOIL[r.prop]
        B.add('lookup', f'In the {SH} (SoilGrids), what is the mean {nm} at 0-5 cm depth in {lab[r.a1]}, in {u}?',
              {'op': 'lookup', 'file': 'data/soil.csv', 'where': [['a1', '==', r.a1], ['prop', '==', r.prop], ['depth', '==', '0-5cm']], 'col': 'mean'}, unit=u, tol={'rel': 0.01}, skills=['table_lookup', 'unit_awareness'])
    # multi-hop via Japanese name
    adj = s['data/admin1.csv']
    adj = adj[adj.name_ja.notna() & ~adj.duplicated(['name_ja'], keep=False) & adj.a1.isin(set(fb30.a1))]
    for r in adj.loc[B.sample(adj.index, 12)].itertuples():
        B.add('multi_hop', f'In the {SH}, how many km² of forest (canopy ≥ 30%) did the region whose Japanese name is 「{r.name_ja}」 lose from 2001 to 2025?',
              {'op': 'chain', 'steps': [
                  {'op': 'lookup', 'file': 'data/admin1.csv', 'where': [['name_ja', '==', r.name_ja]], 'col': 'a1', 'as': 'a1'},
                  {'op': 'lookup', 'file': 'data/forest_base.csv', 'where': [['a1', '==', '$a1'], ['thr', '==', 30]], 'col': 'loss_km2'}]}, unit='km²', difficulty=3, skills=['join', 'multilingual_entity'])
    for r in B.store['data/admin1.csv'].pipe(lambda d: d[d.a1.isin(lab) & d.area_km2.notna()]).pipe(lambda d: d.loc[B.sample(d.index, 10)]).itertuples():
        B.add('lookup', f'In the {SH} region list, what is the area of {lab[r.a1]} in km²?', {'op': 'lookup', 'file': 'data/admin1.csv', 'where': [['a1', '==', r.a1]], 'col': 'area_km2'}, unit='km²', skills=['table_lookup'])
    long_features(B, 'data/features_country.csv', n_lookup=32, n_rank=5, n_cmp=10, n_count=5, n_absent=8, ds_short=SH)