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 i 8
1 p 15
2 u 20
3 a 0
4 g 6
5 b 1
6 w 22
7 j 9
8 h 7
9 v 21


# ascii index
0 q 16
1 w 22
2 o 14
3 t 19
4 n 13
5 j 9
6 z 25
7 a 0
8 p 15
9 y 24


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 p 15 p
1 a 0 a
2 w 22 w
3 j 9 j
4 h 7 h
5 k 10 k
6 y 24 y
7 o 14 o


Gallery generated by Sphinx-Gallery