diff --git a/data/notebooks/pipes_mock/precalc.ipynb b/data/notebooks/pipes_mock/precalc.ipynb index eea60a54..14ba8db8 100644 --- a/data/notebooks/pipes_mock/precalc.ipynb +++ b/data/notebooks/pipes_mock/precalc.ipynb @@ -93,9 +93,20 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "# Strapi setup\n", "strapi = Strapi(url=mysettings.STRAPI_URL)\n", @@ -104,7 +115,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -800,7 +811,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -834,7 +845,144 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/tmp/ipykernel_49/4089503573.py:1: UserWarning: Boolean Series key will be reindexed to match DataFrame index.\n", + " protectedseas_intermediate[\n" + ] + }, + { + "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", + "
iso_teriso_sovincludes_multi_jurisdictional_areaslfparea_sqkmtotal_areapct_total
320NaNESPTrue5142.9730101011023.7760.014141
321NaNESPTrue41639.6820761011023.7760.162180
322NaNESPTrue3214532.8498001011023.77621.219367
323NaNESPTrue215064.1327701011023.7761.489988
324NaNESPTrue1779644.1388001011023.77677.114323
\n", + "
" + ], + "text/plain": [ + " iso_ter iso_sov includes_multi_jurisdictional_areas lfp area_sqkm \\\n", + "320 NaN ESP True 5 142.973010 \n", + "321 NaN ESP True 4 1639.682076 \n", + "322 NaN ESP True 3 214532.849800 \n", + "323 NaN ESP True 2 15064.132770 \n", + "324 NaN ESP True 1 779644.138800 \n", + "\n", + " total_area pct_total \n", + "320 1011023.776 0.014141 \n", + "321 1011023.776 0.162180 \n", + "322 1011023.776 21.219367 \n", + "323 1011023.776 1.489988 \n", + "324 1011023.776 77.114323 " + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "protectedseas_intermediate[\n", + " (\n", + " protectedseas_intermediate.iso_ter.isna()\n", + " & protectedseas_intermediate.includes_multi_jurisdictional_areas.eq(True)\n", + " )\n", + " | (\n", + " protectedseas_intermediate.iso_ter.isna()\n", + " & protectedseas_intermediate.includes_multi_jurisdictional_areas.eq(False)\n", + " & ~protectedseas_intermediate.iso_sov.isin(\n", + " protectedseas_intermediate[\n", + " protectedseas_intermediate.includes_multi_jurisdictional_areas.eq(True)\n", + " ].iso_sov.unique()\n", + " )\n", + " )\n", + "][protectedseas_intermediate.iso_sov.eq(\"ESP\")]" + ] + }, + { + "cell_type": "code", + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -848,17 +996,22 @@ ], "source": [ "final = (\n", - " protectedseas_intermediate.replace(\n", + " protectedseas_intermediate[\n", + " (\n", + " protectedseas_intermediate.iso_ter.isna()\n", + " & protectedseas_intermediate.includes_multi_jurisdictional_areas.eq(True)\n", + " )\n", + " | (\n", + " protectedseas_intermediate.iso_ter.isna()\n", + " & protectedseas_intermediate.includes_multi_jurisdictional_areas.eq(False)\n", + " & ~protectedseas_intermediate.iso_sov.isin(\n", + " protectedseas_intermediate[\n", + " protectedseas_intermediate.includes_multi_jurisdictional_areas.eq(True)\n", + " ].iso_sov.unique()\n", + " )\n", + " )\n", + " ].replace(\n", " {\n", - " \"iso_sov\": {\n", - " \"COK\": \"NZL\",\n", - " \"IOT\": \"GBR\",\n", - " \"NIU\": \"NZL\",\n", - " \"SHN\": \"GBR\",\n", - " \"SJM\": \"NOR\",\n", - " \"UMI\": \"USA\",\n", - " \"NCL\": \"FRA\",\n", - " },\n", " \"lfp\": {\n", " 5: \"highly\",\n", " 4: \"highly\",\n", @@ -867,21 +1020,21 @@ " 1: \"less\",\n", " },\n", " }\n", - " )\n", + " ).groupby([\"iso_sov\", \"lfp\"]).agg({\"area_sqkm\": \"sum\", \"total_area\": \"max\"}).reset_index()\n", " .pipe(\n", " calculate_global_area,\n", " gby_col=[\"lfp\"],\n", " iso_column=\"iso_sov\",\n", - " agg_ops={\"area_sqkm\": \"sum\", \"total_area\": \"max\"},\n", + " agg_ops={\"area_sqkm\": \"sum\", \"total_area\": \"sum\"},\n", " )\n", " .pipe(add_region_iso, iso_column=\"iso_sov\")\n", " .pipe(\n", " calculate_stats,\n", " gby_col=[\"lfp\"],\n", - " ops={\"area_sqkm\": \"sum\", \"total_area\": \"max\"},\n", + " ops={\"area_sqkm\": \"sum\", \"total_area\": \"sum\"},\n", " iso_column=\"iso_sov\",\n", " )\n", - " .pipe(lambda x: x.assign(pct=round(x.area_sqkm / x.total_area, 2)))\n", + " .pipe(lambda x: x.assign(pct=round((x.area_sqkm / x.total_area)*100, 2)))\n", " .pipe(\n", " output,\n", " iso_column=\"iso_sov\",\n", @@ -901,18 +1054,43 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 55, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 55, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "strapi.deleteCollectionData(strapi_collection, list(range(1, 300)))" + "strapi.deleteCollectionData(strapi_collection, list(range(1, 500)))" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 56, "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "HTTPError", + "evalue": "401 Client Error: Unauthorized for url: https://30x30-dev.skytruth.org/cms/api/import-export-entries/content/import", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mHTTPError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[56], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mstrapi\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimportCollectionData\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 2\u001b[0m \u001b[43m \u001b[49m\u001b[43mstrapi_collection\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 3\u001b[0m \u001b[43m \u001b[49m\u001b[43moutput_file\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m)\u001b[49m\n", + "File \u001b[0;32m~/src/helpers/strapi.py:101\u001b[0m, in \u001b[0;36mStrapi.importCollectionData\u001b[0;34m(self, collectionApiID, file_path, idField)\u001b[0m\n\u001b[1;32m 90\u001b[0m data \u001b[38;5;241m=\u001b[39m f\u001b[38;5;241m.\u001b[39mread()\n\u001b[1;32m 92\u001b[0m response \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39msession\u001b[38;5;241m.\u001b[39mpost(\n\u001b[1;32m 93\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39murl\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m/api/import-export-entries/content/import\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 94\u001b[0m json\u001b[38;5;241m=\u001b[39m{\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 99\u001b[0m },\n\u001b[1;32m 100\u001b[0m )\n\u001b[0;32m--> 101\u001b[0m \u001b[43mresponse\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mraise_for_status\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 102\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\n", + "File \u001b[0;32m/opt/conda/lib/python3.12/site-packages/requests/models.py:1024\u001b[0m, in \u001b[0;36mResponse.raise_for_status\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1019\u001b[0m http_error_msg \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 1020\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mstatus_code\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m Server Error: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mreason\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m for url: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39murl\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1021\u001b[0m )\n\u001b[1;32m 1023\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m http_error_msg:\n\u001b[0;32m-> 1024\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m HTTPError(http_error_msg, response\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m)\n", + "\u001b[0;31mHTTPError\u001b[0m: 401 Client Error: Unauthorized for url: https://30x30-dev.skytruth.org/cms/api/import-export-entries/content/import" + ] + } + ], "source": [ "strapi.importCollectionData(\n", " strapi_collection,\n",