Wrong ND vector conversion to numpy array in PyROOT

Hello,

The newest ROOT 6.34.04 can’t correctly convert a 2D or 3D vector to a numpy array, while 6.30.06 was doing it correctly. Here is an example code:

import ROOT
import numpy as np

a = ROOT.vector("vector<vector<float>>")() # 3D
a1 = ROOT.vector("vector<float>")() # 2D
a2 = ROOT.vector("float")()
a2.push_back(1.)
a2.push_back(2.)
a2.push_back(3.)
a1.push_back(a2)
a1.push_back(a2)
a1.push_back(a2)
a.push_back(a1)
a.push_back(a1)
a.push_back(a1)

print(a)

print(np.array(a).shape) # 3D
print(np.array(a1).shape) # 2D

The output should be, as given by 6.30.06:

{ { { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f } }, { { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f } }, { { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f } } }
(3, 3, 3)
(3, 3)

However, for 6.34.04 it is:

{ { { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f } }, { { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f } }, { { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f }, { 1.00000f, 2.00000f, 3.00000f } } }
(3,)
(3,)

This bug is critical for those who use PyROOT to read vectors with more than 1D.

Hi,

Thanks for the report.
We acknowledge the behaviour: [Python Interface] Regression: can't properly convert a vector to 2D/3D numpy array · Issue #17729 · root-project/root · GitHub
Python Interface experts have been informed.

D

Hi Danilo,
Did the code run for you? On 6.32.10 I get an error

    print(np.array(a).shape) # 3D
          ^^^^^^^^^^^
TypeError: __array__() takes no keyword arguments

but using np.asarray(a) (same for a1) works --showing the same wrong output as the original post.