Skip to content

Commit

Permalink
Merge branch 'master' of github.com:beltrame/mmMRI
Browse files Browse the repository at this point in the history
  • Loading branch information
cesine committed Oct 19, 2014
2 parents c7d2924 + 3196063 commit a456d64
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions scripts/ImageReader.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def get_image(self,imagefile):
img = nibabel.load(os.path.join(self.sourcedir,imagefile))

return img.get_data()

def get_raw_image(self,imagefile):
""" Returns a data structure with the voxel contents """

# Load an image and convert it to an array
img = nibabel.load(os.path.join(self.sourcedir,imagefile))

return img

def mask_image(self,imagefile, maskfile):
"""
Expand Down
35 changes: 34 additions & 1 deletion scripts/zscoring.py
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
#Hi.

import os
import numpy as np
import nibabel as nb

# TODO: we have to figure out how to make modules
import ImageReader

def zscoringNII(filename,sourcedir='../../brainimages'):
""" zscores a .nii.gz image only on non-zero values
and saves the same file _Z.nii.gz
"""

# Read images
reader = ImageReader.ImageReader(sourcedir)
img = reader.get_raw_image(filename)
data = img.get_data()
affine = img.get_affine()

# Compute non-zero mean
nonzdata = data[np.nonzero(data)]
meanz = np.mean(nonzdata)

# z-scoring non-zero values
# TODO: Use a matrix formulation for speed
vect = np.vectorize(lambda x : (x-meanz)/meanz if x !=0.0 else 0.0)
x = vect(data)

new_image = nb.Nifti1Image(x, affine)
nb.save(new_image, os.path.join(sourcedir, filename[:-7]+'_Z.nii.gz'))

# This is the standard way of checking if the file is being executed
if __name__ == "__main__":
zscoringNII('111_GS_VBM_GM_reg2STD.nii.gz')

0 comments on commit a456d64

Please sign in to comment.