def main(file_to_denoise, param, output_file_name):

    path, file, ext = sct.extract_fname(file_to_denoise)

    img = nib.load(file_to_denoise)
    hdr_0 = img.get_header()

    data = img.get_data()
    aff = img.get_affine()

    mask = data[:, :, :] > 80

    data = data[:, :, :]

    print("vol size", data.shape)

    t = time()

    sigma = np.std(data[~mask])

    if param.parameter == 'Rician':
        den = nlmeans(data, sigma=sigma, mask=mask, rician=True)
    else:
        den = nlmeans(data, sigma=sigma, mask=mask, rician=False)

    print("total time", time() - t)
    print("vol size", den.shape)

    axial_middle = data.shape[2] / 2

    before = data[:, :, axial_middle].T
    after = den[:, :, axial_middle].T

    diff_3d = np.absolute(den.astype('f8') - data.astype('f8'))
    difference = np.absolute(after.astype('f8') - before.astype('f8'))
    difference[~mask[:, :, axial_middle].T] = 0

    if param.verbose == 2:
        fig, ax = plt.subplots(1, 3)
        ax[0].imshow(before, cmap='gray', origin='lower')
        ax[0].set_title('before')
        ax[1].imshow(after, cmap='gray', origin='lower')
        ax[1].set_title('after')
        ax[2].imshow(difference, cmap='gray', origin='lower')
        ax[2].set_title('difference')
        for i in range(3):
            ax[i].set_axis_off()

        plt.show()
        plt.savefig('denoised_S0.png', bbox_inches='tight')

    #Save files
    img_denoize = nib.Nifti1Image(den, None, hdr_0)
    img_diff = nib.Nifti1Image(diff_3d, None, hdr_0)
    if output_file_name != None:
        output_file_name = output_file_name
    else:
        output_file_name = file + '_denoised' + ext
    nib.save(img_denoize, output_file_name)
    nib.save(img_diff, file + '_difference' + ext)
def main(file_to_denoize, param, output_file_name) :

    path, file, ext = sct.extract_fname(file_to_denoize)

    img = nib.load(file_to_denoize)
    hdr_0 = img.get_header()

    data = img.get_data()
    aff = img.get_affine()

    mask = data[:, :, :] > 80

    data = data[:, :, :]

    print("vol size", data.shape)

    t = time()

    sigma = np.std(data[~mask])

    if param.parameter == 'Rician':
        den = nlmeans(data, sigma=sigma, mask=mask, rician=True)
    else : den = nlmeans(data, sigma=sigma, mask=mask, rician=False)

    print("total time", time() - t)
    print("vol size", den.shape)


    axial_middle = data.shape[2] / 2

    before = data[:, :, axial_middle].T
    after = den[:, :, axial_middle].T

    diff_3d = np.absolute(den.astype('f8') - data.astype('f8'))
    difference = np.absolute(after.astype('f8') - before.astype('f8'))
    difference[~mask[:, :, axial_middle].T] = 0

    if param.verbose == 2 :
        fig, ax = plt.subplots(1, 3)
        ax[0].imshow(before, cmap='gray', origin='lower')
        ax[0].set_title('before')
        ax[1].imshow(after, cmap='gray', origin='lower')
        ax[1].set_title('after')
        ax[2].imshow(difference, cmap='gray', origin='lower')
        ax[2].set_title('difference')
        for i in range(3):
            ax[i].set_axis_off()

        plt.show()
        plt.savefig('denoised_S0.png', bbox_inches='tight')

    #Save files
    img_denoize = nib.Nifti1Image(den, None, hdr_0)
    img_diff = nib.Nifti1Image(diff_3d, None, hdr_0)
    if output_file_name != None :
        output_file_name =output_file_name
    else: output_file_name = file + '_denoized' + ext
    nib.save(img_denoize,output_file_name)
    nib.save(img_diff, file + '_difference' +ext)
示例#3
0
def test_nlmeans_dtype():

    S0 = 200 * np.ones((20, 20, 20, 3), dtype='f4')
    mask = np.zeros((20, 20, 20))
    mask[10:14, 10:14, 10:14] = 1
    S0n = nlmeans(S0, sigma=1, mask=mask, rician=True)
    assert_equal(S0.dtype, S0n.dtype)

    S0 = 200 * np.ones((20, 20, 20), dtype=np.uint16)
    mask = np.zeros((20, 20, 20))
    mask[10:14, 10:14, 10:14] = 1
    S0n = nlmeans(S0, sigma=1, mask=mask, rician=True)
    assert_equal(S0.dtype, S0n.dtype)
示例#4
0
def test_nlmeans_dtype():

    S0 = 200 * np.ones((20, 20, 20, 3), dtype='f4')
    mask = np.zeros((20, 20, 20))
    mask[10:14, 10:14, 10:14] = 1
    S0n = nlmeans(S0, sigma=1, mask=mask, rician=True)
    assert_equal(S0.dtype, S0n.dtype)

    S0 = 200 * np.ones((20, 20, 20), dtype=np.uint16)
    mask = np.zeros((20, 20, 20))
    mask[10:14, 10:14, 10:14] = 1
    S0n = nlmeans(S0, sigma=np.ones((20, 20, 20)), mask=mask, rician=True)
    assert_equal(S0.dtype, S0n.dtype)
示例#5
0
def test_denoise():
    """

    """
    fdata, fbval, fbvec = dpd.get_fnames()
    # Test on 4D image:
    data = nib.load(fdata).get_data()
    sigma1 = estimate_sigma(data)
    nlmeans(data, sigma=sigma1)

    # Test on 3D image:
    data = data[..., 0]
    sigma2 = estimate_sigma(data)
    nlmeans(data, sigma=sigma2)
示例#6
0
文件: denoise.py 项目: Neurita/pypes
def nlmeans_denoise_img(img, mask, N=4):
    """ Apply dipy nlmeans denoising to the img. Useful for diffusion images.
    Parameters
    ----------
    img: nibabel.Nifti1Image
        The diffusion image

    mask: nibabel.Nifti1Image
        A brain mask image.

    N: int
        Number of arrays of the head coil used to acquired the image.

    Returns
    -------
    den_img: nibabel.Nifti1Image
        A denoised nifti image object with the same headers
        and affine as `img`.
    """
    from dipy.denoise.nlmeans import nlmeans
    from dipy.denoise.noise_estimate import estimate_sigma

    data = img.get_data()
    msk  = mask.get_data()

    sigma = estimate_sigma(data, N=N)
    return nlmeans(data, sigma=sigma, mask=msk)
