Refactor masking logic#1815
Conversation
…ved to common masking utilities
…odify mask_arrays so that it can catch integers as well.
…gument doesn't automatically align the mask.
|
I had Chat-GPT Codex create some profiling whether alignment should be done with We probably achieve similar performance as before with this as before this PR. We can consider pre-aligning masks if alignment turns out to be a major bottleneck. Bonus: Because we now have to align explicitly in our codebase, the alignment will show quicker in a profiling exercise. |
| da = self.dataset[var] | ||
| array_mask = _adjust_mask_for_unlayered_data(da, mask) | ||
| active = array_mask > 0 | ||
| def is_float(da: GridDataArray) -> bool: |
There was a problem hiding this comment.
This method and the is_integer method are very generic and can probably also be useful in other places.
If i recall corretly we also have a dataarray utility class or something like that. Would it make sense to move this method there?
There was a problem hiding this comment.
Good one, I looked into this and were were doing similar things in regrid and clipping modules. I have moved them to imod.common.utilities.dtype
There was a problem hiding this comment.
I discovered a hidden bug here, so good that we did this: is_float returned true when getting numpy integers. Big bug, fortunately with little impact as I always first checked is_integer, then is_float. I have fixed this now.
|
|
||
| float = np.nan | ||
| integer = 0 | ||
| msw_default = -9999.0 |
There was a problem hiding this comment.
Do we have more of these constant values in our code?
If so we could consider storing them in a separate module (config? constants?) or a separate file.
The idea behind that move is to break any unintentional coupling. For instance if a class would import this file to use the mask values it will also import the classes and method from imod.typing.grid (lines 13 to 20) which it might not need.
At the moment i don't think there is an issues because all the files that use the MaskValues already import imod.typing.grid themselves
There was a problem hiding this comment.
Good one, I have moved this to a new module imod.common.constants. I also saw that in the regridding and clipping modules we also used these values to mask. We can now get MaskValues instead in these modules, without having to rely on imod.common.utilities.mask. I applied that immediately to clean the code up more.
|
Nice job @JoerivanEngelen! Just wondering, did you also see a performance increase in the testbench? |
…nd call function from masking module
Thanks, I didn't see any major performance differences, but that is expected: xarray's |
… bug, fortunately with little impact as I always first checked is_integer, then is_float.
|



Description
I was busy prototyping some cleanup logic for the storage package and noticed there was still useful logic for masking in the metaswap utilities. This refactor improves the
imod.common.maskconsiderably:MaskValuesdataclass gets a broader application.wherewithotherspecified.