Wrong conversion of bool array to numpy array

Hi,

The following code used to work prior to ROOT 6.38 (I think last tested in ROOT 6.30)

self.Focalsurface = np.array(self.tree.focal_surface, dtype=bool)

not depending on the dimensions of self.tree.focal_surface. I tested it on [1][1] and [2][2]. With ROOT 6.38 it works on [1][1], but not on [2][2]. With focal_surface[2][2]/O I get:

  File "/home/lewhoo/workspace/etos/eusottrees/exptree.py", line 107, in __init__
    self.Focalsurface = np.array(self.tree.focal_surface, dtype=bool)
                        ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Item size 8 for PEP 3118 buffer format string ? does not match the dtype ? item size 1.

The workaround is to have:

			if isinstance(self.tree.focal_surface, bool):
				self.Focalsurface = np.array(self.tree.focal_surface, dtype=bool)
			else:
				self.Focalsurface = np.frombuffer(self.tree.focal_surface, dtype=np.bool, count=self.FocalsurfaceMaxPdmX * self.FocalsurfaceMaxPdmY)

which shows another bug: 1x1 is pythonised to scalar, so frombuffer can’t be used on it. It would give the following error:

    self.Focalsurface = np.frombuffer(self.tree.focal_surface, dtype=np.bool, count=self.FocalsurfaceMaxPdmX * self.FocalsurfaceMaxPdmY)
                        ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: a bytes-like object is required, not 'bool'


ROOT Version: 6.38
Platform: Fedora 43


Hello @LeWhoo , thank you for reporting this :grin:

The solution is not so trivial, so I opened a Github issue that reflects your problem, you can follow the discussion here for updates: [Python] NumPy conversion fails for multidimensional arrays (incorrect itemsize in LowLevelView) · Issue #21378 · root-project/root · GitHub

Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.