Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assd function crashes on different size arrays #131

Closed
BarY7 opened this issue Apr 4, 2024 · 5 comments · Fixed by #134
Closed

assd function crashes on different size arrays #131

BarY7 opened this issue Apr 4, 2024 · 5 comments · Fixed by #134
Assignees
Labels

Comments

@BarY7
Copy link

BarY7 commented Apr 4, 2024

Hello, first time using this lib, but I think a bug was introduced in the latest version from yesterday.
We have in the assd function in metric/binary.py:

    assd = numpy.mean(
        (
            __surface_distances(result, reference, voxelspacing, connectivity),
            __surface_distances(reference, result, voxelspacing, connectivity),
        )
    )
    return assd

When the returning arrays are not of the same dim, numpy.mean crashes with the exception:

setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions.

A possible fix:

    assd = numpy.mean(
        (
            np.concatenate([__surface_distances(result, reference, voxelspacing, connectivity),
            __surface_distances(reference, result, voxelspacing, connectivity)])
        )
    )
    return assd

@oskar-maier-ms
Copy link
Contributor

Hey BarY7, thanks for reporting! I'll take a look when I find some time and release another minor version.

@oskar-maier-ms
Copy link
Contributor

Note: Error stems from __surface_distances(a) and __surface_distances(b) returning results of differing lengths.

@iTomxy
Copy link

iTomxy commented May 13, 2024

+1. When using with numpy==1.24.3, I get:

Traceback (most recent call last):
  File "bone_eval.py", line 208, in <module>
    test_3d(args, model)
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "bone_eval.py", line 159, in test_3d
    evaluator(pred_vol, label_vol)
  File "/data/itom/tyliang/codes/tmp.da-seg-bone/evaluation.py", line 107, in __call__
    a = fn(B_pred_c, B_c)
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/medpy/metric/binary.py", line 470, in assd
    assd = numpy.mean(
  File "<__array_function__ internals>", line 200, in mean
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 3464, in mean
    return _methods._mean(a, axis=axis, dtype=dtype,
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/numpy/core/_methods.py", line 165, in _mean
    arr = asanyarray(a)
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

And when I downgrade numpy to 1.21.6 as suggested in [1], I get:

Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.
Traceback (most recent call last):
  File "bone_eval.py", line 208, in <module>
    test_3d(args, model)
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
    return func(*args, **kwargs)
  File "bone_eval.py", line 159, in test_3d
    evaluator(pred_vol, label_vol)
  File "/data/itom/tyliang/codes/tmp.da-seg-bone/evaluation.py", line 107, in __call__
    a = fn(B_pred_c, B_c)
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/medpy/metric/binary.py", line 470, in assd
    assd = numpy.mean(
  File "<__array_function__ internals>", line 5, in mean
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 3440, in mean
    return _methods._mean(a, axis=axis, dtype=dtype,
  File "/data/itom/miniconda3/envs/cu116_pt1131/lib/python3.8/site-packages/numpy/core/_methods.py", line 179, in _mean
    ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
ValueError: operands could not be broadcast together with shapes (148451,) (73048,)

As suggested in [1], it seems dtype="object" should be passed in line 470 of medpy/metric/binary.py in assd = numpy.mean(.


  1. setting an array element with a sequence requested array has an inhomogeneous shape after 1 dimensions The detected shape was (2,)+inhomogeneous part

@STHxiao
Copy link

STHxiao commented Jun 21, 2024

I met the same problem when using with numpy==1.26.3

@Savitar001
Copy link

I met the same problem when using with numpy==1.26.1,hope be solved

@loli loli self-assigned this Jul 23, 2024
@loli loli added the bug label Jul 23, 2024
loli pushed a commit that referenced this issue Jul 23, 2024
@loli loli mentioned this issue Jul 23, 2024
@loli loli closed this as completed in #134 Jul 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants