Пример #1
0
def make_mask_map_4d(data, infile, outfile):
    """ Make mask map with 4d dimeions
    data: values for levels in infile. Shape = [4th dimension, regions]
    infile: input file to replace levels with values
    outfile: output file name
    """
    from neurosynth.base.mask import Masker
    from neurosynth.base import imageutils
    from nibabel import nifti1

    data = np.array(data)

    # Load image with masker
    masker = Masker(infile)
    img = imageutils.load_imgs(infile, masker)

    header = masker.get_header()

    shape = header.get_data_shape()[0:3] + (data.shape[0], )
    header.set_data_shape(shape)

    result = []

    for t_dim, t_val in enumerate(data):
        result.append(img.copy())
        for num, value in enumerate(t_val):
            np.place(result[t_dim], img == num + 1, [value])

    result = np.hstack(result)

    header.set_data_dtype(result.dtype)  # Avoids loss of precision
    img = nifti1.Nifti1Image(masker.unmask(result).squeeze(), None, header)
    img.to_filename(outfile)
Пример #2
0
def make_mask_map(data, infile, outfile, index=None):
    from neurosynth.base.mask import Masker
    from neurosynth.base import imageutils

    # Load image with masker
    masker = Masker(infile)
    img = imageutils.load_imgs(infile, masker)

    data = list(data)

    if index is None:
        index = np.arange(0, len(data))
        rev_index = None
    else:
        all_reg = np.arange(0, img.max())
        rev_index = all_reg[np.invert(np.in1d(all_reg, index))]

    min_val = img.min()

    for num, value in enumerate(data):
        n = index[num]
        np.place(img, img == n + min_val, [value])

    if rev_index is not None:
        for value in rev_index:
            np.place(img, img == value + min_val, 0)

    img = img.astype('float32')

    imageutils.save_img(img, outfile, masker)
Пример #3
0
  def decode(self, filenames, method=None, save=None, fmt='%.3f'):
    """ Decodes a set of images.

    Args:
      files: A list of filenames of images to decode.
      method: Optional string indicating decoding method to use. If None, use 
        the method set when the Decoder instance was initialized.
      save: Optional filename to save results to. If None (default), returns 
        all results as an array.
      fmt: Optional format to pass to numpy.savetxt() if saving to file.

    Returns:
      An n_files x n_features numpy array, where each feature is a row and 
      each image is a column. The meaning of the values depends on the 
      decoding method used. """

    if method is None: method = self.method
    imgs_to_decode = imageutils.load_imgs(filenames, self.mask)
    methods = {
      'pearson': self._pearson_correlation(imgs_to_decode),
      # 'nb': self._naive_bayes(imgs_to_decode),
      'pattern': self._pattern_expression(imgs_to_decode)
    }

    result = methods[method]

    if save is not None:
      f = open(save, 'w')
      f.write('\t'.join(self.feature_names) + '\n')
      np.savetxt(f, result, fmt='%.3f', delimiter='\t')
    else:
      return methods[method]
Пример #4
0
def make_mask_map_4d(data, infile, outfile):
    """ Make mask map with 4d dimeions
    data: values for levels in infile. Shape = [4th dimension, regions]
    infile: input file to replace levels with values
    outfile: output file name
    """
    from neurosynth.base.mask import Masker
    from neurosynth.base import imageutils
    from nibabel import nifti1

    data = np.array(data)

    # Load image with masker
    masker = Masker(infile)
    img = imageutils.load_imgs(infile, masker)

    header = masker.get_header()

    shape = header.get_data_shape()[0:3] + (data.shape[0],)
    header.set_data_shape(shape)

    result = []

    for t_dim, t_val in enumerate(data):
        result.append(img.copy())
        for num, value in enumerate(t_val):
            np.place(result[t_dim], img == num + 1, [value])

    result = np.hstack(result)

    header.set_data_dtype(result.dtype)  # Avoids loss of precision
    img = nifti1.Nifti1Image(masker.unmask(result).squeeze(), None, header)
    img.to_filename(outfile)
Пример #5
0
def make_mask_map(data, infile, outfile, index=None):
    from neurosynth.base.mask import Masker
    from neurosynth.base import imageutils

    # Load image with masker
    masker = Masker(infile)
    img = imageutils.load_imgs(infile, masker)

    img = np.round(img)

    data = list(data)

    if index is None:
        index = np.unique(img)
        rev_index = None
    else:
        all_reg = np.arange(0, img.max())
        rev_index = all_reg[np.invert(np.in1d(all_reg, index))]

    min_val = img.min()

    for num, value in enumerate(data):
        ix = index[num]

        np.place(img, img == ix, [value])

    if rev_index is not None:
        for value in rev_index:
            np.place(img, img == value + min_val, 0)

    img = img.astype('float32')

    imageutils.save_img(img, outfile, masker)
Пример #6
0
    def decode(self, images, method=None, save=None, round=4, names=None):
        """ Decodes a set of images.

        Args:
          images: The images to decode. Can be:
            - A single String specifying the filename of the image to decode
            - A list of filenames
            - A single NumPy array containing the image data
          method: Optional string indicating decoding method to use. If None, use
            the method set when the Decoder instance was initialized.
          save: Optional filename to save results to. If None (default), returns
            all results as an array.
          round: Optional integer indicating number of decimals to round result
            to. Defaults to 4.
          names: Optional list of names corresponding to the images in filenames.
            If passed, must be of same length and in same order as filenames.
            By default, the columns in the output will be named using the image
            filenames.

        Returns:
          An n_features x n_files numpy array, where each feature is a row and
          each image is a column. The meaning of the values depends on the
          decoding method used. """

        if method is None:
            method = self.method

        if isinstance(images, basestring) or isinstance(images, list):
            imgs_to_decode = imageutils.load_imgs(images, self.mask)
        else:
            imgs_to_decode = images

        methods = {
            'pearson': self._pearson_correlation(imgs_to_decode),
            # 'nb': self._naive_bayes(imgs_to_decode),
            'pattern': self._pattern_expression(imgs_to_decode)
        }

        result = np.around(methods[method], round)

        if save is not None:

            if names is None:
                if type(images).__module__ == np.__name__:
                    names = ['image_%d' for i in range(images.shape[1])]
                else:
                    names = images

            rownames = np.array(self.feature_names, dtype='|S32')[:,
                                                                  np.newaxis]

            f = open(save, 'w')
            f.write('\t'.join(['Feature'] + names) + '\n')
            np.savetxt(f,
                       np.hstack((rownames, result)),
                       fmt='%s',
                       delimiter='\t')
        else:
            return methods[method]
Пример #7
0
    def decode(self, images, method=None, save=None, round=4, names=None):
        """ Decodes a set of images.

        Args:
          images: The images to decode. Can be:
            - A single String specifying the filename of the image to decode
            - A list of filenames
            - A single NumPy array containing the image data
          method: Optional string indicating decoding method to use. If None, use
            the method set when the Decoder instance was initialized.
          save: Optional filename to save results to. If None (default), returns
            all results as an array.
          round: Optional integer indicating number of decimals to round result
            to. Defaults to 4.
          names: Optional list of names corresponding to the images in filenames.
            If passed, must be of same length and in same order as filenames.
            By default, the columns in the output will be named using the image
            filenames.

        Returns:
          An n_features x n_files numpy array, where each feature is a row and
          each image is a column. The meaning of the values depends on the
          decoding method used. """

        if method is None:
            method = self.method

        if isinstance(images, basestring) or isinstance(images, list):
            imgs_to_decode = imageutils.load_imgs(images, self.mask)
        else:
            imgs_to_decode = images

        methods = {
            'pearson': self._pearson_correlation(imgs_to_decode),
            # 'nb': self._naive_bayes(imgs_to_decode),
            'pattern': self._pattern_expression(imgs_to_decode)
        }

        result = np.around(methods[method], round)

        if save is not None:

            if names is None:
                if type(images).__module__ == np.__name__:
                    names = ['image_%d' for i in range(images.shape[1])]
                else:
                    names = filenames

            rownames = np.array(
                self.feature_names, dtype='|S32')[:, np.newaxis]

            f = open(save, 'w')
            f.write('\t'.join(['Feature'] + names) + '\n')
            np.savetxt(f, np.hstack((
                rownames, result)), fmt='%s', delimiter='\t')
        else:
            return methods[method]
