Skip to content

Fix WeatherDataset negative index reporting and validation (#853) - #854

Open
muskan-creates352 wants to merge 3 commits into
mllam:mainfrom
muskan-creates352:fix-weather-dataset-indexing-validation
Open

muskan-creates352 wants to merge 3 commits into
mllam:mainfrom
muskan-creates352:fix-weather-dataset-indexing-validation

Conversation

@muskan-creates352

Copy link
Copy Markdown

Describe your changes

In neural_lam/weather_dataset.py, WeatherDataset implements dataset indexing (__getitem__) and initialization (__init__). However, there were several indexing bugs and missing input validations:

  1. Misleading index reporting in IndexError for out-of-bounds negative indexing:
    When an out-of-range negative index was accessed (e.g. dataset[-9] on a dataset of length 8), idx was mutated in-place by adding n_samples (-9 + 8 = -1). The raised IndexError claimed index -1 out of range for WeatherDataset of length 8, which was misleading and self-contradictory because -1 is within bounds in Python (the last element). Fixed to preserve the original requested index in the error message.
  2. Missing key type validation in __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 boolean True. Added validation to ensure idx is an integer and not a boolean, raising a clean TypeError.
  3. Missing window parameters validation in __init__:
    num_past_forcing_steps, num_future_forcing_steps, num_past_boundary_steps, and num_future_boundary_steps previously accepted negative values (e.g. -1), silently corrupting windowed forcing tensor dimensions. Added validation requiring non-negative integers ($\ge 0$).
  4. Missing split validation in __init__:
    Added validation that split is one of ("train", "val", "test"), raising ValueError otherwise.

Summary of changes:

  1. WeatherDataset.__getitem__:
    • Added isinstance(idx, bool) or not isinstance(idx, int) check raising TypeError.
    • Computed resolved_idx = idx + n_samples if idx < 0 else idx, preserving original idx in IndexError.
    • Updated docstring Raises section with TypeError and IndexError.
  2. WeatherDataset.__init__:
    • Validated that split in ("train", "val", "test").
    • Validated that num_past_forcing_steps, num_future_forcing_steps, num_past_boundary_steps, and num_future_boundary_steps are non-negative integers ($\ge 0$).
    • Updated docstring Raises section.
  3. Unit tests in tests/test_datasets.py:
    • Updated test_dataset_out_of_range_raises_index_error to assert the exact out-of-range index message (-n - 1).
    • Added test_weather_dataset_non_integer_index_raises_type_error covering ["0", 1.5, True, False, [0], None].
    • Added test_weather_dataset_window_parameter_validation_raises covering all 4 window parameters across invalid types and negative values.
    • Added test_weather_dataset_invalid_split_raises covering invalid split strings.
  4. Changelog: Added an entry under ### Fixed referencing Fix WeatherDataset negative index reporting, key type validation, and window parameter validation #853.
  5. Code Style: Kept all line lengths <= 80 characters. Passed all pre-commit hooks (ruff check, ruff format, ty).

Issue Link

Closes #853

Type of change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation (Addition or improvements to documentation)

Checklist before requesting a review

  • My branch is up-to-date with the target branch - if not update your fork with the changes from the target branch (use pull with --rebase option if possible).
  • I have performed a self-review of my code
  • For any new/modified functions/classes I have added docstrings that clearly describe its purpose, expected inputs and returned values
  • I have placed in-line comments to clarify the intent of any hard-to-understand passages of my code
  • I have updated the README to cover introduced code changes
  • If this PR changes contributor workflow, I've updated CONTRIBUTING.md / AGENTS.md
  • I have added tests that prove my fix is effective or that my feature works
  • I have given the PR a name that clearly describes the change, written in imperative form (context).
  • I have requested a reviewer and an assignee (assignee is responsible for merging). This applies only if you have write access to the repo, otherwise feel free to tag a maintainer to add a reviewer and assignee.

Checklist for reviewers

Each PR comes with its own improvements and flaws. The reviewer should check the following:

  • the code is readable
  • the code is well tested
  • the code is documented (including return types and parameters)
  • the code is easy to maintain

Author checklist after completed review

  • I have added a line to the CHANGELOG describing this change, in a section
    reflecting type of change (add section where missing):
    • added: when you have added new functionality
    • changed: when default behaviour of the code has been changed
    • fixes: when your contribution fixes a bug
    • maintenance: when your contribution is relates to repo maintenance, e.g. CI/CD or documentation

Checklist for assignee

  • PR is up to date with the target branch
  • PR title is in the imperative form
  • PR have a description that clearly explains the purpose of the change
  • At least one reviewer approved the PR
  • All checks have passed
  • Relevant documentation has been updated
  • Changelog entry is added
  • PR is labelled properly
  • If any visual changes were introduced, screenshots or videos are included in the description
  • PR is linked to an issue
  • Issue is assigned to a milestone (optional)

@muskan-creates352

Copy link
Copy Markdown
Author

Per feedback on #853 from @joeloskarsson, I have updated this PR to be strictly scoped to WeatherDataset.__getitem__ (key type validation and negative index error reporting).

A separate issue and PR will be opened for the window parameter validation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix WeatherDataset negative index reporting, key type validation, and window parameter validation

1 participant