svmbir icon indicating copy to clipboard operation
svmbir copied to clipboard

Input warning not helpful and problem with numpy data types

Open tbalke opened this issue 2 years ago • 4 comments

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:

  1. First I thought that UserWarning: Parameter num_threads is not a valid int. Setting to default. will take my 2 (numpy.int64) and convert it to 2 (int). But instead it will completely disregard the 2 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.

  1. 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 give UserWarning: 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.

  2. when num_threads=None this warning is still displayed and then num_threads is again set to None.

  3. In my opinion numpy.int64 should be a perfectly reasonable input. Why not do isinstance(num_threads, (int, np.int64, np.int32))? There may also be more elegant ways of doing this.

  4. The logic in the if clause is not obvious. Can there be more parentheses?

  5. In _utils.py there are more instances like this for other parameters that should be improved.

tbalke avatar Nov 13 '21 04:11 tbalke