Skip to content

Commit

Permalink
Merge pull request #296 from Vizzuality/fix/data/protectedseas
Browse files Browse the repository at this point in the history
fixed protected seas adding filter with only sovering data
  • Loading branch information
aagm authored Aug 28, 2024
2 parents 60e8799 + a9f9fb8 commit ca389f6
Showing 1 changed file with 202 additions and 24 deletions.
226 changes: 202 additions & 24 deletions data/notebooks/pipes_mock/precalc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,20 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<helpers.strapi.Strapi at 0x7f2ecc13ba10>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Strapi setup\n",
"strapi = Strapi(url=mysettings.STRAPI_URL)\n",
Expand All @@ -104,7 +115,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -800,7 +811,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -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": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>iso_ter</th>\n",
" <th>iso_sov</th>\n",
" <th>includes_multi_jurisdictional_areas</th>\n",
" <th>lfp</th>\n",
" <th>area_sqkm</th>\n",
" <th>total_area</th>\n",
" <th>pct_total</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>320</th>\n",
" <td>NaN</td>\n",
" <td>ESP</td>\n",
" <td>True</td>\n",
" <td>5</td>\n",
" <td>142.973010</td>\n",
" <td>1011023.776</td>\n",
" <td>0.014141</td>\n",
" </tr>\n",
" <tr>\n",
" <th>321</th>\n",
" <td>NaN</td>\n",
" <td>ESP</td>\n",
" <td>True</td>\n",
" <td>4</td>\n",
" <td>1639.682076</td>\n",
" <td>1011023.776</td>\n",
" <td>0.162180</td>\n",
" </tr>\n",
" <tr>\n",
" <th>322</th>\n",
" <td>NaN</td>\n",
" <td>ESP</td>\n",
" <td>True</td>\n",
" <td>3</td>\n",
" <td>214532.849800</td>\n",
" <td>1011023.776</td>\n",
" <td>21.219367</td>\n",
" </tr>\n",
" <tr>\n",
" <th>323</th>\n",
" <td>NaN</td>\n",
" <td>ESP</td>\n",
" <td>True</td>\n",
" <td>2</td>\n",
" <td>15064.132770</td>\n",
" <td>1011023.776</td>\n",
" <td>1.489988</td>\n",
" </tr>\n",
" <tr>\n",
" <th>324</th>\n",
" <td>NaN</td>\n",
" <td>ESP</td>\n",
" <td>True</td>\n",
" <td>1</td>\n",
" <td>779644.138800</td>\n",
" <td>1011023.776</td>\n",
" <td>77.114323</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"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": [
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -901,18 +1054,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 55,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<helpers.strapi.Strapi at 0x7f2ecc13ba10>"
]
},
"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",
Expand Down

0 comments on commit ca389f6

Please sign in to comment.