irimia-laboratory / USC_BA_estimator

Other
16 stars 12 forks source link

Axis error and Value error in processmgz #4

Open nghuixin opened 6 months ago

nghuixin commented 6 months ago

I've tried running mainfunction provided in the example code on two different mgz files: the first one is from my dataset, the second file is the standard sample file that comes with freesurfer software, and have run into different errors in both. Any pointers would be helpful, thank you!

This is the error when I run it on one test subject from my own dataset.

content/drive/MyDrive/Colab Notebooks/test_brain_age/function/model/saved-model-317.h5
/content/drive/MyDrive/Colab Notebooks/test_brain_age/brain_mri
s180
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-49-fbe88c7d5abe> in <cell line: 15>()
     13 print(path_to_brain_mri)
     14 # the mainfunction will automatically save and output the predicted BAs and saliency maps.
---> 15 predicted_BA, repadded_saliency_maps = mainfunction(
     16     dir_test_brain_age, path_to_model_weights, path_to_brain_mri
     17 )

1 frames
<ipython-input-47-a64dee9d8513> in mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)
     10         brains_tmp.append(zoom(mgh.load(path_to_brain_mri+"/"+mgz_id+"/brain.mgz").get_fdata(), (0.5, 0.5, 0.5)))
     11         print(len(brains_tmp))
---> 12     brains, coordinates = processmgz(brains_tmp)
     13     (
     14         brain_save_path,

/content/drive/MyDrive/Colab Notebooks/test_brain_age/function/preprocess.py in processmgz(brains)
     18     for i in range(X.shape[0]):
     19         buf = X[i, :, :, :]
---> 20         buf = buf.reshape((128, 128, 128))
     21         xmin = 0
     22         xmax = 0

ValueError: cannot reshape array of size 1409024 into shape (128,128,128)

The sample freesurfer file is $FREESURFER_HOME/subjects/sample-001.mgz This is the error:

/content/drive/MyDrive/Colab Notebooks/test_brain_age/function/model/saved-model-317.h5
/content/drive/MyDrive/Colab Notebooks/test_brain_age/brain_mri
sample1
---------------------------------------------------------------------------
AxisError                                 Traceback (most recent call last)
<ipython-input-50-fbe88c7d5abe> in <cell line: 15>()
     13 print(path_to_brain_mri)
     14 # the mainfunction will automatically save and output the predicted BAs and saliency maps.
---> 15 predicted_BA, repadded_saliency_maps = mainfunction(
     16     dir_test_brain_age, path_to_model_weights, path_to_brain_mri
     17 )

4 frames
<ipython-input-47-a64dee9d8513> in mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)
     10         brains_tmp.append(zoom(mgh.load(path_to_brain_mri+"/"+mgz_id+"/brain.mgz").get_fdata(), (0.5, 0.5, 0.5)))
     11         print(len(brains_tmp))
---> 12     brains, coordinates = processmgz(brains_tmp)
     13     (
     14         brain_save_path,

/content/drive/MyDrive/Colab Notebooks/test_brain_age/function/preprocess.py in processmgz(brains)
    119                 ]
    120             )
--> 121     data_new = np.expand_dims(data_new, axis=4)  # reshape the brain MRI from 3D to 4D
    122     return data_new, coord
    123 

/usr/local/lib/python3.10/dist-packages/numpy/lib/shape_base.py in expand_dims(a, axis)
    595 
    596     out_ndim = len(axis) + a.ndim
--> 597     axis = normalize_axis_tuple(axis, out_ndim)
    598 
    599     shape_it = iter(a.shape)

/usr/local/lib/python3.10/dist-packages/numpy/core/numeric.py in normalize_axis_tuple(axis, ndim, argname, allow_duplicate)
   1378             pass
   1379     # Going via an iterator directly is slower than via list comprehension.
-> 1380     axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
   1381     if not allow_duplicate and len(set(axis)) != len(axis):
   1382         if argname:

/usr/local/lib/python3.10/dist-packages/numpy/core/numeric.py in <listcomp>(.0)
   1378             pass
   1379     # Going via an iterator directly is slower than via list comprehension.
-> 1380     axis = tuple([normalize_axis_index(ax, ndim, argname) for ax in axis])
   1381     if not allow_duplicate and len(set(axis)) != len(axis):
   1382         if argname:

AxisError: axis 4 is out of bounds for array of dimension 2
chenzhoy commented 6 months ago

Hi, hope this message finds you well. For the first bug, I think it's because your input matrix size is not (256, 256, 256). Would you pls double check your input matrix size? The secound bug is also a size error, would you please check the shape of this data_new before this line and send it to me (121 data_new = np.expand_dims(data_new, axis=4) # reshape the brain MRI from 3D to 4D)?

nghuixin commented 6 months ago

Hello Chenzhong, glad to hear from you. It looks like the error occurs far earlier than the line you called out above.

I added print statements to lines 19 and 20 of the preprocess.py file. print('Shape of X:', np.shape(X)) print('X.shape[0]:', X.shape[0])

This is what I obtained when running the sample.mgz file, which is obtained from here

