Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save d3netxer/bbef11fde85b865d2acfad62114738f2 to your computer and use it in GitHub Desktop.
Save d3netxer/bbef11fde85b865d2acfad62114738f2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "outstanding-vinyl",
"metadata": {},
"source": [
"# An urban growth model to apply to a single city\n",
"### ideas for further improvement:\n",
"- more growth should occur near areas of more recent growth\n",
"- look into kernels higher than 6 (check)\n",
"- add more datasets such as slope and distance to central business district\n",
"- use other datasets such as roads and parks within urban areas to avoid converting to urban (and reallocate growth elsewhere)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "overhead-genre",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "laden-output",
"metadata": {},
"outputs": [],
"source": [
"#Importing all necessary libraries\n",
"import os, sys, math\n",
"import numpy\n",
"import numpy as np\n",
"import gdal\n",
"from copy import deepcopy\n",
"import pandas as pd\n",
"import geopandas as gpd"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "aggressive-today",
"metadata": {},
"outputs": [],
"source": [
"import rasterio\n",
"from shapely.geometry import box\n",
"from rasterio.mask import mask\n",
"from rasterio.warp import reproject, Resampling, calculate_default_transform"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "lightweight-mercy",
"metadata": {},
"outputs": [],
"source": [
"from shapely.geometry import Point\n",
"import matplotlib.pyplot as plt"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "structured-prediction",
"metadata": {},
"outputs": [],
"source": [
"# Get reference to INFRA_SAP\n",
"sys.path.append(r'C:\\repos\\INFRA_SAP')\n",
"from infrasap.urban_metrics import *"
]
},
{
"cell_type": "markdown",
"id": "preceding-highlight",
"metadata": {},
"source": [
"# Standardize landcover raster to WSF raster\n",
"## Important ESA landcover CCI classes:\n",
"- 10: cropland rainfed\n",
"- 20: cropland irrigated or post-flooding\n",
"- 210: water bodies"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "chinese-chassis",
"metadata": {},
"outputs": [],
"source": [
"# from gostrocks\n",
"def standardizeInputRasters(inR1, inR2, inR1_outFile='', data_type=\"N\"):\n",
" ''' Standardize inR1 to inR2: changes crs, extent, and resolution.\n",
" Inputs:\n",
" inR1, inR2 [rasterio raster object]\n",
" [optional] inR1_outFile [string] - output file for creating inR1 standardized to inR2\n",
" [optional] data_type [string ['C','N']]\n",
" \n",
" Returns:\n",
" [list] - [numpy array, raster metadata]\n",
" '''\n",
" \n",
" if inR1.crs != inR2.crs:\n",
" bounds = gpd.GeoDataFrame(pd.DataFrame([[1, box(*inR2.bounds)]], columns=[\"ID\",\"geometry\"]), geometry='geometry', crs=inR2.crs)\n",
" bounds = bounds.to_crs(inR1.crs)\n",
" b2 = bounds.total_bounds\n",
" boxJSON = [{'type': 'Polygon', 'coordinates': [[[b2[0], b2[1]],[b2[0], b2[3]],[b2[2], b2[3]],[b2[2], b2[1]],[b2[0], b2[1]]]]}]\n",
" else:\n",
" b2 = inR2.bounds\n",
" boxJSON = [{'type': 'Polygon', 'coordinates': [[[b2.left, b2.bottom],[b2.left, b2.top],[b2.right, b2.top],[b2.right, b2.bottom],[b2.left, b2.bottom]]]}]\n",
" \n",
" #Clip R1 to R2\n",
" #Get JSON of bounding box\n",
" out_img, out_transform = mask(inR1, boxJSON, crop=True)\n",
" out_meta = inR1.meta.copy()\n",
" \n",
" #Re-scale resolution of R1 to R2\n",
" newArr = np.empty(shape=(1, inR2.shape[0], inR2.shape[1]))\n",
" \n",
" if data_type == \"N\":\n",
" resampling_type = Resampling.nearest\n",
" elif data_type == \"C\":\n",
" resampling_type = Resampling.cubic\n",
" \n",
" reproject(out_img, newArr, src_transform=out_transform, dst_transform=inR2.transform, src_crs=inR1.crs, dst_crs=inR2.crs, resampling=resampling_type)\n",
" \n",
" out_meta.update({\"driver\": \"GTiff\",\n",
" \"height\": newArr.shape[1],\n",
" \"width\": newArr.shape[2],\n",
" \"transform\": inR2.transform,\n",
" \"crs\": inR2.crs})\n",
" \n",
" if inR1_outFile != \"\":\n",
" with rasterio.open(inR1_outFile, \"w\", **out_meta) as dest:\n",
" dest.write(newArr.astype(out_meta['dtype']))\n",
" \n",
" return([newArr.astype(out_meta['dtype']), out_meta])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "promotional-planning",
"metadata": {},
"outputs": [],
"source": [
"landcover_cci = r\"C:\\Users\\war-machine\\Documents\\world_bank_work\\cityscan\\global_data_sets\\03_landcover\\C3S-LC-L4-LCCS-Map-300m-P1Y-2019-v2_1_1.tif\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "deadly-refund",
"metadata": {},
"outputs": [],
"source": [
"inR1 = rasterio.open(landcover_cci)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "behavioral-outdoors",
"metadata": {},
"outputs": [],
"source": [
"qarshi_wsf = r\"C:\\repos\\urban_growth_pca\\data\\qarshi_wsf_evolution_41N_2.tif\" "
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "vocational-charleston",
"metadata": {},
"outputs": [],
"source": [
"inR2 = rasterio.open(qarshi_wsf)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "funky-frost",
"metadata": {},
"outputs": [],
"source": [
"standardized_cci_raster = standardizeInputRasters(inR1, inR2, inR1_outFile='standardized_landcover_cci_nearest.tif', data_type=\"N\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "twelve-delhi",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[array([[[ 20, 20, 20, ..., 200, 200, 200],\n",
" [ 20, 20, 20, ..., 200, 200, 200],\n",
" [ 20, 20, 20, ..., 200, 200, 200],\n",
" ...,\n",
" [ 0, 0, 0, ..., 0, 0, 0],\n",
" [ 0, 0, 0, ..., 0, 0, 0],\n",
" [ 0, 0, 0, ..., 0, 0, 0]]], dtype=uint8),\n",
" {'driver': 'GTiff',\n",
" 'dtype': 'uint8',\n",
" 'nodata': 0.0,\n",
" 'width': 2083,\n",
" 'height': 2074,\n",
" 'count': 1,\n",
" 'crs': CRS.from_epsg(32641),\n",
" 'transform': Affine(24.12865650504081, 0.0, 716783.0752,\n",
" 0.0, -24.125418563162786, 4320929.3347)}]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"standardized_cci_raster"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "challenging-desperate",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 20, 20, 20, ..., 200, 200, 200],\n",
" [ 20, 20, 20, ..., 200, 200, 200],\n",
" [ 20, 20, 20, ..., 200, 200, 200],\n",
" ...,\n",
" [ 0, 0, 0, ..., 0, 0, 0],\n",
" [ 0, 0, 0, ..., 0, 0, 0],\n",
" [ 0, 0, 0, ..., 0, 0, 0]]], dtype=uint8)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"standardized_cci_raster[0]"
]
},
{
"cell_type": "markdown",
"id": "measured-hardwood",
"metadata": {},
"source": [
"# Define Objects and Functions"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "electronic-breathing",
"metadata": {},
"outputs": [],
"source": [
"#Defining function to read raster file and return array and datasource\n",
"def readraster(file):\n",
" dataSource = gdal.Open(file)\n",
" band = dataSource.GetRasterBand(1)\n",
" band = band.ReadAsArray()\n",
" return(dataSource, band)\n",
"\n",
"def identicalList(inList):\n",
" global logical\n",
" inList = np.array(inList)\n",
" logical = inList==inList[0]\n",
" if sum(logical) == len(inList):\n",
" return(True)\n",
" else:\n",
" return(False)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "adapted-antigua",
"metadata": {},
"outputs": [],
"source": [
"class growthFactors():\n",
"\n",
" def __init__(self, *args):\n",
" self.gf = dict()\n",
" self.gf_ds = dict()\n",
" self.nFactors = len(args)\n",
" n = 0\n",
" for file in args:\n",
" self.gf_ds[n], self.gf[n] = readraster(file)\n",
" n += 1\n",
" self.performChecks()\n",
"\n",
" def performChecks(self):\n",
" print(\"\\nChecking the size of input growth factors...\")\n",
" rows = []\n",
" cols = []\n",
" for n in range(0, self.nFactors):\n",
" rows.append(self.gf_ds[n].RasterYSize)\n",
" cols.append(self.gf_ds[n].RasterXSize)\n",
" if (identicalList(rows) == True) and ((identicalList(cols) == True)):\n",
" print(\"Input factors have same row and column value.\")\n",
" self.row = self.gf_ds[n].RasterYSize\n",
" self.col = self.gf_ds[n].RasterXSize\n",
" else:\n",
" print(\"Input factors have different row and column value.\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "satisfactory-native",
"metadata": {},
"outputs": [],
"source": [
"def randbin(M,N,P): \n",
" return np.random.choice([1, 0], size=(M,N), p=[P, 1-P])[0][0] "
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "starting-suite",
"metadata": {},
"outputs": [],
"source": [
"class fitmodel():\n",
"\n",
" def __init__(self, file1, growthfactorsClass):\n",
" if type(file1) == numpy.ndarray:\n",
" self.arr_wsf = file1\n",
" self.ds_wsf_row, self.ds_wsf_col = file1.shape[0], file1.shape[1]\n",
" else:\n",
" self.ds_wsf, self.arr_wsf = readraster(file1)\n",
" self.ds_wsf_row, self.ds_wsf_col = (self.ds_wsf.RasterYSize, self.ds_wsf.RasterXSize)\n",
" \n",
" self.factors = growthfactorsClass\n",
" self.performChecks()\n",
" self.kernelSize = 3\n",
"\n",
" def performChecks(self):\n",
" print(\"\\nMatching the size of land cover and growth factors...\")\n",
" print(f'ds_wsf row: {self.ds_wsf_row}')\n",
" print(f'ds_wsf col: {self.ds_wsf_col}')\n",
" print(f'self_factors row: {self.factors.row}')\n",
" print(f'self_factors col: {self.factors.col}')\n",
" \n",
" if (self.ds_wsf_row == self.factors.row) and (self.ds_wsf_col == self.factors.col):\n",
" print(\"Size of rasters matched.\")\n",
" self.row = self.factors.row\n",
" self.col = self.factors.col\n",
" else:\n",
" print(\"ERROR! Raster size not matched please check.\")\n",
" \n",
" # thresholds set in example \n",
" # 3, -15000, -10000, 8000, -3, -1\n",
" def setThreshold(self, builtupThreshold, *OtherThresholdsInSequence):\n",
" self.threshold = list(OtherThresholdsInSequence)\n",
" self.builtupThreshold = builtupThreshold\n",
" if len(self.threshold) == (len(self.factors.gf)):\n",
" print(\"\\nThreshold set for factors\")\n",
" else:\n",
" print('ERROR! Please check the number of factors.')\n",
" \n",
" def predict_single_iteration(self, input_raster, next_year, percent_growth_per_surrounding_pixels_dict):\n",
" predicted = deepcopy(input_raster)\n",
" count = 0\n",
" built_up_count = 0\n",
"\n",
" water_pixel_count = 0\n",
" cropland_conversion_count = 0\n",
"\n",
" for y in range(sideMargin,self.row-(sideMargin)):\n",
" for x in range(sideMargin,self.col-(sideMargin)):\n",
"\n",
" count += 1\n",
" # if built-up then skip\n",
" if input_raster[y,x] > 0:\n",
" built_up_count += 1\n",
" continue\n",
"\n",
" for factor in range(0, self.factors.nFactors):\n",
" # if water then skip\n",
" if self.factors.gf[factor][y,x] == 210:\n",
" #print(f\"water pixel detected at: {y,x}\")\n",
" water_pixel_count += 1\n",
" continue\n",
"\n",
" # test this\n",
" kernel = input_raster[y-(sideMargin):y+(sideMargin+1), x-(sideMargin):x+(sideMargin+1)]\n",
" # count how many surrounding cells are already built-up\n",
" builtupCount = ((kernel > 0) & (kernel < year)).sum()\n",
"\n",
" #print(kernel)\n",
"\n",
" if builtupCount > 0:\n",
"\n",
" # run built-up probability function\n",
" probability_result = randbin(1,1,percent_growth_per_surrounding_pixels_dict[str(builtupCount)])\n",
" #probability_result = randbin(1,1,.5)\n",
"\n",
" #print(f\"probability_result is: {probability_result}\")\n",
"\n",
" if probability_result == 1:\n",
" predicted[y, x] = next_year\n",
"\n",
" if self.factors.gf[factor][y,x] == 10 or self.factors.gf[factor][y,x] == 20:\n",
" # count the conversion of cropland to built-up\n",
" cropland_conversion_count += 1\n",
" \n",
" return predicted, cropland_conversion_count\n",
" \n",
" \n",
" def predict_raster(self, year_to_predict, percent_growth_per_surrounding_pixels_dict):\n",
" \n",
" water_pixel_count = 0\n",
" cropland_conversion_count = 0\n",
" \n",
" #kernelSize = 3\n",
" kernelSize = 5\n",
" sideMargin = math.ceil((kernelSize-1)/2)\n",
" \n",
" # max value of array\n",
" present_year = np.max(caModel.arr_wsf)\n",
"\n",
" # predict year\n",
" iterations = year_to_predict - present_year\n",
" \n",
" print(f\"year_to_predict: {year_to_predict}\")\n",
" print(f\"present_year: {present_year}\")\n",
" \n",
" next_year = present_year + 1\n",
" \n",
" predicted, running_cropland_conversion_count = self.predict_single_iteration(self.arr_wsf, next_year, percent_growth_per_surrounding_pixels_dict) \n",
" \n",
" while present_year < year_to_predict:\n",
" # max value of array\n",
" present_year = np.max(predicted)\n",
" next_year = present_year + 1\n",
" print(f\"present year now is: {present_year}\")\n",
" predicted, cropland_conversion_count = self.predict_single_iteration(predicted, next_year, percent_growth_per_surrounding_pixels_dict)\n",
" \n",
" running_cropland_conversion_count = running_cropland_conversion_count + cropland_conversion_count\n",
"\n",
" print(\"complete\")\n",
" return predicted, running_cropland_conversion_count \n",
" \n",
" \n",
" \n",
" def exportPredicted(self, predicted, outFileName):\n",
" driver = gdal.GetDriverByName(\"GTiff\")\n",
" outdata = driver.Create(outFileName, self.col, self.row, 1, gdal.GDT_UInt16) # option: GDT_UInt16, GDT_Float32\n",
" outdata.SetGeoTransform(self.ds_wsf.GetGeoTransform())\n",
" outdata.SetProjection(self.ds_wsf.GetProjection())\n",
" outdata.GetRasterBand(1).WriteArray(predicted)\n",
" outdata.GetRasterBand(1).SetNoDataValue(0) \n",
" outdata.FlushCache() \n",
" outdata = None\n",
" "
]
},
{
"cell_type": "markdown",
"id": "faced-firmware",
"metadata": {},
"source": [
"# Initiate the model"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "normal-providence",
"metadata": {},
"outputs": [],
"source": [
"##################################################\n",
"## Cellular Automata model ends ##\n",
"## The below part of the code should be updated ##\n",
"##################################################\n",
"\n",
"# Assign the directory where files are located\n",
"os.chdir(r\"C:\\repos\\urban_growth_pca\\data\")\n",
"\n",
"# Input land cover GeoTIFF for two time period\n",
"file1 = \"qarshi_wsf_evolution_41N_2.tif\"\n",
"\n",
"file2 = \"standardized_landcover_cci_nearest.tif\""
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "removed-marijuana",
"metadata": {},
"outputs": [],
"source": [
"ds_wsf, arr_wsf = readraster(file1)\n",
"ds_wsf_row, ds_wsf_col = (ds_wsf.RasterYSize, ds_wsf.RasterXSize)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "blond-participation",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2074"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds_wsf_row"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "associate-margin",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2083"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ds_wsf_col"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "intermediate-luxembourg",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Checking the size of input growth factors...\n",
"Input factors have same row and column value.\n"
]
}
],
"source": [
"# Create a factors class that configures all the factors for the model\n",
"myFactors = growthFactors(file2)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "technological-springfield",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Matching the size of land cover and growth factors...\n",
"ds_wsf row: 2074\n",
"ds_wsf col: 2083\n",
"self_factors row: 2074\n",
"self_factors col: 2083\n",
"Size of rasters matched.\n"
]
}
],
"source": [
"# Initiate the model with the above created class\n",
"caModel = fitmodel(file1, myFactors)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "knowing-robin",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2015"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# max value of array\n",
"np.max(caModel.arr_wsf)"
]
},
{
"cell_type": "markdown",
"id": "conventional-medication",
"metadata": {},
"source": [
"## find 5 year avg urban expansion based on the number of surrounding built-up pixels per each non-built-up cell"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "induced-tobacco",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1985: 263130,\n",
" 1986: 0,\n",
" 1987: 0,\n",
" 1988: 4211,\n",
" 1989: 3211,\n",
" 1990: 6420,\n",
" 1991: 3310,\n",
" 1992: 4059,\n",
" 1993: 1606,\n",
" 1994: 3799,\n",
" 1995: 3140,\n",
" 1996: 5533,\n",
" 1997: 1025,\n",
" 1998: 4969,\n",
" 1999: 2490,\n",
" 2000: 2925,\n",
" 2001: 2838,\n",
" 2002: 2941,\n",
" 2003: 2014,\n",
" 2004: 2728,\n",
" 2005: 4496,\n",
" 2006: 5201,\n",
" 2007: 3747,\n",
" 2008: 2952,\n",
" 2009: 4069,\n",
" 2010: 3158,\n",
" 2011: 1630,\n",
" 2012: 2846,\n",
" 2013: 2083,\n",
" 2014: 2864,\n",
" 2015: 1669}"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# testing: view the number of urban pixels created per year\n",
"year_dict = {}\n",
"for year in range(1985, 2016):\n",
" year_dict[year] = np.count_nonzero(arr_wsf == year)\n",
"year_dict"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "failing-webster",
"metadata": {},
"outputs": [],
"source": [
"# kernelSize = 3\n",
"# sideMargin = math.ceil((kernelSize-1)/2)\n",
"# sideMargin"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "boolean-yellow",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# define kernel size\n",
"kernelSize = 5\n",
"sideMargin = math.ceil((kernelSize-1)/2)\n",
"sideMargin"
]
},
{
"cell_type": "markdown",
"id": "southeast-douglas",
"metadata": {},
"source": [
"## Generate statistics of unbuilt cells\n",
"For the most current five years, loops through each cell and looks at its neighbors in a 6 by 6 kernel. Calculates the statistics of how many surrounding cells are already built-up around unbuilt cells."
]
},
{
"cell_type": "code",
"execution_count": 42,
"id": "shaped-employer",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sideMargin is: 2\n"
]
}
],
"source": [
"count = 0\n",
"yearly_adj_unbuilt_influence_dict = {}\n",
"\n",
"print(f\"sideMargin is: {sideMargin}\")\n",
"\n",
"count = 0\n",
"for year in range(2011, 2016, 1):\n",
" arr_wsf_copy = deepcopy(arr_wsf)\n",
" arr_wsf_copy[arr_wsf_copy >= year] = 0\n",
"\n",
" #adj_built_influence_dict = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0}\n",
" adj_built_influence_dict = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '10': 0, '11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0, '24': 0}\n",
" for y in range(sideMargin,caModel.row-(sideMargin)):\n",
" for x in range(sideMargin,caModel.col-(sideMargin)):\n",
" # if not built-up\n",
" if arr_wsf_copy[y,x] == 0:\n",
" # look at surrounding cells\n",
" kernel = arr_wsf_copy[y-(sideMargin):y+(sideMargin+1), x-(sideMargin):x+(sideMargin+1)]\n",
" count += 1\n",
"\n",
" # count how many surrounding cells are already built-up\n",
" builtupCount = ((kernel > 0) & (kernel <= year)).sum()\n",
"\n",
" # to-do: think about additional stats based on time period\n",
" # count how many surrounding cells are already built-up\n",
" # builtupCount_past5 = ((kernel >= 2005) & (kernel < 2010)).sum()\n",
" # builtupCount_past10 = ((kernel >= 2000) & (kernel < 2005)).sum()\n",
" # builtupCount_past15 = ((kernel >= 1995) & (kernel < 2000)).sum()\n",
" # builtupCount_past20 = ((kernel >= 1990) & (kernel < 1995)).sum()\n",
" # builtupCount_past25 = ((kernel >= 1985) & (kernel < 1990)).sum()\n",
"\n",
" adj_built_influence_dict[str(builtupCount)] = adj_built_influence_dict[str(builtupCount)] + 1\n",
" \n",
" yearly_adj_unbuilt_influence_dict[str(year)] = adj_built_influence_dict"
]
},
{
"cell_type": "code",
"execution_count": 43,
"id": "chemical-appointment",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'2011': {'0': 3817354,\n",
" '1': 14589,\n",
" '2': 18249,\n",
" '3': 13385,\n",
" '4': 14344,\n",
" '5': 10331,\n",
" '6': 11338,\n",
" '7': 7423,\n",
" '8': 9030,\n",
" '9': 6422,\n",
" '10': 8126,\n",
" '11': 4731,\n",
" '12': 4617,\n",
" '13': 3393,\n",
" '14': 3231,\n",
" '15': 2280,\n",
" '16': 2310,\n",
" '17': 1736,\n",
" '18': 1534,\n",
" '19': 1361,\n",
" '20': 1135,\n",
" '21': 875,\n",
" '22': 852,\n",
" '23': 622,\n",
" '24': 310},\n",
" '2012': {'0': 3816126,\n",
" '1': 14311,\n",
" '2': 18290,\n",
" '3': 13349,\n",
" '4': 14372,\n",
" '5': 10254,\n",
" '6': 11436,\n",
" '7': 7346,\n",
" '8': 9102,\n",
" '9': 6405,\n",
" '10': 8099,\n",
" '11': 4705,\n",
" '12': 4616,\n",
" '13': 3361,\n",
" '14': 3185,\n",
" '15': 2269,\n",
" '16': 2321,\n",
" '17': 1739,\n",
" '18': 1545,\n",
" '19': 1373,\n",
" '20': 1128,\n",
" '21': 869,\n",
" '22': 826,\n",
" '23': 617,\n",
" '24': 304},\n",
" '2013': {'0': 3814311,\n",
" '1': 14015,\n",
" '2': 18271,\n",
" '3': 13315,\n",
" '4': 14365,\n",
" '5': 10211,\n",
" '6': 11437,\n",
" '7': 7194,\n",
" '8': 9073,\n",
" '9': 6361,\n",
" '10': 8155,\n",
" '11': 4635,\n",
" '12': 4576,\n",
" '13': 3295,\n",
" '14': 3175,\n",
" '15': 2222,\n",
" '16': 2284,\n",
" '17': 1662,\n",
" '18': 1521,\n",
" '19': 1284,\n",
" '20': 1127,\n",
" '21': 868,\n",
" '22': 805,\n",
" '23': 628,\n",
" '24': 312},\n",
" '2014': {'0': 3813028,\n",
" '1': 13775,\n",
" '2': 18209,\n",
" '3': 13341,\n",
" '4': 14341,\n",
" '5': 10143,\n",
" '6': 11459,\n",
" '7': 7121,\n",
" '8': 9085,\n",
" '9': 6322,\n",
" '10': 8233,\n",
" '11': 4526,\n",
" '12': 4538,\n",
" '13': 3302,\n",
" '14': 3115,\n",
" '15': 2192,\n",
" '16': 2288,\n",
" '17': 1633,\n",
" '18': 1458,\n",
" '19': 1253,\n",
" '20': 1094,\n",
" '21': 828,\n",
" '22': 785,\n",
" '23': 632,\n",
" '24': 318},\n",
" '2015': {'0': 3811711,\n",
" '1': 13467,\n",
" '2': 18117,\n",
" '3': 13183,\n",
" '4': 14425,\n",
" '5': 10024,\n",
" '6': 11533,\n",
" '7': 6988,\n",
" '8': 9088,\n",
" '9': 6244,\n",
" '10': 8311,\n",
" '11': 4387,\n",
" '12': 4513,\n",
" '13': 3210,\n",
" '14': 3057,\n",
" '15': 2111,\n",
" '16': 2233,\n",
" '17': 1544,\n",
" '18': 1378,\n",
" '19': 1189,\n",
" '20': 1033,\n",
" '21': 784,\n",
" '22': 747,\n",
" '23': 581,\n",
" '24': 297}}"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"yearly_adj_unbuilt_influence_dict"
]
},
{
"cell_type": "markdown",
"id": "dental-pointer",
"metadata": {},
"source": [
"## Generate statistics of built cells\n",
"For the most current five years, loops through each cell and looks at its neighbors in a 6 by 6 kernel. Calculates the statistics of how many surrounding cells are already built-up around built cells."
]
},
{
"cell_type": "code",
"execution_count": 44,
"id": "upper-identification",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sideMargin is: 2\n"
]
}
],
"source": [
"yearly_adj_built_influence_dict_built = {}\n",
"\n",
"print(f\"sideMargin is: {sideMargin}\")\n",
"\n",
"count = 0\n",
"for year in range(2011, 2016, 1):\n",
" arr_wsf_copy = deepcopy(arr_wsf)\n",
"\n",
" #adj_built_influence_dict = {'1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0}\n",
" adj_built_influence_dict = {'0': 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '10': 0, '11': 0, '12': 0, '13': 0, '14': 0, '15': 0, '16': 0, '17': 0, '18': 0, '19': 0, '20': 0, '21': 0, '22': 0, '23': 0, '24': 0}\n",
" for y in range(sideMargin,caModel.row-(sideMargin)):\n",
" for x in range(sideMargin,caModel.col-(sideMargin)):\n",
" # if built-up\n",
" if arr_wsf[y,x] == year:\n",
" # look at surrounding cells\n",
" kernel = arr_wsf[y-(sideMargin):y+(sideMargin+1), x-(sideMargin):x+(sideMargin+1)]\n",
" count += 1\n",
" # count how many surrounding cells are already built-up\n",
" builtupCount = ((kernel > 0) & (kernel < year)).sum()\n",
"\n",
" #if builtupCount > 1:\n",
" adj_built_influence_dict[str(builtupCount)] = adj_built_influence_dict[str(builtupCount)] + 1\n",
"\n",
" yearly_adj_built_influence_dict_built[str(year)] = adj_built_influence_dict"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "sharp-hotel",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'2011': {'0': 46,\n",
" '1': 40,\n",
" '2': 52,\n",
" '3': 55,\n",
" '4': 49,\n",
" '5': 63,\n",
" '6': 59,\n",
" '7': 76,\n",
" '8': 107,\n",
" '9': 87,\n",
" '10': 134,\n",
" '11': 98,\n",
" '12': 100,\n",
" '13': 105,\n",
" '14': 77,\n",
" '15': 69,\n",
" '16': 68,\n",
" '17': 66,\n",
" '18': 52,\n",
" '19': 46,\n",
" '20': 31,\n",
" '21': 39,\n",
" '22': 52,\n",
" '23': 38,\n",
" '24': 21},\n",
" '2012': {'0': 195,\n",
" '1': 102,\n",
" '2': 82,\n",
" '3': 62,\n",
" '4': 106,\n",
" '5': 93,\n",
" '6': 109,\n",
" '7': 125,\n",
" '8': 148,\n",
" '9': 131,\n",
" '10': 174,\n",
" '11': 157,\n",
" '12': 171,\n",
" '13': 132,\n",
" '14': 145,\n",
" '15': 127,\n",
" '16': 124,\n",
" '17': 129,\n",
" '18': 98,\n",
" '19': 125,\n",
" '20': 79,\n",
" '21': 76,\n",
" '22': 62,\n",
" '23': 62,\n",
" '24': 32},\n",
" '2013': {'0': 44,\n",
" '1': 32,\n",
" '2': 58,\n",
" '3': 42,\n",
" '4': 62,\n",
" '5': 64,\n",
" '6': 87,\n",
" '7': 101,\n",
" '8': 127,\n",
" '9': 107,\n",
" '10': 123,\n",
" '11': 131,\n",
" '12': 125,\n",
" '13': 115,\n",
" '14': 119,\n",
" '15': 111,\n",
" '16': 107,\n",
" '17': 71,\n",
" '18': 85,\n",
" '19': 87,\n",
" '20': 74,\n",
" '21': 72,\n",
" '22': 65,\n",
" '23': 47,\n",
" '24': 27},\n",
" '2014': {'0': 40,\n",
" '1': 21,\n",
" '2': 35,\n",
" '3': 29,\n",
" '4': 54,\n",
" '5': 52,\n",
" '6': 79,\n",
" '7': 116,\n",
" '8': 127,\n",
" '9': 159,\n",
" '10': 170,\n",
" '11': 177,\n",
" '12': 183,\n",
" '13': 166,\n",
" '14': 163,\n",
" '15': 137,\n",
" '16': 168,\n",
" '17': 179,\n",
" '18': 154,\n",
" '19': 109,\n",
" '20': 135,\n",
" '21': 120,\n",
" '22': 107,\n",
" '23': 114,\n",
" '24': 70},\n",
" '2015': {'0': 15,\n",
" '1': 26,\n",
" '2': 21,\n",
" '3': 32,\n",
" '4': 34,\n",
" '5': 31,\n",
" '6': 41,\n",
" '7': 54,\n",
" '8': 75,\n",
" '9': 91,\n",
" '10': 119,\n",
" '11': 103,\n",
" '12': 96,\n",
" '13': 97,\n",
" '14': 92,\n",
" '15': 78,\n",
" '16': 85,\n",
" '17': 70,\n",
" '18': 98,\n",
" '19': 83,\n",
" '20': 79,\n",
" '21': 60,\n",
" '22': 71,\n",
" '23': 74,\n",
" '24': 44}}"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"yearly_adj_built_influence_dict_built"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "tamil-decision",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "charitable-practitioner",
"metadata": {},
"source": [
"## Generate transition matrix"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "wrong-drunk",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>2011</th>\n",
" <th>2012</th>\n",
" <th>2013</th>\n",
" <th>2014</th>\n",
" <th>2015</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>46</td>\n",
" <td>195</td>\n",
" <td>44</td>\n",
" <td>40</td>\n",
" <td>15</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>40</td>\n",
" <td>102</td>\n",
" <td>32</td>\n",
" <td>21</td>\n",
" <td>26</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>52</td>\n",
" <td>82</td>\n",
" <td>58</td>\n",
" <td>35</td>\n",
" <td>21</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>55</td>\n",
" <td>62</td>\n",
" <td>42</td>\n",
" <td>29</td>\n",
" <td>32</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>49</td>\n",
" <td>106</td>\n",
" <td>62</td>\n",
" <td>54</td>\n",
" <td>34</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>63</td>\n",
" <td>93</td>\n",
" <td>64</td>\n",
" <td>52</td>\n",
" <td>31</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>59</td>\n",
" <td>109</td>\n",
" <td>87</td>\n",
" <td>79</td>\n",
" <td>41</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>76</td>\n",
" <td>125</td>\n",
" <td>101</td>\n",
" <td>116</td>\n",
" <td>54</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>107</td>\n",
" <td>148</td>\n",
" <td>127</td>\n",
" <td>127</td>\n",
" <td>75</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>87</td>\n",
" <td>131</td>\n",
" <td>107</td>\n",
" <td>159</td>\n",
" <td>91</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>134</td>\n",
" <td>174</td>\n",
" <td>123</td>\n",
" <td>170</td>\n",
" <td>119</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>98</td>\n",
" <td>157</td>\n",
" <td>131</td>\n",
" <td>177</td>\n",
" <td>103</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>100</td>\n",
" <td>171</td>\n",
" <td>125</td>\n",
" <td>183</td>\n",
" <td>96</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>105</td>\n",
" <td>132</td>\n",
" <td>115</td>\n",
" <td>166</td>\n",
" <td>97</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>77</td>\n",
" <td>145</td>\n",
" <td>119</td>\n",
" <td>163</td>\n",
" <td>92</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>69</td>\n",
" <td>127</td>\n",
" <td>111</td>\n",
" <td>137</td>\n",
" <td>78</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>68</td>\n",
" <td>124</td>\n",
" <td>107</td>\n",
" <td>168</td>\n",
" <td>85</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>66</td>\n",
" <td>129</td>\n",
" <td>71</td>\n",
" <td>179</td>\n",
" <td>70</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>52</td>\n",
" <td>98</td>\n",
" <td>85</td>\n",
" <td>154</td>\n",
" <td>98</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>46</td>\n",
" <td>125</td>\n",
" <td>87</td>\n",
" <td>109</td>\n",
" <td>83</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>31</td>\n",
" <td>79</td>\n",
" <td>74</td>\n",
" <td>135</td>\n",
" <td>79</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>39</td>\n",
" <td>76</td>\n",
" <td>72</td>\n",
" <td>120</td>\n",
" <td>60</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>52</td>\n",
" <td>62</td>\n",
" <td>65</td>\n",
" <td>107</td>\n",
" <td>71</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>38</td>\n",
" <td>62</td>\n",
" <td>47</td>\n",
" <td>114</td>\n",
" <td>74</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>21</td>\n",
" <td>32</td>\n",
" <td>27</td>\n",
" <td>70</td>\n",
" <td>44</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 2011 2012 2013 2014 2015\n",
"0 46 195 44 40 15\n",
"1 40 102 32 21 26\n",
"2 52 82 58 35 21\n",
"3 55 62 42 29 32\n",
"4 49 106 62 54 34\n",
"5 63 93 64 52 31\n",
"6 59 109 87 79 41\n",
"7 76 125 101 116 54\n",
"8 107 148 127 127 75\n",
"9 87 131 107 159 91\n",
"10 134 174 123 170 119\n",
"11 98 157 131 177 103\n",
"12 100 171 125 183 96\n",
"13 105 132 115 166 97\n",
"14 77 145 119 163 92\n",
"15 69 127 111 137 78\n",
"16 68 124 107 168 85\n",
"17 66 129 71 179 70\n",
"18 52 98 85 154 98\n",
"19 46 125 87 109 83\n",
"20 31 79 74 135 79\n",
"21 39 76 72 120 60\n",
"22 52 62 65 107 71\n",
"23 38 62 47 114 74\n",
"24 21 32 27 70 44"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"yearly_adj_built_influence_df_built = pd.DataFrame.from_dict(yearly_adj_built_influence_dict_built)\n",
"yearly_adj_built_influence_df_built"
]
},
{
"cell_type": "code",
"execution_count": 48,
"id": "curious-session",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>2011</th>\n",
" <th>2012</th>\n",
" <th>2013</th>\n",
" <th>2014</th>\n",
" <th>2015</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3817354</td>\n",
" <td>3816126</td>\n",
" <td>3814311</td>\n",
" <td>3813028</td>\n",
" <td>3811711</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>14589</td>\n",
" <td>14311</td>\n",
" <td>14015</td>\n",
" <td>13775</td>\n",
" <td>13467</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>18249</td>\n",
" <td>18290</td>\n",
" <td>18271</td>\n",
" <td>18209</td>\n",
" <td>18117</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>13385</td>\n",
" <td>13349</td>\n",
" <td>13315</td>\n",
" <td>13341</td>\n",
" <td>13183</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>14344</td>\n",
" <td>14372</td>\n",
" <td>14365</td>\n",
" <td>14341</td>\n",
" <td>14425</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>10331</td>\n",
" <td>10254</td>\n",
" <td>10211</td>\n",
" <td>10143</td>\n",
" <td>10024</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>11338</td>\n",
" <td>11436</td>\n",
" <td>11437</td>\n",
" <td>11459</td>\n",
" <td>11533</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>7423</td>\n",
" <td>7346</td>\n",
" <td>7194</td>\n",
" <td>7121</td>\n",
" <td>6988</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>9030</td>\n",
" <td>9102</td>\n",
" <td>9073</td>\n",
" <td>9085</td>\n",
" <td>9088</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>6422</td>\n",
" <td>6405</td>\n",
" <td>6361</td>\n",
" <td>6322</td>\n",
" <td>6244</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>8126</td>\n",
" <td>8099</td>\n",
" <td>8155</td>\n",
" <td>8233</td>\n",
" <td>8311</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>4731</td>\n",
" <td>4705</td>\n",
" <td>4635</td>\n",
" <td>4526</td>\n",
" <td>4387</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>4617</td>\n",
" <td>4616</td>\n",
" <td>4576</td>\n",
" <td>4538</td>\n",
" <td>4513</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>3393</td>\n",
" <td>3361</td>\n",
" <td>3295</td>\n",
" <td>3302</td>\n",
" <td>3210</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>3231</td>\n",
" <td>3185</td>\n",
" <td>3175</td>\n",
" <td>3115</td>\n",
" <td>3057</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>2280</td>\n",
" <td>2269</td>\n",
" <td>2222</td>\n",
" <td>2192</td>\n",
" <td>2111</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>2310</td>\n",
" <td>2321</td>\n",
" <td>2284</td>\n",
" <td>2288</td>\n",
" <td>2233</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>1736</td>\n",
" <td>1739</td>\n",
" <td>1662</td>\n",
" <td>1633</td>\n",
" <td>1544</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>1534</td>\n",
" <td>1545</td>\n",
" <td>1521</td>\n",
" <td>1458</td>\n",
" <td>1378</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>1361</td>\n",
" <td>1373</td>\n",
" <td>1284</td>\n",
" <td>1253</td>\n",
" <td>1189</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>1135</td>\n",
" <td>1128</td>\n",
" <td>1127</td>\n",
" <td>1094</td>\n",
" <td>1033</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>875</td>\n",
" <td>869</td>\n",
" <td>868</td>\n",
" <td>828</td>\n",
" <td>784</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>852</td>\n",
" <td>826</td>\n",
" <td>805</td>\n",
" <td>785</td>\n",
" <td>747</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>622</td>\n",
" <td>617</td>\n",
" <td>628</td>\n",
" <td>632</td>\n",
" <td>581</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>310</td>\n",
" <td>304</td>\n",
" <td>312</td>\n",
" <td>318</td>\n",
" <td>297</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 2011 2012 2013 2014 2015\n",
"0 3817354 3816126 3814311 3813028 3811711\n",
"1 14589 14311 14015 13775 13467\n",
"2 18249 18290 18271 18209 18117\n",
"3 13385 13349 13315 13341 13183\n",
"4 14344 14372 14365 14341 14425\n",
"5 10331 10254 10211 10143 10024\n",
"6 11338 11436 11437 11459 11533\n",
"7 7423 7346 7194 7121 6988\n",
"8 9030 9102 9073 9085 9088\n",
"9 6422 6405 6361 6322 6244\n",
"10 8126 8099 8155 8233 8311\n",
"11 4731 4705 4635 4526 4387\n",
"12 4617 4616 4576 4538 4513\n",
"13 3393 3361 3295 3302 3210\n",
"14 3231 3185 3175 3115 3057\n",
"15 2280 2269 2222 2192 2111\n",
"16 2310 2321 2284 2288 2233\n",
"17 1736 1739 1662 1633 1544\n",
"18 1534 1545 1521 1458 1378\n",
"19 1361 1373 1284 1253 1189\n",
"20 1135 1128 1127 1094 1033\n",
"21 875 869 868 828 784\n",
"22 852 826 805 785 747\n",
"23 622 617 628 632 581\n",
"24 310 304 312 318 297"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"yearly_adj_built_influence_df_unbuilt = pd.DataFrame.from_dict(yearly_adj_unbuilt_influence_dict)\n",
"yearly_adj_built_influence_df_unbuilt"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "scientific-coverage",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>2011</th>\n",
" <th>2012</th>\n",
" <th>2013</th>\n",
" <th>2014</th>\n",
" <th>2015</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.000012</td>\n",
" <td>0.000051</td>\n",
" <td>0.000012</td>\n",
" <td>0.000010</td>\n",
" <td>0.000004</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0.002742</td>\n",
" <td>0.007127</td>\n",
" <td>0.002283</td>\n",
" <td>0.001525</td>\n",
" <td>0.001931</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0.002849</td>\n",
" <td>0.004483</td>\n",
" <td>0.003174</td>\n",
" <td>0.001922</td>\n",
" <td>0.001159</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>0.004109</td>\n",
" <td>0.004645</td>\n",
" <td>0.003154</td>\n",
" <td>0.002174</td>\n",
" <td>0.002427</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>0.003416</td>\n",
" <td>0.007375</td>\n",
" <td>0.004316</td>\n",
" <td>0.003765</td>\n",
" <td>0.002357</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>0.006098</td>\n",
" <td>0.009070</td>\n",
" <td>0.006268</td>\n",
" <td>0.005127</td>\n",
" <td>0.003093</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>0.005204</td>\n",
" <td>0.009531</td>\n",
" <td>0.007607</td>\n",
" <td>0.006894</td>\n",
" <td>0.003555</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>0.010238</td>\n",
" <td>0.017016</td>\n",
" <td>0.014039</td>\n",
" <td>0.016290</td>\n",
" <td>0.007728</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>0.011849</td>\n",
" <td>0.016260</td>\n",
" <td>0.013998</td>\n",
" <td>0.013979</td>\n",
" <td>0.008253</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>0.013547</td>\n",
" <td>0.020453</td>\n",
" <td>0.016821</td>\n",
" <td>0.025150</td>\n",
" <td>0.014574</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>0.016490</td>\n",
" <td>0.021484</td>\n",
" <td>0.015083</td>\n",
" <td>0.020649</td>\n",
" <td>0.014318</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>0.020714</td>\n",
" <td>0.033369</td>\n",
" <td>0.028263</td>\n",
" <td>0.039107</td>\n",
" <td>0.023478</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12</th>\n",
" <td>0.021659</td>\n",
" <td>0.037045</td>\n",
" <td>0.027316</td>\n",
" <td>0.040326</td>\n",
" <td>0.021272</td>\n",
" </tr>\n",
" <tr>\n",
" <th>13</th>\n",
" <td>0.030946</td>\n",
" <td>0.039274</td>\n",
" <td>0.034901</td>\n",
" <td>0.050273</td>\n",
" <td>0.030218</td>\n",
" </tr>\n",
" <tr>\n",
" <th>14</th>\n",
" <td>0.023832</td>\n",
" <td>0.045526</td>\n",
" <td>0.037480</td>\n",
" <td>0.052327</td>\n",
" <td>0.030095</td>\n",
" </tr>\n",
" <tr>\n",
" <th>15</th>\n",
" <td>0.030263</td>\n",
" <td>0.055972</td>\n",
" <td>0.049955</td>\n",
" <td>0.062500</td>\n",
" <td>0.036949</td>\n",
" </tr>\n",
" <tr>\n",
" <th>16</th>\n",
" <td>0.029437</td>\n",
" <td>0.053425</td>\n",
" <td>0.046848</td>\n",
" <td>0.073427</td>\n",
" <td>0.038065</td>\n",
" </tr>\n",
" <tr>\n",
" <th>17</th>\n",
" <td>0.038018</td>\n",
" <td>0.074181</td>\n",
" <td>0.042720</td>\n",
" <td>0.109614</td>\n",
" <td>0.045337</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18</th>\n",
" <td>0.033898</td>\n",
" <td>0.063430</td>\n",
" <td>0.055884</td>\n",
" <td>0.105624</td>\n",
" <td>0.071118</td>\n",
" </tr>\n",
" <tr>\n",
" <th>19</th>\n",
" <td>0.033799</td>\n",
" <td>0.091042</td>\n",
" <td>0.067757</td>\n",
" <td>0.086991</td>\n",
" <td>0.069807</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20</th>\n",
" <td>0.027313</td>\n",
" <td>0.070035</td>\n",
" <td>0.065661</td>\n",
" <td>0.123400</td>\n",
" <td>0.076476</td>\n",
" </tr>\n",
" <tr>\n",
" <th>21</th>\n",
" <td>0.044571</td>\n",
" <td>0.087457</td>\n",
" <td>0.082949</td>\n",
" <td>0.144928</td>\n",
" <td>0.076531</td>\n",
" </tr>\n",
" <tr>\n",
" <th>22</th>\n",
" <td>0.061033</td>\n",
" <td>0.075061</td>\n",
" <td>0.080745</td>\n",
" <td>0.136306</td>\n",
" <td>0.095047</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23</th>\n",
" <td>0.061093</td>\n",
" <td>0.100486</td>\n",
" <td>0.074841</td>\n",
" <td>0.180380</td>\n",
" <td>0.127367</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24</th>\n",
" <td>0.067742</td>\n",
" <td>0.105263</td>\n",
" <td>0.086538</td>\n",
" <td>0.220126</td>\n",
" <td>0.148148</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" 2011 2012 2013 2014 2015\n",
"0 0.000012 0.000051 0.000012 0.000010 0.000004\n",
"1 0.002742 0.007127 0.002283 0.001525 0.001931\n",
"2 0.002849 0.004483 0.003174 0.001922 0.001159\n",
"3 0.004109 0.004645 0.003154 0.002174 0.002427\n",
"4 0.003416 0.007375 0.004316 0.003765 0.002357\n",
"5 0.006098 0.009070 0.006268 0.005127 0.003093\n",
"6 0.005204 0.009531 0.007607 0.006894 0.003555\n",
"7 0.010238 0.017016 0.014039 0.016290 0.007728\n",
"8 0.011849 0.016260 0.013998 0.013979 0.008253\n",
"9 0.013547 0.020453 0.016821 0.025150 0.014574\n",
"10 0.016490 0.021484 0.015083 0.020649 0.014318\n",
"11 0.020714 0.033369 0.028263 0.039107 0.023478\n",
"12 0.021659 0.037045 0.027316 0.040326 0.021272\n",
"13 0.030946 0.039274 0.034901 0.050273 0.030218\n",
"14 0.023832 0.045526 0.037480 0.052327 0.030095\n",
"15 0.030263 0.055972 0.049955 0.062500 0.036949\n",
"16 0.029437 0.053425 0.046848 0.073427 0.038065\n",
"17 0.038018 0.074181 0.042720 0.109614 0.045337\n",
"18 0.033898 0.063430 0.055884 0.105624 0.071118\n",
"19 0.033799 0.091042 0.067757 0.086991 0.069807\n",
"20 0.027313 0.070035 0.065661 0.123400 0.076476\n",
"21 0.044571 0.087457 0.082949 0.144928 0.076531\n",
"22 0.061033 0.075061 0.080745 0.136306 0.095047\n",
"23 0.061093 0.100486 0.074841 0.180380 0.127367\n",
"24 0.067742 0.105263 0.086538 0.220126 0.148148"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"probability_transition_matrix = yearly_adj_built_influence_df_built/yearly_adj_built_influence_df_unbuilt\n",
"probability_transition_matrix"
]
},
{
"cell_type": "code",
"execution_count": 50,
"id": "collect-blues",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 0.000018\n",
"1 0.003122\n",
"2 0.002718\n",
"3 0.003302\n",
"4 0.004246\n",
"5 0.005931\n",
"6 0.006558\n",
"7 0.013062\n",
"8 0.012868\n",
"9 0.018109\n",
"10 0.017605\n",
"11 0.028986\n",
"12 0.029524\n",
"13 0.037122\n",
"14 0.037852\n",
"15 0.047128\n",
"16 0.048240\n",
"17 0.061974\n",
"18 0.065991\n",
"19 0.069879\n",
"20 0.072577\n",
"21 0.087287\n",
"22 0.089638\n",
"23 0.108833\n",
"24 0.125563\n",
"dtype: float64"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"probability_transition_matrix_mean = probability_transition_matrix.mean(axis=1)\n",
"probability_transition_matrix_mean"
]
},
{
"cell_type": "code",
"execution_count": 51,
"id": "elder-spanking",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'0': 1.7822053697832273e-05,\n",
" '1': 0.0031215180812036005,\n",
" '2': 0.0027176967152276145,\n",
" '3': 0.003301815175169756,\n",
" '4': 0.004246001507401446,\n",
" '5': 0.005930959838747289,\n",
" '6': 0.006558218917780069,\n",
" '7': 0.013062273683570458,\n",
" '8': 0.0128677711990417,\n",
" '9': 0.018109093457837032,\n",
" '10': 0.017604833152993427,\n",
" '11': 0.028986449335055085,\n",
" '12': 0.029523719260950508,\n",
" '13': 0.03712241746829452,\n",
" '14': 0.037852032156483854,\n",
" '15': 0.0471278520515533,\n",
" '16': 0.0482404138443222,\n",
" '17': 0.06197392123791071,\n",
" '18': 0.06599094335900328,\n",
" '19': 0.06987899658473193,\n",
" '20': 0.07257718633067071,\n",
" '21': 0.08728714655089775,\n",
" '22': 0.0896382649438797,\n",
" '23': 0.10883331834245653,\n",
" '24': 0.1255634978457479}"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"percent_growth_per_surrounding_pixels_dict = probability_transition_matrix_mean.to_dict()\n",
"percent_growth_per_surrounding_pixels_dict"
]
},
{
"cell_type": "markdown",
"id": "choice-washer",
"metadata": {},
"source": [
"## run"
]
},
{
"cell_type": "code",
"execution_count": 52,
"id": "secondary-belfast",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"year_to_predict: 2018\n",
"present_year: 2015\n",
"sideMargin is: 2\n",
"present year now is: 2016\n",
"sideMargin is: 2\n",
"present year now is: 2017\n",
"sideMargin is: 2\n",
"present year now is: 2018\n",
"sideMargin is: 2\n",
"complete\n"
]
}
],
"source": [
"predicted_raster_2018, cropland_conversion_count = caModel.predict_raster(2018, percent_growth_per_surrounding_pixels_dict)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "centered-registrar",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1922"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.count_nonzero(predicted_raster_2018 == 2018)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "opponent-deployment",
"metadata": {},
"outputs": [],
"source": [
"#caModel.exportPredicted(predicted_raster_2016, 'wsf_2018_predicted.tif')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "veterinary-canon",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment