svmbir
svmbir copied to clipboard
Input warning not helpful and problem with numpy data types
It seems very reasonable to iterate over parameter arrays such as
num_threads = np.array([2, 4]) # num_threads[n] is of type numpy.int64
for n in range(2):
result[n] = svmbir.recon( ..., num_threads=num_threads[n])
However this results in the somewhat useless warning:
UserWarning: Parameter num_threads is not a valid int. Setting to default.
The warning comes from _utils.py
:
if not (isinstance(num_threads, int) and (num_threads > 0)):
warnings.warn(f"Parameter num_threads is not a valid int. Setting to default.")
num_threads = None
Problems with this:
- First I thought that
UserWarning: Parameter num_threads is not a valid int. Setting to default.
will take my2 (numpy.int64)
and convert it to2 (int)
. But instead it will completely disregard the2
and set it to the number of cores!!!
In my opinion the hard setting to the defaults whenever an input is not precisely what is expected seems very strict and can lead to unexpected behavior.
-
Whenever a variable get checked it is advised to display the variable and type. Something like
warnings.warn(f"Parameter num_threads ({num_threads}, {type(num_threads)}) is not a valid int. Setting to default.")
would giveUserWarning: Parameter num_threads (2, <class 'numpy.int64'>) is not a valid int. Setting to default.
Now it is so much easier to understand what is wrong. -
when
num_threads=None
this warning is still displayed and thennum_threads
is again set toNone
. -
In my opinion
numpy.int64
should be a perfectly reasonable input. Why not doisinstance(num_threads, (int, np.int64, np.int32))
? There may also be more elegant ways of doing this. -
The logic in the if clause is not obvious. Can there be more parentheses?
-
In
_utils.py
there are more instances like this for other parameters that should be improved.