`['sample1'
/content/drive/MyDrive/Colab Notebooks/test_brain_age/brain_mri/sample1/brain.mgz
processmgz
Shape of X: (1, 128, 128, 64)
X.shape[0]: 1
Shape of buf:  (128, 128, 64)
ValueError                                Traceback (most recent call last)
[<ipython-input-5-d40b992e75a4>](https://localhost:8080/#) in <cell line: 13>()
     11 
     12 # the mainfunction will automatically save and output the predicted BAs and saliency maps.
---> 13 predicted_BA, repadded_saliency_maps = mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)

1 frames
[<ipython-input-4-524288d4abb0>](https://localhost:8080/#) in mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)
     12 
     13 
---> 14     brains, coordinates = processmgz(brains_tmp)
     15     (
     16         brain_save_path,

[/content/drive/MyDrive/Colab Notebooks/test_brain_age/function/preprocess.py](https://localhost:8080/#) in processmgz(brains)
     23         buf = X[i, :, :, :]
     24         print('Shape of buf: ', np.shape(buf))
---> 25         buf = buf.reshape((128, 128, 128))
     26         xmin = 0
     27         xmax = 0

ValueError: cannot reshape array of size 1048576 into shape (128,128,128)`

Similarly, when I run it on my own data, I also get an error:


`['s223']
/content/drive/MyDrive/Colab Notebooks/test_brain_age/brain_mri/s223/brain.mgz
processmgz
Shape of X: (1, 128, 128, 86)
X.shape[0]: 1
Shape of buf:  (128, 128, 86) 
ValueError                                Traceback (most recent call last)
[<ipython-input-21-d40b992e75a4>](https://localhost:8080/#) in <cell line: 13>()
     11 
     12 # the mainfunction will automatically save and output the predicted BAs and saliency maps.
---> 13 predicted_BA, repadded_saliency_maps = mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)

1 frames
[<ipython-input-20-524288d4abb0>](https://localhost:8080/#) in mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)
     12 
     13 
---> 14     brains, coordinates = processmgz(brains_tmp)
     15     (
     16         brain_save_path,

[/content/drive/MyDrive/Colab Notebooks/test_brain_age/function/preprocess.py](https://localhost:8080/#) in processmgz(brains)
     23         buf = X[i, :, :, :]
     24         print('Shape of buf: ', np.shape(buf))
---> 25         buf = buf.reshape((128, 128, 128))
     26         xmin = 0
     27         xmax = 0

ValueError: cannot reshape array of size 1409024 into shape (128,128,128)`

When both mgz files are in the brain_mri folder, each in its separate folder, this is what I see:

`['sample1']
/content/drive/MyDrive/Colab Notebooks/test_brain_age/brain_mri/sample1/brain.mgz
['sample1', 's223']
/content/drive/MyDrive/Colab Notebooks/test_brain_age/brain_mri/s223/brain.mgz
processmgz 
ValueError                                Traceback (most recent call last)
[<ipython-input-15-d40b992e75a4>](https://localhost:8080/#) in <cell line: 13>()
     11 
     12 # the mainfunction will automatically save and output the predicted BAs and saliency maps.
---> 13 predicted_BA, repadded_saliency_maps = mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)

1 frames
[<ipython-input-13-524288d4abb0>](https://localhost:8080/#) in mainfunction(dir_test_brain_age, path_to_model_weights, path_to_brain_mri)
     12 
     13 
---> 14     brains, coordinates = processmgz(brains_tmp)
     15     (
     16         brain_save_path,

[/content/drive/MyDrive/Colab Notebooks/test_brain_age/function/preprocess.py](https://localhost:8080/#) in processmgz(brains)
     16    # brains = np.asarray(brains)
     17 
---> 18     X = np.asarray(brains)
     19     print('Shape of X:', np.shape(X))
     20     print('X.shape[0]:',  X.shape[0])

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 3 dimensions. The detected shape was (2, 128, 128) + inhomogeneous part.`
nghuixin commented 4 months ago

Hi! By any chance you'd have updates on this? @chenzhoy

chenzhoy commented 4 months ago

Hi. Sorry for the late update. It looks like your input size is (1, 128, 128, 64) which suggest that the input MRI size could be (1, 256, 256, 128). However, our model can only support MRI with 256x256x256 which caused this error.

nghuixin commented 4 months ago

Would you recommend zero padding or interpolation, in this case?


From: chenzhoy @.> Sent: Thursday, April 25, 2024 11:32 PM To: irimia-laboratory/USC_BA_estimator @.> Cc: Hui Xin Ng @.>; Author @.> Subject: Re: [irimia-laboratory/USC_BA_estimator] Axis error and Value error in processmgz (Issue #4)

Hi. Sorry for the late update. It looks like your input size is (1, 128, 128, 64) which suggest that the input MRI size could be (1, 256, 256, 128). However, our model can only support MRI with 256x256x256 which caused this error.

— Reply to this email directly, view it on GitHubhttps://github.com/irimia-laboratory/USC_BA_estimator/issues/4#issuecomment-2078279392, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEAMWV53UZ2YODVFHQVLBSLY7F77PAVCNFSM6AAAAABDPZ2JMOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZYGI3TSMZZGI. You are receiving this because you authored the thread.Message ID: @.***>

chenzhoy commented 4 months ago

I am sorry but I think padding may highly influence the model's performance.