namemap = lesionordermap(info)
info = info.query(nonjumpers)
cract = pd.read_hdf(lesionshamcache, crossingactivity_random_key)
cr = pd.read_hdf(lesionshamcache, crossings_key).query(nonjumpers)
cract.reset_index('time', inplace=True)

# Select data
bias = 2
group = list(names(info))
random = '(session == 13 and trial > 20) or (14 <= session < 17)'
cr = cr.query(random)
for name in group:
    selection = str.format("subject in {0}", [name])
    scr = cr.query(selection)
    stablebias, unstablebias = posturebias(scr, n=bias)
    stablebias = getballistictrials(stablebias)
    unstablebias = getballistictrials(unstablebias)
    if len(stablebias) == 0 or len(unstablebias) == 0:
        continue

    # Select data
    stablebias = stablebias.rename(columns={'index': 'crossing'})
    unstablebias = unstablebias.rename(columns={'index': 'crossing'})
    stablebias.set_index('crossing', append=True, inplace=True)
    unstablebias.set_index('crossing', append=True, inplace=True)
    scract = cract.join(stablebias, how='inner', rsuffix='R')
    ucract = cract.join(unstablebias, how='inner', rsuffix='R')
    scract.xhead = flipleftwards(scract.xhead, scract.side)
    ucract.xhead = flipleftwards(ucract.xhead, ucract.side)
    sb_S = scract.query('stepstate3')
    sb_U = scract.query('not stepstate3')
cract.reset_index(['subject','session','crossing'],inplace=True)
info = pd.read_hdf(lesionshamcache, info_key).query(nonjumpers)
info = info.query("subject != 'JPAK_20'")

# Select data
group = list(names(info))
controls = list(names(control(info)))
lesions = list(names(lesion(info)))
matched = list(cagemates(lesion(info)))
small = list(names(smalllesion(info)))
selection = str.format("subject in {0}",group)
random = '(session == 13 and trial > 20) or (14 <= session < 17)'
steps = steps.query(random).query(selection)
cr = cr.query(random).query(selection)
steps = joinstepactivity(steps,cr,cract)
steps = getballistictrials(steps)

for i in range(21,22):
    offact = crossingoffset(cract,steps,i)
    offact.set_index(['subject','session'],inplace=True)
    normalizeposition(offact,inplace=True)
    stablebias,unstablebias = posturebias(offact,n=2)
    
    fig, (ax1,ax2) = plt.subplots(1,2)
    colors = ['b','cyan','orange','r']
    grouplabels = ['controls','small\nlesions','lesions','matched\ncontrols']
    conditions = [offact.query('stepstate3'),
                  offact.query('not stepstate3')]
    conditioncomparison('yhead',
                    [controls,small,lesions,matched],
                    conditions,colors=[colors[0],colors[-1]],
from datapath import lesionshamcache, crossings_key
from datapath import jumpers

# Load data
stable = '(3 <= session < 5)'
unstable = '(9 <= session < 11)'
restable = '(11 <= session < 13)'
removed = "subject not in ['JPAK_20']"
selected = str.format("({0} or {1} or {2}) and {3} and trial > 0", stable,
                      unstable, restable, removed)
nonjumpers = str.format("subject not in {0}", jumpers)
jumpers = str.format("subject in {0}", jumpers)
info = pd.read_hdf(lesionshamcache, info_key)
namemap = lesionordermap(info)
cr = pd.read_hdf(lesionshamcache, crossings_key)
cr = getballistictrials(cr)

# Select data
column = 'entryspeed'
info['lesionvolume'] = lesionvolume(info)
cr = cr.query(selected).join(info)
cr.protocol[cr.eval(stable)] = 'stable'
cr.protocol[cr.eval(unstable)] = 'unstable'
cr.protocol[cr.eval(restable)] = 'restable'
sm = skipmeasure(cr, column)

# Custom-sort by protocol
protocolorder = {'stable': 0, 'unstable': 1, 'restable': 2}
sm['rank'] = sm['protocol'].map(protocolorder)
sm.sort(columns='rank', inplace=True)
sm.drop(labels='rank', axis=1)
from datapath import crossingactivity_stable_key
from datapath import crossingactivity_unstable_key
from datapath import crossingactivity_restable_key
from datapath import jumpers

# Load data
stable = '(3 <= session < 5)'
unstable = '(9 <= session < 11)'
restable = '(11 <= session < 13)'
removed = "subject not in ['JPAK_20']"
selected = str.format("({0} or {1} or {2}) and {3} and trial > 0", stable,
                      unstable, restable, removed)
nonjumpers = str.format("subject not in {0}", jumpers)
jumpers = str.format("subject in {0}", jumpers)
cr = pd.read_hdf(lesionshamcache, crossings_key)
cr = getballistictrials(cr).query(selected)

# Plot data
f, (sx, ux, rx) = plt.subplots(1, 3)
cract = pd.read_hdf(lesionshamcache, crossingactivity_stable_key)
averagetrajectory(cract, cr.query(stable).query(nonjumpers), color='b', ax=sx)
averagetrajectory(cract, cr.query(stable).query(jumpers), color='r', ax=sx)
proxylegend(['b', 'r'], ['nonjumper', 'jumper'], ax=sx)
sx.set_title(str.format('stable (n = {0} trials)', len(cr.query(stable))))
sx.set_ylabel('y (cm)')
sx.set_ylim(0, 6)

cract = pd.read_hdf(lesionshamcache, crossingactivity_unstable_key)
averagetrajectory(cract,
                  cr.query(unstable).query(nonjumpers),
                  color='b',