Add explicit error handling in _make_tiles.py#1130
Conversation
Add upfront parameter validation with clear error messages to make_tiles(), make_tiles_from_spots(), and their helpers. Validates image/spots keys exist, tile_size is positive, and min_tissue_fraction is in [0, 1]. Adds 5 test cases for the new error conditions. Closes #1086 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
for more information, see https://pre-commit.ci
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
| if image_key not in sdata.images: | ||
| raise KeyError(f"Image key '{image_key}' not found in sdata.images. Available: {list(sdata.images.keys())}") |
There was a problem hiding this comment.
these can be one liners with a common check function. also easier to read
There was a problem hiding this comment.
I prefer not to code-golf at the sake of readability. Generally I find the asser thing, text pattern ugly
There was a problem hiding this comment.
It's not just code golfing, it makes sense as a function itself. When the error message or access pattern changes you could change it from the function instead modifying it everywhere. Also I find it more readable this way. I can just skip reading those functions as they aren't part of the core algo. These kinds of trivial checks bloat the core function.
Idk something like this
def check_element_exists(
sdata,
key,
element_type,
):
store = getattr(sdata, element_type)
if key not in store:
available = list(store.keys())
raise KeyError(
f"Key {key!r} not found in sdata.{element_type}. "
f"Available keys: {available}"
)There was a problem hiding this comment.
I am wondering if we should just let it error from spatialdata? Like the error message would be almost the same
There was a problem hiding this comment.
I am wondering if we should just let it error from spatialdata? Like the error message would be almost the same
I see the rational but dislike it in this case because sdata core has quite a few error messages that a biologist probably wouldn't intuitively understand and they also usually don't proactively offer help by f.e. giving the valid keys. There's also quite a few empty asserts which I find quite problematic.
When the error message or access pattern changes you could change it from the function instead modifying it everywhere. Also I find it more readable this way. I can just skip reading those functions as they aren't part of the core algo. These kinds of trivial checks bloat the core function.
I do see that point, but I think then it should be a separate PR where we do this across the entire codebase. wdyt?
There was a problem hiding this comment.
Sure it can be a separate PR but isn't this Pr already small. Why not start here?
Update: sorry was in the bus so the comment was incomplete bc of that
|
but does this get rid of the try except block as written in the issue linked? |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1130 +/- ##
==========================================
- Coverage 73.10% 72.96% -0.14%
==========================================
Files 38 38
Lines 6473 6484 +11
Branches 1144 1151 +7
==========================================
- Hits 4732 4731 -1
- Misses 1265 1270 +5
- Partials 476 483 +7
🚀 New features to boost your workflow:
|
get_transformation(element, get_all=True) cannot raise KeyError or ValueError for valid SpatialData elements, and callers already validate elements exist before calling this helper. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
True, we're validating beforehand, so the function should never crash and burn. |
selmanozleyen
left a comment
There was a problem hiding this comment.
sorry but I stand by the comments I made earlier.
From the other codes I have read, I find these changes reasonable to expect.
For example see:
https://github.com/scverse/annbatch/blob/d47240f883f64b86524d6bc32731ce3e15be2984/src/annbatch/utils.py#L114
I don't think this is overengineering or anything I think it is a necessity
I just think it's suboptimal to do one function here and then others in a separate PR. If we do something like that, it should be across the entire package. I can just do that in this PR and rename it, but I'd rather not sneak in such a major refactor under "added error handling" |
ok if you say so. I just feel like there are already lot's one can refactor we might as well start when we add new code but if you think you can do the PR then sure. |
|
discussed: have separate PR for type checking refactor |
Summary
KeyError/ValueErrormessages tomake_tiles(),make_tiles_from_spots(),_get_largest_scale_dimensions(), and_get_spot_coordinates()sdata,tile_sizehas positive values, andmin_tissue_fractionis in [0, 1]Closes #1086
Test plan
test_make_tiles_invalid_image_key— bad image key raisesKeyErrortest_make_tiles_invalid_tile_size— non-positive tile size raisesValueErrortest_make_tiles_invalid_min_tissue_fraction— out-of-range fraction raisesValueErrortest_make_tiles_from_spots_invalid_spots_key— bad spots key raisesKeyErrortest_make_tiles_from_spots_invalid_image_key— bad image key raisesKeyError🤖 Generated with Claude Code