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

MRAB python at mrabarnett.plus.com
Fri Jul 8 20:51:44 EDT 2022


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.

> Good luck,
> אבי גרוס
> 
> -----Original Message-----
> From: MRAB <python at mrabarnett.plus.com>
> To: python-list at python.org
> Sent: Fri, Jul 8, 2022 4:47 pm
> Subject: Re: NILEARN - WHY THIS CODE THROWS AN ERROR?????
> 
> On 08/07/2022 14:06, נתי שטרן wrote:
>> 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>
> 
> 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.



More information about the Python-list mailing list