Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jan 15, 2024
1 parent 9f13b68 commit 2c3399a
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 83 deletions.
7 changes: 4 additions & 3 deletions notebooks/Figure-1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"\n",
"import fsspec\n",
"import json\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt"
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
Expand Down
9 changes: 5 additions & 4 deletions notebooks/Figure-3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"\n",
"import fsspec\n",
"import json\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import statsmodels.formula.api as smf\n",
"import matplotlib.pyplot as plt"
"import pandas as pd\n",
"import statsmodels.formula.api as smf"
]
},
{
Expand Down
34 changes: 24 additions & 10 deletions notebooks/Figure-5.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"\n",
"import fsspec\n",
"import json\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd\n",
"from matplotlib import ticker"
]
},
Expand Down Expand Up @@ -93,9 +94,15 @@
" np.percentile(data[key][\"delta_arbocs\"], 75) / get(key)[\"arbocs\"][\"issuance\"]\n",
" for key in data.keys()\n",
"]\n",
"df[\"absolute_low\"] = [np.percentile(data[key][\"delta_arbocs\"], 5) for key in data.keys()]\n",
"df[\"absolute_med\"] = [np.percentile(data[key][\"delta_arbocs\"], 50) for key in data.keys()]\n",
"df[\"absolute_high\"] = [np.percentile(data[key][\"delta_arbocs\"], 95) for key in data.keys()]\n",
"df[\"absolute_low\"] = [\n",
" np.percentile(data[key][\"delta_arbocs\"], 5) for key in data.keys()\n",
"]\n",
"df[\"absolute_med\"] = [\n",
" np.percentile(data[key][\"delta_arbocs\"], 50) for key in data.keys()\n",
"]\n",
"df[\"absolute_high\"] = [\n",
" np.percentile(data[key][\"delta_arbocs\"], 95) for key in data.keys()\n",
"]\n",
"df[\"project_size\"] = [get(key)[\"arbocs\"][\"issuance\"] for key in data.keys()]\n",
"df[\"cp_slag\"] = [get(key)[\"carbon\"][\"common_practice\"][\"value\"] for key in data.keys()]\n",
"df[\"alternate_slag\"] = [np.percentile(data[key][\"alt_slag\"], 50) for key in data.keys()]\n",
Expand All @@ -117,8 +124,10 @@
"metadata": {},
"outputs": [],
"source": [
"def format_si(num, precision=0, suffixes=[\"\", \"K\", \"M\", \"G\", \"T\", \"P\"], show_suffix=False):\n",
" m = sum([abs(num / 1000.0 ** x) >= 1 for x in range(1, len(suffixes))])\n",
"def format_si(\n",
" num, precision=0, suffixes=[\"\", \"K\", \"M\", \"G\", \"T\", \"P\"], show_suffix=False\n",
"):\n",
" m = sum([abs(num / 1000.0**x) >= 1 for x in range(1, len(suffixes))])\n",
" if show_suffix:\n",
" return f\"{num/1000.0**m:.{precision}f}{suffixes[m]}\"\n",
" else:\n",
Expand Down Expand Up @@ -154,7 +163,10 @@
" total.append(np.nansum([data[key][\"delta_arbocs\"][i] for key in data.keys()]))\n",
" total_arbocs = np.percentile(total, [5, 50, 95])\n",
" total_percentage = np.percentile(total, [5, 50, 95]) / np.sum(\n",
" [[x for x in db if x[\"id\"] == key][0][\"arbocs\"][\"issuance\"] for key in data.keys()]\n",
" [\n",
" [x for x in db if x[\"id\"] == key][0][\"arbocs\"][\"issuance\"]\n",
" for key in data.keys()\n",
" ]\n",
" )"
]
},
Expand Down Expand Up @@ -284,7 +296,9 @@
"axs[1].vlines(df[\"id\"], -1, 1.5, color=(0.95, 0.95, 0.95), linewidth=1)\n",
"axs[1].hlines([0], [0], [len(df) - 1], color=(0.75, 0.75, 0.75), linewidth=2)\n",
"axs[1].plot(df[\"id\"], df[\"percent_med\"], \".\", color=\"#7EB36A\", markersize=12)\n",
"axs[1].vlines(df[\"id\"], df[\"percent_low\"], df[\"percent_high\"], color=\"black\", linewidth=1.25)\n",
"axs[1].vlines(\n",
" df[\"id\"], df[\"percent_low\"], df[\"percent_high\"], color=\"black\", linewidth=1.25\n",
")\n",
"axs[1].set_xticks([])\n",
"axs[1].set_ylim([-1.1, 1.1])\n",
"axs[1].set_ylabel(\"Crediting error (%)\")\n",
Expand Down
39 changes: 26 additions & 13 deletions notebooks/Figure-6.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import os\n",
"\n",
"import fsspec\n",
"import json\n",
"import geopandas\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
Expand All @@ -26,7 +27,6 @@
"from mpl_toolkits.axes_grid1.inset_locator import inset_axes\n",
"from shapely.geometry import Point\n",
"\n",
"\n",
"crs = \"+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 +lat_2=45.5 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs=True\""
]
},
Expand Down Expand Up @@ -100,7 +100,9 @@
"outputs": [],
"source": [
"proj_centroids = {\n",
" project[\"opr_id\"]: Point(project[\"shape_centroid\"][0][0], project[\"shape_centroid\"][0][1])\n",
" project[\"opr_id\"]: Point(\n",
" project[\"shape_centroid\"][0][0], project[\"shape_centroid\"][0][1]\n",
" )\n",
" for project in db\n",
" if (79 in project[\"supersection_ids\"])\n",
" and (\n",
Expand All @@ -119,7 +121,9 @@
"outputs": [],
"source": [
"proj_points = geopandas.GeoDataFrame(\n",
" data=list(proj_centroids.keys()), geometry=list(proj_centroids.values()), crs=\"epsg:4326\"\n",
" data=list(proj_centroids.keys()),\n",
" geometry=list(proj_centroids.values()),\n",
" crs=\"epsg:4326\",\n",
")"
]
},
Expand Down Expand Up @@ -214,9 +218,9 @@
"metadata": {},
"outputs": [],
"source": [
"vmin, vmax = arbitrage_df.mean_local_slag.quantile(0.025), arbitrage_df.mean_local_slag.quantile(\n",
" 0.975\n",
")\n",
"vmin, vmax = arbitrage_df.mean_local_slag.quantile(\n",
" 0.025\n",
"), arbitrage_df.mean_local_slag.quantile(0.975)\n",
"\n",
"norm = mpl.colors.Normalize(vmin, vmax)"
]
Expand Down Expand Up @@ -276,9 +280,13 @@
"outputs": [],
"source": [
"supersection_sections = (\n",
" ecomap_sections.loc[ecomap_sections[\"MAP_UNIT_S\"].isin(ecosections)].to_crs(crs).copy()\n",
" ecomap_sections.loc[ecomap_sections[\"MAP_UNIT_S\"].isin(ecosections)]\n",
" .to_crs(crs)\n",
" .copy()\n",
")\n",
"supersection_sections[\"slag\"] = supersection_sections[\"MAP_UNIT_S\"].map(\n",
" slag_per_section\n",
")\n",
"supersection_sections[\"slag\"] = supersection_sections[\"MAP_UNIT_S\"].map(slag_per_section)\n",
"supersection_outline = supersection_sections.dissolve(\"PROJECT\")"
]
},
Expand Down Expand Up @@ -327,10 +335,14 @@
" vmax=55,\n",
" legend_kwds={\"label\": \"\", \"orientation\": \"vertical\"},\n",
")\n",
"cax.set_ylabel(\"$\\Delta$ Carbon\\n(tCO2e / acre)\", loc=\"center\", labelpad=-75, fontsize=12)\n",
"cax.set_ylabel(\n",
" \"$\\\\Delta$ Carbon\\n(tCO2e / acre)\", loc=\"center\", labelpad=-75, fontsize=12\n",
")\n",
"cax.yaxis.set_ticks_position(\"none\")\n",
"\n",
"proj_points.to_crs(crs).plot(ax=ax[0], marker=\"^\", color=\"k\", markersize=100, edgecolor=None)\n",
"proj_points.to_crs(crs).plot(\n",
" ax=ax[0], marker=\"^\", color=\"k\", markersize=100, edgecolor=None\n",
")\n",
"supersection_outline.plot(ax=ax[0], edgecolor=\"k\", lw=0.2, color=\"None\")\n",
"\n",
"supersection_sections.plot(\n",
Expand All @@ -342,7 +354,9 @@
" vmin=-55,\n",
" vmax=55,\n",
")\n",
"proj_points.to_crs(crs).plot(ax=ax[1], marker=\"^\", color=\"k\", markersize=100, edgecolor=None)\n",
"proj_points.to_crs(crs).plot(\n",
" ax=ax[1], marker=\"^\", color=\"k\", markersize=100, edgecolor=None\n",
")\n",
"\n",
"\n",
"xys = {\"M261D\": (0.7, 0.25), \"M261A\": (0.15, 0.8), \"M261B\": (0.4, 0.15)}\n",
Expand All @@ -359,7 +373,6 @@
"\n",
"\n",
"for key in xys.keys():\n",
"\n",
" ax[1].annotate(\n",
" text=key,\n",
" xy=xys[key],\n",
Expand Down
69 changes: 47 additions & 22 deletions notebooks/Inline-Statistics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,18 @@
"metadata": {},
"outputs": [],
"source": [
"from collections import Counter\n",
"import fsspec\n",
"import json\n",
"import os\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import fsspec\n",
"import numpy as np\n",
"import pandas as pd\n",
"import seaborn as sns\n",
"from sklearn.metrics import mean_squared_error\n",
"\n",
"from carbonplan_forest_offsets.analysis.project_crediting_error import get_slag_to_total_scalar\n",
"from carbonplan_forest_offsets.analysis.project_crediting_error import (\n",
" get_slag_to_total_scalar,\n",
")\n",
"from carbonplan_forest_offsets.data import cat\n",
"from carbonplan_forest_offsets.load.issuance import load_issuance_table, ifm_opr_ids"
"from carbonplan_forest_offsets.load.issuance import ifm_opr_ids, load_issuance_table\n",
"from sklearn.metrics import mean_squared_error"
]
},
{
Expand Down Expand Up @@ -133,7 +131,8 @@
"subsets = {\n",
" \"all\": np.tile(True, len(df)),\n",
" \"all_forest\": df[\"project_type\"] == \"forest\",\n",
" \"compliance_ifm\": (df[\"opr_id\"].isin(ifm_opr_ids)) & (df[\"Early Action/ Compliance\"] == \"COP\"),\n",
" \"compliance_ifm\": (df[\"opr_id\"].isin(ifm_opr_ids))\n",
" & (df[\"Early Action/ Compliance\"] == \"COP\"),\n",
" \"non_graduated_compliance_ifms\": (df[\"opr_id\"].isin(compliance_opr_ids))\n",
" & (df[\"Early Action/ Compliance\"] == \"COP\"),\n",
" \"upfront_ifm\": (df[\"opr_id\"].isin(upfront_opr_ids)) & (df[\"arb_rp_id\"].isin([\"A\"])),\n",
Expand Down Expand Up @@ -202,7 +201,9 @@
"source": [
"sc_data = cat.rfia_all(assessment_area_id=297).read()\n",
"\n",
"sc_data = sc_data[sc_data[\"YEAR\"] == 2010].copy() # use 2010 because comporable to CP data\n",
"sc_data = sc_data[\n",
" sc_data[\"YEAR\"] == 2010\n",
"].copy() # use 2010 because comporable to CP data\n",
"\n",
"\n",
"sc_data[\"CARB_ACRE\"] = sc_data[\"CARB_ACRE\"] * 44 / 12 * 0.907185"
Expand Down Expand Up @@ -237,7 +238,9 @@
"source": [
"standing_carbon = {}\n",
"for k, v in fortyps_of_interest.items():\n",
" standing_carbon[k] = round(sc_data.loc[sc_data[\"FORTYPCD\"] == v, \"CARB_ACRE\"].item(), 1)\n",
" standing_carbon[k] = round(\n",
" sc_data.loc[sc_data[\"FORTYPCD\"] == v, \"CARB_ACRE\"].item(), 1\n",
" )\n",
"display(standing_carbon)"
]
},
Expand All @@ -258,7 +261,9 @@
"source": [
"# ak has three assessment areas but lets summarize across all to report inline value\n",
"ak_assessment_areas = [285, 286, 287]\n",
"ak_all = pd.concat([cat.rfia_all(assessment_area_id=aa_id).read() for aa_id in ak_assessment_areas])\n",
"ak_all = pd.concat(\n",
" [cat.rfia_all(assessment_area_id=aa_id).read() for aa_id in ak_assessment_areas]\n",
")\n",
"\n",
"ak_all = ak_all[ak_all[\"YEAR\"] == 2013].copy() # 2013 to match what used in CP\n",
"\n",
Expand Down Expand Up @@ -340,7 +345,9 @@
" ).round(1)\n",
"\n",
" as_frac = crediting_error / project[\"arbocs\"][\"calculated\"]\n",
" print(f\"{project['opr_id']} has a {crediting_error[1]} crediting error ({as_frac[1].round(3)})\")\n",
" print(\n",
" f\"{project['opr_id']} has a {crediting_error[1]} crediting error ({as_frac[1].round(3)})\"\n",
" )\n",
" print(f\"CI: {crediting_error[0]}, {crediting_error[2]}\")\n",
" print(f\"% CI: {as_frac[0].round(3)}, {as_frac[2].round(3)}\")"
]
Expand Down Expand Up @@ -886,7 +893,8 @@
"projects = [\n",
" x\n",
" for x in db\n",
" if x[\"carbon\"][\"initial_carbon_stock\"][\"value\"] > x[\"carbon\"][\"common_practice\"][\"value\"]\n",
" if x[\"carbon\"][\"initial_carbon_stock\"][\"value\"]\n",
" > x[\"carbon\"][\"common_practice\"][\"value\"]\n",
"]"
]
},
Expand Down Expand Up @@ -937,7 +945,7 @@
}
],
"source": [
"sum((cp_df[\"baseline\"] <= cp_df[\"cp\"] * 1.05)) / len(cp_df.dropna())"
"sum(cp_df[\"baseline\"] <= cp_df[\"cp\"] * 1.05) / len(cp_df.dropna())"
]
},
{
Expand All @@ -955,7 +963,7 @@
"metadata": {},
"outputs": [],
"source": [
"fn = f\"https://carbonplan.blob.core.windows.net/carbonplan-forests/offsets/archive/results/common-practice-verification.json\"\n",
"fn = \"https://carbonplan.blob.core.windows.net/carbonplan-forests/offsets/archive/results/common-practice-verification.json\"\n",
"with fsspec.open(fn, mode=\"r\") as f:\n",
" cp_verification = json.load(f)"
]
Expand Down Expand Up @@ -1000,7 +1008,9 @@
}
],
"source": [
"data = pd.DataFrame(cp_verification[\"projects\"])[[\"opr_id\", \"recalculated\", \"project_reported\"]]\n",
"data = pd.DataFrame(cp_verification[\"projects\"])[\n",
" [\"opr_id\", \"recalculated\", \"project_reported\"]\n",
"]\n",
"mean_squared_error(data[\"recalculated\"], data[\"project_reported\"]) ** 0.5"
]
},
Expand All @@ -1011,7 +1021,9 @@
"metadata": {},
"outputs": [],
"source": [
"data[\"diff\"] = (data[\"recalculated\"] - data[\"project_reported\"]) / (data[\"project_reported\"])\n",
"data[\"diff\"] = (data[\"recalculated\"] - data[\"project_reported\"]) / (\n",
" data[\"project_reported\"]\n",
")\n",
"data = data[np.isfinite(data[\"diff\"])] # CAR1186 = infite bc original CP = 0."
]
},
Expand Down Expand Up @@ -1188,7 +1200,9 @@
"\n",
"crediting_df = pd.DataFrame({k: v[\"delta_arbocs\"] for k, v in crediting_error.items()})\n",
"\n",
"median_crediting_error = {k: np.median(v[\"delta_arbocs\"]) for k, v in crediting_error.items()}"
"median_crediting_error = {\n",
" k: np.median(v[\"delta_arbocs\"]) for k, v in crediting_error.items()\n",
"}"
]
},
{
Expand All @@ -1199,7 +1213,10 @@
"outputs": [],
"source": [
"tp = pd.concat(\n",
" [pd.Series(median_crediting_error).rename(\"crediting_error\"), error_cp0.rename(\"cp\")],\n",
" [\n",
" pd.Series(median_crediting_error).rename(\"crediting_error\"),\n",
" error_cp0.rename(\"cp\"),\n",
" ],\n",
" axis=1,\n",
")"
]
Expand Down Expand Up @@ -1332,7 +1349,9 @@
"outputs": [],
"source": [
"ak_assessment_areas = [285, 286, 287]\n",
"ak_all = pd.concat([cat.rfia_all(assessment_area_id=aa_id).read() for aa_id in ak_assessment_areas])"
"ak_all = pd.concat(\n",
" [cat.rfia_all(assessment_area_id=aa_id).read() for aa_id in ak_assessment_areas]\n",
")"
]
},
{
Expand Down Expand Up @@ -1388,7 +1407,13 @@
}
],
"source": [
"sum([project[\"arbocs\"][\"issuance\"] for project in db if 287 in project[\"supersection_ids\"]])"
"sum(\n",
" [\n",
" project[\"arbocs\"][\"issuance\"]\n",
" for project in db\n",
" if 287 in project[\"supersection_ids\"]\n",
" ]\n",
")"
]
},
{
Expand Down
Loading

0 comments on commit 2c3399a

Please sign in to comment.