scikit-hep / iminuit

Jupyter-friendly Python interface for C++ MINUIT2
https://scikit-hep.org/iminuit
Other
278 stars 71 forks source link

cost.LeastSquares fails with data with more than 2 dimensions #974

Open FinnStokes opened 4 months ago

FinnStokes commented 4 months ago

For multidimensional x data formatted as a DxN array as described in the documentation, any D greater than 2 will cause iminuit to fail with the following error:

File "/usr/lib/python3.11/site-packages/iminuit/cost.py", line 1721, in _call
    y, yerror = self._masked.T[self._ndim :]
    ^^^^^^^^^
ValueError: too many values to unpack (expected 2)

See https://stackoverflow.com/questions/76127508/iminuit-high-dimension-multivariate-fit-problem

I believe this is because the following line assumes that x.ndim gives D: https://github.com/scikit-hep/iminuit/blob/298afadc645999fbad32ca00be6c0ebfec7c7bb9/src/iminuit/cost.py#L2194

However, with correctly formatted data, x.ndim can only give 1 or 2, and D is instead given by np.atleast_2d(x).shape[0] or simply x.shape[0] if the atleast_2d call that is already in the method is moved earlier.

HDembinski commented 3 months ago

Thanks for reporting. There is indeed no test for 3D data and so the mistake slipped through. I will see how to fix this.