Skip to content

BUG: Encoder fails due to numerical issues edge cases #837

@aasitvora99

Description

@aasitvora99

Describe the bug

3931502 pulled to enh/discrete-encoding breaks current encoding solution on Infinity. It couldn't handle cases where to_dict fails due to numerical issues. Reverting to a previous commit works fine and shows output properly.

To Reproduce

  • Start Infinity on encoders_official_discretize branch with RPy sourced from enh/discrete-encoding
  • Hit this url on any REST client
curl https://api.rocketpy.org/flights/686ac15e6ce2266e7375bc4a/simulate
  • Observe a chain of errors show up on infinity console.

Expected behavior

No errors ig

Screenshots

2025-07-06 19:02:42,277 - INFO - Call to repositories.flight.find_by_id completed for {'data_id': '680e2fcd275e75a42aef1aee'}
2025-07-06 19:02:42,277 - INFO - Call to repositories.flight.read_flight_by_id completed for {}
2025-07-06 19:02:42,277 - INFO - Call to _get_model completed for FlightController
/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/motors/motor.py:990: UserWarning: burn_time argument (0, 4.264) is out of thrust source time range. Using thrust_source boundary times instead: (0.082, 4.264) s.
If you want to change the burn out time of the curve please use the 'reshape_thrust_curve' argument.
  warnings.warn(
/Users/aasitvora/Desktop/chintu/Infinity-API/.venv/lib/python3.13/site-packages/scipy/integrate/_ivp/lsoda.py:161: UserWarning: lsoda: Repeated convergence failures (perhaps bad Jacobian or tolerances).
  solver._y, solver.t = integrator.run(
/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py:1880: RuntimeWarning: invalid value encountered in divide
  vector_b.extend(3 * ((y[2:] - y[1:-1]) / h[1:] - (y[1:-1] - y[:-2]) / h[:-1]))
2025-07-06 19:02:42,639 - ERROR - get_flight_simulation: Unexpected error array must not contain infs or NaNs
Traceback (most recent call last):
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 3766, in __get__
    val = cache[self.attrname]
          ~~~~~^^^^^^^^^^^^^^^
KeyError: 'ax'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 3771, in __get__
    val = self.func(instance).reset(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'numpy.ndarray' object has no attribute 'reset'. Did you mean: 'repeat'?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/src/controllers/interface.py", line 16, in wrapper
    return await method(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/src/controllers/flight.py", line 108, in get_flight_simulation
    return flight_service.get_flight_simulation()
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/src/services/flight.py", line 61, in get_flight_simulation
    attributes = rocketpy_encoder(self.flight)
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/src/utils.py", line 29, in rocketpy_encoder
    json_str = json.dumps(
        obj,
    ...<4 lines>...
        allow_pickle=False,
    )
  File "/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
          ~~~~~~^^^^^
  File "/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 200, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py", line 261, in iterencode
    return _iterencode(o, 0)
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/_encoders.py", line 74, in default
    encoding = o.to_dict(
        include_outputs=self.include_outputs,
        discretize=self.discretize,
        allow_pickle=self.allow_pickle,
    )
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/simulation/flight.py", line 3539, in to_dict
    "ax": self.ax,
          ^^^^^^^
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 3775, in __get__
    val = Function(source, *args, **kwargs)
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 158, in __init__
    self.set_source(self.source)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 277, in set_source
    self.set_interpolation(self.__interpolation__)
    ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 322, in set_interpolation
    self.__update_interpolation_coefficients(self.__interpolation__)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 337, in __update_interpolation_coefficients
    self.__interpolate_spline__()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/aasitvora/Desktop/chintu/RocketPy/rocketpy/mathutils/function.py", line 1883, in __interpolate_spline__
    c = linalg.solve_banded(
        (1, 1), banded_matrix, vector_b, overwrite_ab=True, overwrite_b=True
    )
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/.venv/lib/python3.13/site-packages/scipy/linalg/_basic.py", line 596, in solve_banded
    b1 = _asarray_validated(b, check_finite=check_finite, as_inexact=True)
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/.venv/lib/python3.13/site-packages/scipy/_lib/_util.py", line 537, in _asarray_validated
    a = toarray(a)
  File "/Users/aasitvora/Desktop/chintu/Infinity-API/.venv/lib/python3.13/site-packages/numpy/lib/function_base.py", line 630, in asarray_chkfinite
    raise ValueError(
        "array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs
2025-07-06 19:02:42,646 - INFO - Call to get_flight_simulation completed for FlightController

Additional context

Add any other context about the problem here.

Metadata

Metadata

Assignees

Labels

BugSomething isn't working

Type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions