From c23af154ed54305feceac9801c468b3c28198d0e Mon Sep 17 00:00:00 2001 From: Zachary Deziel Date: Thu, 21 Nov 2024 09:36:29 +0000 Subject: [PATCH] Update notebook based on nbqa --- notebooks/user-docs/s2s_python_lib.ipynb | 57 +++++++++++++----------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/notebooks/user-docs/s2s_python_lib.ipynb b/notebooks/user-docs/s2s_python_lib.ipynb index f39d6d2..ced1148 100644 --- a/notebooks/user-docs/s2s_python_lib.ipynb +++ b/notebooks/user-docs/s2s_python_lib.ipynb @@ -23,17 +23,16 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Dict\n", "import json\n", + "from typing import Dict\n", "\n", + "import geopandas as gpd\n", "import numpy as np\n", - "from dotenv import load_dotenv\n", "import pandas as pd\n", - "import geopandas as gpd\n", - "from shapely.geometry import shape\n", - "from lonboard import Map, ScatterplotLayer\n", + "from dotenv import load_dotenv\n", "from geojson_pydantic import Feature, Polygon\n", - "\n", + "from lonboard import Map, ScatterplotLayer\n", + "from shapely.geometry import shape\n", "from space2stats import StatsTable" ] }, @@ -155,16 +154,13 @@ " [33.78593974945852, -4.725410543134203],\n", " [41.94362577283266, -4.725410543134203],\n", " [41.94362577283266, 5.115816884114494],\n", - " [33.78593974945852, 5.115816884114494]\n", + " [33.78593974945852, 5.115816884114494],\n", " ]\n", - " ]\n", + " ],\n", " },\n", - " \"properties\": {\n", - " \"name\": \"Updated AOI\"\n", - " }\n", + " \"properties\": {\"name\": \"Updated AOI\"},\n", "}\n", - " \n", - " \n", + "\n", "\n", "feat = AOIModel(**aoi)" ] @@ -397,7 +393,9 @@ ], "source": [ "with StatsTable.connect() as stats_table:\n", - " data = stats_table.summaries(aoi=feat, spatial_join_method=\"centroid\", fields=fields, geometry=\"point\")\n", + " data = stats_table.summaries(\n", + " aoi=feat, spatial_join_method=\"centroid\", fields=fields, geometry=\"point\"\n", + " )\n", " df = pd.DataFrame(data)\n", "\n", "df.head()" @@ -810,8 +808,8 @@ } ], "source": [ - "df['geometry'] = df['geometry'].apply(lambda geom: shape(json.loads(geom)))\n", - "gdf = gpd.GeoDataFrame(df, geometry='geometry', crs='EPSG:4326')\n", + "df[\"geometry\"] = df[\"geometry\"].apply(lambda geom: shape(json.loads(geom)))\n", + "gdf = gpd.GeoDataFrame(df, geometry=\"geometry\", crs=\"EPSG:4326\")\n", "gdf" ] }, @@ -839,26 +837,31 @@ "source": [ "# Define custom breaks and corresponding RGBA colors\n", "breaks = [0, 1, 1000, 10000, 50000, 100000, 200000, gdf[\"sum_pop_2020\"].max()]\n", - "colors = np.array([\n", - " [211, 211, 211, 255], # Light gray for 0\n", - " [255, 255, 0, 255], # Yellow for 1-1000\n", - " [255, 165, 0, 255], # Orange for 1000-10000\n", - " [255, 0, 0, 255], # Red for 10000-50000\n", - " [128, 0, 128, 255], # Purple for 50000-100000\n", - " [0, 0, 255, 255], # Blue for 100000-200000\n", - " [0, 0, 139, 255], # Dark blue for 200000+\n", - "])\n", + "colors = np.array(\n", + " [\n", + " [211, 211, 211, 255], # Light gray for 0\n", + " [255, 255, 0, 255], # Yellow for 1-1000\n", + " [255, 165, 0, 255], # Orange for 1000-10000\n", + " [255, 0, 0, 255], # Red for 10000-50000\n", + " [128, 0, 128, 255], # Purple for 50000-100000\n", + " [0, 0, 255, 255], # Blue for 100000-200000\n", + " [0, 0, 139, 255], # Dark blue for 200000+\n", + " ]\n", + ")\n", "\n", "# Function to assign colors based on custom bins\n", + "\n", + "\n", "def assign_color(value, breaks, colors):\n", " for i in range(len(breaks) - 1):\n", " if breaks[i] <= value < breaks[i + 1]:\n", " return colors[i]\n", " return colors[-1] # In case value exceeds all breaks\n", "\n", + "\n", "# Map sum_pop_2020 values to colors using the custom function\n", - "gdf['color'] = gdf[\"sum_pop_2020\"].apply(lambda x: assign_color(x, breaks, colors))\n", - "colors = np.uint8(gdf['color'].tolist())\n", + "gdf[\"color\"] = gdf[\"sum_pop_2020\"].apply(lambda x: assign_color(x, breaks, colors))\n", + "colors = np.uint8(gdf[\"color\"].tolist())\n", "\n", "# Create the scatterplot layer with the assigned colors\n", "layer = ScatterplotLayer.from_geopandas(gdf, get_radius=2000, get_fill_color=colors)\n",