Repository navigation
Fix WeatherDataset negative index reporting and validation (#853) - #854
Open
muskan-creates352 wants to merge 3 commits into
Open
muskan-creates352 wants to merge 3 commits into
muskan-creates352 wants to merge 3 commits into
Conversation
Author
|
Per feedback on #853 from @joeloskarsson, I have updated this PR to be strictly scoped to A separate issue and PR will be opened for the window parameter validation. |
This branch has not been deployed
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.
Describe your changes
In
neural_lam/weather_dataset.py,WeatherDatasetimplements dataset indexing (__getitem__) and initialization (__init__). However, there were several indexing bugs and missing input validations:IndexErrorfor out-of-bounds negative indexing:When an out-of-range negative index was accessed (e.g.
dataset[-9]on a dataset of length 8),idxwas mutated in-place by addingn_samples(-9 + 8 = -1). The raisedIndexErrorclaimedindex -1 out of range for WeatherDataset of length 8, which was misleading and self-contradictory because-1is within bounds in Python (the last element). Fixed to preserve the original requested index in the error message.__getitem__:Passing non-integer keys raised obscure errors (such as
TypeError: '<' not supported between instances of 'str' and 'int'for strings or deep numpy/xarray slicing TypeErrors for floats) or silently accessed index 1 for booleanTrue. Added validation to ensureidxis an integer and not a boolean, raising a cleanTypeError.__init__:num_past_forcing_steps,num_future_forcing_steps,num_past_boundary_steps, andnum_future_boundary_stepspreviously accepted negative values (e.g.-1), silently corrupting windowed forcing tensor dimensions. Added validation requiring non-negative integers (splitvalidation in__init__:Added validation that
splitis one of("train", "val", "test"), raisingValueErrorotherwise.Summary of changes:
WeatherDataset.__getitem__:isinstance(idx, bool) or not isinstance(idx, int)check raisingTypeError.resolved_idx = idx + n_samples if idx < 0 else idx, preserving originalidxinIndexError.Raisessection withTypeErrorandIndexError.WeatherDataset.__init__:split in ("train", "val", "test").num_past_forcing_steps,num_future_forcing_steps,num_past_boundary_steps, andnum_future_boundary_stepsare non-negative integers (Raisessection.tests/test_datasets.py:test_dataset_out_of_range_raises_index_errorto assert the exact out-of-range index message (-n - 1).test_weather_dataset_non_integer_index_raises_type_errorcovering["0", 1.5, True, False, [0], None].test_weather_dataset_window_parameter_validation_raisescovering all 4 window parameters across invalid types and negative values.test_weather_dataset_invalid_split_raisescovering invalid split strings.### Fixedreferencing Fix WeatherDataset negative index reporting, key type validation, and window parameter validation #853.ruff check,ruff format,ty).Issue Link
Closes #853
Type of change
Checklist before requesting a review
pullwith--rebaseoption if possible).Checklist for reviewers
Each PR comes with its own improvements and flaws. The reviewer should check the following:
Author checklist after completed review
reflecting type of change (add section where missing):
Checklist for assignee