formatted code blocks and captions

This commit is contained in:
Ollie Ballinger
2022-12-21 16:47:12 +00:00
parent 11c23b069a
commit 859e9c3ce2
15 changed files with 16426 additions and 15141 deletions

View File

@@ -2,46 +2,812 @@
"cells": [
{
"cell_type": "code",
"execution_count": 110,
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"58\n",
"['\\n///// ', '\\n// Band Arithmetic ', '\\n///// ', '\\n// Calculate NDVI using Sentinel 2 ', '\\n// Import and filter imagery by location and date. ', '\\n// Display the image as a false color composite. ', '\\n// Extract the near infrared and red bands. ', '\\n// Calculate the numerator and the denominator using subtraction and addition respectively. ', '\\n// Now calculate NDVI. ', '\\n// Add the layer to our map with a palette. ', '\\n// Now use the built-in normalizedDifference function to achieve the same outcome. ', '\\n// Use normalizedDifference to calculate NDWI ', '\\n// Create an NDVI image using Sentinel 2. ', '\\n// And map it. ', '\\n// Implement a threshold. ', '\\n// Map the threshold. ', '\\n// Implement .where. ', '\\n// Create a starting image with all values = 1. ', '\\n// Make all NDVI values less than -0.1 equal 0. ', '\\n// Make all NDVI values greater than 0.5 equal 2. ', '\\n// Map our layer that has been divided into three classes. ', '\\n// Implement masking. ', \"\\n// View the seaVeg layer's current mask. \", '\\n// Create a binary mask of non-forest. ', '\\n// Update the seaVeg mask with the non-forest mask. ', '\\n// Map the updated Veg layer ', '\\n// Map the updated mask ', '\\n// Implement remapping. ', '\\n// Remap the values from the seaWhere layer. ', '\\n// Create an Earth Engine Point object over Milan. ', '\\n// Filter the Landsat 8 collection and select the least cloudy image. ', '\\n// Center the map on that image. ', '\\n// Add Landsat image to the map. ', '\\n// Combine training feature collections. ', '\\n// Define prediction bands. ', '\\n// Sample training points. ', '\\n//////////////// CART Classifier /////////////////// ', '\\n// Train a CART Classifier. ', '\\n// Classify the Landsat image. ', '\\n// Define classification image visualization parameters. ', '\\n// Add the classified image to the map. ', '\\n/////////////// Random Forest Classifier ///////////////////// ', '\\n// Train RF classifier. ', '\\n// Classify Landsat image. ', '\\n// Add classified image to the map. ', '\\n//////////////// Unsupervised classification //////////////// ', '\\n// Make the training dataset. ', '\\n// Instantiate the clusterer and train it. ', '\\n// Cluster the input using the trained clusterer. ', '\\n// Display the clusters with random colors. ', '\\n// Import the reference dataset. ', '\\n// Define the prediction bands. ', '\\n// Split the dataset into training and testing sets. ', '\\n// Train the Random Forest Classifier with the trainingSet. ', \"\\n// Now, to test the classification (verify model's accuracy), \", '\\n// we classify the testingSet and get a confusion matrix. ', '\\n// Print the results. ', '\\n// Hyperparameter tuning. ']\n"
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"0\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"0\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"5\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"y\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"]\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"0\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"0\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"1\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"r\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"0\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"'\n",
"--------------------------------------------------\n",
"}\n",
"--------------------------------------------------\n",
")\n",
"--------------------------------------------------\n",
")\n"
]
}
],
"source": [
"import re\n",
"\n",
"def captions(f):\n",
" regex=r'(!\\[\\].+\\))\\n(\\nFig\\..*)'\n",
"def captions(f): # add captions to figures\n",
" regex=r'(!\\[\\].+\\))\\n(\\nFig\\..*)' # find figures\n",
"\n",
" figures=re.findall(regex, f)\n",
" print(len(figures))\n",
" sub=[]\n",
" for fig in figures:\n",
" formatted=fig[0].replace('[]','[{}]'.format(fig[1].replace('\\n','')))\n",
" f=f.replace(fig[0],formatted)\n",
" f=f.replace(fig[1],\"\")\n",
" figures=re.findall(regex, f) # find figures\n",
" print(len(figures)) # number of figures\n",
" sub=[] # list of figures with captions\n",
" for fig in figures: # for each figure\n",
" formatted=fig[0].replace('[]','[{}]'.format(fig[1].replace('\\n',''))) # add caption\n",
" f=f.replace(fig[0],formatted) # replace figure with formatted figure\n",
" f=f.replace(fig[1],\"\") # remove caption\n",
" return f\n",
"\n",
"def codeblocks(f):\n",
" \n",
" regex=r'(\\n//.*)'\n",
" code=re.findall(regex, f)\n",
" print(len(code))\n",
" print(code)\n",
" regex = r\"^\\/\\/.*(\\n|.)*?;\"\n",
" pattern = re.compile(regex, re.MULTILINE)\n",
" matches = pattern.findall(f)\n",
" for match in matches:\n",
" print('-'*50)\n",
" print(match)\n",
" return f\n",
"\n",
"for i in [2]:#,4,5,6]:\n",
"for i in [2,4,5,6]:\n",
" f = open(\"F{}.qmd\".format(i), \"r\").read().replace('\\xa0', ' ')\n",
" #f = captions(f)\n",
" f = codeblocks(f)\n",
"\n"
" #text_file = open(\"F{}.qmd\".format(i), \"w\")\n",
" #n = text_file.write(f)\n",
" #text_file.close()\n",
" f = codeblocks(f) "
]
},
{