Exemplo n.º 1
0
def compute_surrogates(n_gradient, n_perms):

    data_dir = '/nfs/tank/shemesh/users/julia.huntenburg/revisions/null_models/volume/'
    gradients = np.load(data_dir + 'embed.npy')[:, :6]
    distmat = data_dir + 'distmat.npy'
    index = data_dir + 'index.npy'
    dict_file = data_dir + 'vol{}_n{}.pkl'

    nsurr = 1000
    surr_dict = {}

    # Construct sampled class
    sampled = Sampled(gradients[:, n_gradient],
                      distmat,
                      index,
                      deltas=[0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9],
                      ns=1000,
                      knn=5000,
                      pv=50)

    # Compute and save surrogates
    surrogates = sampled(n=nsurr)
    surr_dict['maps'] = surrogates

    # Compute and saving variograms
    surr_var = np.empty((nsurr, sampled.nh))
    emp_var_samples = np.empty((nsurr, sampled.nh))
    u0_samples = np.empty((nsurr, sampled.nh))
    for i in range(nsurr):
        idx = sampled.sample()  # Randomly sample a subset of brain areas
        v = sampled.compute_variogram(sampled.x, idx)
        u = sampled.D[idx, :]
        umax = np.percentile(u, sampled.pv)
        uidx = np.where(u < umax)
        emp_var_i, u0i = sampled.smooth_variogram(u=u[uidx],
                                                  v=v[uidx],
                                                  return_h=True)
        emp_var_samples[i], u0_samples[i] = emp_var_i, u0i
        # Surrogate
        v_null = sampled.compute_variogram(surrogates[i], idx)
        surr_var[i] = sampled.smooth_variogram(u=u[uidx],
                                               v=v_null[uidx],
                                               return_h=False)

    surr_dict['emp_var'] = emp_var_samples
    surr_dict['u0'] = u0_samples
    surr_dict['surr_var'] = surr_var

    pkl_out = open(dict_file.format(n_gradient, n_perms), 'wb')
    pickle.dump(surr_dict, pkl_out)
    pkl_out.close()
Exemplo n.º 2
0
def generate_surrogates(data_fname,
                        atlases,
                        dist_fname,
                        n_jobs,
                        null_maps,
                        wdr,
                        overwrite=False):
    data_masked = load_and_mask_nifti(data_fname, atlases)
    # Check that data_fname doesn't contain folders.
    data_fname = os.path.basename(data_fname)

    surrogate_fname = f'surrogates_{data_fname}'

    # if overwrite is True or os.path.isfile(f'{surrogate_fname}.npz') is False:
    if overwrite is True or check_file(wdr, f'{surrogate_fname}.npz') is False:
        # Read data and feed surrogate maps
        print(f'Start surrogates for {data_fname}')

        gen = Sampled(x=data_masked,
                      D=dist_fname['D'],
                      index=dist_fname['index'],
                      seed=42,
                      n_jobs=n_jobs)
        surrogate_maps = gen(n=null_maps)

        # Export atlases
        export_file(wdr, surrogate_fname, surrogate_maps)

        print('Resample surrogates')

        sorted_map = np.sort(data_masked)
        ii = np.argsort(surrogate_maps)
        surrogate_resamp = sorted_map[ii]

        export_file(wdr, f'{surrogate_fname}_resamp', surrogate_resamp)
    else:
        print(f'Surrogates found at {surrogate_fname}_resamp.npz. Loading.')
        surrogate_resamp = load_file(wdr, f'{surrogate_fname}_resamp.npz')

    return surrogate_resamp, data_masked
from brainsmash.workbench.geo import volume
from brainsmash.mapgen.sampled import Sampled
import scipy.io as sio
coord_file = "voxel_coordinates.txt"
output_dir = "D:\Data\SurrogateMapForSpatialCorr"

filenames = volume(coord_file, output_dir)

# brain_map = "brain_map_hc_g1.txt"
# gen = Sampled(x=brain_map, D=filenames['D'], index=filenames['index'])
# surrogate_maps = gen(n=10000)
# sio.savemat('surrogate_maps_hc_g1.mat',{'surrogate_maps':surrogate_maps})

# brain_map = "brain_map_hc_g2.txt"
# gen = Sampled(x=brain_map, D=filenames['D'], index=filenames['index'])
# surrogate_maps = gen(n=10000)
# sio.savemat('surrogate_maps_hc_g2.mat',{'surrogate_maps':surrogate_maps})

