Ejemplo n.º 1
0
def _file_exists(url, output_dir):
    """
    Checks whether to-be-downloaded stuff already exists locally

    """

    if url in DONE_URLS:
        return True

    output_filename = os.path.join(output_dir, os.path.basename(url))
    if not os.path.exists(output_filename):
        return False

    for ext in ['.txt', '.mat']:
        if output_filename.endswith(ext):
            if os.path.isfile(output_filename):
                # print "Skipping existing file: %s" % output_filename
                DONE_URLS.append(url)
                return True

    if output_filename.endswith(".nii"):
        try:
            nibabel.load(output_filename)
            nibabel.concat_images([output_filename])
            # print "Skipping existing file: %s" % output_filename
            DONE_URLS.append(url)
            return True
        except Exception, e:
            print "nibabel.load(...) error:", e
            print
            print "Corrupt image %s; redownloading" % output_filename
            print commands.getoutput("rm -f %s*" % output_filename)
            return False
Ejemplo n.º 2
0
def _file_exists(url, output_dir):
    """
    Checks whether to-be-downloaded stuff already exists locally

    """

    if url in DONE_URLS:
        return True

    output_filename = os.path.join(output_dir, os.path.basename(url))
    if not os.path.exists(output_filename):
        return False

    for ext in [".txt", ".mat"]:
        if output_filename.endswith(ext):
            if os.path.isfile(output_filename):
                # print "Skipping existing file: %s" % output_filename
                DONE_URLS.append(url)
                return True

    if output_filename.endswith(".nii"):
        try:
            nibabel.load(output_filename)
            nibabel.concat_images([output_filename])
            # print "Skipping existing file: %s" % output_filename
            DONE_URLS.append(url)
            return True
        except Exception, e:
            print "nibabel.load(...) error:", e
            print
            print "Corrupt image %s; redownloading" % output_filename
            print commands.getoutput("rm -f %s*" % output_filename)
            return False
Ejemplo n.º 3
0
def _openfmri_preproc(out_dir, doc, metadata=None, verbose=1):
    """
        Parameters
        ----------
        metadata: dict
            - run_key: naming the sessions

        Examples
        --------
        {'run_key': ['task001 run001', 'task001 run002',
                     'task002 run001', 'task002 run002']}

    """
    if 'study_id' in doc:
        study_dir = os.path.join(out_dir, doc['study_id'])
    else:
        study_dir = out_dir

    if verbose > 0:
        print '%s@%s: dumping preproc' % (doc['subject_id'], doc['study_id'])

    subject_dir = os.path.join(study_dir, doc['subject_id'])
    anatomy_dir = os.path.join(subject_dir, 'anatomy')

    if not os.path.exists(anatomy_dir):
        os.makedirs(anatomy_dir)

    anatomy = doc['preproc']['anatomy']
    wm_anatomy = doc['final']['anatomy']

    anatomy = nb.load(anatomy)
    wm_anatomy = nb.load(wm_anatomy)

    nb.save(anatomy, os.path.join(anatomy_dir, 'highres001.nii.gz'))
    nb.save(wm_anatomy, os.path.join(anatomy_dir,
                                     'normalized_highres001.nii.gz'))

    bold_dir = os.path.join(subject_dir, 'BOLD')

    for session, run_key in zip(
            doc['slice_timing']['bold'], metadata['run_key']):

        bold = nb.concat_images(session)
        session_dir = os.path.join(bold_dir, run_key.replace(' ', '_'))
        if not os.path.exists(session_dir):
            os.makedirs(session_dir)
        nb.save(bold, os.path.join(session_dir, 'bold.nii.gz'))

    for session, motion, run_key in zip(doc['final']['bold'],
                                        doc['realign']['motion'],
                                        metadata['run_key']):

        bold = nb.concat_images(session)
        session_dir = os.path.join(bold_dir, run_key.replace(' ', '_'))
        if not os.path.exists(session_dir):
            os.makedirs(session_dir)
        nb.save(bold, os.path.join(session_dir, 'normalized_bold.nii.gz'))
        shutil.copyfile(motion, os.path.join(session_dir, 'motion.txt'))
Ejemplo n.º 4
0
    def prep_data(self, nifti1, bval1, bvec1, nifti2, bval2, bvec2):
        ''' Load the reconstructed image files and generate the files that TOPUP needs. '''
        ni1 = nb.load(nifti1)
        ni2 = nb.load(nifti2)
        phase_dim1 = ni1.get_header().get_dim_info()[1]
        phase_dim2 = ni2.get_header().get_dim_info()[1]

        bvals1 = np.loadtxt(bval1)
        bvals2 = np.loadtxt(bval2)
        bvecs1 = np.loadtxt(bvec1)
        bvecs2 = np.loadtxt(bvec2)

        nondwi1 = [im for i,im in enumerate(nb.four_to_three(ni1)) if bvals1[i]<10 and i<self.num_vols]
        nondwi2 = [im for i,im in enumerate(nb.four_to_three(ni2)) if bvals2[i]<10 and i<self.num_vols]

        b0 = nb.concat_images(nondwi1+nondwi2)
        # Topup requires an even number of slices
        if b0.shape[2]%2:
            d = b0.get_data()
            d = np.concatenate((d,np.zeros((d.shape[0],d.shape[1],1,d.shape[3]), dtype=d.dtype)),axis=2)
            b0 = nb.Nifti1Image(d, b0.get_affine())

        nb.save(b0, self.b0_file)
        with open(self.acq_file, 'w') as f:
            for i in xrange(len(nondwi1)):
                row = ['0','0','0',str(self.readout_time1),'\n']
                row[phase_dim1] = str(self.pe_dir1)
                f.write(' '.join(row))
            for i in xrange(len(nondwi2)):
                row = ['0','0','0',str(self.readout_time2),'\n']
                row[phase_dim2] = str(self.pe_dir2)
                f.write(' '.join(row))

        mux_ims1 = nb.four_to_three(ni1)[self.num_cal1:]
        mux_ims2 = nb.four_to_three(ni2)[self.num_cal2:]
        all_ims = nb.concat_images(mux_ims1 + mux_ims2)
        if all_ims.shape[2]%2:
            d = all_ims.get_data()
            d = np.concatenate((d,np.zeros((d.shape[0],d.shape[1],1,d.shape[3]), dtype=d.dtype)),axis=2)
            all_ims = nb.Nifti1Image(d, all_ims.get_affine())

        nb.save(all_ims, self.dwi_base+'.nii.gz')

        indices = ['1' for i in xrange(len(mux_ims1))] + [str(len(nondwi1)+1) for i in xrange(len(mux_ims2))]
        with open(self.index_file, 'w') as f:
            f.write(' '.join(indices))

        bvals = np.concatenate((bvals1[self.num_cal1:],bvals2[self.num_cal2:]), axis=0)
        bvecs = np.concatenate((bvecs1[:,self.num_cal1:],bvecs2[:,self.num_cal2:]), axis=1)
        with open(self.bval_file, 'w') as f:
            f.write(' '.join(['%0.1f' % value for value in bvals]))
        with open(self.bvec_file, 'w') as f:
            f.write(' '.join(['%0.4f' % value for value in bvecs[0,:]]) + '\n')
            f.write(' '.join(['%0.4f' % value for value in bvecs[1,:]]) + '\n')
            f.write(' '.join(['%0.4f' % value for value in bvecs[2,:]]) + '\n')
Ejemplo n.º 5
0
def t_test_2sample(sample_a, sample_b, equal_var=True):
    """t-statistics are positive if a > b"""
    a_stack = nib.concat_images(sample_a, check_affines=False)
    b_stack = nib.concat_images(sample_b, check_affines=False)

    tstats, pvalues = sp.stats.ttest_ind(a_stack.get_data(),
                                         b_stack.get_data(), axis=3,
                                         equal_var=equal_var)
    reject, corrected = mne.stats.fdr_correction(pvalues)

    return (image_like(a_stack, tstats), image_like(a_stack, pvalues),
            image_like(a_stack, corrected))