Пример #8
0
def remove_value(infile, vals_rm, outfile):
    masker = mask.Masker(infile)
    img = imageutils.load_imgs(infile, masker)

    # Remove value
    for val in vals_rm:
        np.place(img, img == val, [0])

        # Save
    imageutils.save_img(img, outfile, masker)
Пример #9
0
    def decode(self, images, save=None, round=4, names=None, **kwargs):
        """ Decodes a set of images.

        Args:
          images: The images to decode. Can be:
            - A single String specifying the filename of the image to decode
            - A list of filenames
            - A single NumPy array containing the image data
          save: Optional filename to save results to. If None (default), returns
            all results as an array.
          round: Optional integer indicating number of decimals to round result
            to. Defaults to 4.
          names: Optional list of names corresponding to the images in filenames.
            If passed, must be of same length and in same order as filenames.
            By default, the columns in the output will be named using the image
            filenames.

        Returns:
          An n_features x n_files numpy array, where each feature is a row and
          each image is a column. The meaning of the values depends on the
          decoding method used. """

        if isinstance(images, string_types):
            images = [images]

        if isinstance(images, list):
            imgs_to_decode = imageutils.load_imgs(images, self.masker)
        else:
            imgs_to_decode = images

        methods = {
            'pearson': self._pearson_correlation,
            'dot': self._dot_product,
            'roi': self._roi_association
        }

        result = np.around(
            methods[self.method](imgs_to_decode, **kwargs), round)

        # if save is not None:

        if names is None:
            if type(images).__module__ == np.__name__:
                names = ['image_%d' % i for i in range(images.shape[1])]
            elif self.method == 'roi':
                names = ['cluster_%d' % i for i in range(result.shape[1])]
            else:
                names = images

        result = pd.DataFrame(result, columns=names, index=self.feature_names)

        if save is not None:
            result.to_csv(save, index_label='Feature')
        return result
Пример #10
0
    def decode(self, images, save=None, round=4, names=None, **kwargs):
        """ Decodes a set of images.

        Args:
          images: The images to decode. Can be:
            - A single String specifying the filename of the image to decode
            - A list of filenames
            - A single NumPy array containing the image data
          save: Optional filename to save results to. If None (default), returns
            all results as an array.
          round: Optional integer indicating number of decimals to round result
            to. Defaults to 4.
          names: Optional list of names corresponding to the images in filenames.
            If passed, must be of same length and in same order as filenames.
            By default, the columns in the output will be named using the image
            filenames.

        Returns:
          An n_features x n_files numpy array, where each feature is a row and
          each image is a column. The meaning of the values depends on the
          decoding method used. """

        if isinstance(images, basestring):
            images = [images]

        if isinstance(images, list):
            imgs_to_decode = imageutils.load_imgs(images, self.masker)
        else:
            imgs_to_decode = images

        methods = {
            'pearson': self._pearson_correlation,
            'dot': self._dot_product,
            'roi': self._roi_association
        }

        result = np.around(
            methods[self.method](imgs_to_decode, **kwargs), round)

        # if save is not None:

        if names is None:
            if type(images).__module__ == np.__name__:
                names = ['image_%d' % i for i in range(images.shape[1])]
            elif self.method == 'roi':
                names = ['cluster_%d' % i for i in range(result.shape[1])]
            else:
                names = images

        result = pd.DataFrame(result, columns=names, index=self.feature_names)

        if save is not None:
            result.to_csv(save, index_label='Feature')
        return result
Пример #11
0
def remove_value(infile, vals_rm, outfile):
    masker = mask.Masker(infile)
    img = imageutils.load_imgs(infile, masker)
    img = np.round(img)

    # Remove value
    for val in vals_rm:
        np.place(img, img == val, [0])

    # Save
    imageutils.save_img(img, outfile, masker)