示例#7
0
def nlmeans_proxy(in_file, settings, noise_mask=None, out_file=None):
    """
    Uses non-local means to denoise 4D datasets
    """
    package_check('dipy', version='0.8.0.dev')
    from dipy.denoise.nlmeans import nlmeans

    if out_file is None:
        fname, fext = op.splitext(op.basename(in_file))
        if fext == '.gz':
            fname, fext2 = op.splitext(fname)
            fext = fext2 + fext
        out_file = op.abspath('./%s_denoise%s' % (fname, fext))

    img = nb.load(in_file)
    hdr = img.header
    data = img.get_data()
    aff = img.affine

    nmask = data[..., 0] > 80
    if noise_mask is not None:
        nmask = noise_mask > 0

    sigma = np.std(data[nmask == 1])
    den = nlmeans(data, sigma, **settings)

    nb.Nifti1Image(den.astype(hdr.get_data_dtype()), aff,
                   hdr).to_filename(out_file)
    return out_file, sigma
	def _run_interface(self, runtime):
		import nibabel as nib
		import numpy as np
		from dipy.denoise.nlmeans import nlmeans
		from nipype.utils.filemanip import split_filename

		fname = self.inputs.in_file
		img = nib.load(fname)
		data = img.get_data()
		affine = img.get_affine()
		mask = data[..., 0] > 80
		a = data.shape

		denoised_data = np.ndarray(shape=data.shape)
		for image in range(0,a[3]):
		    print(str(image + 1) + '/' + str(a[3] + 1))
		    dat = data[...,image]
		    sigma = np.std(dat[~mask]) # Calculating the standard deviation of the noise
		    den = nlmeans(dat, sigma=sigma, mask=mask)
		    denoised_data[:,:,:,image] = den

		_, base, _ = split_filename(fname)
		nib.save(nib.Nifti1Image(denoised_data, affine), base + '_denoised.nii')

		return runtime
示例#9
0
def nlmeans_denoise_img(img, mask, N=4):
    """ Apply dipy nlmeans denoising to the img. Useful for diffusion images.
    Parameters
    ----------
    img: nibabel.Nifti1Image
        The diffusion image

    mask: nibabel.Nifti1Image
        A brain mask image.

    N: int
        Number of arrays of the head coil used to acquired the image.

    Returns
    -------
    den_img: nibabel.Nifti1Image
        A denoised nifti image object with the same headers
        and affine as `img`.
    """
    from dipy.denoise.nlmeans import nlmeans
    from dipy.denoise.noise_estimate import estimate_sigma

    data = img.get_data()
    msk = mask.get_data()

    sigma = estimate_sigma(data, N=N)
    return nlmeans(data, sigma=sigma, mask=msk)
示例#10
0
def preprocess(nifti, name):
    """Preprocess the 3D MRI image before image segmentation"""
    image = nifti.get_fdata()
    sigma = estimate_sigma(image, N=16)  # N: number of coils in the receiver of the MRI scanner
    denoised = nlmeans(image, sigma)
    denoised_nifti = nib.Nifti1Image(denoised, nifti.affine)
    nib.save(denoised_nifti, f'lab4/data/clean_{name}.nii.gz')
	def _run_interface(self, runtime):
		import nibabel as nib
		import numpy as np
		import matplotlib.pyplot as plt
		from dipy.denoise.nlmeans import nlmeans
		from nipype.utils.filemanip import split_filename

		fname = self.inputs.in_file
		img = nib.load(fname)
		data = img.get_data()
		affine = img.get_affine()
		mask = data[..., 0] > 80
		a = data.shape 

		denoised_data = np.ndarray(shape=data.shape)
		for image in range(0,a[3]):
		    print(str(image + 1) + '/' + str(a[3] + 1))
		    dat = data[...,image]
		    sigma = np.std(dat[~mask]) # Calculating the standard deviation of the noise 
		    den = nlmeans(dat, sigma=sigma, mask=mask)
		    denoised_data[:,:,:,image] = den

		_, base, _ = split_filename(fname)
		nib.save(nib.Nifti1Image(denoised_data, affine), base + '_denoised.nii')

		return runtime
示例#12
0
def nlmeans_proxy(in_file, settings,
                  noise_mask=None, out_file=None):
    """
    Uses non-local means to denoise 4D datasets
    """
    package_check('dipy', version='0.8.0.dev')
    from dipy.denoise.nlmeans import nlmeans

    if out_file is None:
        fname, fext = op.splitext(op.basename(in_file))
        if fext == '.gz':
            fname, fext2 = op.splitext(fname)
            fext = fext2 + fext
        out_file = op.abspath('./%s_denoise%s' % (fname, fext))

    img = nb.load(in_file)
    hdr = img.get_header()
    data = img.get_data()
    aff = img.get_affine()

    nmask = data[..., 0] > 80
    if noise_mask is not None:
        nmask = noise_mask > 0

    sigma = np.std(data[nmask == 1])
    den = nlmeans(data, sigma, **settings)

    nb.Nifti1Image(den.astype(hdr.get_data_dtype()), aff,
                   hdr).to_filename(out_file)
    return out_file, sigma
示例#13
0
def main():
    parser = _build_args_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, [args.input])
    assert_outputs_exists(parser, args, [args.output],
                          [args.logfile, args.save_piesno_mask])

    logging.basicConfig()
    log = logging.getLogger(__name__)
    if args.verbose:
        log.setLevel(level=logging.INFO)
    else:
        log.setLevel(level=logging.WARNING)

    if args.logfile is not None:
        log.addHandler(logging.FileHandler(args.logfile, mode='w'))

    vol = nib.load(args.input)
    data = vol.get_data()
    if args.mask is None:
        mask = np.ones(data.shape[:3], dtype=np.bool)
    else:
        mask = nib.load(args.mask).get_data().astype(np.bool)

    sigma = args.sigma
    noise_method = args.noise_method
    if args.N == 0 and sigma is None and noise_method == PIESNO:
        raise ValueError('PIESNO is not designed for Gaussian noise, but you '
                         'specified N = 0.')

    # Check if dataset is 3D. If so, ensure the user didn't ask for PIESNO.
    # This is unsupported.
    if data.ndim == 3 and noise_method == PIESNO:
        parser.error('Cannot use PIESNO noise estimation with a 3D dataset. '
                     'Please use the basic estimation')

    if sigma is not None:
        log.info('User supplied noise standard deviation is %s', sigma)
        # Broadcast the single value to a whole 3D volume for nlmeans
        sigma = np.ones(data.shape[:3]) * sigma
    else:
        log.info('Estimating noise with method %s', args.noise_method)
        if args.noise_method == PIESNO:
            sigma = _get_piesno_sigma(vol, log, args)
        else:
            sigma = _get_basic_sigma(vol.get_data(), log)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=DeprecationWarning)
        data_denoised = nlmeans(data,
                                sigma,
                                mask=mask,
                                rician=args.N > 0,
                                num_threads=args.nbr_processes)

    nib.save(nib.Nifti1Image(data_denoised, vol.affine, vol.header),
             args.output)