Ejemplo n.º 6
0
    def _run_interface(self, runtime):
        allmaps = nb.concat_images(self.inputs.in_files).dataobj
        acm_pos = np.mean(allmaps > self.inputs.threshold,
                          axis=3,
                          dtype=np.float32)
        acm_neg = np.mean(allmaps < -1.0 * self.inputs.threshold,
                          axis=3,
                          dtype=np.float32)
        acm_diff = acm_pos - acm_neg

        template_fname = self.inputs.in_files[0]
        ext = split_filename(template_fname)[2]
        fname_fmt = os.path.join(runtime.cwd, "acm_{}" + ext).format

        self._results["out_file"] = fname_fmt("diff")
        self._results["acm_pos"] = fname_fmt("pos")
        self._results["acm_neg"] = fname_fmt("neg")

        img = nb.load(template_fname)
        img.__class__(acm_diff, img.affine,
                      img.header).to_filename(self._results["out_file"])
        img.__class__(acm_pos, img.affine,
                      img.header).to_filename(self._results["acm_pos"])
        img.__class__(acm_neg, img.affine,
                      img.header).to_filename(self._results["acm_neg"])

        return runtime
Ejemplo n.º 7
0
    def _run_interface(self, runtime):
        in_files = self.inputs.in_files

        indices = list(range(len(in_files)))
        if isdefined(self.inputs.indices):
            indices = self.inputs.indices

        if len(self.inputs.in_files) < 2:
            self._results["out_file"] = in_files[0]
            return runtime

        first_fname = in_files[indices[0]]
        if len(indices) == 1:
            self._results["out_file"] = first_fname
            return runtime

        im = nb.concat_images([in_files[i] for i in indices])
        data = im.get_fdata().sum(axis=3)
        data = np.clip(data, a_min=0.0, a_max=1.0)

        out_file = fname_presuffix(first_fname,
                                   suffix="_tpmsum",
                                   newpath=runtime.cwd)
        newnii = im.__class__(data, im.affine, im.header)
        newnii.set_data_dtype(np.float32)

        # Set visualization thresholds
        newnii.header["cal_max"] = 1.0
        newnii.header["cal_min"] = 0.0
        newnii.to_filename(out_file)
        self._results["out_file"] = out_file

        return runtime
Ejemplo n.º 8
0
    def _run_interface(self, runtime):
        allmaps = nb.concat_images(self.inputs.in_files).get_data()
        acm_pos = np.mean(allmaps > self.inputs.threshold,
                          axis=3,
                          dtype=np.float32)
        acm_neg = np.mean(allmaps < -1.0 * self.inputs.threshold,
                          axis=3,
                          dtype=np.float32)
        acm_diff = acm_pos - acm_neg

        template_fname = self.inputs.in_files[0]
        ext = split_filename(template_fname)[2]
        fname_fmt = os.path.join(runtime.cwd, 'acm_{}' + ext).format

        self._results['out_file'] = fname_fmt('diff')
        self._results['acm_pos'] = fname_fmt('pos')
        self._results['acm_neg'] = fname_fmt('neg')

        img = nb.load(template_fname)
        img.__class__(acm_diff, img.affine,
                      img.header).to_filename(self._results['out_file'])
        img.__class__(acm_pos, img.affine,
                      img.header).to_filename(self._results['acm_pos'])
        img.__class__(acm_neg, img.affine,
                      img.header).to_filename(self._results['acm_neg'])

        return runtime
Ejemplo n.º 9
0
    def _run_interface(self, runtime):

        # Load the 4D raw fieldmap image and select first frame
        raw_img_frames = nib.load(self.inputs.raw_file)
        raw_img = nib.four_to_three(raw_img_frames)[0]
        affine, header = raw_img.affine, raw_img.header

        # Write out the raw image to serve as a registration target
        self.write_image("raw_file", "raw.nii.gz", raw_img)

        # Load the 4D jacobian image
        jac_img_frames = nib.concat_images(self.inputs.jacobian_files)
        jac_data = jac_img_frames.get_data()

        # Load the 4D corrected fieldmap image
        corr_img_frames = nib.load(self.inputs.corrected_file)
        corr_data = corr_img_frames.get_data()

        # Average the corrected image over the final dimension and write
        corr_data = corr_data.mean(axis=-1)
        self.write_image("corrected_file", "func.nii.gz", corr_data, affine,
                         header)

        # Save the jacobian images using the raw geometry
        self.write_image("jacobian_file", "jacobian.nii.gz", jac_data, affine,
                         header)

        # Select the first warpfield image
        # We combine the two fieldmap images so that the first one has
        # a phase encoding that matches the time series data.
        # Also note that when the fieldmap images have multiple frames,
        # the warps corresponding to those frames are identical.
        warp_file = self.inputs.warp_files[0]

        # Load in the the warp file and save out with the correct affine
        # (topup doesn't save the header geometry correctly for some reason)
        warp_data = nib.load(warp_file).get_data()
        self.write_image("warp_file", "warp.nii.gz", warp_data, affine, header)

        # Select the warp along the phase encode direction
        # Note: we elsewhere currently require phase encoding to be AP or PA
        # so because the input node transforms the fieldmap to canonical
        # orientation this will work. But in the future we might want to ne
        # more flexible with what we accept and will need to change this.
        warp_data_y = warp_data[..., 1]

        # Write out a mask to exclude voxels with large distortions/dropout
        mask_data = (np.abs(warp_data_y) < 4).astype(np.int)
        self.write_image("mask_file", "warp_mask.nii.gz", mask_data, affine,
                         header)

        # Generate a QC image of the warpfield
        m = Mosaic(raw_img, warp_data_y)
        m.plot_overlay("coolwarm", vmin=-6, vmax=6, alpha=.75)
        self.write_visualization("warp_plot", "warp.png", m)

        # Generate a QC gif of the unwarping performance
        self.generate_unwarp_gif(runtime, raw_img_frames, corr_img_frames)

        return runtime
Ejemplo n.º 10
0
def genNiftiVol(data, dtype=np.uint8):
    shape = np.array(np.shape(data), dtype=np.float32)
    if np.ndim(data) > 3:
        shape = shape[1:]

    affine = np.identity(4)
    affine[0:3, 3] = -0.5 * shape[:3]
    hdr = nb.Nifti1Header()
    hdr.set_data_dtype(np.uint8)
    hdr.set_xyzt_units('mm')
    hdr.set_data_dtype(dtype)

    hdr['data_type'] = 2
    hdr['qform_code'] = 2  # aligned
    hdr['sform_code'] = 1  # scanner
    hdr['scl_slope'] = 1.0
    hdr['regular'] = np.array('r', dtype='|S1')
    pixdim = np.ones(shape=(8,))
    pixdim[4:] = 0
    hdr['pixdim'] = pixdim

    if np.ndim(data) > 3:
        hdr.set_xyzt_units('mm', 'sec')
        # hdr['xyzt_units'] = 2 + 8  # mm + sec
        pixdim[4] = 1
        hdr['pixdim'] = pixdim
        nii_array = []
        for im in data:
            nii_array.append(nb.Nifti1Image(im.astype(dtype), affine, hdr))
        nii = nb.concat_images(nii_array)

    else:
        nii = nb.Nifti1Image(data.astype(dtype), affine, hdr)
    return nii
Ejemplo n.º 11
0
 def transform(self, raw_data=None, output_dir=None,
               affine=None, prefix='a', basenames=None, ext=None):
     self.output_data_ = STC.transform(self, raw_data=raw_data)
     if not basenames is None:
         self.basenames_ = basenames
     if not affine is None:
         self.affine_ = affine
     if not output_dir is None:
         if not os.path.exists(output_dir):
             os.makedirs(output_dir)
     if hasattr(self, 'affine_'):
         if isinstance(self.affine_, list):
             self.output_data_ = [nibabel.Nifti1Image(
                 self.output_data_[..., t], self.affine_[t])
                 for t in range(self.output_data_.shape[-1])]
             if output_dir is None:
                 self.output_data_ = nibabel.concat_images(
                     self.output_data_, check_affines=False)
         else:
             self.output_data_ = nibabel.Nifti1Image(self.output_data_,
                                                     self.affine_)
         if not output_dir is None:
             self.output_data_ = save_vols(
                 self.output_data_,
                 output_dir, prefix=prefix,
                 basenames=get_basenames(self.basenames_, ext=ext))
     return self.output_data_
Ejemplo n.º 12
0
 def transform(self, raw_data=None, output_dir=None,
               affine=None, prefix='a', basenames=None, ext=None):
     self.output_data_ = STC.transform(self, raw_data=raw_data)
     if basenames is not None:
         self.basenames_ = basenames
     if affine is not None:
         self.affine_ = affine
     if output_dir is not None:
         if not os.path.exists(output_dir):
             os.makedirs(output_dir)
     if hasattr(self, 'affine_'):
         if isinstance(self.affine_, list):
             self.output_data_ = [nibabel.Nifti1Image(
                 self.output_data_[..., t], self.affine_[t])
                 for t in range(self.output_data_.shape[-1])]
             if output_dir is None:
                 self.output_data_ = nibabel.concat_images(
                     self.output_data_, check_affines=False)
         else:
             self.output_data_ = nibabel.Nifti1Image(self.output_data_,
                                                     self.affine_)
         if output_dir is not None:
             self.output_data_ = save_vols(
                 self.output_data_,
                 output_dir, prefix=prefix,
                 basenames=get_basenames(self.basenames_, ext=ext))
     return self.output_data_
Ejemplo n.º 13
0
def genNiftiVol(data, dtype=np.uint8):
    shape = np.array(np.shape(data), dtype=np.float32)
    if np.ndim(data) > 3:
        shape = shape[1:]

    affine = np.identity(4)
    affine[0:3, 3] = -0.5 * shape[:3]
    hdr = nb.Nifti1Header()
    hdr.set_data_dtype(np.uint8)
    hdr.set_xyzt_units("mm")
    hdr.set_data_dtype(dtype)

    hdr["data_type"] = 2
    hdr["qform_code"] = 2  # aligned
    hdr["sform_code"] = 1  # scanner
    hdr["scl_slope"] = 1.0
    hdr["regular"] = np.array("r", dtype="|S1")
    pixdim = np.ones(shape=(8,))
    pixdim[4:] = 0
    hdr["pixdim"] = pixdim

    if np.ndim(data) > 3:
        hdr.set_xyzt_units("mm", "sec")
        # hdr['xyzt_units'] = 2 + 8  # mm + sec
        pixdim[4] = 1
        hdr["pixdim"] = pixdim
        nii_array = []
        for im in data:
            nii_array.append(nb.Nifti1Image(im.astype(dtype), affine, hdr))
        nii = nb.concat_images(nii_array)

    else:
        nii = nb.Nifti1Image(data.astype(dtype), affine, hdr)
    return nii
def autoatlas(locations):
    """Computes a new atlas
    
    Arguments:
        locations {array} -- Output of get_regions_locations()
    
    Returns:
        {str} -- Path to the new atlas
    """
    required_atlases = set(locations[:, 1])
    download_mist(required_atlases)

    regions = []
    for value, resolution, _, _, _ in locations:
        mist_path = f'files/MIST/{resolution}.nii.gz'
        mist = nib.load(mist_path)
        region = (mist.get_fdata() == value) * 1

        region_img = nib.Nifti1Image(region, affine=mist.affine)
        regions.append(region_img)

    atlas = nib.concat_images(regions)
    filename = '{}_autoatlas_{}regions.nii'.format(time.time_ns() // 1000000,
                                                   atlas.shape[-1])
    nib.save(atlas, filename)

    print(
        f'{bcolors.OKGREEN}Success! Autoatlas saved to {filename}{bcolors.ENDC}'
    )

    return filename
Ejemplo n.º 15
0
 def _merge_subject_images(self, images, good_indices=None):
     """Stack a list of 3D images into 4D image."""
     if good_indices is None:
         good_indices = range(len(images))
     images = [img for i, img in enumerate(images) if i in good_indices]
     out_img = nib.concat_images(images)
     return out_img
Ejemplo n.º 16
0
 def _merge_subject_images(self, images, good_indices=None):
     """Stack a list of 3D images into 4D image."""
     if good_indices is None:
         good_indices = range(len(images))
     images = [img for i, img in enumerate(images) if i in good_indices]
     out_img = nib.concat_images(images)
     return out_img
Ejemplo n.º 17
0
    def _run_interface(self, runtime):
        in_files = self.inputs.in_files

        indices = list(range(len(in_files)))
        if isdefined(self.inputs.indices):
            indices = self.inputs.indices

        if len(self.inputs.in_files) < 2:
            self._results['out_file'] = in_files[0]
            return runtime

        first_fname = in_files[indices[0]]
        if len(indices) == 1:
            self._results['out_file'] = first_fname
            return runtime

        im = nb.concat_images([in_files[i] for i in indices])
        data = im.get_data().astype(float).sum(axis=3)
        data = np.clip(data, a_min=0.0, a_max=1.0)

        out_file = fname_presuffix(first_fname, suffix='_tpmsum',
                                   newpath=runtime.cwd)
        newnii = im.__class__(data, im.affine, im.header)
        newnii.set_data_dtype(np.float32)

        # Set visualization thresholds
        newnii.header['cal_max'] = 1.0
        newnii.header['cal_min'] = 0.0
        newnii.to_filename(out_file)
        self._results['out_file'] = out_file

        return runtime
Ejemplo n.º 18
0
def grad2vols(gradients, atlas, labels, image_dim='4D'):
    """
    Takes computed gradient(s) from fitted GradientMaps object and converts them
    to 3D image(s) in volume format based on the atlas utilized in the GradientMaps
    object. The output can either be one 4D image where 3D gradients are concatenated
    or a list of 3D images, one per gradient.
    ----------
    gradients: array_like
        Fitted GradientMaps object.
    atlas_filename: path or Nifti1Image
        Parcellation utilized within the computation of gradients, that is fmrivols2conn.
        Either name of the corresponding parcellation dataset or loaded dataset.
        Uses nilearn.datasets interface to fetch parcellation datasets and their information.
    labels: list of parcellation labels
    image_dim: str
        Image dimensions of the Nifti1Image output. Either one 4D image where gradient
        maps are concatenated along the 4th dimension ('4D') or a list of 3D images, one per
        gradient ('3D'). Default is '4D'.
    Returns
    -------
    gradient_maps_vol: Nifti1Image
       4D image with gradient maps concatenated along the 4th dimension or list of 3D images, one
       per gradient.
    """

    #check if atlas_filename is str and thus should be loaded
    if isinstance(atlas, str):
        atlas_filename = eval('datasets.fetch_%s()' % atlas).maps
        atlas_img = nib.load(atlas_filename)
        atlas_data = atlas_img.get_fdata()
        labels = eval('datasets.fetch_%s()' % atlas).labels
    else:
        #read and define atlas data, affine and header
        atlas_img = atlas
        atlas_data = atlas.get_fdata()
        labels = labels

    gradient_list = []
    gradient_vols = []

    for gradient in range(gradients.n_components):

        gradient_list.append(np.zeros_like(atlas_data))

    for i, g in enumerate(gradient_list):
        for j in range(0, len(labels)):
            g[atlas_data == (j + 1)] = gradients.gradients_.T[i][j]
        gradient_vols.append(
            nib.Nifti1Image(g, atlas_img.affine, atlas_img.header))

    if image_dim is None or image_dim == '4D':
        gradient_vols = nib.concat_images(gradient_vols)
        print('Gradient maps will be concatenated within one 4D image.')
    elif image_dim == '3D':
        print(
            'Gradient maps will be provided as a list of 3D images, one per gradient.'
        )

    return gradient_vols
Ejemplo n.º 19
0
def _gen_coeff(in_file, in_ref, in_movpar):
    """Convert to a valid fieldcoeff"""
    from shutil import copy
    import numpy as np
    import nibabel as nb
    from nipype.interfaces import fsl

    def _get_fname(in_file):
        import os.path as op
        fname, fext = op.splitext(op.basename(in_file))
        if fext == '.gz':
            fname, _ = op.splitext(fname)
        return op.abspath(fname)

    out_topup = _get_fname(in_file)

    # 1. Add one dimension (4D image) of 3D coordinates
    im0 = nb.load(in_file)
    data = np.zeros_like(im0.get_data())
    sizes = data.shape[:3]
    spacings = im0.get_header().get_zooms()[:3]
    im1 = nb.Nifti1Image(data, im0.get_affine(), im0.get_header())
    im4d = nb.concat_images([im0, im1, im1])
    im4d_fname = '{}_{}'.format(out_topup, 'field4D.nii.gz')
    im4d.to_filename(im4d_fname)

    # 2. Warputils to compute bspline coefficients
    to_coeff = fsl.WarpUtils(out_format='spline', knot_space=(2, 2, 2))
    to_coeff.inputs.in_file = im4d_fname
    to_coeff.inputs.reference = in_ref

    # 3. Remove unnecessary dims (Y and Z)
    get_first = fsl.ExtractROI(t_min=0, t_size=1)
    get_first.inputs.in_file = to_coeff.run().outputs.out_file

    # 4. Set correct header
    # see https://github.com/poldracklab/preprocessing-workflow/issues/92
    img = nb.load(get_first.run().outputs.roi_file)
    hdr = img.get_header().copy()
    hdr['intent_p1'] = spacings[0]
    hdr['intent_p2'] = spacings[1]
    hdr['intent_p3'] = spacings[2]
    hdr['intent_code'] = 2016

    sform = np.eye(4)
    sform[:3, 3] = sizes
    hdr.set_sform(sform, code='scanner')
    hdr['qform_code'] = 1

    # For some reason, MCFLIRT's parameters
    # are not compatible, fill with zeroes for now
    mpar = np.loadtxt(in_movpar)
    out_movpar = '{}_movpar.txt'.format(out_topup)
    np.savetxt(out_movpar, np.zeros_like(mpar))
    #copy(in_movpar, out_movpar)
    out_fieldcoef = '{}_fieldcoef.nii.gz'.format(out_topup)
    nb.Nifti1Image(img.get_data(), None, hdr).to_filename(out_fieldcoef)

    return out_fieldcoef, out_movpar
Ejemplo n.º 20
0
    def __init__(self,
                 data,
                 Y,
                 algorithm=None,
                 cv_dict=None,
                 mask=None,
                 output_dir='.',
                 **kwargs):
        """ Initialize Predict.

        Args:
            data: nibabel data instance
            Y: vector of training labels
            subject_id: vector of labels corresponding to each subject
            algorithm: Algorithm to use for prediction.  Must be one of 'svm', 'svr',
                'linear', 'logistic', 'lasso', 'ridge', 'ridgeClassifier','randomforest',
                or 'randomforestClassifier'
            cv_dict: Type of cross_validation to use. A dictionary of
                {'type': 'kfolds', 'n_folds': n},
                {'type': 'kfolds', 'n_folds': n, 'subject_id': holdout}, or
                {'type': 'loso', 'subject_id': holdout},
                where n = number of folds, and subject = vector of subject ids that corresponds to self.Y
            mask: binary nibabel mask
            output_dir: Directory to use for writing all outputs
            **kwargs: Additional keyword arguments to pass to the prediction algorithm

        """

        self.output_dir = output_dir

        if mask is not None:
            if type(mask) is not nib.nifti1.Nifti1Image:
                raise ValueError("mask is not a nibabel instance")
            self.mask = mask
        else:
            self.mask = nib.load(
                os.path.join(get_resource_path(),
                             'MNI152_T1_2mm_brain_mask.nii.gz'))

        if type(data) is list:
            data = nib.concat_images(data)

        if not isinstance(data,
                          (nib.nifti1.Nifti1Image, nib.nifti1.Nifti1Pair)):
            raise ValueError("data is not a nibabel instance")
        self.nifti_masker = NiftiMasker(mask_img=mask)
        self.data = self.nifti_masker.fit_transform(data)

        if type(Y) is list:
            Y = np.array(Y)
        if self.data.shape[0] != len(Y):
            raise ValueError("Y does not match the correct size of data")
        self.Y = Y

        if algorithm is not None:
            self.set_algorithm(algorithm, **kwargs)

        if cv_dict is not None:
            self.cv = set_cv(cv_dict)
Ejemplo n.º 21
0
def t_test_images(images, popmean=0.0):
    """Perform per-entry t-test on nibabel spatial images"""
    stack = nib.concat_images(images, check_affines=False)

    tstats, pvalues = sp.stats.ttest_1samp(stack.get_data(), popmean, axis=3)
    reject, corrected = mne.stats.fdr_correction(pvalues)

    return (image_like(stack, tstats), image_like(stack, pvalues),
            image_like(stack, corrected))
Ejemplo n.º 22
0
def is_3D(image):
    """Check whether image is 3D"""

    if isinstance(image, _basestring):
        image = nibabel.load(image)
    elif isinstance(image, list):
        image = nibabel.concat_images(image, check_affines=False)

    return len(image.shape) == 3
Ejemplo n.º 23
0
def atlas_data():
    """Create a 4D image that repeats the atlas over 10 volumes.

    Every voxel/ROI is the same value across time. Therefore we can test if
    proper values are extracted (and if they are in the proper order).
    """
    atlas = _get_atlas()
    img = nib.load(atlas['maps'])
    return nib.concat_images([img] * 50)
Ejemplo n.º 24
0
def mean_images(overlays):
    """Read set of spatial files, write average (ignoring zeros) to new file"""

    stack = nib.concat_images(overlays, check_affines=False)

    sums = np.sum(stack.get_data(), axis=3)
    counts = np.sum(stack.get_data() != 0, axis=3)
    counts[counts == 0] = 1

    return image_like(stack, sums / counts)
Ejemplo n.º 25
0
def check_niimgs(niimgs):
    niimgs_ = []
    for niimg in niimgs:
        if isinstance(niimg, (str, unicode)):
            niimgs_.append(Niimg(niimg))
        elif isinstance(niimg, list):
            niimgs_.append(nb.concat_images(niimg))
        else:
            niimgs_.append(niimg)
    return niimgs_
Ejemplo n.º 26
0
def compute_mean_image(images, output_filename=None, threeD=False):
    """Computes the mean of --perhaps differently shaped-- images

    Parameters
    ----------
    images: string/image object, or list (-like) of
        image(s) whose mean we seek

    output_filename: string, optional (default None)
        output file where computed mean image will be written

    Returns
    -------
    mean nifti image object

    """

    # sanitize
    if not hasattr(images, '__iter__') or isinstance(images, basestring):
        images = [images]

    # make list of data an affines
    all_data = []
    all_affine = []
    for image in images:
        if not is_niimg(image):
            if isinstance(image, basestring):
                image = nibabel.load(image)
            else:
                image = nibabel.concat_images(image,
                                              check_affines=False
                                              )
        data = image.get_data()

        if threeD:
            if is_4D(image):
                data = data.mean(-1)

        all_data.append(data)
        all_affine.append(image.get_affine())

    # compute mean
    mean_data = np.mean(all_data, axis=0)

    # XXX I'm assuming all the affines are equal
    mean_affine = all_affine[0]

    mean_image = nibabel.Nifti1Image(mean_data, mean_affine)

    # save mean image
    if output_filename:
        nibabel.save(mean_image, output_filename)

    # return return result
    return mean_image
Ejemplo n.º 27
0
def compute_mean_image(images, output_filename=None, threeD=False):
    """Computes the mean of --perhaps differently shaped-- images

    Parameters
    ----------
    images: string/image object, or list (-like) of
        image(s) whose mean we seek

    output_filename: string, optional (default None)
        output file where computed mean image will be written

    Returns
    -------
    mean nifti image object

    """

    # sanitize
    if not hasattr(images, '__iter__') or isinstance(images, _basestring):
        images = [images]

    # make list of data an affines
    all_data = []
    all_affine = []
    for image in images:
        if not is_niimg(image):
            if isinstance(image, _basestring):
                image = nibabel.load(image)
            else:
                image = nibabel.concat_images(image,
                                              check_affines=False
                                              )
        data = image.get_data()

        if threeD:
            if is_4D(image):
                data = data.mean(-1)

        all_data.append(data)
        all_affine.append(image.get_affine())

    # compute mean
    mean_data = np.mean(all_data, axis=0)

    # XXX I'm assuming all the affines are equal
    mean_affine = all_affine[0]

    mean_image = nibabel.Nifti1Image(mean_data, mean_affine)

    # save mean image
    if output_filename:
        nibabel.save(mean_image, output_filename)

    # return return result
    return mean_image
Ejemplo n.º 28
0
def is_3D(image):
    """Check whether image is 3D"""

    if isinstance(image, basestring):
        image = nibabel.load(image)
    elif isinstance(image, list):
        image = nibabel.concat_images(image,
                                      check_affines=False
                                      )

    return len(image.shape) == 3
