Align datasets

Shows how to combine information from two datasets describing the same cases, but not necessarily in the same order.

# Author: Christian Brodbeck <christianbrodbeck@nyu.edu>
import random
import string

from eelbrain import *


# Generate a dataset with known sequence
ds = Dataset()
ds['ascii'] = Factor(string.ascii_lowercase)
# Add an index variable to the dataset to later identify the cases
ds.index()

# Generate two shuffled copies of the dataset (and print them to confirm that
# they are shuffled)
ds1 = ds[random.sample(range(ds.n_cases), 15)]
ds1.head()
# ascii index
0 o 14
1 n 13
2 q 16
3 v 21
4 c 2
5 z 25
6 b 1
7 f 5
8 k 10
9 s 18


# ascii index
0 m 12
1 b 1
2 t 19
3 e 4
4 h 7
5 f 5
6 p 15
7 j 9
8 v 21
9 s 18


Align the datasets

Use the "index" variable added above to identify cases and align the two datasets

ds1_aligned, ds2_aligned = align(ds1, ds2, 'index')

# show the ascii sequences for the two datasets next to each other to
# demonstrate that they are aligned
ds1_aligned['ascii_ds2'] = ds2_aligned['ascii']
ds1_aligned
# ascii index ascii_ds2
0 o 14 o
1 n 13 n
2 v 21 v
3 b 1 b
4 f 5 f
5 k 10 k
6 s 18 s
7 p 15 p
8 t 19 t
9 h 7 h
10 e 4 e


Gallery generated by Sphinx-Gallery