Skip to content

Commit

Permalink
[Update] Updating exploring the data notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
jackdmarquez committed Aug 26, 2024
1 parent d514062 commit 79cf308
Showing 1 changed file with 292 additions and 47 deletions.
339 changes: 292 additions & 47 deletions hands-on/session II/2.Explore_Data.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "250ef129-c335-4f6c-97ea-afc865ca2fda",
"metadata": {},
"outputs": [],
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have successfully prepared your environment.\n"
]
}
],
"source": [
"import os\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
Expand All @@ -44,14 +55,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "f1fa2a34-486d-46fa-b370-4ddcf2572f02",
"metadata": {},
"outputs": [],
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have successfully upladed your file.\n"
]
}
],
"source": [
"# Change the name of data file with the name of your downoaded subregion\n",
"data_file = \"Enter the name of your file\"\n",
"print(\"You have successfully named your data file.\")"
"# Change the name of data file with the name of your downloaded subregion\n",
"data_file = \"enter_the_name.here\"\n"
]
},
{
Expand All @@ -66,16 +86,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "860bbfe3-fe43-4a7a-b937-e801b3fd437e",
"metadata": {},
"outputs": [],
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"You have successfully loaded your data and metadata.\n"
]
}
],
"source": [
"data = np.load(data_file)\n",
"data\n",
"actual_data = data[\"data\"]\n",
"metadata = data[\"lon_lat\"]\n",
"print(\"You have successfully loaded your data and metadata.\")"
"try:\n",
" # Attempt to load the .npz data file\n",
" # This file should contain at least two arrays: 'data' and 'lon_lat'\n",
" data = np.load(data_file)\n",
"\n",
" # Extract the actual data and metadata from the file\n",
" # 'data' contains the selected terrain parameter values, and 'lon_lat' contains the geographical boundaries\n",
" actual_data = data[\"data\"]\n",
" metadata = data[\"lon_lat\"]\n",
" \n",
" print(\"You have successfully loaded your data and metadata.\")\n",
"except Exception as e:\n",
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
]
},
{
Expand All @@ -85,50 +123,257 @@
"source": [
"## Visualizing the Subregion Data\n",
"\n",
"The following cell plots the subregion."
"### Challenge I\n",
"\n",
"(1) Load the downloaded subregion of interest in your local machine\n",
"\n",
"(2) Compute min, max, average and std elevation"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3e6f6b65-5cb4-4515-b163-67229880b966",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" \n",
" # Choose a colormap for visualizing the data\n",
" # The colormap 'inferno' is used here, which is a perceptually uniform colormap\n",
" cmap_instance = plt.get_cmap(\"inferno\")\n",
" \n",
" # Extract the latitude and longitude boundaries from the metadata\n",
" lat_min = metadata[0][0] # Minimum latitude\n",
" lat_max = metadata[0][1] # Maximum latitude\n",
" lon_min = metadata[1][0] # Minimum longitude\n",
" lon_max = metadata[1][1] # Maximum longitude\n",
"\n",
" # Plot the figure with a default range\n",
" # The default range is determined by the full range of the colormap without considering the data limits\n",
" fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
" axs.set_xlim(lat_min, lat_max)\n",
" axs.set_ylim(lon_min, lon_max)\n",
" axs.set_title(\"Selected Subregion Of Interest (Default Range)\")\n",
" axs.set_xlabel(\"Longitude (Degrees)\")\n",
" axs.set_ylabel(\"Latitude (Degrees)\")\n",
" \n",
" # Use imshow with a default color range (use the default colormap range)\n",
" data_fig = axs.imshow(\n",
" actual_data,\n",
" cmap=cmap_instance,\n",
" origin=\"lower\",\n",
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
" )\n",
" \n",
" # Add a colorbar to the plot\n",
" cbar = fig.colorbar(\n",
" data_fig,\n",
" ax=axs,\n",
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
" pad=0.04,\n",
" )\n",
" \n",
" print(\"You have successfully plotted your data with the default range.\")\n",
" \n",
" # Display the plot\n",
" plt.show()\n",
"\n",
"except Exception as e:\n",
" # Handle any errors that occur during data loading or processing\n",
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
]
},
{
"cell_type": "markdown",
"id": "57fabdc2",
"metadata": {},
"source": [
"### Challenge II\n",
"\n",
"(1) Load the downloaded subregion of interest in your local machine\n",
"\n",
"(2) Compute min, max, average and std elevation\n",
"\n",
"(3) Set the color bar to reflect the range of displayed data, from the minimum to the maximum value, providing a more accurate visual representation of the data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4e549d95",
"metadata": {},
"outputs": [],
"source": [
"cmap_instance = plt.get_cmap(\"inferno\")\n",
"lat_min = metadata[0][0]\n",
"lat_max = metadata[0][1]\n",
"lon_min = metadata[1][0]\n",
"lon_max = metadata[1][1]\n",
"fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
"axs.set_xlim(lat_min, lat_max)\n",
"axs.set_ylim(lon_min, lon_max)\n",
"axs.set_title(\"Selected Subregion Of Interest\")\n",
"axs.set_xlabel(\"Longitude (Degrees)\")\n",
"axs.set_ylabel(\"Latitude (Degrees)\")\n",
"data_fig = axs.imshow(\n",
" actual_data,\n",
" cmap=cmap_instance,\n",
" vmin=actual_data.min(),\n",
" vmax=actual_data.max(),\n",
" origin=\"lower\",\n",
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
")\n",
"\n",
"cbar = fig.colorbar(\n",
" data_fig,\n",
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
" pad=0.04,\n",
")\n",
"cbar_ticks = np.linspace(actual_data.min(), actual_data.max(), 8)\n",
"cbar.set_ticks(cbar_ticks)\n",
"print(\"You have successfully plotted your data.\")"
"\n",
"try:\n",
" \n",
" # Choose a colormap for visualizing the data\n",
" # The colormap 'inferno' is used here, which is a perceptually uniform colormap\n",
" cmap_instance = plt.get_cmap(\"inferno\")\n",
" \n",
" # Extract the latitude and longitude boundaries from the metadata\n",
" lat_min = metadata[0][0] # Minimum latitude\n",
" lat_max = metadata[0][1] # Maximum latitude\n",
" lon_min = metadata[1][0] # Minimum longitude\n",
" lon_max = metadata[1][1] # Maximum longitude\n",
"\n",
" print(\"You have successfully plotted your data with the default range.\")\n",
"\n",
" # Replot the figure with the actual min and max range\n",
" # Calculate the min and max values from the data\n",
" vmin = actual_data.min()\n",
" vmax = actual_data.max()\n",
" \n",
" fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
" axs.set_xlim(lat_min, lat_max)\n",
" axs.set_ylim(lon_min, lon_max)\n",
" axs.set_title(\"Selected Subregion Of Interest (Min-Max Range)\")\n",
" axs.set_xlabel(\"Longitude (Degrees)\")\n",
" axs.set_ylabel(\"Latitude (Degrees)\")\n",
" \n",
" # Use imshow with the calculated min and max range\n",
" data_fig = axs.imshow(\n",
" actual_data,\n",
" cmap=cmap_instance,\n",
" vmin=vmin,\n",
" vmax=vmax,\n",
" origin=\"lower\",\n",
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
" )\n",
"\n",
" # Add a colorbar with the min-max range\n",
" cbar = fig.colorbar(\n",
" data_fig,\n",
" ax=axs,\n",
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
" pad=0.04,\n",
" )\n",
" \n",
" # Set the ticks for the colorbar\n",
" cbar_ticks = np.linspace(vmin, vmax, 8)\n",
" cbar.set_ticks(cbar_ticks)\n",
" \n",
" print(\"You have successfully replotted your data with the min-max range.\")\n",
" \n",
" # Calculate statistical values for the data\n",
" max_slope = vmax # Maximum value in the data\n",
" min_slope = vmin # Minimum value in the data\n",
" avg_slope = actual_data.mean() # Average value of the data\n",
" std_slope = actual_data.std() # Standard deviation of the data\n",
" \n",
" # Print the calculated statistical values\n",
" print(f\"Maximum slope value: {max_slope}\")\n",
" print(f\"Minimum slope value: {min_slope}\")\n",
" print(f\"Average slope value: {avg_slope}\")\n",
" print(f\"Standard deviation of slope values: {std_slope}\")\n",
"\n",
" # Display the plot with min-max range\n",
" plt.show()\n",
"\n",
"except Exception as e:\n",
" # Handle any errors that occur during data loading or processing\n",
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
]
},
{
"cell_type": "markdown",
"id": "faa74642-ac27-42fd-8be3-4d0fe23fa0e5",
"metadata": {},
"source": [
"### Challenge III\n",
"\n",
"(1) Load the downloaded subregion of interest in your local machine\n",
"\n",
"(2) Compute min, max, average and std elevation\n",
"\n",
"(3) Set the color bar to reflect the range of displayed data, from the 0 to the a prefered value (e.g., 600), providing a more accurate visual representation of the data "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b3ac27e8",
"metadata": {},
"outputs": [],
"source": [
"try:\n",
" \n",
" # Choose a colormap for visualizing the data\n",
" # The colormap 'inferno' is used here, which is a perceptually uniform colormap\n",
" cmap_instance = plt.get_cmap(\"inferno\")\n",
" \n",
" # Extract the latitude and longitude boundaries from the metadata\n",
" lat_min = metadata[0][0] # Minimum latitude\n",
" lat_max = metadata[0][1] # Maximum latitude\n",
" lon_min = metadata[1][0] # Minimum longitude\n",
" lon_max = metadata[1][1] # Maximum longitude\n",
"\n",
"\n",
" # Replot the figure with a fixed range from 0 to 3000\n",
" # This range is arbitrary and meant to highlight different data ranges\n",
" fixed_vmin = 0\n",
" fixed_vmax = 600\n",
" \n",
" fig, axs = plt.subplots(1, 1, figsize=(10, 8))\n",
" axs.set_xlim(lat_min, lat_max)\n",
" axs.set_ylim(lon_min, lon_max)\n",
" axs.set_title(\"Selected Subregion Of Interest (Range 0 to 3000)\")\n",
" axs.set_xlabel(\"Longitude (Degrees)\")\n",
" axs.set_ylabel(\"Latitude (Degrees)\")\n",
" \n",
" # Use imshow with the fixed range from 0 to 3000\n",
" data_fig = axs.imshow(\n",
" actual_data,\n",
" cmap=cmap_instance,\n",
" vmin=fixed_vmin,\n",
" vmax=fixed_vmax,\n",
" origin=\"lower\",\n",
" extent=(lat_min, lat_max, lon_min, lon_max),\n",
" )\n",
"\n",
" # Add a colorbar with the fixed range\n",
" cbar = fig.colorbar(\n",
" data_fig,\n",
" ax=axs,\n",
" fraction=0.046 * actual_data.shape[0] / actual_data.shape[1],\n",
" pad=0.04,\n",
" )\n",
" \n",
" # Set the ticks for the colorbar\n",
" cbar_ticks = np.linspace(fixed_vmin, fixed_vmax, 8)\n",
" cbar.set_ticks(cbar_ticks)\n",
" \n",
" print(\"You have successfully replotted your data with the range 0 to 3000.\")\n",
" \n",
" # Calculate statistical values for the data\n",
" max_slope = vmax # Maximum value in the data\n",
" min_slope = vmin # Minimum value in the data\n",
" avg_slope = actual_data.mean() # Average value of the data\n",
" std_slope = actual_data.std() # Standard deviation of the data\n",
" \n",
" # Print the calculated statistical values\n",
" print(f\"Maximum slope value: {max_slope}\")\n",
" print(f\"Minimum slope value: {min_slope}\")\n",
" print(f\"Average slope value: {avg_slope}\")\n",
" print(f\"Standard deviation of slope values: {std_slope}\")\n",
"\n",
" # Display the plot with the fixed range\n",
" plt.show()\n",
"\n",
"except Exception as e:\n",
" # Handle any errors that occur during data loading or processing\n",
" print(f\"Error: Failed to load or process data from '{data_file}'. {str(e)}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ab58bc8b",
"metadata": {},
"outputs": [],
"source": []
}
],
Expand All @@ -148,7 +393,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 79cf308

Please sign in to comment.