Ejemplo n.º 29
0
def _flatten_split_merge(in_files):
    if isinstance(in_files, str):
        in_files = [in_files]

    nfiles = len(in_files)

    all_nii = []
    for fname in in_files:
        nii = nb.squeeze_image(nb.load(fname))

        if nii.get_data().ndim > 3:
            all_nii += nb.four_to_three(nii)
        else:
            all_nii.append(nii)

    if len(all_nii) == 1:
        LOGGER.warning('File %s cannot be split', all_nii[0])
        return in_files[0], in_files

    if len(all_nii) == nfiles:
        flat_split = in_files
    else:
        splitname = fname_presuffix(in_files[0],
                                    suffix='_split%04d',
                                    newpath=os.getcwd())
        flat_split = []
        for i, nii in enumerate(all_nii):
            flat_split.append(splitname % i)
            nii.to_filename(flat_split[-1])

    # Only one 4D file was supplied
    if nfiles == 1:
        merged = in_files[0]
    else:
        # More that one in_files - need merge
        merged = fname_presuffix(in_files[0],
                                 suffix='_merged',
                                 newpath=os.getcwd())
        nb.concat_images(all_nii).to_filename(merged)

    return merged, flat_split
Ejemplo n.º 30
0
def is_3D(image):
    """Check whether image is 3D"""

    if isinstance(image, str):
        if os.path.exists(image):
            image = nibabel.load(image)
        else:
            raise FileNotFoundError("File {} not found.".format(image))
    elif isinstance(image, list):
        image = nibabel.concat_images(image, check_affines=False)

    return len(image.shape) == 3
Ejemplo n.º 31
0
def _get_timeseries(data, row_mask, affine=None):
    if isinstance(data, list):
        return nb.concat_images(np.array(data)[row_mask])
    elif isinstance(data, (str, unicode)):
        img = nb.load(data)
        return nb.Nifti1Image(img.get_data()[row_mask, :], img.get_affine())
    elif isinstance(data, (np.ndarray, np.memmap)):
        if affine is None:
            raise Exception("The affine is not optional " "when data is an array")
        return nb.Nifti1Image(data[row_mask, :], affine)
    else:
        raise ValueError('Data type "%s" not supported' % type(data))
Ejemplo n.º 32
0
    def __data_generation(self, list_IDs_temp):
        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
        # Initialization
        X = np.empty((self.batch_size, self.n_channels, *self.dim))
        y = np.empty((self.batch_size, 3, *self.dim))

        # Generate data
        # Decode and load the data
        for i, ID in enumerate(list_IDs_temp):

            img1 = './data/' + ID + '_flair.nii.gz'
            img2 = './data/' + ID + '_t1.nii.gz'
            img3 = './data/' + ID + '_t1ce.nii.gz'
            img4 = './data/' + ID + '_t2.nii.gz'
            img5 = './data/' + ID + '_seg.nii.gz'

            newimage = nib.concat_images([img1, img2, img3, img4, img5])
            cropped = crop_img(newimage)         
            img_array = np.array(cropped.dataobj)
            z = np.rollaxis(img_array, 3, 0)

            padded_image = np.zeros((5,160,192,160))
            padded_image[:z.shape[0],:z.shape[1],:z.shape[2],:z.shape[3]] = z

            a,b,c,d,seg_mask = np.split(padded_image, 5, axis=0)

            images = np.concatenate([a,b,c,d], axis=0)

            # print("images shape:", images.shape, "images values:", np.unique(images.astype(int)))

            # split the channels:
            # seg_mask_1 = copy.deepcopy(seg_mask.astype(int))
            seg_mask_1 = np.zeros((1,160,192,160))
            seg_mask_1[seg_mask.astype(int) == 1] = 1
            seg_mask_2 = np.zeros((1,160,192,160))
            seg_mask_2[seg_mask.astype(int) == 2] = 1
            seg_mask_3 = np.zeros((1,160,192,160))
            seg_mask_3[seg_mask.astype(int) == 4] = 1
            seg_mask_3ch = np.concatenate([seg_mask_1,seg_mask_2,seg_mask_3], axis=0).astype(int)
            
            # 1) the "enhancing tumor" (ET), 2) the "tumor core" (TC), and 3) the "whole tumor" (WT) 
            # The ET is described by areas that show hyper-intensity in T1Gd when compared to T1, but also when compared to “healthy” white matter in T1Gd. The TC describes the bulk of the tumor, which is what is typically resected. The TC entails the ET, as well as the necrotic (fluid-filled) and the non-enhancing (solid) parts of the tumor. The appearance of the necrotic (NCR) and the non-enhancing (NET) tumor core is typically hypo-intense in T1-Gd when compared to T1. The WT describes the complete extent of the disease, as it entails the TC and the peritumoral edema (ED), which is typically depicted by hyper-intense signal in FLAIR.
            # The labels in the provided data are: 
            # 1 for NCR & NET (necrotic (NCR) and the non-enhancing (NET) tumor core) = TC ("tumor core")
            # 2 for ED ("peritumoral edema")
            # 4 for ET ("enhancing tumor")
            # 0 for everything else

            X[i,] = images
            y[i,] = seg_mask_3ch
            
#         return X, keras.utils.to_categorical(y, num_classes=self.n_classes)
        return X, y
Ejemplo n.º 33
0
def is_3D(image):
    """Check whether image is 3D"""

    if isinstance(image, basestring):
        image = nibabel.load(image)
    elif isinstance(image, list):
        image = nibabel.concat_images(image)

    if len(image.shape) == 3:
        return True
    else:
        return len(image.shape) == 4 and image.shape[-1] == 1
Ejemplo n.º 34
0
    def _load_session(x):
        if isinstance(x, basestring):
            x = nibabel.load(x).get_data()
        else:
            x = nibabel.concat_images(x).get_data()

        if x.ndim == 5:
            x = x[:, :, :, 0, :]
        else:
            assert x.ndim == 4, x.shape

        return x
Ejemplo n.º 35
0
def resample_images(paths, ref_affine, ref_shape):
    """ Utility to resample images provided as paths and return an image"""
    from nilearn.image import resample_img
    import nibabel as nib
    imgs = []
    for path in paths:
        fmri_image = resample_img(path,
                                  target_affine=ref_affine,
                                  target_shape=ref_shape)
        imgs.append(fmri_image)
    img = nib.concat_images(imgs)
    return img
Ejemplo n.º 36
0
    def __init__(self, data=None, Y=None, X=None, mask=None, output_file=None, **kwargs):
        if mask is not None:
            if not isinstance(mask, nib.Nifti1Image):
                if type(mask) is str:
                    if os.path.isfile(mask):
                        mask = nib.load(mask)
            else:
                raise ValueError("mask is not a nibabel instance")
            self.mask = mask
        else:
            self.mask = nib.load(os.path.join(get_resource_path(),'MNI152_T1_2mm_brain_mask.nii.gz'))
        self.nifti_masker = NiftiMasker(mask_img=self.mask)

        if data is not None:
            if type(data) is str:
                data=nib.load(data)
            elif type(data) is list:
                data=nib.concat_images(data)
            elif not isinstance(data, nib.Nifti1Image):
                raise ValueError("data is not a nibabel instance")
            self.data = self.nifti_masker.fit_transform(data)

            # Collapse any extra dimension
            if any([x==1 for x in self.data.shape]):
                self.data=self.data.squeeze()
        else:
            self.data = np.array([])

        if Y is not None:
            if type(Y) is str:
                if os.path.isfile(Y):
                    Y=pd.read_csv(Y,header=None,index_col=None)
            if isinstance(Y, pd.DataFrame):
                if self.data.shape[0]!= len(Y):
                    raise ValueError("Y does not match the correct size of data")
                self.Y = Y
            else:
                raise ValueError("Make sure Y is a pandas data frame.")
        else:
            self.Y = pd.DataFrame()

        if X is not None:
            if self.data.shape[0]!= X.shape[0]:
                raise ValueError("X does not match the correct size of data")
            self.X = X
        else:
            self.X = pd.DataFrame()

        if output_file is not None:
            self.file_name = output_file
        else:
            self.file_name = []
Ejemplo n.º 37
0
def _get_timeseries(data, row_mask, affine=None):
    if isinstance(data, list):
        return nb.concat_images(np.array(data)[row_mask])
    elif isinstance(data, (str, unicode)):
        img = nb.load(data)
        return nb.Nifti1Image(img.get_data()[row_mask, :], img.get_affine())
    elif isinstance(data, (np.ndarray, np.memmap)):
        if affine is None:
            raise Exception('The affine is not optional '
                            'when data is an array')
        return nb.Nifti1Image(data[row_mask, :], affine)
    else:
        raise ValueError('Data type "%s" not supported' % type(data))
