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 b 1
1 x 23
2 g 6
3 l 11
4 p 15
5 e 4
6 t 19
7 u 20
8 k 10
9 w 22


# ascii index
0 y 24
1 o 14
2 w 22
3 z 25
4 f 5
5 t 19
6 g 6
7 c 2
8 n 13
9 h 7


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 x 23 x
1 g 6 g
2 p 15 p
3 t 19 t
4 k 10 k
5 w 22 w
6 n 13 n
7 a 0 a
8 z 25 z


Gallery generated by Sphinx-Gallery