示例#14
0
def denoise(x, mask):
    subjectid = x[0][0]
    from dipy.denoise import nlmeans
    from dipy.denoise.noise_estimate import estimate_sigma
    sigma = estimate_sigma(x[1])
    #return(x[0], nlmeans.nlmeans(x[1], num_threads=1, sigma=sigma, mask=mask.value[str(subjectid)]))
    return (x[0],
            nlmeans.nlmeans(x[1], sigma=sigma,
                            mask=mask.value[str(subjectid)]))
示例#15
0
    def run(self, input_files, sigma=0, patch_radius=1, block_radius=5,
            rician=True, out_dir='', out_denoised='dwi_nlmeans.nii.gz'):
        """Workflow wrapping the nlmeans denoising method.

        It applies nlmeans denoise on each file found by 'globing'
        ``input_files`` and saves the results in a directory specified by
        ``out_dir``.

        Parameters
        ----------
        input_files : string
            Path to the input volumes. This path may contain wildcards to
            process multiple inputs at once.
        sigma : float, optional
            Sigma parameter to pass to the nlmeans algorithm
            (default: auto estimation).
        patch_radius : int, optional
            patch size is ``2 x patch_radius + 1``. Default is 1.
        block_radius : int, optional
            block size is ``2 x block_radius + 1``. Default is 5.
        rician : bool, optional
            If True the noise is estimated as Rician, otherwise Gaussian noise
            is assumed.
        out_dir : string, optional
            Output directory (default input file directory)
        out_denoised : string, optional
            Name of the resulting denoised volume (default: dwi_nlmeans.nii.gz)

        References
        ----------
        .. [Descoteaux08] Descoteaux, Maxime and Wiest-Daesslé, Nicolas and
        Prima, Sylvain and Barillot, Christian and Deriche, Rachid.
        Impact of Rician Adapted Non-Local Means Filtering on
        HARDI, MICCAI 2008

        """
        io_it = self.get_io_iterator()
        for fpath, odenoised in io_it:
            if self._skip:
                shutil.copy(fpath, odenoised)
                logging.warning('Denoising skipped for now.')
            else:
                logging.info('Denoising %s', fpath)
                data, affine, image = load_nifti(fpath, return_img=True)

                if sigma == 0:
                    logging.info('Estimating sigma')
                    sigma = estimate_sigma(data)
                    logging.debug('Found sigma {0}'.format(sigma))

                denoised_data = nlmeans(data, sigma=sigma,
                                        patch_radius=patch_radius,
                                        block_radius=block_radius,
                                        rician=rician)
                save_nifti(odenoised, denoised_data, affine, image.header)

                logging.info('Denoised volume saved as %s', odenoised)
示例#16
0
def test_nlmeans_boundary():
    # nlmeans preserves boundaries

    S0 = 100 + np.zeros((20, 20, 20))

    noise = 2 * np.random.standard_normal((20, 20, 20))

    S0 += noise

    S0[:10, :10, :10] = 300 + noise[:10, :10, :10]

    nlmeans(S0, sigma=np.ones((20, 20, 20)) * np.std(noise), rician=False)

    print(S0[9, 9, 9])
    print(S0[10, 10, 10])

    assert_(S0[9, 9, 9] > 290)
    assert_(S0[10, 10, 10] < 110)
示例#17
0
def test_nlmeans_4D_and_mask():
    S0 = 200 * np.ones((20, 20, 20, 3), dtype='f8')

    mask = np.zeros((20, 20, 20))
    mask[10, 10, 10] = 1

    S0n = nlmeans(S0, sigma=1, mask=mask, rician=True)
    assert_equal(S0.shape, S0n.shape)
    assert_equal(np.round(S0n[10, 10, 10]), 200)
    assert_equal(S0n[8, 8, 8], 0)
示例#18
0
def test_nlmeans_boundary():
    # nlmeans preserves boundaries

    S0 = 100 + np.zeros((20, 20, 20))

    noise = 2 * np.random.standard_normal((20, 20, 20))

    S0 += noise

    S0[:10, :10, :10] = 300 + noise[:10, :10, :10]

    nlmeans(S0, sigma=np.ones((20, 20, 20)) * np.std(noise),
            rician=False)

    print(S0[9, 9, 9])
    print(S0[10, 10, 10])

    assert_(S0[9, 9, 9] > 290)
    assert_(S0[10, 10, 10] < 110)
示例#19
0
 def denoise_image(self, image, mask):
     sigma = estimate_sigma(image, N=4)
     den = nlmeans(image,
                   sigma=sigma,
                   mask=mask,
                   patch_radius=1,
                   block_radius=1,
                   rician=True)
     diff = np.abs(den.astype('f8') - image.astype('f8'))
     return den, diff
示例#20
0
def test_nlmeans_4D_and_mask():
    S0 = 200 * np.ones((20, 20, 20, 3), dtype='f8')

    mask = np.zeros((20, 20, 20))
    mask[10, 10, 10] = 1

    S0n = nlmeans(S0, sigma=1, mask=mask, rician=True)
    assert_equal(S0.shape, S0n.shape)
    assert_equal(np.round(S0n[10, 10, 10]), 200)
    assert_equal(S0n[8, 8, 8], 0)
示例#21
0
def test_nlmeans_random_noise():
    S0 = 100 + 2 * np.random.standard_normal((22, 23, 30))

    S0n = nlmeans(S0, sigma=np.ones((22, 23, 30)) * np.std(S0), rician=False)

    print(S0.mean(), S0.min(), S0.max())
    print(S0n.mean(), S0n.min(), S0n.max())

    assert_(S0n.min() > S0.min())
    assert_(S0n.max() < S0.max())
    assert_equal(np.round(S0n.mean()), 100)
示例#22
0
def denoiser(X, method='gaussian'):
    #ss = X.shape
    X = X.reshape(X.shape[1:])
    assert method in ['gaussian', 'nlmean', 'none']
    s = X.std()
    if method == 'gaussian':
        return scipy.ndimage.filters.gaussian_filter(X, sigma=s).reshape(
            1, *X.shape)
    if method == 'nlmean':
        return nlmeans(X, sigma=s / 10, block_radius=1).reshape(1, *X.shape)
    return X.reshape(1, *X.shape)
示例#23
0
def test_nlmeans_random_noise():
    S0 = 100 + 2 * np.random.standard_normal((22, 23, 30))

    S0n = nlmeans(S0, sigma=np.std(S0), rician=False)

    print(S0.mean(), S0.min(), S0.max())
    print(S0n.mean(), S0n.min(), S0n.max())

    assert_(S0n.min() > S0.min())
    assert_(S0n.max() < S0.max())
    assert_equal(np.round(S0n.mean()), 100)
