Coverage for lingpy/convert/plot.py : 91%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Module provides functions for the transformation of text data into visually appealing format. """
gls, treestring, degree=90, fileformat='pdf', **keywords ): """ Plot a gain-loss scenario for a given reference tree. """
# get kewyords figsize=(15, 15), left=0.05, top=0.95, bottom=0.05, right=0.95, radius=0.5, textsize=8, edgewidth=5, linewidth=2, scale_radius=1.2, ylim=1, xlim=1, text=True, gain_color='white', loss_color='black', gain_linestyle='dotted', loss_linestyle='solid', ax_linewidth=0, filename=rcParams['filename'] )
# set filename as variabel for convenience
tree = cg.LoadTree(treestring) except:
gls, tgraph, tree )
# assign nodes and edges
# now plot the stuff
# set the axes linewidht
[xA, xB], [yA, yB], '-', color='black', linewidth=keywords['edgewidth'], zorder=1 )
# now, iterate over nodes (x, y), keywords['radius'], 0, 360, facecolor=keywords['gain_color'], linewidth=keywords['linewidth'], linestyle=keywords['gain_linestyle'] ) (x, y), keywords['radius'] / keywords['scale_radius'], 0, 360, facecolor=keywords['gain_color'], linewidth=keywords['linewidth'] ) (x, y), keywords['radius'], 0, 360, facecolor=keywords['loss_color'], linewidth=keywords['linewidth'], linestyle=keywords['loss_linestyle'] ) else: (x, y), keywords['radius'] / keywords['scale_radius'], 0, 360, facecolor=keywords['loss_color'], linewidth=keywords['linewidth'] )
# if text is chosen as argument else:
x, y, t, size=keywords['textsize'], color=c, va="center", ha="center", fontweight='bold' )
# set x and y-values
left=keywords['left'], right=keywords['right'], top=keywords['top'], bottom=keywords['bottom'] ) filename + '.' + fileformat )
treestring, degree=90, fileformat='pdf', root="root", **keywords ): """ Plot a Newick tree to PDF or other graphical formats.
Parameters ---------- treestring : str A string in Newick format. degree : int Determine the degree of the tree (this determines how "circular" the tree will be). fileformat : str (default="pdf") Select the fileformat to which the tree shall be written. filename : str Determine the name of the file to which the data shall be written. Defaults to a timestamp. figsize : tuple (default=(10,10)) Determine the size of the figure. """
ax_linewidth=0, bg='black', bottom=0.05, change=lambda x: x ** 1.75, edge_list=[], figsize=(10, 10), filename=rcParams['filename'], fontweight='bold', frameon=False, ha='center', labels=[], left=0.05, linecolor='black', linewidth=5, no_labels=False, node_dict={}, nodecolor='black', nodesize=10, right=0.95, start=0, textcolor='white', textsize='10', top=0.95, usetex=False, va='center', xlim=5, xliml=False, xlimr=False, ylim=5, ylimb=False, ylimt=False, rotation_mode='anchor', latex_preamble=False, )
# set filename as variable for convenience
# switch backend, depending on whether tex is used or not
# get the tree-graph treestring, degree=degree, change=keywords['change'], start=keywords['start'], root=root )
# create the figure
# plt.axes(frameon=keywords['frameon'])
# get xlim and ylim # start iterating over edges
# get the coordinates
[xA, xB], [yA, yB], '-', **d ) else: [xA, xB], [yA, yB], '-', color=keywords['linecolor'], linewidth=keywords['linewidth'], )
# get the nodes
# try to get information from the node-dict
# overwrite the stuff in keywords
or d['label'].startswith(root) or keywords['no_labels']: x, y, 'o', markersize=settings['nodesize'], color=settings['nodecolor'], markeredgewidth=settings['linewidth'] ) else: else: x, y, label, # d['label'], color=settings['textcolor'], fontweight=settings['fontweight'], va=settings['va'], ha=g['s'], bbox=dict( facecolor=settings['bg'], boxstyle='square,pad=0.2', ec="none", ), size=settings['textsize'], rotation=r, # g['angle'], rotation_mode=settings['rotation_mode'] )
# set up the xlimits else:
# set up the xlimits else:
left=keywords['left'], right=keywords['right'], top=keywords['top'], bottom=keywords['bottom'] )
scenarios, tree, fileformat='pdf', degree=90, **keywords ): """ Plot the evolution according to the MLN method of all words for a given concept.
Parameters ---------- tree : str A tree representation in Newick format. fileformat : str (default="pdf") A valid fileformat according to Matplotlib. degree : int (default=90) The degree by which the tree is drawn. 360 yields a circular tree, 180 yields a tree filling half of the space of a circle. """
# make defaults figsize=(15, 15), left=0.05, top=0.95, bottom=0.05, right=0.95, colormap=mpl.cm.jet, edgewidth=5, radius=2.5, outer_radius=0.5, inner_radius=0.25, cognates='', usetex=False, latex_preamble=False, textsize=8, change=lambda x: x ** 1.75, xlim=0, ylim=0, xlimr=False, xliml=False, ylimt=False, ylimb=False, rootsize=10, legend=True, legendsize=5, legendAloc='upper right', legendBloc='lower right', markeredgewidth=2.5, wedgeedgewidth=2, gain_linestyle='dotted', loss_linestyle='solid', ax_linewidth=0, labels={}, _prefix='- ', _suffix=' -', colors={}, start=0, filename=rcParams['filename'], loss_alpha=0.1, loss_background='0.75', edges=[], hedge_color="black", hedge_width=5, hedge_linestyle='dashed', )
# set filename as variable for convenience
# XXX customize later XXX
# switch backend, depending on whether tex is used or not
# check for preamble settings
# make a graph
# get the tgraph tree, degree=degree, change=keywords['change'], start=keywords['start'] )
# get the taxa
# set the labels else:
# get the number of paps in order to get the right colors
for i in range(len(scenarios))} else:
# get the wedges for the paps
# set the linestyle for the legend
# add stuff for the legend (0, 0), 1, wedges[pap][0], wedges[pap][1], facecolor=colors[pap], zorder=1, linewidth=keywords['wedgeedgewidth'], edgecolor='black' )
# second legend explains evolution (0, 0), 1, 0, 360, facecolor='0.5', linewidth=keywords['wedgeedgewidth'], edgecolor='black', ) 0, 0, ls, color='black', linewidth=keywords['wedgeedgewidth'] )
# overwrite stuff
# iterate over the paps and append states to the graph
# get the graph with the model gls, tgraph, tree, filename='' )
# iterate over the graph
# add the node if necessary
# add a pap-dictionary if it's not already there
# add data
# create the figure
# iterate over edges first
[xA, xB], [yA, yB], '-', color='black', linewidth=keywords['edgewidth'] )
# add horizontal edges if this option is chosen # get the coordinates gA = g.node[nA]['graphics'] gB = g.node[nB]['graphics'] xA, yA = gA['x'], gA['y'] xB, yB = gB['x'], gB['y']
plt.plot( [xA, xB], [yA, yB], '-', color=keywords['hedge_color'], linewidth=keywords["hedge_width"], linestyle=keywords['hedge_linestyle'] )
# now iterate over the nodes
# get z-value which serves as zorder attribute
# plot the default marker x, y, 'o', markersize=keywords['rootsize'], color='black', zorder=50 ) # check for origins in cpaps (x, y), keywords['radius'] + keywords['outer_radius'], 0, 360, facecolor='white', zorder=57 + z, linewidth=keywords['markeredgewidth'], linestyle=keywords['gain_linestyle'], ) # check for retentions (x, y), keywords['radius'] + keywords['outer_radius'], 0, 360, facecolor='white', zorder=56 + z, linewidth=keywords['markeredgewidth'], linestyle='solid', )
(x, y), keywords['radius'] + keywords['outer_radius'], 0, 360, facecolor=keywords['loss_background'], zorder=58 + z, linewidth=keywords['markeredgewidth'], edgecolor='black', linestyle=keywords['loss_linestyle'] )
(x, y), keywords['radius'] + keywords['outer_radius'], 0, 360, facecolor=keywords['loss_background'], zorder=59 + z, linewidth=keywords['markeredgewidth'], edgecolor='black', )
# plot all wedges
# check for characteristics of this pap
# if it's a loss
(x, y), keywords['radius'], theta1, theta2, facecolor=color, zorder=61 + z, alpha=keywords['loss_alpha'], # 0.25, linewidth=keywords['wedgeedgewidth'], edgecolor='black', linestyle=keywords['loss_linestyle'] )
(x, y), keywords['radius'], theta1, theta2, facecolor=color, zorder=61 + z, linewidth=keywords['wedgeedgewidth'], edgecolor='black' )
(x, y), keywords['radius'], theta1, theta2, facecolor=color, zorder=61 + z, linewidth=keywords['wedgeedgewidth'], edgecolor='black', linestyle=keywords['gain_linestyle'] )
# add the labels if this option is chosen # if node is a tip
# get the values gf = tgraph.node[n]['graphics'] r = gf['angle'] x, y = gf['x'], gf['y'] ha = gf['s']
# modify the text if ha == 'left': text = keywords['_prefix'] + labels[n] else: text = labels[n] + keywords['_suffix']
# plot the text plt.text( x, y, text, size=keywords['textsize'], va='center', ha=ha, fontweight='bold', color='black', rotation=r, rotation_mode='anchor', zorder=z )
# set up the xlimits else:
# set up the xlimits else:
legendEntriesA, legendTextA, loc=keywords['legendAloc'], numpoints=1, prop=prop ) legendEntriesB, legendTextB, loc=keywords['legendBloc'], prop=prop )
left=keywords['left'], right=keywords['right'], top=keywords['top'], bottom=keywords['bottom'] )
wordlist, filename="heatmap", fileformat="pdf", ref='cogid', normalized=False, refB='', **keywords ): """ Create a heatmap-representation of shared cognates for a given wordlist.
Parameters ---------- wordlist : lingpy.basic.wordlist.Wordlist A Wordlist object containing cognate IDs. filename : str (default="heatmap") Name of the file to which the heatmap will be written. fileformat : str (default="pdf") A regular matplotlib-fileformat (pdf, png, pgf, svg). ref : str (default="cogid') The name of the column that contains the cognate identifiers. normalized : {bool str} (default=True) If set to c{False}, don't normalize the data. Otherwise, select the normalization method, choose between:
* "jaccard" for the Jaccard-distance (see :evobib:`Bategelj1995` for details), and * "swadesh" for traditional lexicostatistical calculation of shared cognate percentages.
cmap : matplotlib.cm (default=matplotlib.cm.jet) The color scheme to be used for the heatmap. steps : int (default=5) The number of steps in which names of taxa will be written to the axes. xrotation : int (default=45) The rotation of the taxon-names on the x-axis. colorbar : bool (default=True) Specify, whether a colorbar should be added to the plot. figsize : tuple (default=(10,10)) Specify the size of the figure. tree : str (default='') A tree passed for the taxa in Newick-format. If no tree is specified, the method looks for a tree object in the Wordlist.
Notes ----- This function plots shared cognate percentages.
""" bottom=0.01, # rcParams['phybo_ylimb'] cmap=mpl.cm.jet, colorbar=True, colorbar_label="Shared Cognates", colorbar_shrink=0.75, colorbar_textsize=10, figsize=(10, 5), height=0.8, labels={}, # taxon labels passed for the taxa, left=0.01, # rcParams['phybo_xlimr'], matrix=False, normalization="jaccard", right=0.95, # rcParams['phybo_xliml'], scale=0.075, show_tree=True, steps=20, textsize=5, top=0.95, # rcParams['phybo_ylimt'], tree='', tree_bottom=0.1, tree_left=0.1, tree_width=0.2, vmax=1.0, vmin=0.0, width=0.8, xrotation=90, distances=False )
# access the reference tree of the wordlist and create a function that # orders the taxa accordingly raise ValueError("[i] No tree could be found") else:
# check for normalization "Keyword 'normalized' must be one of 'jaccard','swadesh',False.")
# create an empty matrix else:
# create the figure
# plot the reference tree [ keywords['left'], keywords['bottom'], 0.25 * keywords['width'], keywords['height'] ] ) # [0.01,0.1,0.2,0.7]) np.array(tree_matrix), labels=[t for t in taxa], orientation='left',
)
else: left = keywords['left']
# start iterating over taxa in order of the reference tree and fill in the # matrix with numbers of shared cognates else: taxa=taxonA, flat=True, entry=ref ) taxa=taxonB, flat=True, entry=ref )
else: taxa=taxonA, entry=ref ) taxa=taxonB, entry=ref )
shared = 0 slots = 0
# iterate over cognate sets in meaning slots for key in cogsA.keys(): # check whether keys are present, we follow the # STARLING procedure in ignoring missing data if key in cogsA and key in cogsB:
# check for shared items try: except ZeroDivisionError: [shared, slots, len(cogsA), len(cogsB), taxonA, taxonB]))
# if refB is also a possibiltiy
taxa=taxonA, flat=True, entry=refB ) taxa=taxonB, flat=True, entry=refB )
shared = shared / len(cogsA.union(cogsB)) else: cogsA = wordlist.get_dict( taxa=taxonA, entry=refB ) cogsB = wordlist.get_dict( taxa=taxonB, entry=refB )
shared = 0 slots = 0
# iterate over cognate sets in meaning slots for key in cogsA.keys(): # check whether keys are present, we follow the # STARLING procedure in ignoring missing data if key in cogsA and key in cogsB:
# check for shared items if [k for k in cogsA[key] if k in cogsB[key]]: shared += 1 slots += 1 try: except ZeroDivisionError: [shared, slots, len(cogsA), len(cogsB), taxonA, taxonB]))
taxa=taxonA, flat=True, entry=ref ) else: [ left, # keywords['left']+0.25 * keywords['width']+0.05, keywords['bottom'], keywords['width'], keywords['height'] ] )
# [0.15,0.1,0.7,0.7])
interpolation='nearest', cmap=keywords['cmap'], vmax=keywords['vmax'], vmin=keywords['vmin'] )
# set the xticks
# modify taxon names if this is specified
idxs, selected_taxa, size=keywords['textsize'], rotation=keywords['xrotation'], rotation_mode="default" ) idxs, selected_taxa, size=keywords['textsize'], )
left=keywords['left'], right=keywords['right'], top=keywords['top'], bottom=keywords['bottom'] )
else: |