<jupyter_start><jupyter_text>Cropped area  Total for AEZ<jupyter_code>cropmask=xr.open_rasterio('../testing/eastern_cropmask/results/classifications/20210427/Eastern_gm_mads_two_seasons_20210427_mosaic_clipped_WDPA_masked.tif',
                         chunks={'x':10000, 'y':10000})

cropmask = cropmask.persist()
pixel_length = 20  # in metres
m_per_km = 1000  # conversion from metres to kilometres
area_per_pixel = pixel_length**2 / m_per_km**2
total_area = cropmask.sum(dim=['x', 'y']) * area_per_pixel
total_area = total_area.compute()
print(round(float(total_area.values),2), ' km2')
print(round(float(total_area.values*100/1e6),2), ' Mha')<jupyter_output><empty_output><jupyter_text>Area per country<jupyter_code>afr = gpd.read_file('data/african_countries.shp')
afr = afr.to_crs('epsg:6933')
e = afr[afr['COUNTRY'] == 'Ethiopia']
t = afr[afr['COUNTRY'] == 'Tanzania']
k = afr[afr['COUNTRY'] == 'Kenya']
u = afr[afr['COUNTRY'] == 'Uganda']
r = afr[afr['COUNTRY'] == 'Rwanda']
b = afr[afr['COUNTRY'] == 'Burundi']

countries = [e,t,k,u,r,b]
areas={}
for country in countries:
    #make sure we have 1 polygon per country
    country['agg'] = 1     
    c=country.dissolve(by='agg',  aggfunc='sum')
    print(c['COUNTRY'].values[0][0:3])
    
    #create mask
    with HiddenPrints():
        mask = xr_rasterize(c,cropmask)
    mask = mask.chunk({'x':10000, 'y':10000})
    ds = cropmask.where(mask).astype('float32')
    
    area = ds.sum(dim=['x', 'y']) * area_per_pixel
    area = area.compute()
    print(area.values)
    
    areas[c['COUNTRY'].values[0][0:3]] = area.values
    
    del ds
for key,value in areas.items():
    print(key, round(value[0]*100/1e6,2), ' Mha')<jupyter_output>Eth 22.54  Mha
Tan 20.49  Mha
Ken 7.4  Mha
Uga 9.14  Mha
Rwa 1.1  Mha
Bur 1.2  Mha<jupyter_text>Plotting all reference data  Validation<jupyter_code>c=gpd.read_file('../central_cropmask/data/validation_samples.shp')
e=gpd.read_file('../eastern_cropmask/data/validation_samples.shp')
i=gpd.read_file('../indian_ocean_cropmask/data/validation_samples.shp')
n=gpd.read_file('../northern_cropmask/data/validation_samples.shp')
sa=gpd.read_file('../sahel_cropmask/data/validation_samples.shp')
so=gpd.read_file('../southern_cropmask/data/validation_samples.shp')
w=gpd.read_file('../western_cropmask/data/validation_samples.shp')

val = pd.concat([c,e,i,n,sa,so,w]).reset_index(drop=True)
print('non-crop length: '+str(len(val[val['Class']=='non-crop'])))
print('crop length: '+str(len(val[val['Class']=='crop'])))
val.plot(column='Class', figsize=(12,9), cmap='BrBG_r')
val.to_file('crop_training_africa_20210329.shp')<jupyter_output><empty_output><jupyter_text>Training<jupyter_code>c=gpd.read_file('../central_cropmask/data/ceo_td_polys.geojson')
e=gpd.read_file('../eastern_cropmask/data/Eastern_training_data_20210301.geojson')
i=gpd.read_file('../indian_ocean_cropmask/data/Indian_ocean_training_data_20210115.geojson')
n=gpd.read_file('../northern_cropmask/data/ceo_td_polys.geojson')
sa=gpd.read_file('../sahel_cropmask/data/ceo_td_polys.geojson')
so=gpd.read_file('../southern_cropmask/data/ceo_td_polys.geojson')
w=gpd.read_file('../western_cropmask/data/ceo_td_polys.geojson')

td = pd.concat([c,e,i,n,sa,so,w]).reset_index(drop=True)
td = td.to_crs('epsg:6933')
td['geometry'] = td['geometry'].centroid
td = td.to_crs('epsg:4326')
td['Class'] = np.where(td['Class']=='1', 'crop', td['Class'])
td['Class'] = np.where(td['Class']=='0', 'non-crop', td['Class'])

td['Class'] = np.where(td['Class']==1, 'crop', td['Class'])
td['Class'] = np.where(td['Class']==0, 'non-crop', td['Class'])
print('non-crop length: '+str(len(td[td['Class']=='non-crop'])))
print('crop length: '+str(len(td[td['Class']=='crop'])))
td['Class'].unique()
td.plot(column='Class', figsize=(12,9), cmap='BrBG_r', markersize =0.9)
td.to_file('crop_validation_africa_20210329.shp')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19994853319608852, 'fraction_numerical': 0.03654143077714874, 'mean_word_length': 6.4179389312977095, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '596352', 'license': 'Apache-2.0', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1632, 'n_words': 187, 'path': 'pre-post_processing/plots_and_figs.ipynb', 'repo_name': 'digitalearthafrica/crop-mask', 'size': '3886'}