示例#24
0
def test_nlmeans_4d_3dsigma_and_threads():
    # Input is 4D data and 3D sigma
    data = np.ones((50, 50, 50, 5))
    sigma = np.ones(data.shape[:3])
    mask = np.zeros(data.shape[:3])

    # mask[25-10:25+10] = 1
    mask[:] = 1

    print('cpu count %d' % (cpu_count(), ))

    print('1')
    t = time()
    new_data = nlmeans(data, sigma, mask, num_threads=1)
    duration_1core = time() - t
    print(duration_1core)

    print('All')
    t = time()
    new_data2 = nlmeans(data, sigma, mask, num_threads=None)
    duration_all_core = time() - t
    print(duration_all_core)

    print('2')
    t = time()
    new_data3 = nlmeans(data, sigma, mask, num_threads=2)
    duration_2core = time() - t
    print(duration_all_core)

    assert_array_almost_equal(new_data, new_data2)
    assert_array_almost_equal(new_data2, new_data3)

    if cpu_count() > 2:

        assert_equal(duration_all_core < duration_2core, True)
        assert_equal(duration_2core < duration_1core, True)

    if cpu_count() == 2:

        assert_equal(duration_2core < duration_1core, True)
示例#25
0
def test_nlmeans_4d_3dsigma_and_threads():
    # Input is 4D data and 3D sigma
    data = np.ones((50, 50, 50, 5))
    sigma = np.ones(data.shape[:3])
    mask = np.zeros(data.shape[:3])

    # mask[25-10:25+10] = 1
    mask[:] = 1

    print('cpu count %d' % (cpu_count(),))

    print('1')
    t = time()
    new_data = nlmeans(data, sigma, mask, num_threads=1)
    duration_1core = time() - t
    print(duration_1core)

    print('All')
    t = time()
    new_data2 = nlmeans(data, sigma, mask, num_threads=None)
    duration_all_core = time() - t
    print(duration_all_core)

    print('2')
    t = time()
    new_data3 = nlmeans(data, sigma, mask, num_threads=2)
    duration_2core = time() - t
    print(duration_all_core)

    assert_array_almost_equal(new_data, new_data2)
    assert_array_almost_equal(new_data2, new_data3)

    if cpu_count() > 2:

        assert_equal(duration_all_core < duration_2core, True)
        assert_equal(duration_2core < duration_1core, True)

    if cpu_count() == 2:

        assert_equal(duration_2core < duration_1core, True)
示例#26
0
    def _run_interface(self, runtime):

        import nibabel as nib
        from dipy.denoise.nlmeans import nlmeans
        from dipy.denoise.noise_estimate import estimate_sigma
        import numpy as np

        img = nib.load(self.inputs.in_file)

        data = img.get_data()
        affine = img.affine

        if len(data.shape) > 3:
            den = np.zeros(data.shape)

            for i in range(data.shape[-1]):
                print('direction # ' + str(i))
                sigma = estimate_sigma(data[..., i], N=4)
                den[..., i] = nlmeans(data[..., i],
                                      sigma=sigma,
                                      patch_radius=1,
                                      block_radius=5,
                                      rician=True)

            nib.save(nib.Nifti1Image(den.astype(np.float32), img.affine),
                     'denoised.nii.gz')
        else:
            sigma = estimate_sigma(data, N=4)

            den = nlmeans(data,
                          sigma=sigma,
                          patch_radius=1,
                          block_radius=5,
                          rician=True)

        nib.save(nib.Nifti1Image(den, affine), 'denoised.nii.gz')

        return runtime
示例#27
0
def Nonlocal(data,
             affine,
             keep=False,
             filt=100):  #Preguntar! #No usan denoise images PCA
    if len(data.shape) == 3:
        mask = data > filt
    else:
        mask = data[..., 1] > filt
    data2 = data  #Preguntar
    sigma = np.std(data2[~mask])
    den = nlmeans(data2, sigma=sigma, mask=mask)
    if keep:
        nib.save(nib.Nifti1Image(den.astype(np.float32), affine), "Nonlocal")
    return den
示例#28
0
def main():
    parser = _build_arg_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, args.in_image)
    assert_outputs_exist(parser, args, args.out_image, args.logfile)

    logging.basicConfig()
    log = logging.getLogger(__name__)
    if args.verbose:
        log.setLevel(level=logging.INFO)
    else:
        log.setLevel(level=logging.WARNING)

    if args.logfile is not None:
        log.addHandler(logging.FileHandler(args.logfile, mode='w'))

    vol = nib.load(args.in_image)
    data = vol.get_fdata(dtype=np.float32)
    if args.mask is None:
        mask = np.zeros(data.shape[0:3], dtype=bool)
        if data.ndim == 4:
            mask[np.sum(data, axis=-1) > 0] = 1
        else:
            mask[data > 0] = 1
    else:
        mask = get_data_as_mask(nib.load(args.mask), dtype=bool)

    sigma = args.sigma

    if sigma is not None:
        log.info('User supplied noise standard deviation is {}'.format(sigma))
        # Broadcast the single value to a whole 3D volume for nlmeans
        sigma = np.ones(data.shape[:3]) * sigma
    else:
        log.info('Estimating noise')
        sigma = _get_basic_sigma(vol.get_fdata(dtype=np.float32), log)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=DeprecationWarning)
        data_denoised = nlmeans(data,
                                sigma,
                                mask=mask,
                                rician=args.number_coils > 0,
                                num_threads=args.nbr_processes)

    nib.save(nib.Nifti1Image(data_denoised, vol.affine, header=vol.header),
             args.out_image)
示例#29
0
def example_output():
    data, sigma = load_data(flat=False, name=dataset_name)
    for b in range(1, 8):
        for p in range(1, 4):
            global B
            B = b
            global P
            P = p

            t_start = time.time()
            dipynlmeans = nlmeans.nlmeans(np.copy(data), sigma, mask=None,
                                          patch_radius=P, block_radius=B, rician=False)
            t_dipy = time.time()-t_start
            print("%d, %d, Time: %.3fs" % (b, p, t_dipy))
            imsave(path_to_folder + "/images/den_dipy_b%d_p%d_%s.png" % (b, p, dataset_name),
                   dipynlmeans[:, :, int(sh[2]/2)])
示例#30
0
文件: denoise.py 项目: emanuele/dipy
    def run(self,
            input_files,
            sigma=0,
            out_dir='',
            out_denoised='dwi_nlmeans.nii.gz'):
        """ Workflow wrapping the nlmeans denoising method.

        It applies nlmeans denoise on each file found by 'globing'
        ``input_files`` and saves the results in a directory specified by
        ``out_dir``.

        Parameters
        ----------
        input_files : string
            Path to the input volumes. This path may contain wildcards to
            process multiple inputs at once.
        sigma : float, optional
            Sigma parameter to pass to the nlmeans algorithm
            (default: auto estimation).
        out_dir : string, optional
            Output directory (default input file directory)
        out_denoised : string, optional
            Name of the resuting denoised volume (default: dwi_nlmeans.nii.gz)
        """
        io_it = self.get_io_iterator()
        for fpath, odenoised in io_it:
            if self._skip:
                shutil.copy(fpath, odenoised)
                logging.warning('Denoising skipped for now.')
            else:
                logging.info('Denoising {0}'.format(fpath))
                image = nib.load(fpath)
                data = image.get_data()

                if sigma == 0:
                    logging.info('Estimating sigma')
                    sigma = estimate_sigma(data)
                    logging.debug('Found sigma {0}'.format(sigma))

                denoised_data = nlmeans(data, sigma)
                denoised_image = nib.Nifti1Image(denoised_data,
                                                 image.get_affine(),
                                                 image.get_header())

                denoised_image.to_filename(odenoised)
                logging.info('Denoised volume saved as {0}'.format(odenoised))
示例#31
0
def denoise_nlmeans(data_in, patch_radius=1, block_radius=5):
    """
    :param data_in: nd_array to denoise

    .. note::
        for more info about patch_radius and block radius, please refer to the dipy website: http://dipy.org/dipy/reference/dipy.denoise.html#dipy.denoise.nlmeans.nlmeans
    """

    data_in = np.asarray(data_in)

    block_radius_max = min(data_in.shape) - 1
    block_radius = block_radius_max if block_radius > block_radius_max else block_radius

    sigma = estimate_sigma(data_in)
    denoised = nlmeans(data_in, sigma, patch_radius=patch_radius, block_radius=block_radius)

    return denoised
示例#32
0
def denoise_nlmeans(data_in, patch_radius=1, block_radius=5):
    """
    data_in: nd_array to denoise
    for more info about patch_radius and block radius, please refer to the dipy website: http://nipy.org/dipy/reference/dipy.denoise.html#dipy.denoise.nlmeans.nlmeans
    """
    from dipy.denoise.nlmeans import nlmeans
    from dipy.denoise.noise_estimate import estimate_sigma
    from numpy import asarray
    data_in = asarray(data_in)

    block_radius_max = min(data_in.shape)-1
    block_radius = block_radius_max if block_radius > block_radius_max else block_radius

    sigma = estimate_sigma(data_in)
    denoised = nlmeans(data_in, sigma, patch_radius=patch_radius, block_radius=block_radius)

    return denoised
示例#33
0
def denoise_nlmeans(data_in, patch_radius=1, block_radius=5):
    """
    data_in: nd_array to denoise
    for more info about patch_radius and block radius, please refer to the dipy website: http://nipy.org/dipy/reference/dipy.denoise.html#dipy.denoise.nlmeans.nlmeans
    """
    from dipy.denoise.nlmeans import nlmeans
    from dipy.denoise.noise_estimate import estimate_sigma
    from numpy import asarray
    data_in = asarray(data_in)
    block_radius_max = min(data_in.shape) - 1
    block_radius = block_radius_max if block_radius > block_radius_max else block_radius
    sigma = estimate_sigma(data_in)
    denoised = nlmeans(data_in,
                       sigma,
                       patch_radius=patch_radius,
                       block_radius=block_radius)
    return denoised
示例#34
0
def run_nonLocalMean(path_input,path_output):   
    print('    - running NonLocal Mean algoritm...')
    finalFileName = os.path.join(path_output, utils.to_extract_filename(path_input) + d.id_non_local_mean + d.extension)

    if not (os.path.exists(finalFileName)):
        img = nib.load(path_input)
        data = img.get_data()

        newData = np.zeros(data.shape)
        gradientDirections = data.shape[-1]

        for index in range(gradientDirections):
            print(index)
            sigma = estimate_sigma(data[:, :, :, index], N=8)
            newData[:, :, :, index] = nlmeans(data[:, :, :, index], sigma=sigma)

        nib.save(nib.Nifti1Image(newData.astype(np.float32), img.affine), finalFileName)
    return finalFileName
	def _run_interface(self, runtime):
		import nibabel as nib
		import numpy as np
		from dipy.denoise.nlmeans import nlmeans
		from nipype.utils.filemanip import split_filename

		fname = self.inputs.in_file
		img = nib.load(fname)
		data = img.get_data()
		affine = img.get_affine()
		mask = data > 20

		sigma = np.std(data[~mask]) # Calculating the standard deviation of the noise
		denoised_data = nlmeans(data, sigma=sigma, mask=mask)

		_, base, _ = split_filename(fname)
		nib.save(nib.Nifti1Image(denoised_data, affine), base + '_denoised.nii')

		return runtime
示例#36
0
def main():
    parser = _build_args_parser()
    args = parser.parse_args()

    assert_inputs_exist(parser, [args.input])
    assert_outputs_exists(parser, args, [args.output], [args.logfile])

    logging.basicConfig()
    log = logging.getLogger(__name__)
    if args.verbose:
        log.setLevel(level=logging.INFO)
    else:
        log.setLevel(level=logging.WARNING)

    if args.logfile is not None:
        log.addHandler(logging.FileHandler(args.logfile, mode='w'))

    vol = nb.load(args.input)
    data = vol.get_data()
    if args.mask is None:
        mask = np.ones(data.shape[:3], dtype=np.bool)
    else:
        mask = nb.load(args.mask).get_data().astype(np.bool)

    sigma = args.sigma

    if sigma is not None:
        log.info('User supplied noise standard deviation is %s', sigma)
        # Broadcast the single value to a whole 3D volume for nlmeans
        sigma = np.ones(data.shape[:3]) * sigma
    else:
        log.info('Estimating noise')
        sigma = _get_basic_sigma(vol.get_data(), log)

    with warnings.catch_warnings():
        warnings.simplefilter("ignore", category=DeprecationWarning)
        data_denoised = nlmeans(data,
                                sigma,
                                mask=mask,
                                rician=args.N > 0,
                                num_threads=args.nbr_processes)

    nb.save(nb.Nifti1Image(data_denoised, vol.affine, vol.header), args.output)
