Skip to content

Fix ExclusionContainer.add_raster ignoring codes=0 - #525

Open
Blizzeq wants to merge 2 commits into
PyPSA:masterfrom
Blizzeq:fix/exclusion-raster-code-zero
Open

Blizzeq wants to merge 2 commits into
PyPSA:masterfrom
Blizzeq:fix/exclusion-raster-code-zero

Conversation

@Blizzeq

@Blizzeq Blizzeq commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Changes proposed in this Pull Request

ExclusionContainer.add_raster(raster, codes=0) currently excludes every non-zero cell instead of the cells with value 0, because shape_availability checks the truthiness of codes and the integer 0 is handled like a missing argument. The same check breaks numpy arrays: np.array([0]) gives the inverted mask and a longer array raises a ValueError. I changed the check to is not None, so 0 and arrays now behave like the list form codes=[0]. One side effect to point out: an empty list used to fall back to "exclude all non-zero cells" and now excludes nothing. I also corrected the docstring, which said the default is 1 while it is None. The new test in test/test_gis.py needs no download and fails on master.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in doc.
  • Unit tests for new features were added (if applicable).
  • Newly introduced dependencies are added to environment.yaml, environment_docs.yaml and setup.py (if applicable).
  • A note for the release notes doc/release_notes.rst of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

shape_availability checked the truthiness of codes, so the integer 0
was handled like a missing codes argument: all non-zero cells were
excluded instead of the cells with value 0. A numpy array raised a
ValueError for the same reason. Check for None instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant