from fretbursts import *

sns = init_notebook(apionly=True)
print('seaborn version: ', sns.__version__)

# Tweak here matplotlib style
import matplotlib as mpl
mpl.rcParams['font.sans-serif'].insert(0, 'Arial')
mpl.rcParams['font.size'] = 12
%config InlineBackend.figure_format = 'retina'
from IPython.display import display

"""
Explanation: Example - Plotting timetraces with bursts
This notebook is part of smFRET burst analysis software FRETBursts.

In this notebook shows how to plot timetraces with burst information.
For a complete tutorial on burst analysis see 
FRETBursts - us-ALEX smFRET burst analysis.
End of explanation
"""


url = 'http://files.figshare.com/2182601/0023uLRpitc_NTP_20dT_0.5GndCl.hdf5'
download_file(url, save_dir='./data')
full_fname = "./data/0023uLRpitc_NTP_20dT_0.5GndCl.hdf5"

d = loader.photon_hdf5(full_fname)
loader.alex_apply_period(d)
d.calc_bg(bg.exp_fit, time_s=1000, tail_min_us=(800, 4000, 1500, 1000, 3000))
d.burst_search(min_rate_cps=8e3)
ds = d.select_bursts(select_bursts.size, add_naa=True, th1=40)
dsf = ds.fuse_bursts(ms=10)

"""
Explanation: Get and process data
End of explanation
"""


dplot(dsf, timetrace, tmin=0, tmax=1, bursts=True);

"""
Explanation: Plot Timetraces
Default plot:
End of explanation
"""


fig, ax = plt.subplots(figsize=(32, 3))
dplot(dsf, timetrace, tmin=0, tmax=3, binwidth=0.5e-3, bursts=True, 
      ax=ax, plot_style=dict(lw=0.7))
plt.xlim(0, 3)
plt.grid(False)

"""
Explanation: We can plot a longer figure that scrolls horizontally in the notebook:
End of explanation
"""


dx = dsf
num_time_points = 6
window = 3
kws = dict(figsize=(32, 3), bursts=True, binwidth=0.5e-3, 
           plot_style=dict(lw=0.7))

# Timepoints equally distributed along the measurement
time_points = np.round(np.linspace(dx.time_min+1, dx.time_max-window-1, num=num_time_points))

for i in time_points:
    ax = dplot(dx, timetrace, tmin=i, tmax=i+window, **kws);
    plt.xlim(i, i+window)
    display(plt.gcf())
    plt.close(plt.gcf())

"""
Explanation: Using the previous plot we can sample different times of the measurement to have an overview of the timetrace:
End of explanation
"""