brain_map = "brain_map_hc_g3.txt"
gen = Sampled(x=brain_map, D=filenames['D'], index=filenames['index'])
surrogate_maps = gen(n=10000)
sio.savemat('surrogate_maps_hc_g3.mat', {'surrogate_maps': surrogate_maps})
Exemplo n.º 4
0
def sampled_fit(x, D, index, nsurr=10, **params):
    """
    Evaluate variogram fits for Sampled class.

    Parameters
    ----------
    x : (N,) np.ndarray
        Target brain map
    D : (N,N) np.ndarray or np.memmap
        Pairwise distance matrix between elements of `x`
    index : (N,N) np.ndarray or np.memmap
        See :class:`brainsmash.mapgen.sampled.Sampled`
    nsurr : int, default 10
        Number of simulated surrogate maps from which to compute variograms
    params
        Keyword arguments for :class:`brainsmash.mapgen.sampled.Sampled`

    Returns
    -------
    None

    Notes
    -----
    Generates and shows a matplotlib plot instance illustrating the fit of
    the surrogates' variograms to the target map's variogram.

    """

    # Instantiate surrogate map generator
    generator = Sampled(x=x, D=D, index=index, **params)

    # Simulate surrogate maps
    surrogate_maps = generator(n=nsurr)

    # Compute target & surrogate map variograms
    surr_var = np.empty((nsurr, generator.nh))
    emp_var_samples = np.empty((nsurr, generator.nh))
    u0_samples = np.empty((nsurr, generator.nh))
    for i in range(nsurr):
        idx = generator.sample()  # Randomly sample a subset of brain areas
        v = generator.compute_variogram(generator.x, idx)
        u = generator.D[idx, :]
        umax = np.percentile(u, generator.pv)
        uidx = np.where(u < umax)
        emp_var_i, u0i = generator.smooth_variogram(u=u[uidx],
                                                    v=v[uidx],
                                                    return_h=True)
        emp_var_samples[i], u0_samples[i] = emp_var_i, u0i
        # Surrogate
        v_null = generator.compute_variogram(surrogate_maps[i], idx)
        surr_var[i] = generator.smooth_variogram(u=u[uidx],
                                                 v=v_null[uidx],
                                                 return_h=False)

    # # Create plot for visual comparison
    u0 = u0_samples.mean(axis=0)
    emp_var = emp_var_samples.mean(axis=0)

    # Plot target variogram
    fig = plt.figure(figsize=(3, 3))
    ax = fig.add_axes([0.12, 0.15, 0.8, 0.77])
    ax.scatter(u0,
               emp_var,
               s=20,
               facecolor='none',
               edgecolor='k',
               marker='o',
               lw=1,
               label='Empirical')

    # Plot surrogate maps' variograms
    mu = surr_var.mean(axis=0)
    sigma = surr_var.std(axis=0)
    ax.fill_between(u0,
                    mu - sigma,
                    mu + sigma,
                    facecolor='#377eb8',
                    edgecolor='none',
                    alpha=0.3)
    ax.plot(u0, mu, color='#377eb8', label='SA-preserving', lw=1)

    # Make plot nice
    leg = ax.legend(loc=0)
    leg.get_frame().set_linewidth(0.0)
    ax.spines['top'].set_visible(False)
    ax.spines['right'].set_visible(False)
    plt.setp(ax.get_yticklabels(), visible=False)
    plt.setp(ax.get_yticklines(), visible=False)
    plt.setp(ax.get_xticklabels(), visible=False)
    plt.setp(ax.get_xticklines(), visible=False)
    ax.set_xlabel("Spatial separation\ndistance")
    ax.set_ylabel("Variance")
    plt.show()
Exemplo n.º 5
0
filenames = volume(coord_file, output_dir)

### visually inspect the variogram fit, and alter parameters until good fit

from brainsmash.mapgen.eval import sampled_fit

brain_map = "Desktop/brain_map_Lefty_rS.txt"

# These are three of the key parameters affecting the variogram fit
kwargs = {'ns': 500, 'knn': 500, 'pv': 70}

# Running this command will generate a matplotlib figure
sampled_fit(brain_map, filenames['D'], filenames['index'], nsurr=100, **kwargs)

### once fit is good, generate the autocorrelation-preserving permuted brain maps

from brainsmash.mapgen.sampled import Sampled

gen = Sampled(x=brain_map,
              D=filenames['D'],
              index=filenames['index'],
              resample=True,
              **kwargs)
surrogate_maps = gen(n=10000)

### export permutated maps to CSV to then conduct permutation stat test in R

np.savetxt("ACpreserving_rStriatum_MDmap_permutations_Lefty.csv",
           surrogate_maps,
           delimiter=",")
Exemplo n.º 6
0
import time
import numpy as np
from brainsmash.mapgen.sampled import Sampled

brain_data = "/project/6050199/rhaast/03_Ongoing/melas/fmri/notebooks/out_brainsmash_vol/brain_map.txt"
D_file = "/project/6050199/rhaast/03_Ongoing/melas/fmri/notebooks/out_brainsmash_vol/distmat.npy"
index_file = "/project/6050199/rhaast/03_Ongoing/melas/fmri/notebooks/out_brainsmash_vol/index.npy"

out_data = "/project/6050199/rhaast/03_Ongoing/melas/fmri/notebooks/out_brainsmash_vol/single_surrogate.npy"

kwargs = {'ns': 500, 'knn': 1500, 'pv': 70, 'verbose': True}

print('Started sampling data...')

t = time.time()
gen = Sampled(x=brain_data, D=D_file, index=index_file, **kwargs)
elapsed = time.time() - t

print('Sampling data took: ')
print(elapsed)

t = time.time()
surrogate_maps = gen(n=1)
elapsed = time.time() - t

print('Computing one surrogate map took: ')
print(elapsed)

np.save(out_data, surrogate_maps)

print('Finished')