这些脚本是用什么方法?{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# This notebook simply changes the column names of the sampled pixel values to reference keys (corresponding to a JSON dictionary) so as to allow upload of the CSV as a Shapefile to Google Earth Engine without violating character limits on column names\n",
"## The dictionary to translate the column placeholders to actual covariate names can be found in each of the JS scripts using the Shapefile\n",
"## Note: shapefiles were created using QGIS/GDAL"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Import the necessary libraries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import string as string\n",
"import itertools as itertools\n",
"import pandas as pd\n",
"\n",
"# Define a function to repeat letters of the alphabet like Excel column names\n",
"def alphabetSeries(lengthOfSeries):\n",
" def helper():\n",
" n = 1\n",
" while True:\n",
" yield from (''.join(group) for group in itertools.product(string.ascii_uppercase, repeat=n))\n",
" n += 1\n",
" return list(itertools.islice(helper(), lengthOfSeries))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1874, 85)\n",
"Index(['Abs_Lat', 'Annual_Mean_Radiation', 'Annual_Mean_Temperature',\n",
" 'Annual_Precipitation', 'Aridity_Index', 'Bacterivor',\n",
" 'Bulk_Density_15cm', 'CContent_15cm', 'CatIonExcCap_15cm',\n",
" 'Clay_Content_15cm', 'CoOfVar_EVI_Hetero_1km',\n",
" 'Contrast_EVI_Hetero_1km', 'CorFragVolPerc_15cm',\n",
" 'Correlation_EVI_Hetero_1km', 'Depth_to_Bedrock',\n",
" 'Dissimilarity_EVI_Hetero_1km', 'EVI', 'Entropy_EVI_Hetero_1km',\n",
" 'Evenness_EVI_Hetero_1km', 'Fpar', 'Fungivores', 'Global_Biomass_IPCC',\n",
" 'Gpp', 'Highest_Weekly_Radiation', 'Homogeneity_EVI_Hetero_1km',\n",
" 'Human_Development', 'Isothermality', 'Lai', 'Lowest_Weekly_Radiation',\n",
" 'Max_Temperature_of_Warmest_Month', 'Maximum_EVI_Hetero_1km',\n",
" 'Mean_Diurnal_Range', 'Mean_Temperature_of_Coldest_Quarter',\n",
" 'Mean_Temperature_of_Driest_Quarter',\n",
" 'Mean_Temperature_of_Warmest_Quarter',\n",
" 'Mean_Temperature_of_Wettest_Quarter',\n",
" 'Min_Temperature_of_Coldest_Month', 'NDVI', 'Nadir_Reflectance_Band1',\n",
" 'Nadir_Reflectance_Band2', 'Nadir_Reflectance_Band3',\n",
" 'Nadir_Reflectance_Band4', 'Nadir_Reflectance_Band5',\n",
" 'Nadir_Reflectance_Band6', 'Nadir_Reflectance_Band7', 'Npp',\n",
" 'Omnivores', 'OrgCStockTHa_0to15cm', 'PET', 'Pixel_Lat', 'Pixel_Long',\n",
" 'Plant_feed', 'Population_Density', 'Precipitation_Seasonality',\n",
" 'Precipitation_of_Coldest_Quarter', 'Precipitation_of_Driest_Month',\n",
" 'Precipitation_of_Driest_Quarter', 'Precipitation_of_Warmest_Quarter',\n",
" 'Precipitation_of_Wettest_Month', 'Precipitation_of_Wettest_Quarter',\n",
" 'PredProb_of_R_Horizon', 'Predators', 'Radiation_Seasonality',\n",
" 'Radiation_of_Coldest_Quarter', 'Radiation_of_Driest_Quarter',\n",
" 'Radiation_of_Warmest_Quarter', 'Radiation_of_Wettest_Quarter',\n",
" 'Range_EVI_Hetero_1km', 'Sand_Content_15cm', 'Shannon_Index_1km',\n",
" 'Silt_Content_15cm', 'Simpson_Index_1km', 'Std_EVI_Hetero_1km',\n",
" 'Temperature_Annual_Range', 'Temperature_Seasonality', 'Total_Numb',\n",
" 'Unidentifi', 'Uniformity_EVI_Hetero_1km', 'Variance_EVI_Hetero_1km',\n",
" 'eastness', 'elevation', 'hillshade', 'northness', 'pHinHOX_15cm',\n",
" 'slope'],\n",
" dtype='object')\n",
" Abs_Lat Annual_Mean_Radiation Annual_Mean_Temperature \\n",
"0 22.437500 140.071213 18.141666 \n",
"1 22.037500 143.425690 19.049999 \n",
"2 21.304167 152.697372 19.695833 \n",
"3 21.320833 151.420517 19.487499 \n",
"4 21.445833 149.989777 18.904167 \n",
"\n",
" Annual_Precipitation Aridity_Index Bacterivor Bulk_Density_15cm \\n",
"0 1519.0 10354.312500 120.0 1236.0 \n",
"1 1528.0 9590.197266 45.0 1283.0 \n",
"2 1614.0 10367.072266 390.0 1334.0 \n",
"3 1619.0 10574.904297 224.0 1317.0 \n",
"4 1616.0 10974.227539 40.0 1239.0 \n",
"\n",
" CContent_15cm CatIonExcCap_15cm Clay_Content_15cm ... Total_Numb \\n",
"0 25.0 12.0 34.0 ... 482.0 \n",
"1 27.0 9.0 40.0 ... 285.0 \n",
"2 19.0 7.0 37.0 ... 1650.0 \n",
"3 17.0 7.0 36.0 ... 636.0 \n",
"4 27.0 8.0 35.0 ... 470.0 \n",
"\n",
" Unidentifi Uniformity_EVI_Hetero_1km Variance_EVI_Hetero_1km eastness \\n",
"0 0.0 756.0 70497.0 0.678042 \n",
"1 0.0 1792.0 29684.0 0.207282 \n",
"2 0.0 618.0 67425.0 -0.168783 \n",
"3 0.0 514.0 136302.0 -0.194088 \n",
"4 0.0 538.0 716117.0 0.255504 \n",
"\n",
" elevation hillshade northness pHinHOX_15cm slope \n",
"0 901.304993 157.524994 0.121048 52.0 9.735556 \n",
"1 976.426392 171.230835 -0.064766 53.0 7.336111 \n",
"2 928.925537 181.953613 -0.105951 52.0 2.933611 \n",
"3 986.726379 183.036667 -0.575699 52.0 3.099444 \n",
"4 1141.481079 171.678604 -0.821737 51.0 10.041111 \n",
"\n",
"[5 rows x 85 columns]\n"
]
}
],
"source": [
"# Import the raw data from a CSV file\n",
"pathToFile = '20180706_NematodePoints_SampledPixelValues.csv'\n",
"rawData = pd.read_csv(pathToFile,float_precision='round_trip')\n",
"\n",
"# Optionally drop columns of disinterest\n",
"dataOI = rawData.drop(columns=['system:index','.geo'])\n",
"\n",
"print(dataOI.shape)\n",
"print(dataOI.columns)\n",
"print(dataOI.head(5))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Create a new list of column names\n",
"newColNames = alphabetSeries(len(list(dataOI.columns)))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Abs_Lat': 'A', 'Annual_Mean_Radiation': 'B', 'Annual_Mean_Temperature': 'C', 'Annual_Precipitation': 'D', 'Aridity_Index': 'E', 'Bacterivor': 'F', 'Bulk_Density_15cm': 'G', 'CContent_15cm': 'H', 'CatIonExcCap_15cm': 'I', 'Clay_Content_15cm': 'J', 'CoOfVar_EVI_Hetero_1km': 'K', 'Contrast_EVI_Hetero_1km': 'L', 'CorFragVolPerc_15cm': 'M', 'Correlation_EVI_Hetero_1km': 'N', 'Depth_to_Bedrock': 'O', 'Dissimilarity_EVI_Hetero_1km': 'P', 'EVI': 'Q', 'Entropy_EVI_Hetero_1km': 'R', 'Evenness_EVI_Hetero_1km': 'S', 'Fpar': 'T', 'Fungivores': 'U', 'Global_Biomass_IPCC': 'V', 'Gpp': 'W', 'Highest_Weekly_Radiation': 'X', 'Homogeneity_EVI_Hetero_1km': 'Y', 'Human_Development': 'Z', 'Isothermality': 'AA', 'Lai': 'AB', 'Lowest_Weekly_Radiation': 'AC', 'Max_Temperature_of_Warmest_Month': 'AD', 'Maximum_EVI_Hetero_1km': 'AE', 'Mean_Diurnal_Range': 'AF', 'Mean_Temperature_of_Coldest_Quarter': 'AG', 'Mean_Temperature_of_Driest_Quarter': 'AH', 'Mean_Temperature_of_Warmest_Quarter': 'AI', 'Mean_Temperature_of_Wettest_Quarter': 'AJ', 'Min_Temperature_of_Coldest_Month': 'AK', 'NDVI': 'AL', 'Nadir_Reflectance_Band1': 'AM', 'Nadir_Reflectance_Band2': 'AN', 'Nadir_Reflectance_Band3': 'AO', 'Nadir_Reflectance_Band4': 'AP', 'Nadir_Reflectance_Band5': 'AQ', 'Nadir_Reflectance_Band6': 'AR', 'Nadir_Reflectance_Band7': 'AS', 'Npp': 'AT', 'Omnivores': 'AU', 'OrgCStockTHa_0to15cm': 'AV', 'PET': 'AW', 'Pixel_Lat': 'AX', 'Pixel_Long': 'AY', 'Plant_feed': 'AZ', 'Population_Density': 'BA', 'Precipitation_Seasonality': 'BB', 'Precipitation_of_Coldest_Quarter': 'BC', 'Precipitation_of_Driest_Month': 'BD', 'Precipitation_of_Driest_Quarter': 'BE', 'Precipitation_of_Warmest_Quarter': 'BF', 'Precipitation_of_Wettest_Month': 'BG', 'Precipitation_of_Wettest_Quarter': 'BH', 'PredProb_of_R_Horizon': 'BI', 'Predators': 'BJ', 'Radiation_Seasonality': 'BK', 'Radiation_of_Coldest_Quarter': 'BL', 'Radiation_of_Driest_Quarter': 'BM', 'Radiation_of_Warmest_Quarter': 'BN', 'Radiation_of_Wettest_Quarter': 'BO', 'Range_EVI_Hetero_1km': 'BP', 'Sand_Content_15cm': 'BQ', 'Shannon_Index_1km': 'BR', 'Silt_Content_15cm': 'BS', 'Simpson_Index_1km': 'BT', 'Std_EVI_Hetero_1km': 'BU', 'Temperature_Annual_Range': 'BV', 'Temperature_Seasonality': 'BW', 'Total_Numb': 'BX', 'Unidentifi': 'BY', 'Uniformity_EVI_Hetero_1km': 'BZ', 'Variance_EVI_Hetero_1km': 'CA', 'eastness': 'CB', 'elevation': 'CC', 'hillshade': 'CD', 'northness': 'CE', 'pHinHOX_15cm': 'CF', 'slope': 'CG'}\n"
]
}
],
"source": [
"# Make an exportable dictionary of band/column names\n",
"keys = list(dataOI.columns)\n",
"values = newColNames\n",
"bandColNameDictionary = dict(zip(keys, values))\n",
"print(bandColNameDictionary)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1874, 85)\n",
"Index(['Abs_Lat', 'Annual_Mean_Radiation', 'Annual_Mean_Temperature',\n",
" 'Annual_Precipitation', 'Aridity_Index', 'Bacterivor',\n",
" 'Bulk_Density_15cm', 'CContent_15cm', 'CatIonExcCap_15cm',\n",
" 'Clay_Content_15cm', 'CoOfVar_EVI_Hetero_1km',\n",
" 'Contrast_EVI_Hetero_1km', 'CorFragVolPerc_15cm',\n",
" 'Correlation_EVI_Hetero_1km', 'Depth_to_Bedrock',\n",
" 'Dissimilarity_EVI_Hetero_1km', 'EVI', 'Entropy_EVI_Hetero_1km',\n",
" 'Evenness_EVI_Hetero_1km', 'Fpar', 'Fungivores', 'Global_Biomass_IPCC',\n",
" 'Gpp', 'Highest_Weekly_Radiation', 'Homogeneity_EVI_Hetero_1km',\n",
" 'Human_Development', 'Isothermality', 'Lai', 'Lowest_Weekly_Radiation',\n",
" 'Max_Temperature_of_Warmest_Month', 'Maximum_EVI_Hetero_1km',\n",
" 'Mean_Diurnal_Range', 'Mean_Temperature_of_Coldest_Quarter',\n",
" 'Mean_Temperature_of_Driest_Quarter',\n",
" 'Mean_Temperature_of_Warmest_Quarter',\n",
" 'Mean_Temperature_of_Wettest_Quarter',\n",
" 'Min_Temperature_of_Coldest_Month', 'NDVI', 'Nadir_Reflectance_Band1',\n",
" 'Nadir_Reflectance_Band2', 'Nadir_Reflectance_Band3',\n",
" 'Nadir_Reflectance_Band4', 'Nadir_Reflectance_Band5',\n",
" 'Nadir_Reflectance_Band6', 'Nadir_Reflectance_Band7', 'Npp',\n",
" 'Omnivores', 'OrgCStockTHa_0to15cm', 'PET', 'Pixel_Lat', 'Pixel_Long',\n",
" 'Plant_feed', 'Population_Density', 'Precipitation_Seasonality',\n",
" 'Precipitation_of_Coldest_Quarter', 'Precipitation_of_Driest_Month',\n",
" 'Precipitation_of_Driest_Quarter', 'Precipitation_of_Warmest_Quarter',\n",
" 'Precipitation_of_Wettest_Month', 'Precipitation_of_Wettest_Quarter',\n",
" 'PredProb_of_R_Horizon', 'Predators', 'Radiation_Seasonality',\n",
" 'Radiation_of_Coldest_Quarter', 'Radiation_of_Driest_Quarter',\n",
" 'Radiation_of_Warmest_Quarter', 'Radiation_of_Wettest_Quarter',\n",
" 'Range_EVI_Hetero_1km', 'Sand_Content_15cm', 'Shannon_Index_1km',\n",
" 'Silt_Content_15cm', 'Simpson_Index_1km', 'Std_EVI_Hetero_1km',\n",
" 'Temperature_Annual_Range', 'Temperature_Seasonality', 'Total_Numb',\n",
" 'Unidentifi', 'Uniformity_EVI_Hetero_1km', 'Variance_EVI_Hetero_1km',\n",
" 'eastness', 'elevation', 'hillshade', 'northness', 'pHinHOX_15cm',\n",
" 'slope'],\n",
" dtype='object')\n",
" Abs_Lat Annual_Mean_Radiation Annual_Mean_Temperature \\n",
"0 22.437500 140.071213 18.141666 \n",
"1 22.037500 143.425690 19.049999 \n",
"2 21.304167 152.697372 19.695833 \n",
"3 21.320833 151.420517 19.487499 \n",
"4 21.445833 149.989777 18.904167 \n",
"\n",
" Annual_Precipitation Aridity_Index Bacterivor Bulk_Density_15cm \\n",
"0 1519.0 10354.312500 120.0 1236.0 \n",
"1 1528.0 9590.197266 45.0 1283.0 \n",
"2 1614.0 10367.072266 390.0 1334.0 \n",
"3 1619.0 10574.904297 224.0 1317.0 \n",
"4 1616.0 10974.227539 40.0 1239.0 \n",
"\n",
" CContent_15cm CatIonExcCap_15cm Clay_Content_15cm ... Total_Numb \\n",
"0 25.0 12.0 34.0 ... 482.0 \n",
"1 27.0 9.0 40.0 ... 285.0 \n",
"2 19.0 7.0 37.0 ... 1650.0 \n",
"3 17.0 7.0 36.0 ... 636.0 \n",
"4 27.0 8.0 35.0 ... 470.0 \n",
"\n",
" Unidentifi Uniformity_EVI_Hetero_1km Variance_EVI_Hetero_1km eastness \\n",
"0 0.0 756.0 70497.0 0.678042 \n",
"1 0.0 1792.0 29684.0 0.207282 \n",
"2 0.0 618.0 67425.0 -0.168783 \n",
"3 0.0 514.0 136302.0 -0.194088 \n",
"4 0.0 538.0 716117.0 0.255504 \n",
"\n",
" elevation hillshade northness pHinHOX_15cm slope \n",
"0 901.304993 157.524994 0.121048 52.0 9.735556 \n",
"1 976.426392 171.230835 -0.064766 53.0 7.336111 \n",
"2 928.925537 181.953613 -0.105951 52.0 2.933611 \n",
"3 986.726379 183.036667 -0.575699 52.0 3.099444 \n",
"4 1141.481079 171.678604 -0.821737 51.0 10.041111 \n",
"\n",
"[5 rows x 85 columns]\n",
"(1874, 85)\n",
"Index(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',\n",
" 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB',\n",
" 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN',\n",
" 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ',\n",
" 'BA', 'BB', 'BC', 'BD', 'BE', 'BF', 'BG', 'BH', 'BI', 'BJ', 'BK', 'BL',\n",
" 'BM', 'BN', 'BO', 'BP', 'BQ', 'BR', 'BS', 'BT', 'BU', 'BV', 'BW', 'BX',\n",
" 'BY', 'BZ', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG'],\n",
" dtype='object')\n",
" A B C D E F G \\n",
"0 22.437500 140.071213 18.141666 1519.0 10354.312500 120.0 1236.0 \n",
"1 22.037500 143.425690 19.049999 1528.0 9590.197266 45.0 1283.0 \n",
"2 21.304167 152.697372 19.695833 1614.0 10367.072266 390.0 1334.0 \n",
"3 21.320833 151.420517 19.487499 1619.0 10574.904297 224.0 1317.0 \n",
"4 21.445833 149.989777 18.904167 1616.0 10974.227539 40.0 1239.0 \n",
"\n",
" H I J ... BX BY BZ CA CB \\n",
"0 25.0 12.0 34.0 ... 482.0 0.0 756.0 70497.0 0.678042 \n",
"1 27.0 9.0 40.0 ... 285.0 0.0 1792.0 29684.0 0.207282 \n",
"2 19.0 7.0 37.0 ... 1650.0 0.0 618.0 67425.0 -0.168783 \n",
"3 17.0 7.0 36.0 ... 636.0 0.0 514.0 136302.0 -0.194088 \n",
"4 27.0 8.0 35.0 ... 470.0 0.0 538.0 716117.0 0.255504 \n",
"\n",
" CC CD CE CF CG \n",
"0 901.304993 157.524994 0.121048 52.0 9.735556 \n",
"1 976.426392 171.230835 -0.064766 53.0 7.336111 \n",
"2 928.925537 181.953613 -0.105951 52.0 2.933611 \n",
"3 986.726379 183.036667 -0.575699 52.0 3.099444 \n",
"4 1141.481079 171.678604 -0.821737 51.0 10.041111 \n",
"\n",
"[5 rows x 85 columns]\n"
]
}
],
"source": [
"# Rename the columns with placeholders\n",
"renamedData = dataOI.rename(index=str, columns=bandColNameDictionary)\n",
"\n",
"print(dataOI.shape)\n",
"print(dataOI.columns)\n",
"print(dataOI.head(5))\n",
"\n",
"print(renamedData.shape)\n",
"print(renamedData.columns)\n",
"print(renamedData.head(5))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# Export the CSV to convert into a ShapeFile\n",
"renamedData.to_csv('20180706_Nematode_SampledData_ColNamePlaceHold.csv',index=False)"
]
}
],
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}