Skip to content

Fix: ValueError when x_dtype != y_dtype with numpy 2.x#36

Open
yousuke08 wants to merge 1 commit into
donghoonpark:masterfrom
yousuke08:fix/numpy2-compatibility
Open

Fix: ValueError when x_dtype != y_dtype with numpy 2.x#36
yousuke08 wants to merge 1 commit into
donghoonpark:masterfrom
yousuke08:fix/numpy2-compatibility

Conversation

@yousuke08

Copy link
Copy Markdown

Problem

When parsing LTspice RAW files where x_dtype (time data type) differs from y_dtype (variable data type), the library raises a ValueError: ValueError: setting an array element with a sequence.
TypeError: only 0-dimensional arrays can be converted to Python scalars

Root Cause

In parse() method, when x_dtype != y_dtype, the code tries to assign an array (returned by np.frombuffer()) to a scalar position (self.x_raw[i]):

self.x_raw[i] = np.frombuffer(d, dtype=self._x_dtype)
This fails with numpy 2.x due to stricter type checking.

Solution

Changed the approach to concatenate byte buffers first, then convert to array in one operation:

time_buffer = b""
for i in range(self._point_num):
    start = i * (self._variable_num + diff) * variable_data_size
    end = start + time_data_size
    time_buffer += data[start:end]
self.x_raw = np.frombuffer(time_buffer, dtype=self._x_dtype)

Testing

  • Tested with a RAW file (119,238 points, 14 signals) where x_dtype=float64 and y_dtype=float32
  • Successfully parsed all signals.
  • Tested with Python 3.14, numpy 2.4.2

Environment

  • Python 3.14.2
  • numpy 2.4.2
  • ltspice 1.0.6

- Fix numpy 2.x compatibility issue in parse() method
- Changed time data extraction from per-element assignment to buffer concatenation
- Fixes 'ValueError: setting an array element with a sequence'
- Tested with Python 3.14, numpy 2.4.2
@mimeiners

Copy link
Copy Markdown

Thx for that fix @yousuke08, works for me with Python 3.13.13 and numpy 2.4.4
Please, merge this into main branch soon @donghoonpark.

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