示例#37
0
文件: denoise.py 项目: MarcCote/dipy
    def run(self, input_files, sigma=0, out_dir='',
            out_denoised='dwi_nlmeans.nii.gz'):
        """ Workflow wrapping the nlmeans denoising method.

        It applies nlmeans denoise on each file found by 'globing'
        ``input_files`` and saves the results in a directory specified by
        ``out_dir``.

        Parameters
        ----------
        input_files : string
            Path to the input volumes. This path may contain wildcards to
            process multiple inputs at once.
        sigma : float, optional
            Sigma parameter to pass to the nlmeans algorithm
            (default: auto estimation).
        out_dir : string, optional
            Output directory (default input file directory)
        out_denoised : string, optional
            Name of the resuting denoised volume (default: dwi_nlmeans.nii.gz)
        """
        io_it = self.get_io_iterator()
        for fpath, odenoised in io_it:
            if self._skip:
                shutil.copy(fpath, odenoised)
                logging.warning('Denoising skipped for now.')
            else:
                logging.info('Denoising {0}'.format(fpath))
                image = nib.load(fpath)
                data = image.get_data()

                if sigma == 0:
                    logging.info('Estimating sigma')
                    sigma = estimate_sigma(data)
                    logging.debug('Found sigma {0}'.format(sigma))

                denoised_data = nlmeans(data, sigma)
                denoised_image = nib.Nifti1Image(
                    denoised_data, image.affine, image.header)

                denoised_image.to_filename(odenoised)
                logging.info('Denoised volume saved as {0}'.format(odenoised))
示例#38
0
def calc(triple):
    """ Calculation of one chunk - "Core-Function"
    """
    values = triple[1][0]
    sigmas = triple[1][1]

    new_values = np.zeros([SB, SB, SB], np.float64)
    ts = time.time()
    if use_cython:
        if use_dipy:
            new_values = nlmeans.nlmeans(values, (sigmas[0],), mask=None,
                                         patch_radius=P, block_radius=B,
                                         rician=False, add_padding=False,
                                         num_threads=1)
        else:
            new_values = denspeed.nlmeans_3d_single(values, None, sigmas,
                                                    P, B, False, None, SB)
            new_values = np.sqrt(new_values)
    else:
        sumw = np.zeros([SB, SB, SB], np.float64)
        patch_vol_size = (2*P+1)**3
        add_filter = np.ones([2*P+1, 2*P+1, 2*P+1])
        center_block = values[B-P: B+SB+P, B-P: B+SB+P, B-P: B+SB+P]
        for m in range(P, 2*B+1-P):
            for n in range(P, 2*B+1-P):
                for o in range(P, 2*B+1-P):
                    d = (center_block - values[m-P: m+SB+P, n-P: n+SB+P, o-P: o+SB+P])**2
                    summs = convolve(d, add_filter, mode="constant")[P:-P, P:-P, P:-P]
                    sigm = convolve(sigmas[m-P: m+SB+P, n-P: n+SB+P, o-P: o+SB+P],
                                    add_filter, mode="constant")[P:-P, P:-P, P:-P]

                    denom = np.sqrt(2) * (sigm / patch_vol_size)**2

                    ws = np.exp(-(summs / patch_vol_size) / denom)
                    sumw += ws
                    new_values += ws*values[m: m+SB, n: n+SB, o: o+SB]**2

        new_values[sumw != 0] /= sumw[sumw != 0]
        new_values[sumw == 0] = 0
        new_values = np.sqrt(new_values)

    return triple[0], new_values, time.time()-ts
示例#39
0
def denoise(dt):
    from dipy.denoise import nlmeans
    from dipy.denoise.noise_estimate import estimate_sigma
    import itertools
    item = dt[0]
    image = item[1]
    mask = item[2]
    sigma = estimate_sigma(image)
    denoised_data = nlmeans.nlmeans(image, sigma=sigma, mask=mask)
    [xp,yp,zp] = [4,4,4]
    [xSize,ySize,zSize] = [denoised_data.shape[0]/xp, denoised_data.shape[1]/yp, denoised_data.shape[2]/zp]
    datalist = []
    for x,y,z in itertools.product(range(xp), range(yp), range(zp)):
        [xS, yS, zS] = [x*xSize, y*ySize, z*zSize]
        [xE, yE, zE] = [denoised_data.shape[0] if x == xp - 1 else (x+1)*xSize, \
                        denoised_data.shape[1] if y == yp - 1 else (y+1)*ySize, \
                        denoised_data.shape[2] if z == zp - 1 else (z+1)*zSize]
        tup =(denoised_data[xS:xE, yS:yE, zS:zE],mask[xS:xE, yS:yE, zS:zE])
        datalist.append(tup)
    return datalist
def img_denoise(data, patch_rad=3, block_rad=4, n_sigma=4):

    #data = np.load(img)['vol_data']
    new_data = np.zeros((data.shape[1], data.shape[2], data.shape[0]))
    for ch in range(data.shape[0]):
        new_data[:, :, ch] = data[ch, :, :]

    sigma = estimate_sigma(new_data, N=n_sigma)
    #den = nlmeans(data, sigma=sigma, mask=mask, patch_radius= 1, block_radius = 1, rician= True)
    den = nlmeans(new_data,
                  sigma=sigma,
                  patch_radius=patch_rad,
                  block_radius=block_rad,
                  rician=True)
    #print("total time", time() - t)
    res_data = np.zeros(data.shape)
    for ch in range(data.shape[0]):
        res_data[ch, :, :] = den[:, :, ch]

    return res_data
示例#41
0
def execution(self, context):

    data_vol = aims.read(self.dwi_data.fullPath())
    header = data_vol.header()
    data = vol_to_array(data_vol)
    sigma_vol = aims.read(self.sigma.fullPath())
    sigma = vol_to_array(sigma_vol)
    if self.brain_mask is not None:
        brain_mask_vol = aims.read(self.brain_mask.fullPath())
        brain_mask = vol_to_array(brain_mask_vol)
    else:
        brain_mask = None

    denoised_data = nlmeans(data,
                            sigma,
                            mask=brain_mask,
                            patch_radius=self.patch_radius,
                            block_radius=self.block_radius,
                            rician=self.rician_noise)
    denoised_data_vol = array_to_vol(denoised_data, header=header)
    aims.write(denoised_data_vol, self.denoised_dwi_data.fullPath())
def denoise_dipy(input_dwi, input_bval, input_bvec, mask_image, output_dwi):
    #This function uses nlmeans as part of dipy to remove noise from images
    img = nib.load(input_dwi)
    data = img.get_data()
    mask = nib.load(mask_image).get_data()
    aff = img.get_affine()
    sform = img.get_sform()
    qform = img.get_qform()

    bvals, bvecs = read_bvals_bvecs(input_bval, input_bvec)
    values = np.array(bvals)
    ii = np.where(values == bvals.min())[0]

    sigma = estimate_sigma(data)
    sigma = np.mean(sigma[ii])

    den = nlmeans(data, sigma=sigma, mask=mask)

    den_img = nib.Nifti1Image(den.astype(np.float32), aff, img.header)
    den_img.set_sform(sform)
    den_img.set_qform(qform)
    nib.save(den_img, output_dwi)
def main(file_to_denoise, param, output_file_name) :

    path, file, ext = sct.extract_fname(file_to_denoise)

    img = nib.load(file_to_denoise)
    hdr_0 = img.get_header()

    data = img.get_data()
    # aff = img.get_affine()

    if min(data.shape) <= 5:
        sct.printv('One of the image dimensions is <= 5 : reducing the size of the block radius.')
        block_radius = min(data.shape) - 1
    else:
        block_radius = 5  # default value

    # Process for manual detecting of background
    # mask = data[:, :, :] > noise_threshold
    # data = data[:, :, :]

    if '-std' in arguments:
        sigma = std_noise
        # Application of NLM filter to the image
        print 'Applying Non-local mean filter...'
        if param.parameter == 'Rician':
            den = nlmeans(data, sigma=sigma, mask=None, rician=True, block_radius=block_radius)
        else : den = nlmeans(data, sigma=sigma, mask=None, rician=False, block_radius=block_radius)
    else:
        # # Process for manual detecting of background
        mask = data > noise_threshold
        sigma = np.std(data[~mask])
        # Application of NLM filter to the image
        print 'Applying Non-local mean filter...'
        if param.parameter == 'Rician':
            den = nlmeans(data, sigma=sigma, mask=mask, rician=True, block_radius=block_radius)
        else: den = nlmeans(data, sigma=sigma, mask=mask, rician=False, block_radius=block_radius)

    t = time()
    print("total time", time() - t)
    print("vol size", den.shape)


    axial_middle = data.shape[2] / 2

    before = data[:, :, axial_middle].T
    after = den[:, :, axial_middle].T

    diff_3d = np.absolute(den.astype('f8') - data.astype('f8'))
    difference = np.absolute(after.astype('f8') - before.astype('f8'))
    if '-std' not in arguments:
        difference[~mask[:, :, axial_middle].T] = 0

    if param.verbose == 2 :
        fig, ax = plt.subplots(1, 3)
        ax[0].imshow(before, cmap='gray', origin='lower')
        ax[0].set_title('before')
        ax[1].imshow(after, cmap='gray', origin='lower')
        ax[1].set_title('after')
        ax[2].imshow(difference, cmap='gray', origin='lower')
        ax[2].set_title('difference')
        for i in range(3):
            ax[i].set_axis_off()

        plt.show()

    #Save files
    img_denoise = nib.Nifti1Image(den, None, hdr_0)
    img_diff = nib.Nifti1Image(diff_3d, None, hdr_0)
    if output_file_name != None :
        output_file_name =output_file_name
    else: output_file_name = file + '_denoised' + ext
    nib.save(img_denoise,output_file_name)
    nib.save(img_diff, file + '_difference' +ext)
示例#44
0
    saving_mask_start = time()
    nib.save(nib.Nifti1Image(mask.astype(int), affine), mask_file)
    print("[{0}] Saved mask to {2} in {1}s".format(datetime.datetime.now(),
                                                   time() - saving_mask_start,
                                                   mask_file))
else:
    print("[{0}] Skipping mask save - already exists".format(
        datetime.datetime.now()))

print("[{0}] Denoising".format(datetime.datetime.now()))
denoise_start = time()
from dipy.denoise import nlmeans
from dipy.denoise.noise_estimate import estimate_sigma

sigma = estimate_sigma(data)
denoised_data = nlmeans.nlmeans(data, num_threads=8, sigma=sigma, mask=mask)
print("[{0}] Finished denoising in {1}s".format(datetime.datetime.now(),
                                                time() - denoise_start))

denoised_file = file_prefix + 'denoised_data.nii.gz'
if not op.exists(denoised_file):
    print("[{0}] Saving denoising result".format(datetime.datetime.now()))
    saving_denoise_start = time()
    nib.save(nib.Nifti1Image(denoised_data, affine), denoised_file)
    print("[{0}] Saved denoised file to {1} in {2}s".format(
        datetime.datetime.now(), denoised_file,
        time() - saving_denoise_start))
else:
    print("[{0}] Skipping denoised file save - already exists".format(
        datetime.datetime.now()))
示例#45
0
data pre-processing step for diffusion kurtosis fitting is to denoise our data.
For this, we use Dipy's non-local mean filter (see
:ref:`example-denoise-nlmeans`). Note that, since the HCP-like data has a large
number of diffusion-weigthed volumes, this procedure can take a couple of hours
to compute the entire dataset. Therefore, to speed the run time in this example
we only denoise an axial slice of the data.
"""

axial_slice = 40

sigma = estimate_sigma(data, N=4)

mask_roi = np.zeros(data.shape[:-1], dtype=bool)
mask_roi[:, :, axial_slice] = mask[:, :, axial_slice]

den = nlmeans(data, sigma=sigma, mask=mask_roi)
den = den[:, :, axial_slice, :]

"""
Now that we have loaded and prepared the voxels to process we can go forward
with the voxel reconstruction. This can be done by first instantiating the
DiffusionKurtosisModel in the following way:
"""

dkimodel = dki.DiffusionKurtosisModel(gtab)

"""
To fit the data using the defined model object, we call the ``fit`` function of
this object:
"""
示例#46
0
"""
Calling the main function ``non_local_means``
"""

den = non_local_means(
    data,
    sigma=sigma,
    mask=mask,
    patch_radius=1,
    block_radius=1,
    rician=True)
print("total time", time() - t)

t = time()

den = nlmeans(data, sigma=sigma, mask=mask, patch_radius= 1, block_radius = 1, rician= True)

print("total time", time() - t)
"""
Let us plot the axial slice of the denoised output
"""

axial_middle = data.shape[2] / 2

before = data[:, :, axial_middle].T
after = den[:, :, axial_middle].T

difference = np.abs(after.astype('f8') - before.astype('f8'))

difference[~mask[:, :, axial_middle].T] = 0
示例#47
0
def nlmeans_proxy(in_file, settings,
                  snr=None,
                  smask=None,
                  nmask=None,
                  out_file=None):
    """
    Uses non-local means to denoise 4D datasets
    """
    from dipy.denoise.nlmeans import nlmeans
    from scipy.ndimage.morphology import binary_erosion
    from scipy import ndimage

    if out_file is None:
        fname, fext = op.splitext(op.basename(in_file))
        if fext == '.gz':
            fname, fext2 = op.splitext(fname)
            fext = fext2 + fext
        out_file = op.abspath('./%s_denoise%s' % (fname, fext))

    img = nb.load(in_file)
    hdr = img.header
    data = img.get_data()
    aff = img.affine

    if data.ndim < 4:
        data = data[..., np.newaxis]

    data = np.nan_to_num(data)

    if data.max() < 1.0e-4:
        raise RuntimeError('There is no signal in the image')

    df = 1.0
    if data.max() < 1000.0:
        df = 1000. / data.max()
        data *= df

    b0 = data[..., 0]

    if smask is None:
        smask = np.zeros_like(b0)
        smask[b0 > np.percentile(b0, 85.)] = 1

    smask = binary_erosion(
        smask.astype(np.uint8), iterations=2).astype(np.uint8)

    if nmask is None:
        nmask = np.ones_like(b0, dtype=np.uint8)
        bmask = settings['mask']
        if bmask is None:
            bmask = np.zeros_like(b0)
            bmask[b0 > np.percentile(b0[b0 > 0], 10)] = 1
            label_im, nb_labels = ndimage.label(bmask)
            sizes = ndimage.sum(bmask, label_im, range(nb_labels + 1))
            maxidx = np.argmax(sizes)
            bmask = np.zeros_like(b0, dtype=np.uint8)
            bmask[label_im == maxidx] = 1
        nmask[bmask > 0] = 0
    else:
        nmask = np.squeeze(nmask)
        nmask[nmask > 0.0] = 1
        nmask[nmask < 1] = 0
        nmask = nmask.astype(bool)

    nmask = binary_erosion(nmask, iterations=1).astype(np.uint8)

    den = np.zeros_like(data)

    est_snr = True
    if snr is not None:
        snr = [snr] * data.shape[-1]
        est_snr = False
    else:
        snr = []

    for i in range(data.shape[-1]):
        d = data[..., i]
        if est_snr:
            s = np.mean(d[smask > 0])
            n = np.std(d[nmask > 0])
            snr.append(s / n)

        den[..., i] = nlmeans(d, snr[i], **settings)

    den = np.squeeze(den)
    den /= df

    nb.Nifti1Image(den.astype(hdr.get_data_dtype()), aff,
                   hdr).to_filename(out_file)
    return out_file, snr
示例#48
0
def test_nlmeans_4d_3dsigma():
    # Input is 4D data and 3D sigma
    data = np.ones((10,10,10,5))
    sigma = np.ones((10,10,10))
    nlmeans(data, sigma)
示例#49
0
def sparky(sparki=True, dipyi=False, plot=False, n_cores=8):
    """" Runs the NLMeans on the predefined data

    :param sparki: bool
        True: spark implementation is used
    :param dipyi: bool
        True: dipy implementation is used
    :param plot: bool
        True: images of the processed data is written to the
              predefined folder
    :param n_cores: int
        number of cores of the used system
        used for overhead calculation
    :return:
        sparkyout: result of spark implementation (retransformed out)
        dipynlmeans: result of dipy implementation
        data: raw data
        out: output of spark implementation
        t_ov: time overhead of spark implementation
        t_core: runtime of calculation of spark implementation
        t_core_std: std of calc runtime of calculation spark implementation
        t_dipy: runtime of dipy implementation
    """

    t_ov = 0
    t_core = 0
    t_core_std = 0
    t_dipy = 0
    out = []
    data = []
    sparkyout = []
    dipynlmeans = []

    if sparki:
        data = load_data(name=dataset_name)

        conf = SparkConf()
        conf.setMaster("local[*]")
        conf.setAppName("NLMeans")
        conf.set("spark.default.parallelism", "30")

        t_start = time.time()
        sc = SparkContext(conf=conf, batchSize=batchSize)
        rdd = sc.parallelize(data, 30)
        rdd = rdd.flatMap(superblock_mapper).reduceByKey(lambda p, q: p+q)\
                 .flatMap(stitch_mapper).map(calc)
        out = rdd.collect()
        sc.stop()
        t_ov = time.time()-t_start

        sparkyout = retransform_data(out)

    data, sigma = load_data(flat=False, name=dataset_name)

    if dipyi:
        t_start = time.time()
        dipynlmeans = nlmeans.nlmeans(np.copy(data), sigma, mask=None,
                                      patch_radius=P, block_radius=B, rician=False)
        t_dipy = time.time()-t_start

    if sparki:
        t_temp = []
        for triple in out:
            t_temp.append(triple[2])
        t_core = np.mean(t_temp)
        t_core_std = np.std(t_temp)

    print("\n\nTimes-----------------------------")
    print("Time pyspark overall: %.2fs" % t_ov)
    print("Time pyspark overhead: %.2fs" % (t_ov - t_core * np.product(np.array(sh)/SB)/n_cores))
    print("Time dipy: %.2fs" % t_dipy)

    # Save images for visual inspection
    if plot:
        if len(sparkyout):
            imsave(path_to_folder + "/images/den_sparky.png", sparkyout[:, :, int(sh[2]/2)])
        if len(dipynlmeans):
            imsave(path_to_folder + "/images/den_dipy.png", dipynlmeans[:, :, int(sh[2]/2)])
        if len(sparkyout) and len(dipynlmeans):
            diff = sparkyout-dipynlmeans
            print("Mean difference spark/dipy: %.3f" % (np.mean(np.abs(diff))))
            diff[diff < 0] = 0
            imsave(path_to_folder + "/images/den_diff_sp_di.png", diff[:, :, int(sh[2]/2)])
            diff = dipynlmeans-sparkyout
            diff[diff < 0] = 0
            imsave(path_to_folder + "/images/den_diff_di_sp.png", diff[:, :, int(sh[2]/2)])
        if not isinstance(data, list):
            imsave(path_to_folder + "/images/den_data.png", data[:, :, int(sh[2]/2)])
        if len(sparkyout) and not isinstance(data, list):
            diff = np.abs(sparkyout-data)
            print("Mean difference spark/data: %.3f" % (np.mean(diff)))
            imsave(path_to_folder + "/images/den_data_sp_diff.png", diff[:, :, int(sh[2]/2)])
        if len(dipynlmeans) and not isinstance(data, list):
            diff = np.abs(dipynlmeans-data)
            print("Mean difference dipy/data: %.3f" % (np.mean(diff)))
            imsave(path_to_folder + "/images/den_data_di_diff.png", diff[:, :, int(sh[2]/2)])

    return sparkyout, dipynlmeans, data, out, t_ov, t_core, t_core_std, t_dipy
示例#50
0
def test_nlmeans_static():
    S0 = 100 * np.ones((20, 20, 20), dtype='f8')
    S0n = nlmeans(S0, sigma=np.ones((20, 20, 20)), rician=False)
    assert_array_almost_equal(S0, S0n)