Repository navigation
Bugfix forced log scales in raster plots - #1334
Open
ChrisFairless wants to merge 2 commits into
Open
ChrisFairless wants to merge 2 commits into
ChrisFairless wants to merge 2 commits into
Conversation
ChrisFairless
requested review from
chahank,
emanuel-schmid and
peanutfun
as code owners
October 10, 2026 01:29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes proposed in this PR:
I hope you're ready for another PR from me where the explanation is 20 times longer than the actual fix!
Overview:
When you want to plot a raster for an Exposure or an Impact, so that plot
then CLIMADA unintentionally overrides your choice of function.
More precisely, when
Exposures.plot_raster()hasfill=False, it overrides the user-providedraster_fparameter.In detail:
In the method,
raster_fis a lambda function that transforms the value of each raster cell and is logarithmic by default (raster_f=lambda x: np.log10((np.fmax(x + 1, 1)))). This bug means you can't actually plot a raster with its raw values (i.e. withraster_f: lambda x: x).The culprit is in lines 1063-1065:
The intention made sense: the default parameters with
fill=Trueuseraster_f=lambda x: np.log10((np.fmax(x + 1, 1))). This lambda usesnp.fmax, meaningraster_f(np.nan) = 0meaning that missing values are treated as zeroes and coloured accordingly.But when
fill=Falsethe lines above kick in and override with a newraster_f=lambda x: np.log10((np.maximum(x + 1, 1))). Usingnp.maximumnow meansraster_f(np.nan) = np.naninstead, and therefore missing values (and zeroes, due to the mask applied one line earlier) are treated as missing. Which was a neat trick. But it forget thatraster_fcould be any function, and we don't actually to override it.The fix:
The solution is just to apply a missing value mask if
fill=Falseinstead.Aside: but wait, what is the method trying to do?
It took me a while to work out the intended behaviour:
fill=True:np.nan-> 0fill=False:np.nan->np.nanfill=True: 0 -> 0fill=False: 0 -> ?According to the docstring, this should be 0 BUT the code explicitly masks them, making them
np.nan.I assume the code is exactly what is intended and we always want to mask zero values when
fill=False. This makes sense when plotting financially-valued assets.So that's what I've implemented (
fill=False: 0 ->np.nan) and I've updated the docstring.Finally: I think what this function actually wanted to accomplish all this time was a
mpl.colors.LogNormcolourbar, like inplot.plot_from_gdf. But let's not go into that.Tests:
Codex wrote an obscure test that took me 10 minutes to understand so I thought it wasn't worth including. A smarter approach would be to move all the masking and transforming into a separate helper function and test that, but I'm not sure we really need it, and this PR is long enough already???
PR Author Checklist
develop)PR Reviewer Checklist