Пример #1
0
def test_electrode_contingencies_1_null_set():

    # set random seed to default and noise to 0
    random_seed = np.random.seed(123)
    noise = 0

    # load mini model
    gray = se.Brain(se.load('gray', vox_size=20))

    # extract 20 locations
    gray_locs = gray.locs.iloc[:5]

    # create model from 10 locations
    mo_locs = gray_locs.sample(3, random_state=random_seed).sort_values(
        ['x', 'y', 'z'])

    # create covariance matrix from random seed
    c = se.create_cov(cov='random', n_elecs=5)

    # pull out model from covariance matrix
    data = c[:, mo_locs.index][mo_locs.index, :]

    # create model from subsetted covariance matrix and locations
    model = se.Model(numerator=np.array(data),
                     denominator=np.ones(np.shape(data)),
                     locs=mo_locs,
                     n_subs=1)

    # create brain object from the remaining locations - first find remaining locations
    sub_locs = gray_locs[~gray_locs.index.isin(mo_locs.index)]

    # create a brain object with all gray locations
    bo = se.simulate_bo(n_samples=5,
                        sample_rate=1000,
                        locs=gray_locs,
                        noise=noise,
                        random_seed=random_seed)

    # parse brain object to create synthetic patient data
    data = bo.data.iloc[:, sub_locs.index]

    # put data and locations together in new sample brain object
    bo_sample = se.Brain(data=data.as_matrix(),
                         locs=sub_locs,
                         sample_rate=1000)

    # predict activity at all unknown locations
    recon = model.predict(bo_sample, nearest_neighbor=False)

    # actual = bo.data.iloc[:, unknown_ind]
    actual = bo.data.iloc[:, recon.locs.index]

    corr_vals = _corr_column(actual.as_matrix(), recon.data.as_matrix())

    assert 1 >= corr_vals.mean() >= -1
Пример #2
0
def test_electrode_contingencies_2_subset():

    random_seed = np.random.seed(123)

    noise = 0

    gray = se.Brain(se.load('gray', vox_size=20))

    # extract locations
    gray_locs = gray.locs.iloc[:5]

    mo_locs = gray_locs

    c = se.create_cov(cov='random', n_elecs=5)

    data = c[:, mo_locs.index][mo_locs.index, :]

    model = se.Model(numerator=np.array(data),
                     denominator=np.ones(np.shape(data)),
                     locs=mo_locs,
                     n_subs=1)

    # create brain object from the remaining locations - first find remaining locations
    sub_locs = mo_locs.sample(2, random_state=random_seed).sort_values(
        ['x', 'y', 'z'])

    # create a brain object with all gray locations
    bo = se.simulate_bo(n_samples=5,
                        sample_rate=1000,
                        locs=gray_locs,
                        noise=noise,
                        random_seed=random_seed)

    # parse brain object to create synthetic patient data
    data = bo.data.iloc[:, sub_locs.index]

    # put data and locations together in new sample brain object
    bo_sample = se.Brain(data=data.as_matrix(),
                         locs=sub_locs,
                         sample_rate=1000)

    # predict activity at all unknown locations
    recon = model.predict(bo_sample, nearest_neighbor=False)

    actual = bo.data.iloc[:, recon.locs.index]

    corr_vals = _corr_column(actual.as_matrix(), recon.data.as_matrix())

    #assert np.allclose(zscore(recon_2), recon.data, equal_nan=True)
    assert 1 >= corr_vals.mean() >= -1
Пример #3
0
original brain object.

"""

# Code source: Lucy Owen & Andrew Heusser
# License: MIT

import supereeg as se

# simulate 100 locations
locs = se.simulate_locations(n_elecs=100)

# simulate brain object
bo = se.simulate_bo(n_samples=1000,
                    sample_rate=1000,
                    cov='toeplitz',
                    locs=locs,
                    noise=.3)

# sample 10 locations, and get indices
sub_locs = locs.sample(10, replace=False).sort_values(['x', 'y', 'z'])

R = se.create_cov(cov='random', n_elecs=len(sub_locs))
toe_model = se.Model(data=R, locs=sub_locs)

bo_s = toe_model.predict(bo, nearest_neighbor=False)

# sample 10 locations, and get indices
sub_locs = locs.sample(10).sort_values(['x', 'y', 'z']).index.values.tolist()

# index brain object to get sample patient
Пример #4
0
# create covariance matrix from random seed
c = se.create_cov(cov='random', n_elecs=100)

# pull out model from covariance matrix
data = c[:, mo_locs.index][mo_locs.index, :]

# create model from subsetted covariance matrix and locations
model = se.Model(data=data, locs=mo_locs, n_subs=1)

# create brain object from the remaining locations - first find remaining 25 locations
sub_locs = locs[~locs.index.isin(mo_locs.index)]

# create a brain object with all gray locations
bo = se.simulate_bo(n_samples=1000,
                    sample_rate=100,
                    locs=locs,
                    noise=noise,
                    random_seed=random_seed)

# parse brain object to create synthetic patient data
data = bo.data.iloc[:, sub_locs.index]

# put data and locations together in new sample brain object
bo_sample = se.Brain(data=data.values, locs=sub_locs, sample_rate=100)

# predict activity at all unknown locations
recon = model.predict(bo_sample, nearest_neighbor=False)

# get reconstructed indices
recon_labels = np.where(np.array(recon.label) != 'observed')
Пример #5
0
            se.simulate_model_bos(n_samples=1000,
                                  sample_rate=100,
                                  locs=locs,
                                  sample_locs=m,
                                  noise=.3) for x in range(p)
        ]

        # create model from subsampled gray locations
        model = se.Model(model_bos, locs=locs)

        # brain object locations subsetted entirely from both model and gray locations
        sub_locs = locs.sample(n).sort_values(['x', 'y', 'z'])

        # simulate brain object
        bo = se.simulate_bo(n_samples=1000,
                            sample_rate=100,
                            locs=locs,
                            noise=.3)

        # parse brain object to create synthetic patient data
        data = bo.data.iloc[:, sub_locs.index]

        # create synthetic patient (will compare remaining activations to predictions)
        bo_sample = se.Brain(data=data.as_matrix(),
                             sample_rate=100,
                             locs=sub_locs)

        # reconstruct at 'unknown' locations
        bo_r = model.predict(bo_sample)

        # find the reconstructed indices
        recon_inds = [
Пример #6
0
"""