Ejemplo n.º 38
0
def _flatten_split_merge(in_files):
    from builtins import bytes, str

    if isinstance(in_files, (bytes, str)):
        in_files = [in_files]

    nfiles = len(in_files)

    all_nii = []
    for fname in in_files:
        nii = nb.squeeze_image(nb.load(fname))

        if nii.get_data().ndim > 3:
            all_nii += nb.four_to_three(nii)
        else:
            all_nii.append(nii)

    if len(all_nii) == 1:
        LOGGER.warn('File %s cannot be split', all_nii[0])
        return in_files[0], in_files

    if len(all_nii) == nfiles:
        flat_split = in_files
    else:
        splitname = genfname(in_files[0], suffix='split%04d')
        flat_split = []
        for i, nii in enumerate(all_nii):
            flat_split.append(splitname % i)
            nii.to_filename(flat_split[-1])

    # Only one 4D file was supplied
    if nfiles == 1:
        merged = in_files[0]
    else:
        # More that one in_files - need merge
        merged = genfname(in_files[0], suffix='merged')
        nb.concat_images(all_nii).to_filename(merged)

    return merged, flat_split
Ejemplo n.º 39
0
def make_img(sub_path, sub_id):
    niftis = sorted(glob.glob(os.path.join(sub_path, "func/*run-*.nii.gz")))
    outpath = os.path.join(sub_path, "func")
    FILENAME = "{}_task-runs_bold.nii.gz".format(sub_id)
    print("Generating files {}.......".format(FILENAME))
    #load in all the files from the glob above, then convert them from nifti1 to nifti2\n",
    ni2_funcs = (nib.Nifti2Image.from_image(nib.load(func)) for func in niftis)
    concat, this is with nibabel, but should work with nilearn too
    ni2_concat = nib.concat_images(ni2_funcs, check_affines=False, axis=3)
    #set the output file name
    outfile=os.path.join(outpath,FILENAME)
    #write the file
    ni2_concat.to_filename(outfile)
Ejemplo n.º 40
0
def save_raw(subject_dir, doc):
    if 'bold' in doc:
        run_key = doc['runs']
        for label, session_data in zip(run_key, doc['bold']):
            if isinstance(session_data, (list, np.ndarray)):
                img = nb.concat_images(session_data, check_affines=False)
            else:
                img = nb.load(session_data)
            session_dir = make_dir(subject_dir, 'BOLD', label, strict=False)
            nb.save(img, os.path.join(session_dir, 'bold.nii.gz'))
    if 'anatomy' in doc:
        anat_dir = make_dir(subject_dir, 'anatomy', strict=False)
        img = nb.load(doc['anatomy'])
        nb.save(img, os.path.join(anat_dir, 'highres001.nii.gz'))
Ejemplo n.º 41
0
    def __init__(self, data, Y, algorithm=None, cv_dict=None, mask=None,
                 output_dir='.', **kwargs):
        """ Initialize Predict.

        Args:
            data: nibabel data instance
            Y: vector of training labels
            subject_id: vector of labels corresponding to each subject
            algorithm: Algorithm to use for prediction.  Must be one of 'svm', 'svr',
                'linear', 'logistic', 'lasso', 'ridge', 'ridgeClassifier','randomforest',
                or 'randomforestClassifier'
            cv_dict: Type of cross_validation to use. A dictionary of
                {'type': 'kfolds', 'n_folds': n},
                {'type': 'kfolds', 'n_folds': n, 'subject_id': holdout}, or
                {'type': 'loso', 'subject_id': holdout},
                where n = number of folds, and subject = vector of subject ids that corresponds to self.Y
            mask: binary nibabel mask
            output_dir: Directory to use for writing all outputs
            **kwargs: Additional keyword arguments to pass to the prediction algorithm

        """

        self.output_dir = output_dir

        if mask is not None:
            if type(mask) is not nib.nifti1.Nifti1Image:
                raise ValueError("mask is not a nibabel instance")
            self.mask = mask
        else:
            self.mask = nib.load(os.path.join(get_resource_path(),'MNI152_T1_2mm_brain_mask.nii.gz'))

        if type(data) is list:
            data=nib.concat_images(data)

        if not isinstance(data,(nib.nifti1.Nifti1Image, nib.nifti1.Nifti1Pair)):
            raise ValueError("data is not a nibabel instance")
        self.nifti_masker = NiftiMasker(mask_img=mask)
        self.data = self.nifti_masker.fit_transform(data)

        if type(Y) is list:
            Y=np.array(Y)
        if self.data.shape[0]!= len(Y):
            raise ValueError("Y does not match the correct size of data")
        self.Y = Y

        if algorithm is not None:
            self.set_algorithm(algorithm, **kwargs)

        if cv_dict is not None:
            self.cv = set_cv(cv_dict)
Ejemplo n.º 42
0
def test_simulator(tmpdir):
    sim = Simulator()
    r = 10
    sigma = 1
    y = [0, 1]
    n_reps = 3
    output_dir = str(tmpdir)
    sim.create_data(y, sigma, reps=n_reps, output_dir=output_dir)
    flist = glob.glob(str(tmpdir.join('centered*nii.gz')))

    shape = (91, 109, 91)
    sim_img = nb.concat_images(flist)
    assert len(sim.data) == n_reps * len(y)
    assert sim_img.shape[0:3] == shape
Ejemplo n.º 43
0
def save_raw(subject_dir, doc):
    if 'bold' in doc:
        run_key = doc['runs']
        for label, session_data in zip(run_key, doc['bold']):
            if isinstance(session_data, (list, np.ndarray)):
                img = nb.concat_images(session_data, check_affines=False)
            else:
                img = nb.load(session_data)
            session_dir = make_dir(subject_dir, 'BOLD', label, strict=False)
            nb.save(img, os.path.join(session_dir, 'bold.nii.gz'))
    if 'anatomy' in doc:
        anat_dir = make_dir(subject_dir, 'anatomy', strict=False)
        img = nb.load(doc['anatomy'])
        nb.save(img, os.path.join(anat_dir, 'highres001.nii.gz'))
Ejemplo n.º 44
0
def _concat_rois(in_file, in_mask, ref_header):
    nii = nb.load(in_file)
    mask_nii = nb.load(in_mask)

    # we have to do this explicitly because of potential differences in
    # qform_code between the two files that prevent SignalExtraction to do
    # the concatenation
    concat_nii = nb.concat_images(
        [resample_to_img(nii, mask_nii, interpolation='nearest'), mask_nii])
    concat_nii = nb.Nifti1Image(concat_nii.get_data(),
                                nb.load(ref_header).affine,
                                nb.load(ref_header).header)
    concat_nii.to_filename("concat.nii.gz")
    return os.path.abspath("concat.nii.gz")
Ejemplo n.º 45
0
def _combine_rois(in_files, ref_header):
    if len(in_files) < 2:
        raise RuntimeError('Combining ROIs requires at least two inputs')

    nii = nb.concat_images([nb.load(f) for f in in_files])
    combined = nii.get_data().astype(int).sum(3)
    combined[combined > 0] = 1
    # we have to do this explicitly because of potential differences in
    # qform_code between the two files that prevent aCompCor to work
    new_nii = nb.Nifti1Image(combined,
                             nb.load(ref_header).affine,
                             nb.load(ref_header).header)
    new_nii.to_filename("logical_or.nii.gz")
    return os.path.abspath("logical_or.nii.gz")
Ejemplo n.º 46
0
def test_simulator(tmpdir):
    sim = Simulator()
    r = 10
    sigma = 1
    y = [0, 1]
    n_reps = 3
    output_dir = str(tmpdir)
    sim.create_data(y, sigma, reps=n_reps, output_dir=output_dir)
    flist = glob.glob(str(tmpdir.join("centered*nii.gz")))

    shape = (91, 109, 91)
    sim_img = nb.concat_images(flist)
    assert len(sim.data) == n_reps * len(y)
    assert sim_img.shape[0:3] == shape