Пример #12
0
def extract_roi(mask_img, data_img, masker):
	""" Exctract values from an image for each region in an atlas
	Args:
	    mask_img: Mask image or atlas image with multiple integer values denoting different regions
	    data_img: Image to be extract_roi
	    masker: A masker instance

	Returns: A list of tuples containing the region number mean value within each ROI,
	"""

	mask = imageutils.load_imgs(mask_img, masker)

	data = imageutils.load_imgs(data_img, masker)

	mask_values = np.unique(mask)[1:]

	mean_vals = []
	for region in mask_values:
		mean_vals.append((region, data[np.where(mask == region)[0]].mean()))

	return pd.DataFrame(mean_vals, columns = ['roi', 'value'])
Пример #13
0
    def _load_features_from_images(self, images, names=None):
        """ Load feature image data from image files.

        Args:
          images: A list of image filenames.
          names: An optional list of strings to use as the feature names. Must be
            in the same order as the images.
        """
        if names is not None and len(names) != len(images):
            raise Exception( "Lists of feature names and image files must be of same length!")
        self.feature_names = names if names is not None else images
        self.feature_images = imageutils.load_imgs(images, self.masker)
Пример #14
0
def extract_roi(mask_img, data_img, masker):
    """ Exctract values from an image for each region in an atlas
	Args:
	    mask_img: Mask image or atlas image with multiple integer values denoting different regions
	    data_img: Image to be extract_roi
	    masker: A masker instance

	Returns: A list of tuples containing the region number mean value within each ROI,
	"""

    mask = imageutils.load_imgs(mask_img, masker)

    data = imageutils.load_imgs(data_img, masker)

    mask_values = np.unique(mask)[1:]

    mean_vals = []
    for region in mask_values:
        mean_vals.append((region, data[np.where(mask == region)[0]].mean()))

    return pd.DataFrame(mean_vals, columns=['roi', 'value'])
Пример #15
0
    def _load_features_from_images(self, images, names=None):
        """ Load feature image data from image files.

        Args:
          images: A list of image filenames.
          names: An optional list of strings to use as the feature names. Must
            be in the same order as the images.
        """
        if names is not None and len(names) != len(images):
            raise Exception(
                "Lists of feature names and images must be of same length!")
        self.feature_names = names if names is not None else images
        self.feature_images = imageutils.load_imgs(images, self.masker)
Пример #16
0
  def decode(self,images,outfile,mrs=None,round=4):
    if not self.decoder:
      self.decoder = decode.Decoder(self.db)

    # If mrs is not specified, do decoding against neurosynth database
    if not mrs:
      result = self.decoder.decode(images, save=outfile)
  
    # If mrs is specified, do decoding against custom set of images
    else:
      # This is akin to traditional neurosynth method - pearson's r correlation
      imgs_to_compare = imageutils.load_imgs(mrs,self.masker)
      imgs_to_decode = imageutils.load_imgs(images,self.masker)
      x, y = imgs_to_compare.astype(float),imgs_to_decode.astype(float)
      x, y = x - x.mean(0), y - y.mean(0)
      x, y = x / np.sqrt((x ** 2).sum(0)), y / np.sqrt((y ** 2).sum(0))
      result = np.around(x.T.dot(y).T,round)
      features = [os.path.basename(m) for m in mrs]
      rownames = [os.path.basename(m) for m in images]
      df = pd.DataFrame(result,columns=features)
      df.index = rownames
      df.to_csv(outfile,sep="\t")
    return result
Пример #17
0
    def decode(self, images, outfile, mrs=None, round=4):
        if not self.decoder:
            self.decoder = decode.Decoder(self.db)

        # If mrs is not specified, do decoding against neurosynth database
        if not mrs:
            result = self.decoder.decode(images, save=outfile)

        # If mrs is specified, do decoding against custom set of images
        else:
            # This is akin to traditional neurosynth method - pearson's r correlation
            imgs_to_compare = imageutils.load_imgs(mrs, self.masker)
            imgs_to_decode = imageutils.load_imgs(images, self.masker)
            x, y = imgs_to_compare.astype(float), imgs_to_decode.astype(float)
            x, y = x - x.mean(0), y - y.mean(0)
            x, y = x / np.sqrt((x ** 2).sum(0)), y / np.sqrt((y ** 2).sum(0))
            result = np.around(x.T.dot(y).T, round)
            features = [os.path.basename(m) for m in mrs]
            rownames = [os.path.basename(m) for m in images]
            df = pd.DataFrame(result, columns=features)
            df.index = rownames
            df.to_csv(outfile, sep="\t")
        return result
