Exemplo n.º 1
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
Exemplo n.º 2
0
def test_model_get_slice():
    mo = se.Model(data=data[1:3], locs=locs)
    inds = [0, 1]
    s = mo.get_slice(inds)
    assert(type(s) == se.Model)
    s_model = s.get_model()
    assert s_model.shape[0] == s_model.shape[1]
    assert s_model.shape[0] == len(inds)
    assert s_model.shape[0] == len(inds)

    mo.get_slice(inds, inplace=True)
    assert(type(mo) == se.Model)
    mo_model = mo.get_model()
    assert mo_model.shape[0] == mo_model.shape[1]
    assert mo_model.shape[0] == len(inds)
    assert mo_model.shape[0] == len(inds)
Exemplo n.º 3
0
def test_cpu_vs_gpu_single_subject():
    cupy = pytest.importorskip("cupy")
    locs = np.random.randn(25, 3)
    bo1 = se.simulate_bo(n_samples=512*30, locs=locs, sample_rate=512)
    bo2 = se.simulate_bo(n_samples=512*30, locs=locs, sample_rate=512)

    mo_cpu = se.Model([bo1, bo2])
    mo_gpu = se.Model([bo1, bo2], gpu=True)
    assert mo_cpu.locs.equals(mo_gpu.locs)
    assert np.allclose(mo_cpu.get_model(), mo_gpu.get_model())

    mo_cpu = se.Model(bo1) + se.Model(bo2)
    mo_gpu = se.Model(bo1, gpu=True) + se.Model(bo2, gpu=True)
    assert mo_cpu.locs.equals(mo_gpu.locs)
    assert np.allclose(mo_cpu.get_model(), mo_gpu.get_model())
Exemplo n.º 4
0
def test_cpu_vs_gpu_mult_subject():
    cupy = pytest.importorskip("cupy")
    locs1 = np.random.randn(25, 3)
    bo1a = se.simulate_bo(n_samples=512*30, locs=locs1, sample_rate=512)
    bo1b = se.simulate_bo(n_samples=512*30, locs=locs1, sample_rate=512)

    locs2 = np.random.randn(25, 3)
    bo2a = se.simulate_bo(n_samples=512*30, locs=locs2, sample_rate=512)
    bo2b = se.simulate_bo(n_samples=512*30, locs=locs2, sample_rate=512)

    mo_cpu = se.Model([bo1a, bo1b, bo2a, bo2b])
    mo_gpu = se.Model([bo1a, bo1b, bo2a, bo2b], gpu=True)
    assert mo_cpu.locs.equals(mo_gpu.locs)
    assert np.allclose(mo_cpu.get_model(), mo_gpu.get_model())

    mo_cpu = se.Model([bo1a, bo1b]) + se.Model([bo2a, bo2b])
    mo_gpu = se.Model([bo1a, bo1b], gpu=True) + se.Model([bo2a, bo2b], gpu=True)
    assert mo_cpu.locs.equals(mo_gpu.locs)
    assert np.allclose(mo_cpu.get_model(), mo_gpu.get_model())
Exemplo n.º 5
0
                 [-21.,  63.,  -3.],
                 [ -1., -37.,  37.],
                 [ -1.,  23.,  17.],
                 [ 19., -57., -23.],
                 [ 19.,  23.,  -3.],
                 [ 39., -57.,  17.],
                 [ 39.,   3.,  37.],
                 [ 59., -17.,  17.]])

n_samples = 10
n_subs = 3
n_elecs = 10
data = [se.simulate_model_bos(n_samples=10, sample_rate=10, locs=locs,
                              sample_locs = n_elecs) for x in range(n_subs)]
test_bo = data[0]
test_model = se.Model(data=data, locs=locs, rbf_width=20)
bo = se.load('example_data')

def test_load_example_data():
    bo = se.load('example_data')
    assert isinstance(bo, se.Brain)

def test_load_example_filter():
    bo = se.load('example_filter')
    assert isinstance(bo, se.Brain)

def test_load_example_model():
    model = se.load('example_model')
    assert isinstance(model, se.Model)

def test_load_nifti():
Exemplo n.º 6
0
# 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
bo_sample = bo[:, sub_locs]

# plot sample patient locations
bo_sample.plot_locs()

# plot sample patient data
bo_sample.plot_data()
total_patients = len(files)

divided_patients = int(total_patients / 2)

all_idx = np.arange(total_patients)

n_samples = total_patients  - divided_patients

corrs_rand = np.zeros((rand_iters, n_samples))

for r in np.arange(rand_iters):

    half_1_idx = np.random.choice(total_patients, replace=False, size=divided_patients)
    half_2_idx = np.delete(all_idx, half_1_idx)

    half_1_mo = se.Model(list(np.array(files)[half_1_idx]), n_subs=len(half_1_idx))

    #np.random.shuffle(half_2_idx)

    for e, i in enumerate(half_2_idx):

        if e == 0:

            mo_2 = se.Model(np.array(files)[half_2_idx[e]], n_subs=1)

        else:

            mo_i = se.Model(np.array(files)[half_2_idx[e]], n_subs=1)

            mo_2 = mo_2 + mo_i
Exemplo n.º 8
0
                    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
bo_sample.plot_data()

# make model from brain object
r_model = se.Model(data=bo, locs=locs)

# predict
bo_s = r_model.predict(bo_sample, nearest_neighbor=False)

# find indices for reconstructed locations
recon_labels = np.where(np.array(bo_s.label) != 'observed')

# find correlations between predicted and actual data
corrs = _corr_column(bo.get_data().as_matrix(), bo_s.get_data().as_matrix())

# index reconstructed correlations
corrs[recon_labels].mean()
Exemplo n.º 9
0
def test_model_predict_nn_0():
    model = se.Model(data=data[0:2], locs=locs)
    bo_1 = model.predict(data[0], nearest_neighbor=True, match_threshold=0)
    bo_2 = model.predict(data[0], nearest_neighbor=False)
    assert isinstance(bo_1, se.Brain)
    assert np.allclose(bo_1.get_data(), bo_2.get_data())
Exemplo n.º 10
0
def test_model_predict_nn():
    print(data[0].dur)
    model = se.Model(data=data[0:2], locs=locs)
    bo = model.predict(data[0], nearest_neighbor=True)
    assert isinstance(bo, se.Brain)
Exemplo n.º 11
0
def test_model_update_inplace():
    mo = se.Model(data=data[1:3], locs=locs)
    mo = mo.update(data[0])
    assert mo is None
Exemplo n.º 12
0
def test_create_model_model():
    mo = se.Model(data=data[1:3], locs=locs)
    model = se.Model(mo)
    assert isinstance(model, se.Model)
Exemplo n.º 13
0
                 [ 59., -17.,  17.]])


# number of timeseries samples
n_samples = 10
# number of subjects
n_subs = 6
# number of electrodes
n_elecs = 5
# simulate correlation matrix

data = [se.simulate_model_bos(n_samples=10, sample_rate=10, locs=locs, sample_locs = n_elecs,
                              set_random_seed=123, noise=0) for x in range(n_subs)]

# test model to compare
test_model = se.Model(data=data[0:3], locs=locs, rbf_width=20, n_subs=3)


def test_create_model_1bo():
    model = se.Model(data=data[0], locs=locs)
    assert isinstance(model, se.Model)

def test_create_model_2bo():
    model = se.Model(data=data[0:2], locs=locs)
    assert isinstance(model, se.Model)

def test_create_model_superuser():
    locs = np.random.multivariate_normal(np.zeros(3), np.eye(3), size=10)
    numerator = scipy.linalg.toeplitz(np.linspace(0,10,len(locs))[::-1])
    denominator = np.random.multivariate_normal(np.zeros(10), np.eye(10), size=10)
    model = se.Model(numerator=numerator, denominator=denominator, locs=locs, n_subs=2)
Exemplo n.º 14
0
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.]])

n_samples = 10
n_subs = 3
n_elecs = 10
data = [
    se.simulate_model_bos(n_samples=10,
                          sample_rate=10,
                          locs=locs,
                          sample_locs=n_elecs) for x in range(n_subs)
]
test_bo = data[0]
test_model = se.Model(data=data, locs=locs)
bo = se.load('example_data')


def test_load_example_data():
    bo = se.load('example_data')
    assert isinstance(bo, se.Brain)


def test_load_example_filter():
    bo = se.load('example_filter')
    assert isinstance(bo, se.Brain)


def test_load_example_model():
    model = se.load('example_model')
Exemplo n.º 15
0
locs = se.simulate_locations(n_elecs=100)

# simulate correlation matrix
R = se.create_cov(cov='toeplitz', n_elecs=len(locs))

# simulate brain objects for the model that subsample n_elecs for each synthetic patient
model_bos = [
    se.simulate_model_bos(n_samples=1000,
                          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
Exemplo n.º 16
0
        except:
            numtries += 1
            time.sleep(5)
    bo = se.load(sys.argv[1])

    # load original brain object
    og_fname = os.path.join(config['og_bodir'], fname.split('_' + freq)[0] + '.bo')
    try:
        og_bo = se.load(og_fname)
    except:
        og_bo = se.load(sys.argv[1])
    og_bo.update_filter_inds()

    # turn it into fancy ~BandBrain~
    bo = BandBrain(bo, og_bo=og_bo, og_filter_inds=og_bo.filter_inds)

    # filter
    bo.apply_filter()

    # turn it back into a vanilla Brain
    bo = se.Brain(bo)

    # make model
    mo = se.Model(bo, locs=R)

    # save model
    mo.save(os.path.join(results_dir, fname))

else:
    print('skipping model (not enough electrodes pass kurtosis threshold): ' + sys.argv[1])
Exemplo n.º 17
0
# nii = se.load('gray', vox_size=3)
#
# locs = nii.get_locs()

locs_dir = config['locs_resultsdir']

mo_locs = np.load(os.path.join(locs_dir, 'mo_locs.npz'))

## for full locations
#locs = mo_locs['full_locs']

## for every 100th locations
locs = mo_locs['sub_locs']

fname = os.path.basename(os.path.splitext(fname)[0])

print('creating model object: ' + fname)

# load brain object
bo = se.load(sys.argv[1])

# filter
bo.filter = None

# make model
mo = se.Model(bo, locs=locs)

# save model
mo.save(os.path.join(results_dir, fname))
Exemplo n.º 18
0
def test_create_model_2bo():
    model = se.Model(data=data[0:2], locs=locs)
    assert isinstance(model, se.Model)
Exemplo n.º 19
0
def test_create_model_superuser():
    locs = np.random.multivariate_normal(np.zeros(3), np.eye(3), size=10)
    numerator = scipy.linalg.toeplitz(np.linspace(0,10,len(locs))[::-1])
    denominator = np.random.multivariate_normal(np.zeros(10), np.eye(10), size=10)
    model = se.Model(numerator=numerator, denominator=denominator, locs=locs, n_subs=2)
    assert isinstance(model, se.Model)
Exemplo n.º 20
0
def test_model_update_with_model():
    mo = se.Model(data=data[1:3], locs=locs)
    mo = mo.update(mo, inplace=False)
    assert isinstance(mo, se.Model)
Exemplo n.º 21
0
def test_model_predict_nn_thresh():
    model = se.Model(data=data[0:2], locs=locs)
    bo = model.predict(data[0], nearest_neighbor=True, match_threshold=30)
    assert isinstance(bo, se.Brain)
Exemplo n.º 22
0
def test_model_update_with_model_and_bo():
    mo = se.Model(data=data[1:3], locs=locs)
    mo = se.Model([mo, data[0]])
    assert isinstance(mo, se.Model)
Exemplo n.º 23
0
def test_create_model_str():
    model = se.Model('example_data')
    assert isinstance(model, se.Model)
Exemplo n.º 24
0
def test_model_update_with_array():
    mo = se.Model(data=data[1:3], locs=locs)
    d = np.random.rand(*mo.numerator.shape)
    mo = se.Model([mo, d], locs=mo.get_locs())
    assert isinstance(mo, se.Model)
Exemplo n.º 25
0
for p, m, n in param_grid:
    d = []

    for i in range(iter_val):
        # create brain objects with m_patients and loop over the number of model locations and subset locations to build model
        model_bos = [
            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(),
Exemplo n.º 26
0
def test_model_get_model():
    mo = se.Model(data=data[1:3], locs=locs)
    m = mo.get_model()
    assert isinstance(m, np.ndarray)
Exemplo n.º 27
0
# locs
locs = se.simulate_locations(n_elecs=100, set_random_seed=random_seed)

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

# 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
Exemplo n.º 28
0
def test_model_update_with_smaller_array_locs_specified():
    mo = se.Model(data=data[1:3], locs=locs)
    d = np.random.rand(3, 3)
    mo = mo.update(d, inplace=False, locs=d)
    assert isinstance(mo, se.Model)
Exemplo n.º 29
0
freq = sys.argv[1]

model_dir = os.path.join(config['datadir'])

results_dir = config['resultsdir']
model_dir = config['datadir']

if os.path.exists(os.path.join(results_dir, 'ave_mat_' + freq)):
    print('ave mat already exists')
    exit()

try:
    if not os.path.exists(results_dir):
        os.makedirs(results_dir)
except OSError as err:
   print(err)

mos = glob.glob(os.path.join(model_dir, '*' + freq + '.mo'))

if freq == 'raw':
    freqnames = ['delta', 'theta', 'alpha', 'beta', 'lgamma', 'hgamma', 'broadband']
    mos = set(glob.glob(os.path.join(model_dir, '*')))
    for fre in freqnames:
        mos -= set(glob.glob(os.path.join(model_dir, '*' + fre + '*')))
    mos = list(mos)

print(len(mos))

mo = se.Model(mos, n_subs=len(mos))

mo.save(os.path.join(results_dir, 'ave_mat_' + freq))
Exemplo n.º 30
0
def test_model_predict():
    model = se.Model(data=data[0:2], locs=locs)
    bo = model.predict(data[0], nearest_neighbor=False)
    assert isinstance(bo, se.Brain)