Fix Block.linearize() DynamicOperator crash, Register 0-d array bug, and duplicate TestLUT1D class by miguelpi314 · Pull Request #240 · pathsim/pathsim · GitHub
Skip to content

Fix Block.linearize() DynamicOperator crash, Register 0-d array bug, and duplicate TestLUT1D class - #240

Merged
milanofthe merged 2 commits into
pathsim:masterfrom
miguelpi314:fix/block-linearize-register-lut1d-bugs
Jul 26, 2026
Merged

milanofthe merged 2 commits into
pathsim:masterfrom
miguelpi314:fix/block-linearize-register-lut1d-bugs

Conversation

@miguelpi314

Copy link
Copy Markdown

Summary

Three independent bugs found while building the aircraft trim/linearize example in #237, split out on their own per review there — none of them touch the linearization design discussed in #195/#239.

Changes

  • blocks/_block.py: fix Block.linearize() TypeError for stateless blocks whose op_alg is a DynamicOperator (e.g. DynamicalFunction); regression test in test_function.py.
  • utils/register.py: update_from_array now handles 0-d numpy arrays (e.g. from scipy.interpolate.interp1d called on a scalar) as scalars instead of crashing on len(); __setitem__ now tolerates a 1-element array assigned to a scalar index.
  • tests/pathsim/blocks/test_table.py: two classes were both named TestLUT1D, silently shadowing 8 tests — including the ones that would have caught the register bug above. Renamed the first to TestLUT1DEmbedding; fixed one test's wrong expected value (2.25 -> 2.5, LUT1D interpolates linearly); skipped test_sensitivity with an explicit reason (imports pathsim.optim.value.Value, which doesn't exist anywhere in src/).

Test plan

  • pytest -m "slow or not slow" passes (1171 passed, 14 skipped, 1 pre-existing unrelated FMU failure also present on master)
  • New code covered by tests under tests/pathsim/

Miguel added 2 commits July 25, 2026 20:05
… op_alg

Block.linearize()'s no-engine branch called op_alg.linearize(u) with a
single argument regardless of operator type, which raised TypeError for any
stateless block whose op_alg is a DynamicOperator (e.g. DynamicalFunction),
since DynamicOperator.linearize requires (x, u, t). Dispatch on the
operator type instead. No test previously exercised this combination.
Found while building an aircraft trim example using LUT1D as a CL(alpha)/
Cm(alpha) lookup table. LUT1D's underlying scipy interp1d call, for a
single scalar output, returns a 0-d numpy array; Register.update_from_array
did len(arr) on anything isinstance(arr, np.ndarray), which raises
TypeError for 0-d arrays. Special-case 0-d arrays as scalars.

Root cause of why this went uncaught: tests/pathsim/blocks/test_table.py
had two classes both named TestLUT1D. Python silently let the second
shadow the first at module import, so pytest never collected the first
class's tests (test_embedding_siso, test_update_siso, etc.) -- including
the coverage that would have caught this. Renamed the first class to
TestLUT1DEmbedding to restore it.

Unshadowing surfaced two more latent issues, fixed here:
- Register.__setitem__ didn't tolerate a 1-element numpy array assigned to
  a scalar int index (numpy 2.x: "setting an array element with a
  sequence") -- extended the existing 0-d scalar-coercion branch.
- test_linearization_siso asserted the wrong expected value: linear
  interpolation between grid points (1, 1) and (2, 4) at x=1.5 correctly
  gives 2.5, not the exact quadratic 1.5**2=2.25 the test asserted.

test_sensitivity in the same file imports pathsim.optim.value.Value, which
does not exist anywhere in the current src/ tree -- looks like a stale test
for removed/never-merged AD functionality. Marked @unittest.skip with an
explicit reason instead of guessing at reimplementing it; worth reporting
separately.
@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

@milanofthe
milanofthe merged commit 789766c into pathsim:master Jul 26, 2026
2 checks passed
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.

2 participants