{ "cells": [ { "cell_type": "markdown", "id": "64c5ed1a-5f6b-4251-9096-cbb571d3f07c", "metadata": {}, "source": [ "### MS Delta Land-Cover Transition Dynamics Between 2008 and 2021\n", "\n", "This Jupyter Notebook calculates the land-cover transitions for the MS Delta for each of the 1,614,385 unique categories identified by Heintzman et al. (in preparation). These categories characterize the land-cover transitions among 9 land-cover classes during the 13-year span 2008-2021. The classes were reclassified categories from the U.S. Department of Agriculture, National Agricultural Statistics Service [CropScape - Cropland Data Layer](https://nassgeodata.gmu.edu/CropScape/) (see Heintzman et al., in preparation): 1, soybeans; 2, corn; 3, cotton; 4, rice; 5, wetlands; 6, water; 7, upland forest; 8, other agriculture; and 9, developed. The 1,614,385 unique categories land-cover transitions sequences are stored in a comma-separated values file named `comboall1.csv`, and are spatially linked a raster of the MS Delta with a cell size of 30x30 m.\n", "\n", "The below Python code uses the Python libraries [Pandas](https://pandas.pydata.org/) and [NumPy](https://numpy.org/) to:\n", "1. read the csv file into a Pandas Dataframe;\n", "2. calculate the number of unique land use codes present for each unique 13-year land-cover transition;\n", "3. calculate the number of cultivated and non-cultivated crop categories for each unique 13-year land-cover transition; and\n", "4. calculate the number of transitions between only cultivated, between only non-cultivated, and between cultivated and no-cultivated crop classes for each unique 13-year land-cover transition.\n", "\n", "Reference:\n", "\n", "Heintzman, L.J., McIntyre, N.E., and Langendoen, E.J. Cultivation and dynamic cropping processes as drivers of land cover heterogeneity within industrial agricultural systems: a metrics-based case study in the Yazoo-Mississippi Delta (USA). For submission to [Landscape Ecology](https://www.springer.com/journal/10980)." ] }, { "cell_type": "code", "execution_count": 1, "id": "675073bf-5da4-4387-bbab-621b89ad6f88", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "\n", "# set cultivated and non-cultivated crop categories\n", "cultivated = [1, 2, 3, 4, 8]\n", "noncultivated = [5, 6, 7, 9]" ] }, { "cell_type": "code", "execution_count": 2, "id": "f527c470-1204-4d07-94a8-d344c1749924", "metadata": {}, "outputs": [], "source": [ "# read in the comboall csv file\n", "df = pd.read_csv('./files/comboall1.csv')\n", "\n", "# drop the OID_ column and shorten the LUC column names to year only\n", "df.drop('OID_', axis=1, inplace=True)\n", "df.rename(columns={'MF8H_2008': '2008', 'MF8H_2009': '2009', 'MF8H_2010': '2010', 'MF8H_2011': '2011', 'MF8H_2012': '2012', 'MF8H_2013': '2013', 'MF8H_2014': '2014', 'MF8H_2015': '2015', 'MF8H_2016': '2016', 'MF8H_2017': '2017', 'MF8H_2018': '2018', 'MF8H_2019': '2019', 'MF8H_2020': '2020', 'MF8H_2021': '2021'}, inplace=True)\n", "\n", "# add the number of unique land use codes by row as a new column\n", "df['nunique'] = df.loc[:,'2008':'2021'].nunique(axis=1)\n", "\n", "# determine if rows have focal and non-focal categories and add results as new columns\n", "df['ncultivated'] = df.loc[:,'2008':'2021'].isin(cultivated).sum(axis=1)\n", "df['nnoncultivated'] = df.loc[:,'2008':'2021'].isin(noncultivated).sum(axis=1)" ] }, { "cell_type": "code", "execution_count": 3, "id": "18e38d14-a9c7-4c5b-800e-78b1b0043145", "metadata": {}, "outputs": [], "source": [ "# add LUC transition columns\n", "# add all LUC transitions\n", "df['ntrans_all'] = (df.loc[:, '2008':'2021'].shift(axis=1) != df.loc[:, '2008':'2021']).sum(axis=1)-1\n", "\n", "# add all LUC transitions within a focal crop series\n", "# mask cultivated crops\n", "m = df.loc[:,'2008':'2021'].isin(cultivated)\n", "df2 = df.loc[:,'2008':'2021'].mask(~m)\n", "# iterate over the masked non-cultivated crops dataframe\n", "results = np.zeros(df2.shape[0])\n", "for idx, row in df2.iterrows():\n", " series = row.values\n", " dseries = np.diff(series)\n", " dseries[np.isnan(dseries)] = 0\n", " results[idx] = np.count_nonzero(dseries)\n", "# set dataframe column on non-cultivated crop transitions\n", "df['ntrans_cultivated'] = results\n", "df['ntrans_cultivated'] = df['ntrans_cultivated'].apply(np.int64)\n", "\n", "# add all LUC transitions within a non-cultivated crop series\n", "# mask non-cultivated crops\n", "m = df.loc[:,'2008':'2021'].isin(noncultivated)\n", "df2 = df.loc[:,'2008':'2021'].mask(~m)\n", "# iterate over the masked non-cultivated crops dataframe\n", "results = np.zeros(df2.shape[0])\n", "for idx, row in df2.iterrows():\n", " series = row.values\n", " dseries = np.diff(series)\n", " dseries[np.isnan(dseries)] = 0\n", " results[idx] = np.count_nonzero(dseries)\n", "# set dataframe column on non-cultivated crop transitions\n", "df['ntrans_noncultivated'] = results\n", "df['ntrans_noncultivated'] = df['ntrans_noncultivated'].apply(np.int64)\n", "\n", "# add all LUC transitions between cultivated and non-cultivated crop series\n", "# mask cultivated crops\n", "m = df.loc[:,'2008':'2021'].isin(cultivated)\n", "df['ntrans_cross'] = (m.shift(axis=1) != m).sum(axis=1)-1" ] }, { "cell_type": "code", "execution_count": 12, "id": "4a4d4b81-641b-4720-a5d2-9085b2df3233", "metadata": { "jupyter": { "source_hidden": true }, "tags": [] }, "outputs": [], "source": [ "# save dataframe as a parquet file\n", "df.to_parquet('./files/comboall.parquet.gzip', compression='gzip')" ] }, { "cell_type": "code", "execution_count": 13, "id": "c0c0f125-6a3c-4cc9-b076-c1655421548a", "metadata": { "jupyter": { "source_hidden": true }, "tags": [] }, "outputs": [], "source": [ "df.to_csv('./files/comboall.csv', index=False)" ] }, { "cell_type": "code", "execution_count": 3, "id": "4d16699c-c0f8-483d-a691-55b1997b3544", "metadata": { "jupyter": { "source_hidden": true }, "tags": [] }, "outputs": [], "source": [ "df = pd.read_parquet('./files/comboall.parquet.gzip')" ] }, { "cell_type": "code", "execution_count": 4, "id": "8048b685-e3fe-48e9-8cd0-dad93144b918", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
VALUECOUNT20082009201020112012201320142015...201920202021nuniquencultivatednnoncultivatedntrans_allntrans_cultivatedntrans_noncultivatedntrans_cross
013274277777777...77710140000
12355555575...55520144040
23257555575...55520146060
34157777777...21742127142
45157777777...27731134022
563957777757...77720143030
67255777755...55520144040
7827377777757...77720142020
899677777777...77821131001
910777777788...77824105005
1011187888787...88821048008
1112188888888...88831313102
1213588888877...77821044004
131431777777777...77721132002
14151177877778...77722124004
151621588888881...88821402200
1617278488888881...88121403300
1718588188881...31131405500
18197381818888...88821404400
1920121199898999...99922124004
\n", "

20 rows × 23 columns

\n", "
" ], "text/plain": [ " VALUE COUNT 2008 2009 2010 2011 2012 2013 2014 2015 ... 2019 \\\n", "0 1 32742 7 7 7 7 7 7 7 7 ... 7 \n", "1 2 3 5 5 5 5 5 5 7 5 ... 5 \n", "2 3 2 5 7 5 5 5 5 7 5 ... 5 \n", "3 4 1 5 7 7 7 7 7 7 7 ... 2 \n", "4 5 1 5 7 7 7 7 7 7 7 ... 2 \n", "5 6 39 5 7 7 7 7 7 5 7 ... 7 \n", "6 7 2 5 5 7 7 7 7 5 5 ... 5 \n", "7 8 273 7 7 7 7 7 7 5 7 ... 7 \n", "8 9 96 7 7 7 7 7 7 7 7 ... 7 \n", "9 10 7 7 7 7 7 7 7 8 8 ... 7 \n", "10 11 1 8 7 8 8 8 7 8 7 ... 8 \n", "11 12 1 8 8 8 8 8 8 8 8 ... 8 \n", "12 13 5 8 8 8 8 8 8 7 7 ... 7 \n", "13 14 317 7 7 7 7 7 7 7 7 ... 7 \n", "14 15 11 7 7 8 7 7 7 7 8 ... 7 \n", "15 16 215 8 8 8 8 8 8 8 1 ... 8 \n", "16 17 2784 8 8 8 8 8 8 8 1 ... 8 \n", "17 18 5 8 8 1 8 8 8 8 1 ... 3 \n", "18 19 73 8 1 8 1 8 8 8 8 ... 8 \n", "19 20 1211 9 9 8 9 8 9 9 9 ... 9 \n", "\n", " 2020 2021 nunique ncultivated nnoncultivated ntrans_all \\\n", "0 7 7 1 0 14 0 \n", "1 5 5 2 0 14 4 \n", "2 5 5 2 0 14 6 \n", "3 1 7 4 2 12 7 \n", "4 7 7 3 1 13 4 \n", "5 7 7 2 0 14 3 \n", "6 5 5 2 0 14 4 \n", "7 7 7 2 0 14 2 \n", "8 7 8 2 1 13 1 \n", "9 7 8 2 4 10 5 \n", "10 8 8 2 10 4 8 \n", "11 8 8 3 13 1 3 \n", "12 7 8 2 10 4 4 \n", "13 7 7 2 1 13 2 \n", "14 7 7 2 2 12 4 \n", "15 8 8 2 14 0 2 \n", "16 8 1 2 14 0 3 \n", "17 1 1 3 14 0 5 \n", "18 8 8 2 14 0 4 \n", "19 9 9 2 2 12 4 \n", "\n", " ntrans_cultivated ntrans_noncultivated ntrans_cross \n", "0 0 0 0 \n", "1 0 4 0 \n", "2 0 6 0 \n", "3 1 4 2 \n", "4 0 2 2 \n", "5 0 3 0 \n", "6 0 4 0 \n", "7 0 2 0 \n", "8 0 0 1 \n", "9 0 0 5 \n", "10 0 0 8 \n", "11 1 0 2 \n", "12 0 0 4 \n", "13 0 0 2 \n", "14 0 0 4 \n", "15 2 0 0 \n", "16 3 0 0 \n", "17 5 0 0 \n", "18 4 0 0 \n", "19 0 0 4 \n", "\n", "[20 rows x 23 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head(20)" ] }, { "cell_type": "code", "execution_count": null, "id": "565d18b0-6e7f-408b-9554-3c4bfc1a99e4", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }