Python: boolean operator of dataObject gives an unwanted behaviour

Create issue
Issue #119 new
M. Gronle created an issue

Currently, the bool operator of itom.dataObject gives an unwanted behaviour:

import numpy as np

print(bool(dataObject.zeros([1,]))) # -> True (ERROR)
print(bool(dataObject.ones([1,])))# -> True (OK)
print(bool(dataObject.zeros([2,2])))# -> True (ERROR)
print(bool(dataObject.ones([2,2])))# -> True (ERROR)

print(bool(np.zeros([1,])))# -> False
print(bool(np.ones([1,])))# -> True
print(bool(np.zeros([2,2]))) # -> ValueError
print(bool(np.ones([2,2]))) # -> ValueError

The dataObject should behave like numpy arrays. The bool operator is also used, if the dataObject is used in an if condition.

The desired behaviour is:

  1. If the dataObject or np.ndarray has one value, the bool operator returns False if the value is 0, else True.
  2. In any other case, the bool operator return a ValueError with the message The truth value of an array with more than one element is ambiguous.

Currently, the if-condition:

d = dataObject.zeros([1,])
if d:

always had the output yes. This is not logical and has to be changed.

Currently (itom > 3.2.1, but < 4.0), the bool operator of itom.dataObject will raise a temporary warning, saying that the behaviour will change in the future.

Question: When is the right time to change the behaviour to the same behaviour than Numpy? itom 4.0 or later?

Comments (5)

  1. M. Gronle reporter

    2020-04-16: Decision: The old behaviour with the deprecated warning will be kept active (e.g. for the itom setup 4.0) and will be changed to the new behaviour in 6-12 month (with the next official setup)

  2. Log in to comment