Пример #18
0
from neurosynth.base.dataset import Dataset
import neurosynth.base.imageutils as it

dataset = Dataset.load("../data/datasets/abs_topics_filt.pkl")

print "Filtering voxels..."

data = dataset.image_table.data.toarray()

voxel_mask = data.mean(axis=1) > 0.005

img = it.load_imgs('../masks/ward/30.nii.gz', dataset.masker)

good_voxels = img[voxel_mask]

it.save_img(good_voxels, "../masks/ward/30_masked.nii.gz", dataset.masker)
Пример #19
0
# removes parcels below some size and reorders in order 
# while also outputting the region and community number
from neurosynth.base.mask import Masker
from neurosynth.base import imageutils
import numpy as np
import csv

min_vox = 300

file = '../masks/Andy/aal_MNI_V4.nii'
outfile = '../masks/Andy/aal_MNI_V4_' + str(min_vox) + '.nii'


# Load image with masker
masker = Masker(file)
img = imageutils.load_imgs(file, masker)

# How many levels in the original image
print "Original shape:"
print np.bincount([int(vox) for vox in img]).shape

# Get how many voxels per level and calc those that pass min_vox
count = np.bincount(img.astype('int').squeeze())
non_0_ix = np.where(count >= min_vox)[0]
zero_ix = np.where(count < min_vox)[0]

# Remove those not in a good community
bad = list(set(zero_ix))

# Remove
for value in bad:
Пример #20
0
dat.fullpath = '/Users/lukechang/Research/Trust_Friend/Analyses/NeurosynthDecode/Friend.nii';
write(dat)

# 2) Reorient using FSL - Unix
fslreorient2std Friend Friend_Or

# 3) Coregister to 2mm MNI space - Unix
/usr/local/fsl/bin/flirt -in /Users/lukechang/Research/Trust_Friend/Analyses/NeurosynthDecode/Friend_Or.nii.gz -ref /usr/local/fsl/data/standard/MNI152_T1_2mm_brain -out /Users/lukechang/Research/Trust_Friend/Analyses/NeurosynthDecode/Friend_Or_Mni.nii.gz -omat /Users/lukechang/Research/Trust_Friend/Analyses/NeurosynthDecode/Friend_Or_Mni.mat -bins 256 -cost corratio -searchrx -90 90 -searchry -90 90 -searchrz -90 90 -dof 12  -interp trilinear

# 4) Decode - Python
DATASET_FILE = '/Users/lukechang/Dropbox/Github/neurosynth/topics.pkl'
PREFIX = '/Users/lukechang/Research/Trust_Friend/Analyses/NeurosynthDecode/'
INFILE = 'Friend_Or_Mni.nii.gz'
dataset = Dataset.load(DATASET_FILE)
decoder = decode.Decoder(dataset) #takes awhile to load, should only do this once.
img = imageutils.load_imgs(PREFIX + INFILE, decoder.mask)
result = decoder.decode(img)
np.savetxt(PREFIX + 'Friend_Decoded.txt', result)

# 5) Threshold at .001 - unix
fslmaths Friend_Or_Mni -thr 3 Friend_Or_Mni_001

# 6) Decode thresholded map - python
DATASET_FILE = '/Users/lukechang/Dropbox/Github/neurosynth/topics.pkl'
PREFIX = '/Users/lukechang/Research/Trust_Friend/Analyses/NeurosynthDecode/'
INFILE = 'Friend_Or_Mni_001.nii.gz'
dataset = Dataset.load(DATASET_FILE)
decoder = decode.Decoder(dataset) #takes awhile to load, should only do this once.
img = imageutils.load_imgs(PREFIX + INFILE, decoder.mask)
result = decoder.decode(img)
np.savetxt(PREFIX + 'Friend_001_Decoded.txt', result)