oldlm.stype.unique()

Dlm = {}
for stype in tqdm.tqdm(oldlm.stype.unique()):
    # These two lines may need to be modified based on stype structure
    s = stype.split('-')[0]
    c = stype.split('-')[-1]
    
    # Add sample type dictionary if not already present
    if s not in Dlm.keys():
        Dlm[s] = {}
    
    # Save sample specific landmark data to dictionary
    Dlm[s][c] = oldlm[oldlm.stype==stype]

"""
Explanation: Restructure Data
We will sort landmark data according to stype and organize it in a two tiered dictionary according to sample type (s) and channel (c).
End of explanation
"""


gdata = {}
gdata['you'] = {}
gdata['wt'] = {}

gdata['you']['ZRF'] = ds.graphData(Dlm['you']['ZRF'],colors[3])
gdata['wt']['ZRF'] = ds.graphData(Dlm['wt']['ZRF'],colors[1])
gdata['you']['AT'] = ds.graphData(Dlm['you']['AT'],colors[2])
gdata['wt']['AT'] = ds.graphData(Dlm['wt']['AT'],colors[0])

"""
Explanation: Set up graph data
End of explanation
"""


crop = 40 # microns
legend = False
save = True
a = 0.3
pthresh = 0.01

channel = 'ZRF'

fig,axr = plt.subplots(2,4,figsize=(12,5),sharey='row',sharex=True)

if crop is not None:
    mask = np.where((xarr>-crop)&(xarr<crop) == True)[0]
    xmin = mask.min()
    xmax = mask.max()
    xarrcr = xarr[xmin:xmax+1]
else:
    xarrcr = xarr

for I,dtype in enumerate(['r','pts']):
    go1 = gdata['wt'][channel]
    go1.prepare_data(xarrcr,tarr,dtype)
    go2 = gdata['you'][channel]
    go2.prepare_data(xarrcr,tarr,dtype)
    
    parr = stats.ttest_ind(go1.arr_masked,go2.arr_masked,axis=2,nan_policy='omit')[1]

    parr[parr<pthresh] = 0
    parr[parr>pthresh] = 1
    
    # Plot wildtype data
    go = go1
    for i,p in enumerate(tpairs):
        n = i
        i = I
        
        ti1 = np.where(tarr==p[0])[0][0]
        ti2 = np.where(tarr==p[1])[0][0]

        axr[i,n].fill_between(xarrcr,go.avg[:,ti1]+go.sem[:,ti1],go.avg[:,ti1]-go.sem[:,ti1],alpha=a,color=go.c,zorder=1)
        axr[i,n].fill_between(xarrcr,-go.avg[:,ti2]+go.sem[:,ti2],-go.avg[:,ti2]-go.sem[:,ti2],alpha=a,color=go.c,zorder=1)

        axr[i,n].plot(xarrcr,go.avg[:,ti1],c=go.c,zorder=2,label='{} {}'.format(go.arr.shape[-1],'wt'))
        axr[i,n].plot(xarrcr,-go.avg[:,ti2],c=go.c,zorder=2)
        
    # Plot mutant data
    go = go2
    for i,p in enumerate(tpairs):
        n = i
        i = I
        
        ti1 = np.where(tarr==p[0])[0][0]
        ti2 = np.where(tarr==p[1])[0][0]

        axr[i,n].fill_between(xarrcr,go.avg[:,ti1]+go.sem[:,ti1],go.avg[:,ti1]-go.sem[:,ti1],alpha=a,color=go.c,zorder=1)
        axr[i,n].fill_between(xarrcr,-go.avg[:,ti2]+go.sem[:,ti2],-go.avg[:,ti2]-go.sem[:,ti2],alpha=a,color=go.c,zorder=1)

        axr[i,n].plot(xarrcr,go.avg[:,ti1],c=go.c,zorder=2,label='{} {}'.format(go.arr.shape[-1],'yot'))
        axr[i,n].plot(xarrcr,-go.avg[:,ti2],c=go.c,zorder=2)
        
        axr[i,n].scatter(xarrcr,go.avg[:,ti1],c=parr[:,ti1],cmap='Greys_r',zorder=3,vmin=0,vmax=1,edgecolor='k')
        axr[i,n].scatter(xarrcr,-go.avg[:,ti2],c=parr[:,ti2],cmap='Greys_r',zorder=3,vmin=0,vmax=1,edgecolor='k')
        
        axr[0,n].legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, mode="expand", borderaxespad=0.)

plt.tight_layout()
        
tstamp = datetime.datetime.now().strftime('%Y-%m-%d')

if save:
    fig.savefig(tstamp+'_yot-wt-{}.pdf'.format(channel))

channel = 'AT'

fig,axr = plt.subplots(2,4,figsize=(12,5),sharey='row',sharex=True)

if crop is not None:
    mask = np.where((xarr>-crop)&(xarr<crop) == True)[0]
    xmin = mask.min()
    xmax = mask.max()
    xarrcr = xarr[xmin:xmax+1]
else:
    xarrcr = xarr

for I,dtype in enumerate(['r','pts']):
    go1 = gdata['wt'][channel]
    go1.prepare_data(xarrcr,tarr,dtype)
    go2 = gdata['you'][channel]
    go2.prepare_data(xarrcr,tarr,dtype)
    
    parr = stats.ttest_ind(go1.arr_masked,go2.arr_masked,axis=2,nan_policy='omit')[1]

    parr[parr<pthresh] = 0
    parr[parr>pthresh] = 1
    
    # Plot wildtype data
    go = go1
    for i,p in enumerate(tpairs):
        n = i
        i = I
        
        ti1 = np.where(tarr==p[0])[0][0]
        ti2 = np.where(tarr==p[1])[0][0]

        axr[i,n].fill_between(xarrcr,go.avg[:,ti1]+go.sem[:,ti1],go.avg[:,ti1]-go.sem[:,ti1],alpha=a,color=go.c,zorder=1)
        axr[i,n].fill_between(xarrcr,-go.avg[:,ti2]+go.sem[:,ti2],-go.avg[:,ti2]-go.sem[:,ti2],alpha=a,color=go.c,zorder=1)

        axr[i,n].plot(xarrcr,go.avg[:,ti1],c=go.c,zorder=2,label='{} {}'.format(go.arr.shape[-1],'wt'))
        axr[i,n].plot(xarrcr,-go.avg[:,ti2],c=go.c,zorder=2)
        
    # Plot mutant data
    go = go2
    for i,p in enumerate(tpairs):
        n = i
        i = I
        
        ti1 = np.where(tarr==p[0])[0][0]
        ti2 = np.where(tarr==p[1])[0][0]

        axr[i,n].fill_between(xarrcr,go.avg[:,ti1]+go.sem[:,ti1],go.avg[:,ti1]-go.sem[:,ti1],alpha=a,color=go.c,zorder=1)
        axr[i,n].fill_between(xarrcr,-go.avg[:,ti2]+go.sem[:,ti2],-go.avg[:,ti2]-go.sem[:,ti2],alpha=a,color=go.c,zorder=1)

        axr[i,n].plot(xarrcr,go.avg[:,ti1],c=go.c,zorder=2,label='{} {}'.format(go.arr.shape[-1],'yot'))
        axr[i,n].plot(xarrcr,-go.avg[:,ti2],c=go.c,zorder=2)
        
        axr[i,n].scatter(xarrcr,go.avg[:,ti1],c=parr[:,ti1],cmap='Greys_r',zorder=3,vmin=0,vmax=1,edgecolor='k')
        axr[i,n].scatter(xarrcr,-go.avg[:,ti2],c=parr[:,ti2],cmap='Greys_r',zorder=3,vmin=0,vmax=1,edgecolor='k')
        
        axr[0,n].legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,ncol=2, mode="expand", borderaxespad=0.)

plt.tight_layout()

tstamp = datetime.datetime.now().strftime('%Y-%m-%d')

if save:
    fig.savefig(tstamp+'_yot-wt-{}.pdf'.format(channel))

"""
Explanation: Graphs
End of explanation
"""


