Skip to content

fix: handle 1D interpolation result in resample for single-state ODEs#75

Closed
yasumorishima wants to merge 4 commits intonasa:mainfrom
yasumorishima:fix/resample-single-state
Closed

fix: handle 1D interpolation result in resample for single-state ODEs#75
yasumorishima wants to merge 4 commits intonasa:mainfrom
yasumorishima:fix/resample-single-state

Conversation

@yasumorishima
Copy link
Copy Markdown

@yasumorishima yasumorishima commented Mar 19, 2026

Summary

Fix TrajectoryAnalysis.resample failing for ODESystems with a single state variable.

When the system has only one state, scipy's interpolation returns a 1D array (n,) instead of 2D (n, 1), causing a broadcast error on assignment to the pre-allocated 2D xs array.

The fix checks ndim and adds a column axis when needed.

Fixes #70

Test plan

  • Added 6 tests in TestResampleSingleState class:
    • Single-state basic resample
    • Resampled values match analytical solution (exponential decay)
    • include_output=False path
    • dynamic_output with single-state ODE
    • Different dt values
    • Multi-state regression (harmonic oscillator, ensures fix doesn't break existing behavior)
  • All 13 tests pass (+ 1 skipped)

Note

During testing, discovered that include_events=True (the default) triggers a separate IndexError at line 741 due to t_size under-allocation. This is the same bug reported in #71 and is not addressed in this PR. All tests use include_events=False to isolate the ndim fix.

yasumorishima and others added 4 commits March 19, 2026 11:55
When an ODESystem has only a single state variable, scipy interpolation
returns a 1D array (n,) instead of 2D (n, 1). This causes a broadcast
error when assigning to the pre-allocated 2D xs array.

Fix by checking ndim and adding a column axis when needed.

Fixes nasa#70
6 tests covering:
- Basic single-state resample
- Resampled values match analytical solution (exponential decay)
- include_output=False path
- dynamic_output with single-state ODE
- Different dt values
- Multi-state ODE regression (harmonic oscillator)

Also discovered that include_events=True triggers a separate IndexError
(issue nasa#71, t_size calculation off-by-2) — tests use include_events=False
to isolate the ndim fix from that pre-existing bug.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Address CodeRabbit review: assert resampled.velocity values match
analytical solution (-0.5 * exp(-0.5 * t)), not just successful execution.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@ixjlyons
Copy link
Copy Markdown
Contributor

Thanks for submitting - I ended up addressing this along with a bunch of other issues in #73.

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.

TrajectoryAnalysis.resample fails on ODEs with single state

2 participants