Ejemplo n.º 47
0
def estimate_motion(nifti_image):
    # BEGIN STDOUT SUPRESSION
    actualstdout = sys.stdout
    sys.stdout = open(os.devnull, 'w')
    # We want to use the middle time point as the reference. But the algorithm does't allow that, so fake it.
    ref_vol = nifti_image.shape[3] / 2 + 1
    ims = nb.four_to_three(nifti_image)
    reg = Realign4d(
        nb.concat_images([ims[ref_vol]] +
                         ims))  # in the next release, we'll need to add tr=1.

    reg.estimate(loops=3)  # default: loops=5
    aligned = reg.resample(0)[:, :, :, 1:]
    sys.stdout = actualstdout
    # END STDOUT SUPRESSION
    abs_disp = []
    rel_disp = []
    transrot = []
    prev_T = None
    # skip the first one, since it's the reference volume
    for T in reg._transforms[0][1:]:
        # get the full affine for this volume by pre-multiplying by the reference affine
        #mc_affine = np.dot(ni.get_affine(), T.as_affine())
        transrot.append(T.translation.tolist() + T.rotation.tolist())
        # Compute the mean displacement
        # See http://www.fmrib.ox.ac.uk/analysis/techrep/tr99mj1/tr99mj1/node5.html
        # radius of the spherical head assumption (in mm):
        R = 80.
        # The center of the volume. Assume 0,0,0 in world coordinates.
        # Note: it might be better to use the center of mass of the brain mask.
        xc = np.matrix((0, 0, 0)).T
        T_error = T.as_affine() - np.eye(4)
        A = np.matrix(T_error[0:3, 0:3])
        t = np.matrix(T_error[0:3, 3]).T
        abs_disp.append(
            np.sqrt(R**2. / 5 * np.trace(A.T * A) + (t + A * xc).T *
                    (t + A * xc)).item())
        if prev_T != None:
            T_error = T.as_affine() - prev_T.as_affine()  # - np.eye(4)
            A = np.matrix(T_error[0:3, 0:3])
            t = np.matrix(T_error[0:3, 3]).T
            rel_disp.append(
                np.sqrt(R**2. / 5 * np.trace(A.T * A) + (t + A * xc).T *
                        (t + A * xc)).item())
        else:
            rel_disp.append(0.0)
        prev_T = T
    return aligned, np.array(abs_disp), np.array(rel_disp), np.array(transrot)
Ejemplo n.º 48
0
def concat_RL(R_img, L_img, rl_idx_pair, rl_sign_pair=None):
    """
    Given R and L ICA images and their component index pairs, concatenate images to
    create bilateral image using the index pairs. Sign flipping can be specified in rl_sign_pair.

    """
    # Make sure images have same number of components and indices are less than the n_components
    assert R_img.shape == L_img.shape
    n_components = R_img.shape[3]
    assert np.max(rl_idx_pair) < n_components
    n_rl_imgs = len(rl_idx_pair[0])
    assert n_rl_imgs == len(rl_idx_pair[1])
    if rl_sign_pair:
        assert n_rl_imgs == len(rl_sign_pair[0])
        assert n_rl_imgs == len(rl_sign_pair[1])

    # Match indice pairs and combine
    terms = R_img.terms.keys()
    rl_imgs = []
    rl_term_vals = []

    for i in range(n_rl_imgs):
        rci, lci = rl_idx_pair[0][i], rl_idx_pair[1][i]
        R_comp_img = index_img(R_img, rci)
        L_comp_img = index_img(L_img, lci)

        # sign flipping
        r_sign = rl_sign_pair[0][i] if rl_sign_pair else 1
        l_sign = rl_sign_pair[1][i] if rl_sign_pair else 1

        R_comp_img = math_img("%d*img" % (r_sign), img=R_comp_img)
        L_comp_img = math_img("%d*img" % (l_sign), img=L_comp_img)

        # combine images
        rl_imgs.append(math_img("r+l", r=R_comp_img, l=L_comp_img))

        # combine terms
        if terms:
            r_ic_terms, r_ic_term_vals = get_ic_terms(R_img.terms, rci, sign=r_sign)
            l_ic_terms, l_ic_term_vals = get_ic_terms(L_img.terms, lci, sign=l_sign)
            rl_term_vals.append((r_ic_term_vals + l_ic_term_vals) / 2)

    # Squash into single image
    concat_img = nib.concat_images(rl_imgs)
    if terms:
        concat_img.terms = dict(zip(terms, np.asarray(rl_term_vals).T))
    return concat_img
def create4Ddwi(name,out):
	import glob
	print("Looking for %s" % name)
	name = str(name)
	out = str(out)
	a = glob.glob(name + "*.img")
	assert(len(a) >= 30)
	print("%i volumes found" % len(a))
	a.sort(key=lambda x: x[-8:])
	import nibabel as nb
	print("Concatenating %i files" % len(a))
	b = nb.concat_images(a)
	b.get_header()
	out_file = out+".nii.gz"
	print("Writing to %s" % out_file)
	nb.save(b, out_file)
	return out_file
Ejemplo n.º 50
0
def create4Ddwi(name, out):
    import glob
    print("Looking for %s" % name)
    name = str(name)
    out = str(out)
    a = glob.glob(name + "*.img")
    assert (len(a) >= 30)
    print("%i volumes found" % len(a))
    a.sort(key=lambda x: x[-8:])
    import nibabel as nb
    print("Concatenating %i files" % len(a))
    b = nb.concat_images(a)
    b.get_header()
    out_file = out + ".nii.gz"
    print("Writing to %s" % out_file)
    nb.save(b, out_file)
    return out_file
Ejemplo n.º 51
0
def smooth_image(img, fwhm, **kwargs):
    """Function wrapper LinearFilter class. Spatially smoothens img with
    kernel of size fwhm.

    Parameters
    ----------
    img: ``ni.Nifti1Image``
        image to be smoothen
    fwhm: 1D array like of size as big as there are spatial dimensions
    in the image
        FWHM of smoothing kernel
    **kwargs: dict-like
        key-word arguments passed to LinearFilter constructor.

    Returns
    -------
    Smoothened image, same type and size as the input img.

    """

    if isinstance(img, basestring):
        img = ni.load(img)
    elif isinstance(img, tuple):
        assert len(img) == 2
        return smooth_image(ni.Nifti1Image(img[0], img[1]), fwhm, **kwargs)
    elif isinstance(img, list):
        return [smooth_image(x, fwhm, **kwargs) for x in img]
    else:
        assert is_niimg(img)

    if len(img.shape) == 4:
        return ni.concat_images(
            [smooth_image(vol, fwhm, **kwargs)
             for vol in ni.four_to_three(img)])
    else:
        assert len(img.shape) == 3

        smoothing_kernel = LinearFilter(
            img.get_affine(),
            img.shape,
            fwhm=fwhm,
            **kwargs)

        return ni.Nifti1Image(smoothing_kernel.smooth(img.get_data(),
                                                      clean=True),
                              img.get_affine())
def create_mean_img(raw_paths, memory_load):

    print('calculating mean image')
    if memory_load == 'high':
        print('loading res images...')
        imgs = ni.concat_images(raw_paths).get_data()
        print('calculating...')
        mean_img = res_imgs.mean()
    else:
        mean_img = ni.load(raw_paths[0]).get_data()
        for path in raw_paths[1:]:
            img = ni.load(path).get_data()
            mean_img += img
        divisor = np.full_like(mean_img, len(raw_paths))
        mean_img = mean_img / divisor

    return mean_img
Ejemplo n.º 53
0
def smooth_image(img, fwhm, **kwargs):
    """Function wrapper LinearFilter class. Spatially smoothens img with
    kernel of size fwhm.

    Parameters
    ----------
    img: ``ni.Nifti1Image``
        image to be smoothen
    fwhm: 1D array like of size as big as there are spatial dimensions
    in the image
        FWHM of smoothing kernel
    **kwargs: dict-like
        key-word arguments passed to LinearFilter constructor.

    Returns
    -------
    Smoothened image, same type and size as the input img.

    """

    if isinstance(img, basestring):
        img = ni.load(img)
    elif isinstance(img, tuple):
        assert len(img) == 2
        return smooth_image(ni.Nifti1Image(img[0], img[1]), fwhm, **kwargs)
    elif isinstance(img, list):
        return [smooth_image(x, fwhm, **kwargs) for x in img]
    else:
        assert is_niimg(img)

    if len(img.shape) == 4:
        return ni.concat_images([
            smooth_image(vol, fwhm, **kwargs) for vol in ni.four_to_three(img)
        ])
    else:
        assert len(img.shape) == 3

        smoothing_kernel = LinearFilter(img.get_affine(),
                                        img.shape,
                                        fwhm=fwhm,
                                        **kwargs)

        return ni.Nifti1Image(
            smoothing_kernel.smooth(img.get_data(), clean=True),
            img.get_affine())