# Code source: Lucy Owen & Andrew Heusser
# License: MIT

import supereeg as se
from supereeg.helpers import _corr_column
import numpy as np

# simulate 100 locations
locs = se.simulate_locations(n_elecs=100)

# simulate brain object
bo = se.simulate_bo(n_samples=1000,
                    sample_rate=100,
                    cov='random',
                    locs=locs,
                    noise=.1)

# sample 10 locations, and get indices
sub_locs = locs.sample(90,
                       replace=False).sort_values(['x', 'y',
                                                   'z']).index.values.tolist()

# index brain object to get sample patient
bo_sample = bo[:, sub_locs]

# plot sample patient locations
bo_sample.plot_locs()

# plot sample patient data
Пример #7
0
def test_brain_getrowcols():
    bo = se.simulate_bo(n_samples=10, sample_rate=100)
    bo = bo[:5, 3]
    assert bo.data.shape==(5, 1)
Пример #8
0
def test_brain_getitem_2():
    bo = se.simulate_bo(n_samples=10, sample_rate=100)
    bos = [b for b in bo[:2]]
    assert all(isinstance(b, se.Brain) for b in bos)
Пример #9
0
def test_brain_getitem():
    bo = se.simulate_bo(n_samples=10, sample_rate=100)
    bo = bo[:2]
    assert bo.data.shape[0]==2
Пример #10
0
def test_brain_brain():
    bo = se.simulate_bo(n_samples=10, sample_rate=100)
    bo = se.Brain(bo)
    assert isinstance(bo, se.Brain)
Пример #11
0
from builtins import str
import pytest
import os
import supereeg as se
import numpy as np
import pandas as pd
import nibabel as nib

bo = se.simulate_bo(n_samples=10, sample_rate=100)

nii = se.load('example_nifti')
bo_n = se.Brain(nii)

mo = se.load('example_model')
bo_m = se.Brain(mo)

def test_create_bo():
    assert isinstance(bo, se.Brain)

def test_bo_data_nifti():
    assert isinstance(bo_n, se.Brain)

def test_bo_data_model():
    assert isinstance(bo_m, se.Brain)

def test_bo_data_df():
    assert isinstance(bo.data, pd.DataFrame)

def test_bo_locs_df():
    assert isinstance(bo.locs, pd.DataFrame)
Пример #12
0
                          sample_rate=1000,
                          locs=locs,
                          sample_locs=10,
                          cov='toeplitz') for x in range(3)
]

# create the model object
model = se.Model(data=model_bos, locs=locs, n_subs=3)
model.plot_data()

# brain object locations subsetted
sub_locs = locs.sample(10).sort_values(['x', 'y', 'z'])

# simulate a new brain object using the same covariance matrix
bo = se.simulate_bo(n_samples=1000,
                    sample_rate=1000,
                    locs=sub_locs,
                    cov='toeplitz')

# update the model
new_model = model.update(bo, inplace=False)

# initialize subplots
f, (ax1, ax2) = plt.subplots(1, 2)
f.set_size_inches(14, 6)

# plot it and set the title
model.plot_data(ax=ax1,
                show=False,
                yticklabels=False,
                xticklabels=False,
                cbar=True,
Пример #13
0
    _nifti_to_brain, _brain_to_nifti, _to_log_complex, _to_exp_real, _logsubexp
from supereeg.model import _recover_model

locs = np.array([[-61., -77., -3.], [-41., -77., -23.], [-21., -97., 17.],
                 [-21., -37., 77.], [-21., 63., -3.], [-1., -37., 37.],
                 [-1., 23., 17.], [19., -57., -23.], [19., 23., -3.],
                 [39., -57., 17.], [39., 3., 37.], [59., -17., 17.]])

# number of timeseries samples
n_samples = 10
# number of subjects
n_subs = 3
# number of electrodes
n_elecs = 5
# full brain object to parse and compare
bo_full = se.simulate_bo(n_samples=10, sessions=2, sample_rate=10, locs=locs)
# create brain object from subset of locations
sub_locs = bo_full.locs.iloc[6:]
sub_data = bo_full.data.iloc[:, sub_locs.index]
bo = se.Brain(data=sub_data.as_matrix(),
              sessions=bo_full.sessions,
              locs=sub_locs,
              sample_rate=10,
              meta={'brain object locs sampled': 2})
# simulate correlation matrix
data = [
    se.simulate_model_bos(n_samples=10, locs=locs, sample_locs=n_elecs)
    for x in range(n_subs)
]
# test model to compare
test_model = se.Model(data=data, locs=locs, rbf_width=100)
Пример #14
0
def test_simulate_bo_no_locs():
    bo = se.simulate_bo(n_samples=10, sample_rate=1000)
    assert isinstance(bo, se.Brain)