Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

NILEARN - WHY THIS CODE THROWS AN ERROR?????

99 views
Skip to first unread message

נתי שטרן

unread,
Jul 8, 2022, 3:53:02 PM7/8/22
to
fullcode:



import nilearn.plotting as plot
import os,gzip,io
import nibabel as nib
path="C:/users/administrator/desktop/nii"
path2="C:/users/administrator/desktop/nii/out/"
for i in os.listdir(path):
if(".nii.gz" in i):
pass
else:

if(".nii" in i):
img = nib.load(path+"/"+i)
data = img.get_fdata()
print(data)
import imageio
X=0
for s in data:
import numpy
aleph=numpy.array(s,dtype=numpy.int8)
X=X+1
plot.plot_img(aleph)

imageio.imwrite("C:\\users\\administrator\\desktop\\nii\\"+i
+str(X)+'.jpg', s)






error:
Data given cannot be loaded because it is not compatible with nibabel
format
<https://netanel.ml>

MRAB

unread,
Jul 8, 2022, 4:51:06 PM7/8/22
to
What's the complete traceback?

It might help you to identify the problem if you add messages to tell
you what it's doing, e.g. what file it's about to load.

Apparently, one of the files is not compatible with nibabel.

Avi Gross

unread,
Jul 8, 2022, 6:21:03 PM7/8/22
to
Nati Stern has asked several questions here, often about relatively technical uses of python code that many of us have never used and still is not providing more exact info that tends to be needed before anyone can even think of diagnosing the problem.

I have learned to stay away from some such questioners. But I am wondering if some people (others too) think this forum is a generalized help desk staffed by College Professors with nothing else to do.

Many questions are best handled locally where people can look over your shoulder or use the same software and may have some fluency in your native language. And sometimes you need to do more investigating on your own, and perhaps tell us what you tried and why it was not useful, or we end up making endless suggestions and being told we are not working on your real issue and so on.

The code below is just babel or maybe babble. Something nested in a loop had a problem. Why not try something drastic and look at the  files and PICK ONE and use it step by step and see when it fails?

It looks like the code wants to ask for all files then ignore some. 

Why you would import numpy repeatedly in a loop is beyond me! LOL!

But which command line failed? My GUESS is:

data = img.get_fdata()


If so, did you try to see the current value of the filename you call "i" in the loop and see what name was loaded in what looks like a file ending in .nii in this code:

img = nib.load(path+"/"+i)


You need to proceed step by step and see if any previous steps failed. 
But what is possible is you got a file with .nii in middle of the name that does not end in .gz, or is not in the format needed.
Good luck,
אבי גרוס
--
https://mail.python.org/mailman/listinfo/python-list

MRAB

unread,
Jul 8, 2022, 8:55:20 PM7/8/22
to
On 08/07/2022 23:20, Avi Gross via Python-list wrote:
> Nati Stern has asked several questions here, often about relatively technical uses of python code that many of us have never used and still is not providing more exact info that tends to be needed before anyone can even think of diagnosing the problem.
>
> I have learned to stay away from some such questioners. But I am wondering if some people (others too) think this forum is a generalized help desk staffed by College Professors with nothing else to do.
>
> Many questions are best handled locally where people can look over your shoulder or use the same software and may have some fluency in your native language. And sometimes you need to do more investigating on your own, and perhaps tell us what you tried and why it was not useful, or we end up making endless suggestions and being told we are not working on your real issue and so on.
>
> The code below is just babel or maybe babble. Something nested in a loop had a problem. Why not try something drastic and look at the  files and PICK ONE and use it step by step and see when it fails?
>
> It looks like the code wants to ask for all files then ignore some.
>
> Why you would import numpy repeatedly in a loop is beyond me! LOL!
>
> But which command line failed? My GUESS is:
>
> data = img.get_fdata()
>
>
> If so, did you try to see the current value of the filename you call "i" in the loop and see what name was loaded in what looks like a file ending in .nii in this code:
>
> img = nib.load(path+"/"+i)
>
>
> You need to proceed step by step and see if any previous steps failed.
> But what is possible is you got a file with .nii in middle of the name that does not end in .gz, or is not in the format needed.

Indeed, it writes JPEG files whose filename contains the original
filename (with the ".nii") into the same folder, so if it has already
been run and produced an output file, the next time it's run, it'll trip
itself up.

All this would've been clear to the OP if it had printed messages as it
went.

Chris Angelico

unread,
Jul 8, 2022, 8:59:01 PM7/8/22
to
On Sat, 9 Jul 2022 at 10:57, MRAB <pyt...@mrabarnett.plus.com> wrote:
>
> On 08/07/2022 23:20, Avi Gross via Python-list wrote:
> > Nati Stern has asked several questions here, often about relatively technical uses of python code that many of us have never used and still is not providing more exact info that tends to be needed before anyone can even think of diagnosing the problem.
> >
> > I have learned to stay away from some such questioners. But I am wondering if some people (others too) think this forum is a generalized help desk staffed by College Professors with nothing else to do.
> >
> > Many questions are best handled locally where people can look over your shoulder or use the same software and may have some fluency in your native language. And sometimes you need to do more investigating on your own, and perhaps tell us what you tried and why it was not useful, or we end up making endless suggestions and being told we are not working on your real issue and so on.
> >
> > The code below is just babel or maybe babble. Something nested in a loop had a problem. Why not try something drastic and look at the files and PICK ONE and use it step by step and see when it fails?
> >
> > It looks like the code wants to ask for all files then ignore some.
> >
> > Why you would import numpy repeatedly in a loop is beyond me! LOL!
> >
> > But which command line failed? My GUESS is:
> >
> > data = img.get_fdata()
> >
> >
> > If so, did you try to see the current value of the filename you call "i" in the loop and see what name was loaded in what looks like a file ending in .nii in this code:
> >
> > img = nib.load(path+"/"+i)
> >
> >
> > You need to proceed step by step and see if any previous steps failed.
> > But what is possible is you got a file with .nii in middle of the name that does not end in .gz, or is not in the format needed.
>
> Indeed, it writes JPEG files whose filename contains the original
> filename (with the ".nii") into the same folder, so if it has already
> been run and produced an output file, the next time it's run, it'll trip
> itself up.
>
> All this would've been clear to the OP if it had printed messages as it
> went.

Or if the OP had renamed them all to "shrubbery" along the way.

ChrisA
0 new messages