Add possibility to have a raster path_to_mask in Workflows config file - #970
Add possibility to have a raster path_to_mask in Workflows config file#970marinebcht wants to merge 19 commits into
Conversation
|
@belletva raster path_to_mask must it necessarily be a boolean or are int+ rasters accepted? |
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
|
Hi @rhugonnet, can you confirm the process presented above to translate a raster in a actual mask ? :) Thanks |
|
@marinebcht Seems good to me. The only thing that comes to mind right now is that the reprojection step should probably be done BEFORE masking if the mask is derived from a continuous-value raster, such as elevation (e.g., elevation > 500 m and < 1000 m), but I don't think this applies to a user input for Workflows (where the mask is a file already on disk). Another question for the API and to facilitate workflow input is: Do we only accept a single type of mask file from users (0 + NaN = False, other = True), or do we also allow a tag "invert_mask" to use the binary opposite of the mask (whether it is from a raster or vector)? |
|
@belletva @rhugonnet ready to review so :) |
|
|
||
| inlier_mask = inlier_mask.reproject(dem, silent=True) | ||
| except rasterio.errors.RasterioIOError: | ||
| raise ValueError("You provided a 'path_to_mask' value that is not recognised as a mask.") |
There was a problem hiding this comment.
We should be more specific on the error message, and maybe collect original errors to return them through this one, something like:
# Treat the mask according to its type (Vector or Raster)
try:
mask = gu.Vector(mask_path)
inlier_mask = ~mask.create_mask(dem)
except pyogrio.errors.DataSourceError as vector_error:
try:
inlier_mask = gu.Raster(mask_path).astype(bool)
inlier_mask.data = inlier_mask.data.filled(False)
inlier_mask = inlier_mask.reproject(dem, silent=True)
except rasterio.errors.RasterioIOError as raster_error:
errors = ExceptionGroup(
f"Could not read {mask_path!r} as either a vector or raster mask.",
[vector_error, raster_error],
)
raise ValueError(
"You provided a 'path_to_mask' value that is not recognised as a mask."
) from errorsThere was a problem hiding this comment.
Thanks for introducing me to ExceptionGroup; I’ve just discovered it.
But what is the added value, as it is not the dependancies ?
PS1: Non-existent files also first raise an error during the schema validation step.
PS2: rasterio gives the reason above our "ValueError"
There was a problem hiding this comment.
The added value is that the errors are traced all the way back, instead of being interrupted by our own ValueError in xDEM.
Concretely: In case of an error, instead of just printing "ValueError in xdem.xxx: You provided a .... that is not recognized as a mask" and stopping there, it also prints "from RasterioIOError in rasterio.xxx: message". Most often, the "message" from Rasterio will be "GDAL could not recognize filename path_to_mask as a valid input...".
So the user knows all the underlying causes, and it's easier to debug.
Not sure I understood your point about "not in the dependencies": if related to ExceptionGroup, it is available in base Python! 😄
rhugonnet
left a comment
There was a problem hiding this comment.
Perfect, a few comments above.
I wondered: do we also need to update the documentation to specify how this behaves?
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
… rotation[/translation] (GlacioHack#975)
Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
|
|
||
| inlier_mask = inlier_mask.reproject(dem, silent=True) | ||
| except rasterio.errors.RasterioIOError: | ||
| raise ValueError("You provided a 'path_to_mask' value that is not recognised as a mask.") |
There was a problem hiding this comment.
Thanks for introducing me to ExceptionGroup; I’ve just discovered it.
But what is the added value, as it is not the dependancies ?
PS1: Non-existent files also first raise an error during the schema validation step.
PS2: rasterio gives the reason above our "ValueError"
| ::: | ||
|
|
||
| :::{note} | ||
|
|
There was a problem hiding this comment.
remove empty line
|
|
||
| To set the vertical CRS or to override one that might exist in the metadata with ``force_vcrs``, | ||
| please refer to {ref}`vertical-ref`. | ||
|
|



Resolves #954
Workflow accuracy and topo now accept raster path_to_mask, no matter its projection/footprint :
Nan values in the mask
Nan value are translated by False : see here. For example, in the glacier mask case: NaN (no data before or after reprojection) values are not conidarated as Glacier.
Operation
This mask is :