diff --git a/.github/dependabot.yml b/.github/dependabot.yml index df1c2e0..670fb5d 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -1,7 +1,7 @@ version: 2 updates: - package-ecosystem: pip - directory: "/" + directory: '/' schedule: interval: daily - package-ecosystem: 'github-actions' diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 9309cab..916b1a3 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -2,9 +2,15 @@ name: CI on: push: - branches: "*" + branches: + - main pull_request: - branches: "*" + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true jobs: test: @@ -19,7 +25,7 @@ jobs: - uses: conda-incubator/setup-miniconda@v2.1.1 with: python-version: ${{ matrix.python-version }} - mamba-version: "*" + mamba-version: '*' activate-environment: carbonplan channels: conda-forge channel-priority: true @@ -73,10 +79,3 @@ jobs: push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.docker_meta.outputs.tags }} labels: ${{ steps.docker_meta.outputs.labels }} - - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2.4.0 - - uses: actions/setup-python@v2.3.1 - - uses: pre-commit/action@v2.0.3 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 20ccfca..a958314 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,44 +1,45 @@ +ci: + autoupdate_schedule: quarterly +exclude: notebooks/fire/figure_foresttype.ipynb repos: - -- repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.4.0 + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 hooks: - - id: trailing-whitespace - - id: end-of-file-fixer - - id: check-docstring-first - - id: check-json - - id: check-yaml - - id: pretty-format-json - args: ["--autofix", "--indent=2", "--no-sort-keys"] + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-docstring-first + - id: debug-statements + - id: mixed-line-ending -- repo: https://github.com/ambv/black - rev: 20.8b1 + - repo: https://github.com/asottile/pyupgrade + rev: v3.2.2 hooks: - - id: black - args: ["--line-length", "100", "--skip-string-normalization"] + - id: pyupgrade + args: + - '--py310-plus' -- repo: https://gitlab.com/pycqa/flake8 - rev: 3.9.0 + - repo: https://github.com/psf/black + rev: 22.10.0 hooks: - - id: flake8 + - id: black + - id: black-jupyter -- repo: https://github.com/asottile/seed-isort-config - rev: v2.2.0 + - repo: https://github.com/keewis/blackdoc + rev: v0.3.8 hooks: - - id: seed-isort-config -- repo: https://github.com/pre-commit/mirrors-isort - rev: v5.8.0 + - id: blackdoc + + - repo: https://github.com/PyCQA/flake8 + rev: 5.0.4 hooks: - - id: isort + - id: flake8 -- repo: https://github.com/deathbeds/prenotebook - rev: f5bdb72a400f1a56fe88109936c83aa12cc349fa + - repo: https://github.com/PyCQA/isort + rev: 5.10.1 hooks: - - id: prenotebook + - id: isort -# for R code -# - repo: https://github.com/lorenzwalthert/precommit -# rev: v0.1.3 -# hooks: -# - id: style-files -# args: [--style_pkg=styler, --style_fun=tidyverse_style] + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v3.0.0-alpha.4 + hooks: + - id: prettier diff --git a/.prettierrc.toml b/.prettierrc.toml new file mode 100644 index 0000000..addd6d3 --- /dev/null +++ b/.prettierrc.toml @@ -0,0 +1,3 @@ +tabWidth = 2 +semi = false +singleQuote = true diff --git a/README.md b/README.md index 7a83a9f..3b3d6c6 100644 --- a/README.md +++ b/README.md @@ -32,12 +32,13 @@ Several notebooks are additionally provided that show the use of these tools for ## data products As part of this project we have created derived data products for five key variables relevant to evaluating forest carbon storage potential and risks. + - `biomass` The potential carbon storage in forests assuming continued growth of existing forests. - `fire` The risks associated with forest fires. - `drought` The risk to forests from insect-related tree mortality. - `insects` The risk to forests from insect-related tree mortality. -Gridded rasters for each of these layers are available for the continental United States at a 4km spatial scale. For biomass and fire, projections are shown through the end of the 21st century in decadal increments. Drought and insect models are still in development so we currently only show historical risks for these disturbance types. All data are accessible via this [catalog](https://github.com/carbonplan/forest-risks/blob/master/carbonplan_forest_risks/data/catalog.yaml). Additional formats and download options will be provided in the future. +Gridded rasters for each of these layers are available for the continental United States at a 4km spatial scale. For biomass and fire, projections are shown through the end of the 21st century in decadal increments. Drought and insect models are still in development so we currently only show historical risks for these disturbance types. All data are accessible via this [catalog](https://github.com/carbonplan/forest-risks/blob/master/carbonplan_forest_risks/data/catalog.yaml). Additional formats and download options will be provided in the future. ## license diff --git a/carbonplan_forest_risks/load/cmip.py b/carbonplan_forest_risks/load/cmip.py index d7dfb85..e187c32 100644 --- a/carbonplan_forest_risks/load/cmip.py +++ b/carbonplan_forest_risks/load/cmip.py @@ -50,7 +50,6 @@ def cmip( if member is None: member = members[model] - path = setup.loading(store) prefix = f'cmip6/{method}/conus/4000m/{sampling}/{model}.{scenario}.{member}.zarr' diff --git a/carbonplan_forest_risks/load/fia.py b/carbonplan_forest_risks/load/fia.py index 4b369a1..d3f90d7 100644 --- a/carbonplan_forest_risks/load/fia.py +++ b/carbonplan_forest_risks/load/fia.py @@ -444,7 +444,7 @@ def fia_state_grouped(store, state, clean): key = f'year_{year}' if key in df.columns: if clean: - df = df[(df[key] < 9999) | np.isnan((df[key]))] + df = df[(df[key] < 9999) | np.isnan(df[key])] if sum(np.isnan(df[key])) == len(df): del df[key] diff --git a/carbonplan_forest_risks/plot/fire.py b/carbonplan_forest_risks/plot/fire.py index 009911c..bc201e7 100644 --- a/carbonplan_forest_risks/plot/fire.py +++ b/carbonplan_forest_risks/plot/fire.py @@ -321,7 +321,7 @@ def full_eval( } for metric, performance in eval_metrics.items(): - print('performance at {} scale is: {}'.format(metric, performance)) + print(f'performance at {metric} scale is: {performance}') return eval_metrics, evaluation( data, diff --git a/carbonplan_forest_risks/prepare.py b/carbonplan_forest_risks/prepare.py index dbee1c0..db24a84 100644 --- a/carbonplan_forest_risks/prepare.py +++ b/carbonplan_forest_risks/prepare.py @@ -39,7 +39,7 @@ def annualize( elif variable == 'cwd': aggregated = aggregated.max() else: - print('{} not implemented'.format(variable)) + print(f'{variable} not implemented') # drop your first year if you were rolling if climate_prepend is not None: diff --git a/carbonplan_forest_risks/utils.py b/carbonplan_forest_risks/utils.py index 42d687d..15b0788 100644 --- a/carbonplan_forest_risks/utils.py +++ b/carbonplan_forest_risks/utils.py @@ -128,7 +128,7 @@ def weighted_mean(ds, *args, **kwargs): def get_store(bucket, prefix, account_key=None): - ''' helper function to create a zarr store''' + '''helper function to create a zarr store''' if account_key is None: account_key = os.environ.get('BLOB_ACCOUNT_KEY', None) diff --git a/envs/python-notebook/environment.yml b/envs/python-notebook/environment.yml index bdfe513..503ec4f 100644 --- a/envs/python-notebook/environment.yml +++ b/envs/python-notebook/environment.yml @@ -2,7 +2,7 @@ name: carbonplan channels: - conda-forge dependencies: - - pangeo-notebook=2020.10.08 # don't bump until we bump the dask-gateway chart + - pangeo-notebook=2020.10.08 # don't bump until we bump the dask-gateway chart - adlfs - altair - altair_data_server @@ -83,7 +83,7 @@ dependencies: - numbagg - showit - git+https://github.com/jhamman/scikit-downscale.git@a502557f322cf6a8cd2e34008b31f1b89b7e8190 - - git+https://github.com/jhamman/zarr.git@fix/absstore # for current version of azure api - - git+https://github.com/jhamman/climate_indices.git@39bbf038c77922f8f0a46bb69704d6aace4179f9 # for no-nco deps, expanded range of pdsi - - git+https://github.com/altair-viz/altair_data_server.git # for remote hosting + - git+https://github.com/jhamman/zarr.git@fix/absstore # for current version of azure api + - git+https://github.com/jhamman/climate_indices.git@39bbf038c77922f8f0a46bb69704d6aace4179f9 # for no-nco deps, expanded range of pdsi + - git+https://github.com/altair-viz/altair_data_server.git # for remote hosting - git+https://github.com/carbonplan/cmip6-downscaling.git@e8cc4ad89ed3da47a1e46ccc441cdda8eebbc152 diff --git a/notebooks/biomass/biomass_model.ipynb b/notebooks/biomass/biomass_model.ipynb index 04d5e61..1a09b5c 100644 --- a/notebooks/biomass/biomass_model.ipynb +++ b/notebooks/biomass/biomass_model.ipynb @@ -78,9 +78,7 @@ "f = [df[inds][\"tmean_mean\"], df[inds][\"ppt_mean\"]]\n", "(\n", " plot.xy(x=x, y=y, color=f[0], cmap=\"magma\", xlim=[0, 250], ylim=[0, 600])\n", - " | plot.xy(\n", - " x=x, y=y, color=f[1], cmap=\"viridis\", xlim=[0, 250], ylim=[0, 600]\n", - " )\n", + " | plot.xy(x=x, y=y, color=f[1], cmap=\"viridis\", xlim=[0, 250], ylim=[0, 600])\n", ").resolve_scale(color=\"independent\")" ] }, @@ -182,10 +180,7 @@ "xlim = [0, 250]\n", "ylim = [-200, 700]\n", "(\n", - " (\n", - " plot.xy(x=x, y=y, xlim=xlim, ylim=ylim)\n", - " + plot.line(x=x, y=model.predict(x, f, [50, 50]))\n", - " )\n", + " (plot.xy(x=x, y=y, xlim=xlim, ylim=ylim) + plot.line(x=x, y=model.predict(x, f, [50, 50])))\n", " | (\n", " plot.xy(x=x, y=model.sample(x, f), xlim=xlim, ylim=ylim)\n", " + plot.line(x=x, y=model.predict(x, f, [50, 50]))\n", @@ -222,10 +217,7 @@ "xlim = [0, 250]\n", "ylim = [-200, 700]\n", "(\n", - " (\n", - " plot.xy(x=x, y=y, xlim=xlim, ylim=ylim)\n", - " + plot.line(x=x, y=model.predict(x, f, [50, 50]))\n", - " )\n", + " (plot.xy(x=x, y=y, xlim=xlim, ylim=ylim) + plot.line(x=x, y=model.predict(x, f, [50, 50])))\n", " | (\n", " plot.xy(x=x, y=model.sample(x, f), xlim=xlim, ylim=ylim)\n", " + plot.line(x=x, y=model.predict(x, f, [50, 50]))\n", diff --git a/notebooks/drought/drought_results.ipynb b/notebooks/drought/drought_results.ipynb index 27eba43..6e0e589 100644 --- a/notebooks/drought/drought_results.ipynb +++ b/notebooks/drought/drought_results.ipynb @@ -46,9 +46,7 @@ "metadata": {}, "outputs": [], "source": [ - "pf = pd.read_parquet(\n", - " \"/Users/freeman/github/carbonplan/forests/scripts/data/drought.parquet\"\n", - ")" + "pf = pd.read_parquet(\"/Users/freeman/github/carbonplan/forests/scripts/data/drought.parquet\")" ] }, { diff --git a/notebooks/fire/fire_model.ipynb b/notebooks/fire/fire_model.ipynb index b132a9f..79d68ed 100644 --- a/notebooks/fire/fire_model.ipynb +++ b/notebooks/fire/fire_model.ipynb @@ -81,9 +81,7 @@ "outputs": [], "source": [ "mask = load.nlcd(store=\"local\", classes=\"all\", year=2001)\n", - "groups = load.nftd(\n", - " store=\"local\", groups=\"all\", coarsen=coarsen, mask=mask, area_threshold=1500\n", - ")" + "groups = load.nftd(store=\"local\", groups=\"all\", coarsen=coarsen, mask=mask, area_threshold=1500)" ] }, { @@ -101,9 +99,7 @@ }, "outputs": [], "source": [ - "climate = load.terraclim(\n", - " store=\"local\", tlim=tlim, coarsen=coarsen, data_vars=data_vars, mask=mask\n", - ")" + "climate = load.terraclim(store=\"local\", tlim=tlim, coarsen=coarsen, data_vars=data_vars, mask=mask)" ] }, { @@ -188,9 +184,7 @@ "metadata": {}, "outputs": [], "source": [ - "r = rasterio.open(\n", - " \"/Users/freeman/workdir/carbonplan-data/processed/mtbs/conus/30m/2006.tif\"\n", - ")" + "r = rasterio.open(\"/Users/freeman/workdir/carbonplan-data/processed/mtbs/conus/30m/2006.tif\")" ] }, { @@ -303,9 +297,7 @@ "x = mtbs[\"time\"]\n", "y = mtbs[\"vlf\"].mean([\"x\", \"y\"])\n", "\n", - "plot.line(\n", - " x=x, y=y, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2\n", - ")" + "plot.line(x=x, y=y, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2)" ] }, { @@ -468,9 +460,7 @@ " color=\"rgb(175,91,92)\",\n", " strokeWidth=2,\n", " )\n", - " + plot.line(\n", - " x=x, y=y2, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2\n", - " )\n", + " + plot.line(x=x, y=y2, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2)\n", ")" ] }, diff --git a/notebooks/fire/fire_model_redux.ipynb b/notebooks/fire/fire_model_redux.ipynb index b5d7852..1c93126 100644 --- a/notebooks/fire/fire_model_redux.ipynb +++ b/notebooks/fire/fire_model_redux.ipynb @@ -97,10 +97,9 @@ "metadata": {}, "outputs": [], "source": [ - "mask = (\n", - " load.nlcd(store=store, year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - ").astype(\"float\")" + "mask = (load.nlcd(store=store, year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25).astype(\n", + " \"float\"\n", + ")" ] }, { @@ -220,9 +219,7 @@ "x = mtbs[\"time\"]\n", "y = mtbs[\"monthly\"].mean([\"x\", \"y\"])\n", "\n", - "plot.line(\n", - " x=x, y=y, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2\n", - ")" + "plot.line(x=x, y=y, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2)" ] }, { @@ -303,9 +300,7 @@ "source": [ "plt.figure(figsize=(15, 8))\n", "xr.DataArray(\n", - " np.repeat(\n", - " single_pixel[\"tmean\"].sel(time=sl).groupby(\"time.year\").max().values, 12\n", - " ),\n", + " np.repeat(single_pixel[\"tmean\"].sel(time=sl).groupby(\"time.year\").max().values, 12),\n", " coords=single_pixel.sel(time=sl).coords,\n", ").plot(label=\"groupby\")\n", "single_pixel[\"tmean\"].rolling(time=12).max().sel(time=sl).plot(label=\"rolling\")\n", @@ -321,13 +316,9 @@ "outputs": [], "source": [ "ts = climate[\"tmean\"].groupby(\"time.year\").max().mean(dim=[\"x\", \"y\"]).values\n", - "groupby_global_max_then_mean = xr.DataArray(\n", - " np.repeat(ts, 12), coords=single_pixel.coords\n", - ")\n", + "groupby_global_max_then_mean = xr.DataArray(np.repeat(ts, 12), coords=single_pixel.coords)\n", "ts = climate[\"tmean\"].mean(dim=[\"x\", \"y\"]).groupby(\"time.year\").max().values\n", - "groupby_global_mean_then_max = xr.DataArray(\n", - " np.repeat(ts, 12), coords=single_pixel.coords\n", - ")" + "groupby_global_mean_then_max = xr.DataArray(np.repeat(ts, 12), coords=single_pixel.coords)" ] }, { @@ -368,9 +359,7 @@ "# groupby_global_max_annual.sel(time=inspection_slice).plot(label='groupby')\n", "\n", "groupby_global_mean_then_max.sel(time=inspection_slice).plot(label=\"groupby\")\n", - "climate[\"tmean\"].sel(time=inspection_slice).mean(dim=[\"x\", \"y\"]).plot(\n", - " label=\"raw\"\n", - ")\n", + "climate[\"tmean\"].sel(time=inspection_slice).mean(dim=[\"x\", \"y\"]).plot(label=\"raw\")\n", "plt.ylim(16, 24)\n", "plt.legend()" ] @@ -633,9 +622,7 @@ " color=\"rgb(175,91,92)\",\n", " strokeWidth=2,\n", " )\n", - " + plot.line(\n", - " x=x, y=y2, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2\n", - " )\n", + " + plot.line(x=x, y=y2, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2)\n", ")" ] }, @@ -766,7 +753,9 @@ "for (model, member) in cmip_models:\n", " for scenario in [\"historical\", \"ssp245\", \"ssp370\", \"ssp585\"]:\n", "\n", - " prefix = f\"cmip6/{downscaling}/conus/4000m/{sampling}/{model}.{scenario}.{member}.zarr\".format()\n", + " prefix = (\n", + " f\"cmip6/{downscaling}/conus/4000m/{sampling}/{model}.{scenario}.{member}.zarr\".format()\n", + " )\n", "\n", " if store == \"az\":\n", " mapper = zarr.storage.ABSStore(\n", @@ -835,11 +824,7 @@ "x = prediction[\"time\"]\n", "y2 = prediction[\"prediction\"].mean([\"x\", \"y\"])\n", "\n", - "(\n", - " plot.line(\n", - " x=x, y=y2, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2\n", - " )\n", - ")" + "(plot.line(x=x, y=y2, width=900, height=200, color=\"rgb(175,91,92)\", strokeWidth=2))" ] } ], diff --git a/notebooks/fire/fire_model_supersections.ipynb b/notebooks/fire/fire_model_supersections.ipynb index 0edf389..29ddb17 100644 --- a/notebooks/fire/fire_model_supersections.ipynb +++ b/notebooks/fire/fire_model_supersections.ipynb @@ -180,14 +180,10 @@ "metadata": {}, "outputs": [], "source": [ - "before_2000 = (\n", - " mtbs.sel(time=slice(\"1984\", \"2000\")).groupby(\"time.year\").sum().mean(\"year\")\n", - ")\n", + "before_2000 = mtbs.sel(time=slice(\"1984\", \"2000\")).groupby(\"time.year\").sum().mean(\"year\")\n", "risks_before_2000 = np.asarray(\n", " [\n", - " before_2000.monthly.where(masks.sel(region=i))\n", - " .mean([\"x\", \"y\"])\n", - " .values.item()\n", + " before_2000.monthly.where(masks.sel(region=i)).mean([\"x\", \"y\"]).values.item()\n", " for i in masks[\"region\"]\n", " ]\n", ")\n", @@ -217,14 +213,10 @@ "metadata": {}, "outputs": [], "source": [ - "after_2000 = (\n", - " mtbs.sel(time=slice(\"2001\", \"2018\")).groupby(\"time.year\").sum().mean(\"year\")\n", - ")\n", + "after_2000 = mtbs.sel(time=slice(\"2001\", \"2018\")).groupby(\"time.year\").sum().mean(\"year\")\n", "risks_after_2000 = np.asarray(\n", " [\n", - " after_2000.monthly.where(masks.sel(region=i))\n", - " .mean([\"x\", \"y\"])\n", - " .values.item()\n", + " after_2000.monthly.where(masks.sel(region=i)).mean([\"x\", \"y\"]).values.item()\n", " for i in masks[\"region\"]\n", " ]\n", ")\n", diff --git a/notebooks/insects/insects_results.ipynb b/notebooks/insects/insects_results.ipynb index cf79568..f62b7bc 100644 --- a/notebooks/insects/insects_results.ipynb +++ b/notebooks/insects/insects_results.ipynb @@ -46,9 +46,7 @@ "metadata": {}, "outputs": [], "source": [ - "pf = pd.read_parquet(\n", - " \"/Users/freeman/github/carbonplan/forests/scripts/data/insects.parquet\"\n", - ")" + "pf = pd.read_parquet(\"/Users/freeman/github/carbonplan/forests/scripts/data/insects.parquet\")" ] }, { diff --git a/notebooks/paper/Calculations/change-factor-calculations.ipynb b/notebooks/paper/Calculations/change-factor-calculations.ipynb index 053a96b..5a99426 100644 --- a/notebooks/paper/Calculations/change-factor-calculations.ipynb +++ b/notebooks/paper/Calculations/change-factor-calculations.ipynb @@ -49,10 +49,7 @@ "outputs": [], "source": [ "mask = (\n", - " (\n", - " load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - " )\n", + " (load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25)\n", " .astype(\"float\")\n", " .coarsen(x=coarsen, y=coarsen, boundary=\"trim\")\n", " .mean()\n", @@ -75,9 +72,7 @@ "outputs": [], "source": [ "historical_fire = xr.open_zarr(\n", - " get_store(\n", - " \"carbonplan-forests\", \"risks/results/paper/fire_terraclimate_v6.zarr\"\n", - " )\n", + " get_store(\"carbonplan-forests\", \"risks/results/paper/fire_terraclimate_v6.zarr\")\n", ").load()\n", "fire_mask = ~np.isnan(historical_fire.historical.isel(time=0).drop(\"time\"))" ] @@ -90,9 +85,7 @@ "outputs": [], "source": [ "ds = (\n", - " xr.open_zarr(\n", - " get_store(\"carbonplan-forests\", \"risks/results/paper/fire_cmip.zarr\")\n", - " )\n", + " xr.open_zarr(get_store(\"carbonplan-forests\", \"risks/results/paper/fire_cmip.zarr\"))\n", " .assign_coords({\"x\": mask.x, \"y\": mask.y})\n", " .where(fire_mask)\n", " .groupby(\"time.year\")\n", @@ -116,8 +109,7 @@ " hist_slice = slice(1990, 2019)\n", " fut_slice = slice(2080, 2099)\n", " change = (\n", - " ts.sel(year=fut_slice).mean(dim=\"year\")\n", - " / ts.sel(year=hist_slice).mean(dim=\"year\").values\n", + " ts.sel(year=fut_slice).mean(dim=\"year\") / ts.sel(year=hist_slice).mean(dim=\"year\").values\n", " )\n", " print(\"change factor for {} is {}\".format(scenario, change.values))" ] diff --git a/notebooks/paper/Figure-1/Figure-1.ipynb b/notebooks/paper/Figure-1/Figure-1.ipynb index e9d652e..80bb816 100644 --- a/notebooks/paper/Figure-1/Figure-1.ipynb +++ b/notebooks/paper/Figure-1/Figure-1.ipynb @@ -45,6 +45,7 @@ "from mpl_toolkits.axes_grid1 import make_axes_locatable\n", "\n", "from carbonplan_data import utils\n", + "\n", "alt.data_transformers.disable_max_rows()" ] }, @@ -145,10 +146,9 @@ "metadata": {}, "outputs": [], "source": [ - "mask = (\n", - " load.nlcd(store=store, year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - ").astype(\"float\")" + "mask = (load.nlcd(store=store, year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25).astype(\n", + " \"float\"\n", + ")" ] }, { @@ -158,14 +158,10 @@ "metadata": {}, "outputs": [], "source": [ - "ds_dict[\"Fire_observed\"] = load.mtbs(\n", - " store=store, coarsen=coarsen, tlim=tlim, mask=mask\n", - ")\n", + "ds_dict[\"Fire_observed\"] = load.mtbs(store=store, coarsen=coarsen, tlim=tlim, mask=mask)\n", "ds_dict[\"Fire_observed\"] = (\n", " ds_dict[\"Fire_observed\"]\n", - " .assign_coords(\n", - " {\"x\": ds_dict[\"Fire_modeled\"].x, \"y\": ds_dict[\"Fire_modeled\"].y}\n", - " )\n", + " .assign_coords({\"x\": ds_dict[\"Fire_modeled\"].x, \"y\": ds_dict[\"Fire_modeled\"].y})\n", " .assign_coords(\n", " {\n", " \"lat\": ds_dict[\"Fire_modeled\"].lat,\n", @@ -202,9 +198,7 @@ " \"modeled\": base_url_tempate.format(\n", " \"Fig1D_DroughtModel_ModeledFIAlongEnsembleHistMort_04-22-2021.tiff\"\n", " ),\n", - " \"observed\": base_url_tempate.format(\n", - " \"Fig1C_DroughtModel_FIAwide-ObsMort_05-08-2021.tiff\"\n", - " ),\n", + " \"observed\": base_url_tempate.format(\"Fig1C_DroughtModel_FIAwide-ObsMort_05-08-2021.tiff\"),\n", " },\n", " \"Insects\": {\n", " \"modeled\": base_url_tempate.format(\n", diff --git a/notebooks/paper/Figure-2/Figure-2.ipynb b/notebooks/paper/Figure-2/Figure-2.ipynb index 27d2431..9d9caf1 100644 --- a/notebooks/paper/Figure-2/Figure-2.ipynb +++ b/notebooks/paper/Figure-2/Figure-2.ipynb @@ -34,12 +34,14 @@ "from carbonplan_forest_risks.utils import get_store\n", "import rioxarray\n", "from carbonplan.data import cat\n", + "\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "import scipy as sp\n", "import geopandas as gpd\n", "import regionmask as rm\n", "import altair as alt\n", + "\n", "alt.themes.enable(\"carbonplan_light\")" ] }, @@ -83,10 +85,7 @@ "outputs": [], "source": [ "mask = (\n", - " (\n", - " load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - " )\n", + " (load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25)\n", " .astype(\"float\")\n", " .coarsen(x=coarsen, y=coarsen, boundary=\"trim\")\n", " .mean()\n", @@ -298,9 +297,7 @@ "axarr[0].legend()\n", "## Drought and insects\n", "for i, impact in enumerate([\"drought\", \"insects\"]):\n", - " historical_ts[impact].probability.plot(\n", - " ax=axarr[i + 1], color=\"k\", zorder=50\n", - " )\n", + " historical_ts[impact].probability.plot(ax=axarr[i + 1], color=\"k\", zorder=50)\n", "\n", " for scenario in [\"ssp245\", \"ssp370\", \"ssp585\"]:\n", " for (gcm, location) in gcms:\n", @@ -312,9 +309,7 @@ " )\n", "\n", " for scenario in [\"ssp245\", \"ssp370\", \"ssp585\"]:\n", - " future_ts[impact].probability.sel(scenario=scenario).mean(\n", - " dim=\"gcm\"\n", - " ).plot(\n", + " future_ts[impact].probability.sel(scenario=scenario).mean(dim=\"gcm\").plot(\n", " ax=axarr[i + 1],\n", " color=scenario_colors[scenario],\n", " label=ssp_rename[scenario],\n", diff --git a/notebooks/paper/Figure-3/Figure-3.ipynb b/notebooks/paper/Figure-3/Figure-3.ipynb index bc6bc05..9e79363 100644 --- a/notebooks/paper/Figure-3/Figure-3.ipynb +++ b/notebooks/paper/Figure-3/Figure-3.ipynb @@ -25,6 +25,7 @@ "import matplotlib.pyplot as plt\n", "from carbonplan_styles.mpl import get_colormap\n", "import warnings\n", + "\n", "warnings.filterwarnings('ignore')\n", "import matplotlib\n", "from carbonplan_data import utils\n", @@ -51,10 +52,7 @@ "source": [ "coarsen = 4\n", "mask = (\n", - " (\n", - " load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - " )\n", + " (load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25)\n", " .astype(\"float\")\n", " .coarsen(x=coarsen, y=coarsen, boundary=\"trim\")\n", " .mean()\n", @@ -102,9 +100,7 @@ "outputs": [], "source": [ "future_maps = {}\n", - "future_maps[\"Fire\"] = (\n", - " ds.sel(year=slice(\"2080\", \"2099\")).mean(dim=\"year\").compute()\n", - ")" + "future_maps[\"Fire\"] = ds.sel(year=slice(\"2080\", \"2099\")).mean(dim=\"year\").compute()" ] }, { @@ -169,10 +165,7 @@ " scenario_dict[risk] = {}\n", " for scenario in [\"ssp245\", \"ssp370\", \"ssp585\"]:\n", " scenario_dict[risk][scenario] = (\n", - " future_maps[risk]\n", - " .probability.sel(scenario=scenario)\n", - " .mean(dim=\"gcm\")\n", - " .drop(\"scenario\")\n", + " future_maps[risk].probability.sel(scenario=scenario).mean(dim=\"gcm\").drop(\"scenario\")\n", " )" ] }, @@ -270,9 +263,7 @@ " transform=axarr[row, column].transAxes,\n", " fontsize=18,\n", " )\n", - " cax = fig.add_axes(\n", - " [1.03, plot_params[impact][\"cbar_ylocation\"], 0.018, 0.12]\n", - " )\n", + " cax = fig.add_axes([1.03, plot_params[impact][\"cbar_ylocation\"], 0.018, 0.12])\n", " cax.text(\n", " 0.5,\n", " -0.12,\n", diff --git a/notebooks/paper/Figure-4/Figure-4.ipynb b/notebooks/paper/Figure-4/Figure-4.ipynb index 1ffe63c..5ba7de2 100644 --- a/notebooks/paper/Figure-4/Figure-4.ipynb +++ b/notebooks/paper/Figure-4/Figure-4.ipynb @@ -68,10 +68,7 @@ "outputs": [], "source": [ "mask = (\n", - " (\n", - " load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - " )\n", + " (load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25)\n", " .astype(\"float\")\n", " .coarsen(x=coarsen, y=coarsen, boundary=\"trim\")\n", " .mean()\n", @@ -163,12 +160,7 @@ "def load_regional_results(ds, timestep, region):\n", " selected = ds.sel(**region)\n", " if timestep == \"annual\":\n", - " return (\n", - " selected.mean(dim=[\"x\", \"y\"])\n", - " .rolling(dim={\"year\": 10})\n", - " .mean()\n", - " .compute()\n", - " )\n", + " return selected.mean(dim=[\"x\", \"y\"]).rolling(dim={\"year\": 10}).mean().compute()\n", " elif timestep == \"decadal\":\n", " return selected.mean(dim=[\"x\", \"y\"]).compute()" ] diff --git a/notebooks/paper/Supplementary-Figure-01/Supplementary-Figure-01.ipynb b/notebooks/paper/Supplementary-Figure-01/Supplementary-Figure-01.ipynb index cd3f1f9..686b6f8 100644 --- a/notebooks/paper/Supplementary-Figure-01/Supplementary-Figure-01.ipynb +++ b/notebooks/paper/Supplementary-Figure-01/Supplementary-Figure-01.ipynb @@ -198,9 +198,7 @@ " figsize=(12, 5),\n", ")\n", "\n", - "for i, field in enumerate(\n", - " [\"roc\", \"r2\", \"spatial_auc\", \"annual_r2\", \"seasonal_r2\"]\n", - "):\n", + "for i, field in enumerate([\"roc\", \"r2\", \"spatial_auc\", \"annual_r2\", \"seasonal_r2\"]):\n", " plot_lines(axs[i], field)\n", " plot_dots(axs[i], field)\n", " style(axs[i], plot_params[field])\n", diff --git a/notebooks/paper/Supplementary-Figure-02/StankeSFigData.csv b/notebooks/paper/Supplementary-Figure-02/StankeSFigData.csv index 2370620..99191be 100644 --- a/notebooks/paper/Supplementary-Figure-02/StankeSFigData.csv +++ b/notebooks/paper/Supplementary-Figure-02/StankeSFigData.csv @@ -6,4 +6,4 @@ StankeFSI,DrtMort,SP.CD -0.7,0.016539729,ABLA -1.1,0.015749563,POTR -1.2,0.019647979,PIEN --1.6,0.021229233,PICO +-1.6,0.021229233,PICO diff --git a/notebooks/paper/Supplementary-Figure-03/Supplementary-Figure-03.ipynb b/notebooks/paper/Supplementary-Figure-03/Supplementary-Figure-03.ipynb index 22584e2..ebce038 100644 --- a/notebooks/paper/Supplementary-Figure-03/Supplementary-Figure-03.ipynb +++ b/notebooks/paper/Supplementary-Figure-03/Supplementary-Figure-03.ipynb @@ -57,9 +57,7 @@ " \"modeled\": base_url_tempate.format(\n", " \"Fig1F_InsectModel_ModeledFIAlongEnsembleHistMort_04-22-2021.tif\"\n", " ),\n", - " \"observed\": base_url_tempate.format(\n", - " \"Fig1E_InsectModel_FIAwide-ObsMort_05-08-2021.tif\"\n", - " ),\n", + " \"observed\": base_url_tempate.format(\"Fig1E_InsectModel_FIAwide-ObsMort_05-08-2021.tif\"),\n", " \"reference\": \"SuppFig_Williamsetal2016_BarkBeetleOccurrenceMap_04-19-2021.tif\",\n", " },\n", "}" diff --git a/notebooks/paper/Supplementary-Figure-04/Supplementary-Figure-04.ipynb b/notebooks/paper/Supplementary-Figure-04/Supplementary-Figure-04.ipynb index 2fb578e..d0ee37b 100644 --- a/notebooks/paper/Supplementary-Figure-04/Supplementary-Figure-04.ipynb +++ b/notebooks/paper/Supplementary-Figure-04/Supplementary-Figure-04.ipynb @@ -43,18 +43,10 @@ "width = 0.35\n", "labels = list(df[\"Coefficient\"])\n", "index = np.arange(len(df.index))\n", - "rects1a = axs[0].bar(\n", - " index - width / 2, df[\"Limber.Binom\"], width, color=\"black\", label=\"Men\"\n", - ")\n", - "rects1b = axs[0].bar(\n", - " index + width / 2, df[\"Limber.Beta\"], width, color=\"gray\", label=\"Women\"\n", - ")\n", - "rects2a = axs[1].bar(\n", - " index - width / 2, df[\"Lodgepole.Binom\"], width, color=\"black\", label=\"Men\"\n", - ")\n", - "rects2b = axs[1].bar(\n", - " index + width / 2, df[\"Lodgepole.Beta\"], width, color=\"gray\", label=\"Women\"\n", - ")\n", + "rects1a = axs[0].bar(index - width / 2, df[\"Limber.Binom\"], width, color=\"black\", label=\"Men\")\n", + "rects1b = axs[0].bar(index + width / 2, df[\"Limber.Beta\"], width, color=\"gray\", label=\"Women\")\n", + "rects2a = axs[1].bar(index - width / 2, df[\"Lodgepole.Binom\"], width, color=\"black\", label=\"Men\")\n", + "rects2b = axs[1].bar(index + width / 2, df[\"Lodgepole.Beta\"], width, color=\"gray\", label=\"Women\")\n", "axs[0].spines[\"top\"].set_visible(False)\n", "axs[0].spines[\"right\"].set_visible(False)\n", "axs[1].spines[\"top\"].set_visible(False)\n", diff --git a/notebooks/paper/Supplementary-Figure-05/Drought_USwide_CrossValidationR2_4-21-21.csv b/notebooks/paper/Supplementary-Figure-05/Drought_USwide_CrossValidationR2_4-21-21.csv index f2a55bf..25d1dd7 100644 --- a/notebooks/paper/Supplementary-Figure-05/Drought_USwide_CrossValidationR2_4-21-21.csv +++ b/notebooks/paper/Supplementary-Figure-05/Drought_USwide_CrossValidationR2_4-21-21.csv @@ -497,4 +497,4 @@ 0.1597307 0.1765175 0.1771784 -0.2067463 +0.2067463 diff --git a/notebooks/paper/Supplementary-Figure-05/Insect_USwide_CrossValidationR2_4-21-21.csv b/notebooks/paper/Supplementary-Figure-05/Insect_USwide_CrossValidationR2_4-21-21.csv index b6549a0..2d4e63b 100644 --- a/notebooks/paper/Supplementary-Figure-05/Insect_USwide_CrossValidationR2_4-21-21.csv +++ b/notebooks/paper/Supplementary-Figure-05/Insect_USwide_CrossValidationR2_4-21-21.csv @@ -497,4 +497,4 @@ 0.3108796 0.2919352 0.305531 -0.3426035 +0.3426035 diff --git a/notebooks/paper/Supplementary-Figure-05/Supplementary-Figure-05.ipynb b/notebooks/paper/Supplementary-Figure-05/Supplementary-Figure-05.ipynb index 782a63e..b66c2be 100644 --- a/notebooks/paper/Supplementary-Figure-05/Supplementary-Figure-05.ipynb +++ b/notebooks/paper/Supplementary-Figure-05/Supplementary-Figure-05.ipynb @@ -51,12 +51,7 @@ " source = xr.open_rasterio(url)\n", " source = source.where(source > -1)\n", " ds = source.rio.reproject_match(target, resampling=Resampling.bilinear)\n", - " ds = (\n", - " ds.where(ds > -1)\n", - " .coarsen(x=coarsen, y=coarsen, boundary=\"trim\")\n", - " .mean()\n", - " .sel(band=1)\n", - " )\n", + " ds = ds.where(ds > -1).coarsen(x=coarsen, y=coarsen, boundary=\"trim\").mean().sel(band=1)\n", "\n", " return ds" ] @@ -183,12 +178,8 @@ " plot.add_colorbar(\n", " fig,\n", " to_plot=map_plot,\n", - " y_location=plot_params[\"{}_{}\".format(dataset, metric)][\n", - " \"cbar_ylocation\"\n", - " ],\n", - " x_location=plot_params[\"{}_{}\".format(dataset, metric)][\n", - " \"cbar_xlocation\"\n", - " ],\n", + " y_location=plot_params[\"{}_{}\".format(dataset, metric)][\"cbar_ylocation\"],\n", + " x_location=plot_params[\"{}_{}\".format(dataset, metric)][\"cbar_xlocation\"],\n", " vmin=plot_params[\"{}_{}\".format(dataset, metric)][\"var_lims\"][0],\n", " vmax=plot_params[\"{}_{}\".format(dataset, metric)][\"var_lims\"][1],\n", " cbar_label=plot_params[\"{}_{}\".format(dataset, metric)][\"label\"],\n", @@ -204,12 +195,8 @@ "metadata": {}, "outputs": [], "source": [ - "df_drought = pd.read_csv(\n", - " \"Drought_USwide_CrossValidationR2_4-21-21.csv\", header=None\n", - ")\n", - "df_insects = pd.read_csv(\n", - " \"Insect_USwide_CrossValidationR2_4-21-21.csv\", header=None\n", - ")" + "df_drought = pd.read_csv(\"Drought_USwide_CrossValidationR2_4-21-21.csv\", header=None)\n", + "df_insects = pd.read_csv(\"Insect_USwide_CrossValidationR2_4-21-21.csv\", header=None)" ] }, { diff --git a/notebooks/paper/Supplementary-Figure-07/BAcomparison_toCP.csv b/notebooks/paper/Supplementary-Figure-07/BAcomparison_toCP.csv index 3c0de91..0bb327e 100644 --- a/notebooks/paper/Supplementary-Figure-07/BAcomparison_toCP.csv +++ b/notebooks/paper/Supplementary-Figure-07/BAcomparison_toCP.csv @@ -2215,4 +2215,4 @@ Log_CMIPBA,Log_StatModelBA -3.40459364,-0.907853249 -3.049975847,-0.879897579 -3.045393173,-0.862103215 --3.035635537,-0.857932482 +-3.035635537,-0.857932482 diff --git a/notebooks/paper/Supplementary-Figure-07/cVegTreecomparison_toCP.csv b/notebooks/paper/Supplementary-Figure-07/cVegTreecomparison_toCP.csv index 4603cf0..fa735cb 100644 --- a/notebooks/paper/Supplementary-Figure-07/cVegTreecomparison_toCP.csv +++ b/notebooks/paper/Supplementary-Figure-07/cVegTreecomparison_toCP.csv @@ -2215,4 +2215,4 @@ Log_DeltaBA,CMIP_deltacVegTree -0.956027606,4.322232962 -0.927622416,1.241251693 -0.916333707,1.477494075 --0.916241492,1.993711251 +-0.916241492,1.993711251 diff --git a/notebooks/paper/Supplementary-Figure-08/Supplementary-Figure-08.ipynb b/notebooks/paper/Supplementary-Figure-08/Supplementary-Figure-08.ipynb index d392fe6..3ee1c02 100644 --- a/notebooks/paper/Supplementary-Figure-08/Supplementary-Figure-08.ipynb +++ b/notebooks/paper/Supplementary-Figure-08/Supplementary-Figure-08.ipynb @@ -24,9 +24,11 @@ "import cartopy.feature as cfeature\n", "import matplotlib.pyplot as plt\n", "import warnings\n", + "\n", "warnings.filterwarnings('ignore')\n", "import matplotlib\n", "from carbonplan_data import utils\n", + "\n", "alt.themes.enable(\"carbonplan_light\")" ] }, @@ -49,10 +51,7 @@ "source": [ "coarsen = 4\n", "mask = (\n", - " (\n", - " load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", - " )\n", + " (load.nlcd(store=\"az\", year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25)\n", " .astype(\"float\")\n", " .coarsen(x=coarsen, y=coarsen, boundary=\"trim\")\n", " .mean()\n", diff --git a/notebooks/paper/Supplementary-Figure-09/Supplementary-Figure-09.ipynb b/notebooks/paper/Supplementary-Figure-09/Supplementary-Figure-09.ipynb index b00383d..7c83cc1 100644 --- a/notebooks/paper/Supplementary-Figure-09/Supplementary-Figure-09.ipynb +++ b/notebooks/paper/Supplementary-Figure-09/Supplementary-Figure-09.ipynb @@ -29,18 +29,10 @@ "metadata": {}, "outputs": [], "source": [ - "df_drought_coef = pd.read_csv(\n", - " \"SuppFig_DroughtModelCoefficients-WeightedUnweighted_4-26-21.csv\"\n", - ")\n", - "df_drought_pred = pd.read_csv(\n", - " \"SuppFig_DroughtModelMortPredictions-WeightedUnweighted_4-26-21.csv\"\n", - ")\n", - "df_insects_coef = pd.read_csv(\n", - " \"SuppFig_InsectModelCoefficients-WeightedUnweighted_4-26-21.csv\"\n", - ")\n", - "df_insects_pred = pd.read_csv(\n", - " \"SuppFig_InsectModelMortPredictions-WeightedUnweighted_4-26-21.csv\"\n", - ")" + "df_drought_coef = pd.read_csv(\"SuppFig_DroughtModelCoefficients-WeightedUnweighted_4-26-21.csv\")\n", + "df_drought_pred = pd.read_csv(\"SuppFig_DroughtModelMortPredictions-WeightedUnweighted_4-26-21.csv\")\n", + "df_insects_coef = pd.read_csv(\"SuppFig_InsectModelCoefficients-WeightedUnweighted_4-26-21.csv\")\n", + "df_insects_pred = pd.read_csv(\"SuppFig_InsectModelMortPredictions-WeightedUnweighted_4-26-21.csv\")" ] }, { diff --git a/notebooks/paper/Supplementary-Figure-10/Supplementary-Figure-10.ipynb b/notebooks/paper/Supplementary-Figure-10/Supplementary-Figure-10.ipynb index d1c410e..dd40836 100644 --- a/notebooks/paper/Supplementary-Figure-10/Supplementary-Figure-10.ipynb +++ b/notebooks/paper/Supplementary-Figure-10/Supplementary-Figure-10.ipynb @@ -28,6 +28,7 @@ "from mpl_toolkits.axes_grid1 import make_axes_locatable\n", "\n", "from carbonplan_data import utils\n", + "\n", "alt.data_transformers.disable_max_rows()" ] }, @@ -73,10 +74,7 @@ "outputs": [], "source": [ "historical_fire = (\n", - " historical_fire.groupby(\"time.month\")\n", - " .mean()\n", - " .where(fire_mask)\n", - " .compute()[\"historical\"]\n", + " historical_fire.groupby(\"time.month\").mean().where(fire_mask).compute()[\"historical\"]\n", ") * 100 # cast to percent" ] }, diff --git a/notebooks/paper/Supplementary-Figure-11/Supplementary-Figure-11.ipynb b/notebooks/paper/Supplementary-Figure-11/Supplementary-Figure-11.ipynb index a03f701..19204d8 100644 --- a/notebooks/paper/Supplementary-Figure-11/Supplementary-Figure-11.ipynb +++ b/notebooks/paper/Supplementary-Figure-11/Supplementary-Figure-11.ipynb @@ -28,6 +28,7 @@ "from mpl_toolkits.axes_grid1 import make_axes_locatable\n", "\n", "from carbonplan_data import utils\n", + "\n", "alt.data_transformers.disable_max_rows()" ] }, @@ -83,8 +84,7 @@ "outputs": [], "source": [ "forest_mask = (\n", - " load.nlcd(store=store, year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\")\n", - " > 0.25\n", + " load.nlcd(store=store, year=2001).sel(band=[41, 42, 43, 90]).sum(\"band\") > 0.25\n", ").astype(\"float\")" ] }, @@ -95,14 +95,10 @@ "metadata": {}, "outputs": [], "source": [ - "ds_dict[\"Observed\"][\"raw\"] = load.mtbs(\n", - " store=store, coarsen=coarsen, tlim=tlim, mask=forest_mask\n", - ")\n", + "ds_dict[\"Observed\"][\"raw\"] = load.mtbs(store=store, coarsen=coarsen, tlim=tlim, mask=forest_mask)\n", "ds_dict[\"Observed\"][\"raw\"] = (\n", " ds_dict[\"Observed\"][\"raw\"]\n", - " .assign_coords(\n", - " {\"x\": ds_dict[\"Modeled\"][\"raw\"].x, \"y\": ds_dict[\"Modeled\"][\"raw\"].y}\n", - " )\n", + " .assign_coords({\"x\": ds_dict[\"Modeled\"][\"raw\"].x, \"y\": ds_dict[\"Modeled\"][\"raw\"].y})\n", " .assign_coords(\n", " {\n", " \"lat\": ds_dict[\"Modeled\"][\"raw\"].lat,\n", diff --git a/notebooks/share/fire_risks_counties.ipynb b/notebooks/share/fire_risks_counties.ipynb index 88912f5..714349d 100644 --- a/notebooks/share/fire_risks_counties.ipynb +++ b/notebooks/share/fire_risks_counties.ipynb @@ -49,9 +49,7 @@ "metadata": {}, "outputs": [], "source": [ - "counties = geopandas.read_file(\n", - " \"cb_2021_us_county_500k/cb_2021_us_county_500k.shp\"\n", - ")" + "counties = geopandas.read_file(\"cb_2021_us_county_500k/cb_2021_us_county_500k.shp\")" ] }, { @@ -99,9 +97,7 @@ "outputs": [], "source": [ "scenarios = [\"ssp245\", \"ssp370\", \"ssp585\"]\n", - "columns = [\"state_code\", \"state_name\", \"county_code\", \"county_name\"] + list(\n", - " ds.year.values\n", - ")\n", + "columns = [\"state_code\", \"state_name\", \"county_code\", \"county_name\"] + list(ds.year.values)\n", "data = {key: [] for key in scenarios}\n", "dfs = {key: [] for key in scenarios}\n", "\n", @@ -169,9 +165,7 @@ "metadata": {}, "outputs": [], "source": [ - "counties.plot(\n", - " column=\"fire\", cmap=\"hot\", vmin=0, vmax=50, figsize=(15, 10), legend=True\n", - ")" + "counties.plot(column=\"fire\", cmap=\"hot\", vmin=0, vmax=50, figsize=(15, 10), legend=True)" ] }, { diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..ae32336 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,7 @@ +[tool.isort] +profile = "black" + +[tool.black] +line-length = 100 +target-version = ['py310'] +skip-string-normalization = true diff --git a/scripts/article_prep.py b/scripts/article_prep.py index 98149b6..8ffa110 100644 --- a/scripts/article_prep.py +++ b/scripts/article_prep.py @@ -25,7 +25,7 @@ def default(self, obj): elif isinstance(obj, np.ndarray): return obj.tolist() else: - return super(NpEncoder, self).default(obj) + return super().default(obj) warnings.filterwarnings('ignore') @@ -108,7 +108,7 @@ def repackage_drought_insects(ds): all_gcms = [] for (gcm, ensemble_member) in gcms: all_gcms.append( - ds[['{}-{}'.format(gcm, scenario) for scenario in scenarios]] + ds[[f'{gcm}-{scenario}' for scenario in scenarios]] .to_array(dim='scenario', name='probability') .assign_coords({'scenario': scenarios}) ) @@ -179,7 +179,7 @@ def timeseries_dict(ds, time_period='historical'): ds = build_climate_cube() # grab the risks data else: - store_path = 'risks/results/web/{}_full.zarr'.format(impact) + store_path = f'risks/results/web/{impact}_full.zarr' ds = xr.open_zarr( get_store( 'carbonplan-forests', @@ -216,7 +216,7 @@ def timeseries_dict(ds, time_period='historical'): # loop through each of the regions of interest for region, bbox in region_bboxes.items(): - print('Calculating regional averages over the {} for {}'.format(region, impact)) + print(f'Calculating regional averages over the {region} for {impact}') # initialize the dictionary results_dict[impact][region] = {} # select out the box you want diff --git a/scripts/fire.py b/scripts/fire.py index 657bbb0..e718fc2 100644 --- a/scripts/fire.py +++ b/scripts/fire.py @@ -74,7 +74,7 @@ ) for year in np.arange(1984, 2024, 10): ds = xr.Dataset() - print('[fire] evaluating on decade beginning in {}'.format(year)) + print(f'[fire] evaluating on decade beginning in {year}') prepend_time_slice = slice(str(year - 1), str(year - 1)) analysis_time_slice = slice(str(year), str(year + 9)) prepend = climate.sel(time=prepend_time_slice) @@ -107,7 +107,7 @@ elif store == 'az': path = get_store( 'carbonplan-forests', - 'risks/results/paper/fire_terraclimate_{}.zarr'.format(run_name), + f'risks/results/paper/fire_terraclimate_{run_name}.zarr', account_key=account_key, ) if year == 1984: @@ -144,9 +144,7 @@ for year in np.arange(1970, 2100, 10): ds_future = xr.Dataset() - print( - '[fire] conducting prediction for {} {} {}'.format(cmip_model, scenario, year) - ) + print(f'[fire] conducting prediction for {cmip_model} {scenario} {year}') prepend_time_slice = slice(str(year - 1), str(year - 1)) analysis_time_slice = slice(str(year), str(year + 9)) @@ -180,7 +178,7 @@ ) path = get_store( 'carbonplan-scratch', - 'data/fire_future_{}_{}_{}.zarr'.format(run_name, cmip_model, scenario), + f'data/fire_future_{run_name}_{cmip_model}_{scenario}.zarr', account_key=account_key, ) # if it's the first year then make a fresh store by overwriting; if it's later, append to existing file @@ -210,7 +208,7 @@ scenarios = ['ssp245', 'ssp370', 'ssp585'] out_path = get_store( 'carbonplan-forests', - 'risks/results/paper/fire_cmip_{}.zarr'.format(run_name), + f'risks/results/paper/fire_cmip_{run_name}.zarr', account_key=account_key, ) @@ -221,13 +219,11 @@ for scenario in scenarios: path = get_store( 'carbonplan-scratch', - 'data/fire_future_{}_{}_{}.zarr'.format(run_name, gcm, scenario), + f'data/fire_future_{run_name}_{gcm}_{scenario}.zarr', account_key=account_key, ) - scenario_list.append( - xr.open_zarr(path).rename({'{}_{}'.format(gcm, scenario): 'probability'}) - ) - print('{} {} is done!'.format(scenario, gcm)) + scenario_list.append(xr.open_zarr(path).rename({f'{gcm}_{scenario}': 'probability'})) + print(f'{scenario} {gcm} is done!') ds = xr.concat(scenario_list, dim='scenario') ds = ds.assign_coords({'scenario': scenarios}) gcm_list.append(ds) diff --git a/scripts/website_prep.py b/scripts/website_prep.py index 335097b..74dc0ed 100644 --- a/scripts/website_prep.py +++ b/scripts/website_prep.py @@ -35,7 +35,7 @@ ds = xr.open_zarr( get_store( 'carbonplan-forests', - 'risks/results/web/{}_cmip_high_res.zarr'.format(impact), + f'risks/results/web/{impact}_cmip_high_res.zarr', account_key=account_key, ) ) @@ -60,7 +60,7 @@ # first write out the full ds which will be used for the article out_path = get_store( 'carbonplan-forests', - 'risks/results/web/{}_full.zarr'.format(impact), + f'risks/results/web/{impact}_full.zarr', account_key=account_key, ) ds.to_zarr(out_path, mode='w') @@ -69,6 +69,6 @@ ds = ds.mean(dim='gcm').probability.to_dataset(dim='scenario') out_path = get_store( - 'carbonplan-forests', 'risks/results/web/{}.zarr'.format(impact), account_key=account_key + 'carbonplan-forests', f'risks/results/web/{impact}.zarr', account_key=account_key ) ds.to_zarr(out_path, mode='w') diff --git a/setup.cfg b/setup.cfg index 5bc9dc1..189e27f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -4,12 +4,3 @@ ignore = E203,E266,E501,W503,E722,E402,C901,E731 max-line-length = 100 max-complexity = 18 select = B,C,E,F,W,T4,B9 - -[isort] -known_first_party=carbonplan_forest_risks -known_third_party=altair,carbonplan_data,cartopy,cmip6_downscaling,dask,fsspec,geopandas,intake,matplotlib,networkx,numpy,pandas,pkg_resources,pyproj,rasterio,regionmask,rioxarray,scipy,setuptools,sklearn,tenacity,tqdm,urlpath,vega_datasets,xarray,zarr -multi_line_output=3 -include_trailing_comma=True -force_grid_wrap=0 -combine_as_imports=True -line_length=100