Ejemplo n.º 54
0
def merge_first(inlist, out_file='first_merged.nii.gz'):
    import os.path as op
    import nibabel as nb
    import numpy as np
    import natsort as ns

    out_file = op.abspath(out_file)
    inlist = ns.natsorted(inlist, key=lambda y: y.lower())

    imgs = [nb.load(f) for f in inlist]
    im = nb.concat_images(imgs)
    data = np.clip(np.sum(np.squeeze(im.get_data()), axis=3),
                   0.0, 1.0)

    nb.Nifti1Image(data, imgs[0].get_affine(),
                   imgs[0].get_header()).to_filename(out_file)

    return out_file
Ejemplo n.º 55
0
Archivo: fc.py Proyecto: IBT-FMI/SAMRI
def dual_regression(
    substitutions_a,
    substitutions_b,
    all_merged_path="~/all_merged.nii.gz",
    components=9,
    group_level="concat",
    tr=1,
    ts_file_template="{data_dir}/preprocessing/{preprocessing_dir}/sub-{subject}/ses-{session}/func/sub-{subject}_ses-{session}_task-{scan}.nii.gz",
):

    all_merged_path = path.abspath(path.expanduser(all_merged_path))

    ts_a = []
    for substitution in substitutions_a:
        ts_a.append(
            path.abspath(
                path.expanduser(ts_file_template.format(**substitution))))
    ts_b = []
    for substitution in substitutions_b:
        ts_b.append(
            path.abspath(
                path.expanduser(ts_file_template.format(**substitution))))

    ts_all = ts_a + ts_b
    if group_level == "concat" and not path.isfile(all_merged_path):
        ts_all_merged = nib.concat_images(ts_all, axis=3)
        ts_all_merged.to_filename(all_merged_path)

    ica = fsl.model.MELODIC()
    ica.inputs.report = True
    ica.inputs.tr_sec = tr
    ica.inputs.no_bet = True
    ica.inputs.no_mask = True
    ica.inputs.sep_vn = True
    if components:
        ica.inputs.dim = int(components)
    if group_level == "migp":
        ica.inputs.in_files = ts_all
        ica._cmd = 'melodic --migp'
    elif group_level == "concat":
        ica.inputs.approach = "concat"
        ica.inputs.in_files = all_merged_path
    print(ica.cmdline)
    ica_run = ica.run()
Ejemplo n.º 56
0
def estimate_motion(nifti_image):
    # BEGIN STDOUT SUPRESSION
    actualstdout = sys.stdout
    sys.stdout = open(os.devnull,'w')
    # We want to use the middle time point as the reference. But the algorithm does't allow that, so fake it.
    ref_vol = nifti_image.shape[3]/2 + 1
    ims = nb.four_to_three(nifti_image)
    reg = Realign4d(nb.concat_images([ims[ref_vol]] + ims)) # in the next release, we'll need to add tr=1.

    reg.estimate(loops=3) # default: loops=5
    aligned = reg.resample(0)[:,:,:,1:]
    sys.stdout = actualstdout
    # END STDOUT SUPRESSION
    abs_disp = []
    rel_disp = []
    transrot = []
    prev_T = None
    # skip the first one, since it's the reference volume
    for T in reg._transforms[0][1:]:
        # get the full affine for this volume by pre-multiplying by the reference affine
        #mc_affine = np.dot(ni.get_affine(), T.as_affine())
        transrot.append(T.translation.tolist()+T.rotation.tolist())
        # Compute the mean displacement
        # See http://www.fmrib.ox.ac.uk/analysis/techrep/tr99mj1/tr99mj1/node5.html
        # radius of the spherical head assumption (in mm):
        R = 80.
        # The center of the volume. Assume 0,0,0 in world coordinates.
        # Note: it might be better to use the center of mass of the brain mask.
        xc = np.matrix((0,0,0)).T
        T_error = T.as_affine() - np.eye(4)
        A = np.matrix(T_error[0:3,0:3])
        t = np.matrix(T_error[0:3,3]).T
        abs_disp.append(np.sqrt( R**2. / 5 * np.trace(A.T * A) + (t + A*xc).T * (t + A*xc) ).item())
        if prev_T!=None:
            T_error = T.as_affine() - prev_T.as_affine() # - np.eye(4)
            A = np.matrix(T_error[0:3,0:3])
            t = np.matrix(T_error[0:3,3]).T
            rel_disp.append(np.sqrt( R**2. / 5 * np.trace(A.T * A) + (t + A*xc).T * (t + A*xc) ).item())
        else:
            rel_disp.append(0.0)
        prev_T = T
    return aligned,np.array(abs_disp),np.array(rel_disp),np.array(transrot)
Ejemplo n.º 57
0
def save_preproc(model_dir, doc):
    if 'swabold' in doc:
        run_key = doc['runs']
        for label, session_data, motion in zip(
                run_key, doc['swabold'], doc['motion']):
            if isinstance(session_data, (list, np.ndarray)):
                img = nb.concat_images(session_data)
            else:
                img = nb.load(session_data)
            session_dir = make_dir(model_dir, 'BOLD', label)
            nb.save(img, os.path.join(session_dir, 'bold.nii.gz'))
            if isinstance(motion, (str, unicode)):
                shutil.copyfile(
                    motion, os.path.join(session_dir, 'motion.txt'))
            else:
                np.savetxt(os.path.join(session_dir, 'motion.txt'), motion)
    if 'wmanatomy' in doc:
        anat_dir = make_dir(model_dir, 'anatomy')
        img = nb.load(doc['wmanatomy'])
        nb.save(img, os.path.join(anat_dir, 'highres001_brain.nii.gz'))
def clean_warp_field_fn(combined_warp_x, combined_warp_y, combined_warp_z, default_value):
    import os.path as op
    from nipype import logging
    from nipype.utils.filemanip import split_filename
    import nibabel as nb
    import numpy as np
    path, name, ext = split_filename(combined_warp_x)
    out_file = op.abspath(name + 'CleanedWarp.nii')
    iflogger = logging.getLogger('interface')
    iflogger.info(default_value)
    imgs = []
    filenames = [combined_warp_x, combined_warp_y, combined_warp_z]
    for fname in filenames:
        img = nb.load(fname)
        data = img.get_data()
        data[data==default_value] = np.NaN
        new_img = nb.Nifti1Image(data=data, header=img.get_header(), affine=img.get_affine())
        imgs.append(new_img)
    image4d = nb.concat_images(imgs, check_affines=True)
    nb.save(image4d, out_file)
    return out_file
Ejemplo n.º 59
0
def load_4D_img(img):
    """
    Loads a single 4D image or list of 3D images into a single 4D niimg.

    Parameters
    ----------
    img: string, list of strings, nibabel image object, or list of
    nibabel image objects.
        image(s) to load

    Returns
    -------
    4D nibabel image object

    """

    if isinstance(img, basestring):
        img = nibabel.load(img)
    elif isinstance(img, list):
        img = nibabel.concat_images(img, check_affines=False)
    elif isinstance(img, tuple):
        assert len(img) == 2
        img = nibabel.Nifti1Image(*img)
    else:
        assert is_niimg(img)

    assert len(img.shape) > 3

    if len(img.shape) > 4:
        assert len(img.shape) == 5
        if img.shape[-1] == 1:
            img = nibabel.Nifti1Image(img.get_data()[..., 0],
                                      img.get_affine())
        else:
            assert img.shape[3] == 1
            img = nibabel.Nifti1Image(img.get_data()[..., 0, ...],
                                      img.get_affine())

    return img
Ejemplo n.º 60
0
def eddy_current_correction(data,affine,target=None,target_affine=None):
    result=[]
    
    no_dirs=data.shape[-1]

    if target==None and target_affine==None:
        target=ni.Nifti1Image(data[:,:,:,0],affine)
    else:
        target=ni.Nifti1Image(target,target_affine)
        
    for i in range(1,no_dirs):        
        
        source=ni.Nifti1Image(data[:,:,:,i],affine)        
        T=dp.volume_register(source,target,similarity,\
                                 interp,subsampling,search,optimizer)
        sourceT=dp.volume_transform(source, T.inv(), reference=target)
        print i, sourceT.get_data().shape, sourceT.affine.shape
        result.append(sourceT)

    result.insert(0,target)
    print 'no of images',len(result)
    return ni.concat_images(result)