Skip to content

Commit 4ed1aaf

Browse files
committed
plots
1 parent b948235 commit 4ed1aaf

7 files changed

+345
-158
lines changed

04-models/notebooks/prediction-stats.ipynb

+199-45
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
},
6464
{
6565
"cell_type": "code",
66-
"execution_count": 12,
66+
"execution_count": 5,
6767
"metadata": {},
6868
"outputs": [],
6969
"source": [
@@ -75,7 +75,7 @@
7575
},
7676
{
7777
"cell_type": "code",
78-
"execution_count": 9,
78+
"execution_count": 6,
7979
"metadata": {},
8080
"outputs": [],
8181
"source": [
@@ -87,7 +87,7 @@
8787
},
8888
{
8989
"cell_type": "code",
90-
"execution_count": 10,
90+
"execution_count": 7,
9191
"metadata": {},
9292
"outputs": [],
9393
"source": [
@@ -98,38 +98,61 @@
9898
]
9999
},
100100
{
101-
"cell_type": "markdown",
101+
"cell_type": "code",
102+
"execution_count": 10,
102103
"metadata": {},
104+
"outputs": [
105+
{
106+
"name": "stdout",
107+
"output_type": "stream",
108+
"text": [
109+
"{'driver': 'GTiff', 'dtype': 'int16', 'nodata': -1.0, 'width': 20381, 'height': 22512, 'count': 1, 'crs': CRS.from_epsg(4326), 'transform': Affine(0.00026949458523585647, 0.0, -62.64186038139295,\n",
110+
" 0.0, -0.00026949458523585647, -19.287457970745013), 'tiled': False, 'interleave': 'band'}\n",
111+
"{'driver': 'GTiff', 'dtype': 'float32', 'nodata': -1.0, 'width': 20381, 'height': 22512, 'count': 1, 'crs': CRS.from_epsg(4326), 'transform': Affine(0.00026949458523585647, 0.0, -62.64186038139295,\n",
112+
" 0.0, -0.00026949458523585647, -19.287457970745013), 'tiled': False, 'interleave': 'band'}\n"
113+
]
114+
}
115+
],
103116
"source": [
104-
"# 25"
117+
"# Open the land use raster file\n",
118+
"with rasterio.open(sim25_lup) as src:\n",
119+
" land_use_array = src.read(1)\n",
120+
" p1 = src.profile\n",
121+
"\n",
122+
"# Open the deforestation probability raster file\n",
123+
"with rasterio.open(sim25_lup_prediction) as src:\n",
124+
" deforestation_probability_array = src.read(1)\n",
125+
" p2 = src.profile\n",
126+
"print(p1)\n",
127+
"print(p2)"
105128
]
106129
},
107130
{
108-
"cell_type": "code",
109-
"execution_count": null,
131+
"cell_type": "markdown",
110132
"metadata": {},
111-
"outputs": [],
112-
"source": []
133+
"source": [
134+
"# 25"
135+
]
113136
},
114137
{
115138
"cell_type": "code",
116-
"execution_count": 20,
139+
"execution_count": 11,
117140
"metadata": {},
118141
"outputs": [
119142
{
120143
"name": "stdout",
121144
"output_type": "stream",
122145
"text": [
123-
"Area of land use type 0: 0 square meters\n",
124-
"Area of land use type 1: 13140840600 square meters\n",
125-
"Area of land use type 2: 25707976200 square meters\n",
126-
"Area of land use type 3: 59036355000 square meters\n",
127-
"Area of land use type 4: 912870900 square meters\n",
146+
"Area of land use type 0: 0.0 square meters\n",
147+
"Area of land use type 1: 12236239734.029999 square meters\n",
148+
"Area of land use type 2: 23938267682.809998 square meters\n",
149+
"Area of land use type 3: 54972357917.75 square meters\n",
150+
"Area of land use type 4: 850029881.545 square meters\n",
128151
"Area of land use type 0: 0.0 hectares\n",
129-
"Area of land use type 1: 1314084.06 hectares\n",
130-
"Area of land use type 2: 2570797.62 hectares\n",
131-
"Area of land use type 3: 5903635.5 hectares\n",
132-
"Area of land use type 4: 91287.09 hectares\n"
152+
"Area of land use type 1: 1223623.9734029998 hectares\n",
153+
"Area of land use type 2: 2393826.7682809997 hectares\n",
154+
"Area of land use type 3: 5497235.791775 hectares\n",
155+
"Area of land use type 4: 85002.9881545 hectares\n"
133156
]
134157
}
135158
],
@@ -152,14 +175,14 @@
152175
"counts = np.bincount(land_use_array_flat)\n",
153176
"\n",
154177
"# Calculate the area of each land use type\n",
155-
"pixel_area = 900 # replace this with the actual area represented by each pixel\n",
178+
"pixel_area = 838.045 # 28.94785483442850804 m * 28.94785483442850804 m = 838.045 square meters EPSG:32721\n",
156179
"areas = counts * pixel_area\n",
157180
"\n",
158181
"# Print the area of each land use type\n",
159182
"for i, area in enumerate(areas):\n",
160183
" print(f\"Area of land use type {i}: {area} square meters\")\n",
161184
"# Calculate the deforested area for each pixel\n",
162-
"deforested_area_array = deforestation_probability_array * 900 # replace 900 with the actual area represented by each pixel\n",
185+
"deforested_area_array = deforestation_probability_array * 838.045 # replace 900 with the actual area represented by each pixel\n",
163186
"\n",
164187
"# Initialize arrays to hold the total deforested area, non-deforested area, and total area for each land use type\n",
165188
"deforested_areas = np.zeros(4) # number of land use types\n",
@@ -230,23 +253,23 @@
230253
},
231254
{
232255
"cell_type": "code",
233-
"execution_count": 21,
256+
"execution_count": 13,
234257
"metadata": {},
235258
"outputs": [
236259
{
237260
"name": "stdout",
238261
"output_type": "stream",
239262
"text": [
240-
"Area of land use type 0: 0 square meters\n",
241-
"Area of land use type 1: 8532334800 square meters\n",
242-
"Area of land use type 2: 50576823000 square meters\n",
243-
"Area of land use type 3: 38781358200 square meters\n",
244-
"Area of land use type 4: 912873600 square meters\n",
263+
"Area of land use type 0: 0.0 square meters\n",
264+
"Area of land use type 1: 7944978352.74 square meters\n",
265+
"Area of land use type 2: 47095170701.149994 square meters\n",
266+
"Area of land use type 3: 36111692591.909996 square meters\n",
267+
"Area of land use type 4: 850032395.68 square meters\n",
245268
"Area of land use type 0: 0.0 hectares\n",
246-
"Area of land use type 1: 853233.48 hectares\n",
247-
"Area of land use type 2: 5057682.3 hectares\n",
248-
"Area of land use type 3: 3878135.82 hectares\n",
249-
"Area of land use type 4: 91287.36 hectares\n"
269+
"Area of land use type 1: 794497.835274 hectares\n",
270+
"Area of land use type 2: 4709517.070114999 hectares\n",
271+
"Area of land use type 3: 3611169.2591909994 hectares\n",
272+
"Area of land use type 4: 85003.23956799999 hectares\n"
250273
]
251274
}
252275
],
@@ -269,14 +292,14 @@
269292
"counts = np.bincount(land_use_array_flat)\n",
270293
"\n",
271294
"# Calculate the area of each land use type\n",
272-
"pixel_area = 900 # replace this with the actual area represented by each pixel\n",
295+
"pixel_area = 838.045 # replace this with the actual area represented by each pixel\n",
273296
"areas = counts * pixel_area\n",
274297
"\n",
275298
"# Print the area of each land use type\n",
276299
"for i, area in enumerate(areas):\n",
277300
" print(f\"Area of land use type {i}: {area} square meters\")\n",
278301
"# Calculate the deforested area for each pixel\n",
279-
"deforested_area_array = deforestation_probability_array * 900 # replace 900 with the actual area represented by each pixel\n",
302+
"deforested_area_array = deforestation_probability_array * 838.045 # replace 900 with the actual area represented by each pixel\n",
280303
"\n",
281304
"# Initialize arrays to hold the total deforested area, non-deforested area, and total area for each land use type\n",
282305
"deforested_areas = np.zeros(4) # number of land use types\n",
@@ -338,25 +361,32 @@
338361
"df_hectares.to_csv(output_file_hectares, index=False)"
339362
]
340363
},
364+
{
365+
"cell_type": "markdown",
366+
"metadata": {},
367+
"source": [
368+
"# 5%"
369+
]
370+
},
341371
{
342372
"cell_type": "code",
343-
"execution_count": 22,
373+
"execution_count": 14,
344374
"metadata": {},
345375
"outputs": [
346376
{
347377
"name": "stdout",
348378
"output_type": "stream",
349379
"text": [
350-
"Area of land use type 0: 0 square meters\n",
351-
"Area of land use type 1: 17575208100 square meters\n",
352-
"Area of land use type 2: 5497432200 square meters\n",
353-
"Area of land use type 3: 74801823300 square meters\n",
354-
"Area of land use type 4: 912858300 square meters\n",
380+
"Area of land use type 0: 0.0 square meters\n",
381+
"Area of land use type 1: 16365350302.404999 square meters\n",
382+
"Area of land use type 2: 5118995075.61 square meters\n",
383+
"Area of land use type 3: 69652548897.165 square meters\n",
384+
"Area of land use type 4: 850018148.915 square meters\n",
355385
"Area of land use type 0: 0.0 hectares\n",
356-
"Area of land use type 1: 1757520.81 hectares\n",
357-
"Area of land use type 2: 549743.22 hectares\n",
358-
"Area of land use type 3: 7480182.33 hectares\n",
359-
"Area of land use type 4: 91285.83 hectares\n"
386+
"Area of land use type 1: 1636535.0302404999 hectares\n",
387+
"Area of land use type 2: 511899.50756099995 hectares\n",
388+
"Area of land use type 3: 6965254.8897164995 hectares\n",
389+
"Area of land use type 4: 85001.81489149999 hectares\n"
360390
]
361391
}
362392
],
@@ -379,14 +409,14 @@
379409
"counts = np.bincount(land_use_array_flat)\n",
380410
"\n",
381411
"# Calculate the area of each land use type\n",
382-
"pixel_area = 900 # replace this with the actual area represented by each pixel\n",
412+
"pixel_area = 838.045 # replace this with the actual area represented by each pixel\n",
383413
"areas = counts * pixel_area\n",
384414
"\n",
385415
"# Print the area of each land use type\n",
386416
"for i, area in enumerate(areas):\n",
387417
" print(f\"Area of land use type {i}: {area} square meters\")\n",
388418
"# Calculate the deforested area for each pixel\n",
389-
"deforested_area_array = deforestation_probability_array * 900 # replace 900 with the actual area represented by each pixel\n",
419+
"deforested_area_array = deforestation_probability_array * 838.045 # replace 900 with the actual area represented by each pixel\n",
390420
"\n",
391421
"# Initialize arrays to hold the total deforested area, non-deforested area, and total area for each land use type\n",
392422
"deforested_areas = np.zeros(4) # number of land use types\n",
@@ -447,6 +477,130 @@
447477
"# Export the DataFrame to a CSV file\n",
448478
"df_hectares.to_csv(output_file_hectares, index=False)"
449479
]
480+
},
481+
{
482+
"cell_type": "markdown",
483+
"metadata": {},
484+
"source": [
485+
"# Hedgerows"
486+
]
487+
},
488+
{
489+
"cell_type": "code",
490+
"execution_count": 15,
491+
"metadata": {},
492+
"outputs": [
493+
{
494+
"name": "stdout",
495+
"output_type": "stream",
496+
"text": [
497+
"Area of land use type 0: 0.0 square meters\n",
498+
"Area of land use type 1: 20277853893.765 square meters\n",
499+
"Area of land use type 2: 2886681200.39 square meters\n",
500+
"Area of land use type 3: 67989778784.395 square meters\n",
501+
"Area of land use type 4: 850034071.77 square meters\n",
502+
"Area of land use type 0: 0.0 hectares\n",
503+
"Area of land use type 1: 2027785.3893765 hectares\n",
504+
"Area of land use type 2: 288668.120039 hectares\n",
505+
"Area of land use type 3: 6798977.8784395 hectares\n",
506+
"Area of land use type 4: 85003.407177 hectares\n"
507+
]
508+
}
509+
],
510+
"source": [
511+
"# Open the land use raster file\n",
512+
"with rasterio.open(simhedges_lup) as src:\n",
513+
" land_use_array = src.read(1)\n",
514+
"\n",
515+
"# Open the deforestation probability raster file\n",
516+
"with rasterio.open(simhedges_lup_prediction) as src:\n",
517+
" deforestation_probability_array = src.read(1)\n",
518+
"\n",
519+
"# Mask the NoData values\n",
520+
"'''This line is creating a masked array from the land_use_array. \n",
521+
"A masked array is a numpy array that has a separate Boolean mask that \n",
522+
"indicates whether each value in the array is valid or not. \n",
523+
"In this case, the masked_less function is used to create a mask \n",
524+
"that is True for all values in the array that are less than 0, \n",
525+
"and False for all other values. This effectively ignores all values less than 0 in the \n",
526+
"subsequent calculations.'''\n",
527+
"land_use_array_masked = np.ma.masked_less(land_use_array, 0)\n",
528+
"\n",
529+
"# Flatten the array\n",
530+
"land_use_array_flat = land_use_array_masked.compressed()\n",
531+
"\n",
532+
"# Count the number of pixels of each land use type\n",
533+
"counts = np.bincount(land_use_array_flat)\n",
534+
"\n",
535+
"# Calculate the area of each land use type\n",
536+
"pixel_area = 838.045 # replace this with the actual area represented by each pixel\n",
537+
"areas = counts * pixel_area\n",
538+
"\n",
539+
"# Print the area of each land use type\n",
540+
"for i, area in enumerate(areas):\n",
541+
" print(f\"Area of land use type {i}: {area} square meters\")\n",
542+
"# Calculate the deforested area for each pixel\n",
543+
"deforested_area_array = deforestation_probability_array * 838.045 # replace 900 with the actual area represented by each pixel\n",
544+
"\n",
545+
"# Initialize arrays to hold the total deforested area, non-deforested area, and total area for each land use type\n",
546+
"deforested_areas = np.zeros(4) # number of land use types\n",
547+
"non_deforested_areas = np.zeros(4) # number of land use types\n",
548+
"total_areas = np.zeros(4) # number of land use types\n",
549+
"\n",
550+
"# Loop over the land use types\n",
551+
"for i in range(4): # number of land use types\n",
552+
" # Select the deforested area for the pixels of the current land use type\n",
553+
" deforested_area = deforested_area_array[land_use_array == i+1]\n",
554+
" \n",
555+
" # Sum the deforested area\n",
556+
" deforested_areas[i] = deforested_area.sum()\n",
557+
" \n",
558+
" # Calculate the total area\n",
559+
" total_areas[i] = (land_use_array == i+1).sum() * pixel_area\n",
560+
"\n",
561+
" # Calculate the non-deforested area\n",
562+
" non_deforested_areas[i] = total_areas[i] - deforested_areas[i]\n",
563+
" # Calculate the area of each land use type in hectares\n",
564+
"areas_hectares = areas / 10000 # 1 hectare = 10,000 square meters\n",
565+
"\n",
566+
"# Print the area of each land use type in hectares\n",
567+
"for i, area in enumerate(areas_hectares):\n",
568+
" print(f\"Area of land use type {i}: {area} hectares\")\n",
569+
"\n",
570+
"# Calculate the deforested area for each pixel in hectares\n",
571+
"deforested_area_array_hectares = deforestation_probability_array * pixel_area / 10000\n",
572+
"\n",
573+
"# Initialize arrays to hold the total deforested area, non-deforested area, and total area for each land use type in hectares\n",
574+
"deforested_areas_hectares = np.zeros(4) # number of land use types\n",
575+
"non_deforested_areas_hectares = np.zeros(4) # number of land use types\n",
576+
"total_areas_hectares = np.zeros(4) # number of land use types\n",
577+
"\n",
578+
"# Loop over the land use types\n",
579+
"for i in range(4): # number of land use types\n",
580+
" # Select the deforested area for the pixels of the current land use type\n",
581+
" deforested_area_hectares = deforested_area_array_hectares[land_use_array == i+1]\n",
582+
" \n",
583+
" # Sum the deforested area\n",
584+
" deforested_areas_hectares[i] = deforested_area_hectares.sum()\n",
585+
" \n",
586+
" # Calculate the total area in hectares\n",
587+
" total_areas_hectares[i] = (land_use_array == i+1).sum() * pixel_area / 10000\n",
588+
"\n",
589+
" # Calculate the non-deforested area in hectares\n",
590+
" non_deforested_areas_hectares[i] = total_areas_hectares[i] - deforested_areas_hectares[i]\n",
591+
" \n",
592+
"# Create a DataFrame with the deforested area of each land use type in hectares\n",
593+
"df_hectares = pd.DataFrame({\n",
594+
" 'LandUseType': ['Hedgerow', 'Forest Reserve', 'Paddocks', 'Riparian Corridor'],\n",
595+
" 'DeforestedArea': deforested_areas_hectares,\n",
596+
" 'NonDeforestedArea': non_deforested_areas_hectares,\n",
597+
" 'TotalArea': total_areas_hectares\n",
598+
"})\n",
599+
"\n",
600+
"output_file_hectares = os.path.join(output_folder, \"sim-hedges-log-lut-area-hectares.csv\")\n",
601+
"# Export the DataFrame to a CSV file\n",
602+
"df_hectares.to_csv(output_file_hectares, index=False)"
603+
]
450604
}
451605
],
452606
"metadata": {

0 commit comments

Comments
 (0)