Reputation: 2706
I am attempting to read a NIFTI file inside a .zip without having to extract the directory to the root directory. More specifically, I am working with the ADNI database and the files are stored by subjectID in individual .zip files. Inside the .zip file there is all the data pertaining to that subject, I would like to extract the NIFTI file (.nii.gz) from within the .zip without extracting the files.
Currently I have the following code snippet
def openNIFTI(filename):
return nib.load(filename).get_data()
zip_filename = filepath + str(subject_id) + '_3T_Structural_unproc.zip'
filename = str(subject_id) + '/unprocessed/3T/T1w_MPR1/' + str(subject_id) + '_3T_T1w_MPR1.nii.gz'
file = zf.extract(filename)
data = openNIFTI(file)
The filepath is the path to the collection of .zip files. filename is the path inside the .zip file to the NIFTI file I want to extract.
(Edit)
It seem the error is coming from the nibabel load function. Then function checks
if not op.exists(filename):
Upon testing the os.path.exists(filename) function independently I found that.
os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip/100206/unprocessed/3T/T1w_MPR1/100206_3T_T1w_MPR1.nii.gz')
False
However, this path is copy/pasted from the file I am attempting to open. It seems to me the error appears due to the .zip in the file path because
os.path.exists(r'C:/Users/eee/workspace_python/Image Reconstruction/data/ADNI/MRI data/100206_3T_Structural_unproc.zip')
True
Is there another way of doing this?
Upvotes: 1
Views: 3736
Reputation: 28390
Please see the accepted answer to python: Open file from zip without temporary extracting it this shows how to read the data from within a zipfile without extracting the file, (of course you need enough RAM to handle the file contents).
Upvotes: 1