parameters = ho.read_session_data(alias, 'parameters')
parameters.head(5)

trial_phases = ho.read_session_data(alias, 'trial_phases')
trial_phases.head(5)

trials = ho.read_session_data(alias, 'trials')
trials.head(5)

screen_dims = ho.screen_dimensions_during_trial(alias=alias,trial_nr=0)
pix_per_degrees = screen_dims[0] / 40.0 # the screen @ this distance is 40 degrees wide.

"""
Explanation: load the data from the hdf5 file.
We'll print some of the outputs just to show you what's contained in the file
End of explanation
"""


coherent_times = np.array(trial_phases[trial_phases['trial_phase_index'] == 2]['trial_phase_EL_timestamp'])
incoherent_times = np.array(trial_phases[trial_phases['trial_phase_index'] == 3]['trial_phase_EL_timestamp'])

gaze_during_coherence = [
    np.array(
        ho.data_from_time_period(alias=alias, columns=['L_gaze_x_int','L_gaze_y_int'], time_period=(tp[0]-250,tp[1]+250))
            ) for tp in zip(coherent_times, incoherent_times)]

"""
Explanation: First, dividing everything up into trials.
We combine information from the tables with the gaze data.
End of explanation
"""


# just a handy function to make sure everything is scaled the same way.
def zscore(x):
    return (x-x.mean()) / x.std()

# smoothing width
smw = 250
s2 = int(smw/2)

# which trials to plot
which_trials = np.random.choice(np.arange(len(parameters)), size=10, replace=False)
f = pl.figure(figsize = (16,24))
for i, trial in enumerate(which_trials):
    s = f.add_subplot(len(which_trials),1,i+1)
    gd = zscore(gaze_during_coherence[trial][:2000,0])
    gd -= gd[250]
    pl.plot(gd, 'k', label='gaze position')
    pl.plot(np.diff(gd), 'r', label='velocity', alpha=0.5)
    
    pl.plot(np.nan_to_num(rolling_mean(gd, smw))[s2:], 'k', label='gaze position, smooth')
    pl.plot(zscore(np.nan_to_num(rolling_mean(np.diff(gd), smw)))[s2:], 'b', label='velocity, smooth', alpha=0.5)

    s.axvline(x=parameters.coherent_dots_duration[trial]+s2, c='b', lw=3)
    s.set_title('trial ' + str(trial) + ' answer: ' + str(parameters.answer[trial]))
    s.set_ylim([-5,5])

    sn.despine(ax=s, offset=10)
pl.legend()
pl.tight_layout()

"""
Explanation: Again, plot some single trials to see what's up.
What happens with the gaze direction, i.e. velocity, when coherency is suddenly increased? Recall that we only changed the coherence in the x-direction, so that should be the direction we look at.
End of explanation
"""


