mamoth_p3
index
/Users/katja/Documents/PhD/Output/Paper/Matrixmethods Paper/Algorithms submission/mamoth2/mamoth_p3.py

Methods to visualize and approximate big transition matrices arising from 
state-rich Markov chain models, e.g. in ecology and evolution.
 
Licence:
----------
authored 2014 by Cedric Midoux*, Valentin Bahier*, Katja Reichel* and Solenn Stoeckel*
*Institut National de la Recherche Agronomique (INRA)
This module is published under the GNU general public license: 
<http://www.gnu.org/licenses/gpl-2.0.html> .
 
 
Scientific citation:
--------------------
K. Reichel, V. Bahier, C. Midoux, N. Parisey, J.-P. Masson, S. Stoeckel (2015): 
"Interpretation and approximation tools for big, dense Markov chain 
transition matrices in population genetics"
tba.
 
 
Notes:
------
This module includes supplementary functions only and produces no output by 
itself. For a test run, use the example provided along with the source code.
The use of the * operator for importing this module is discouraged.
 
 
Version:
--------
This is version 2.0 [08/2015]. 
The "t_spec" option was added to the "networkplot" function, the "mp_path" 
option in the the "networkplot" function corrected, and the "testvectors" 
function corrected.
 
 
Functions:
----------
loadmatrix : loads a matrix from a text file (.txt, .csv etc.)
 
histogrid : generates a 2D matrix histogram
 
histo3D : generates a 3D matrix histogram
 
networkplot : generates a network with various optional visual statistics 
    based on the matrix
 
percolation : auxillary function for networkplot
 
eigenone : calculates the eigenvector to the dominant eigenvalue (usualy 
    equal to one) of the matrix
 
appromatrix : approximates matrix by introducing zeros, optional output in 
    a sparse format
 
testvectors : performs a G-test on two vectors
 
 
Requires:
---------
Sys, Numpy, Scipy, NetworkX, Matplotlib

 
Modules
       
numpy
networkx
matplotlib.pyplot
scipy.sparse

 
Functions
       
appromatrix(matrix, s, sparse=None, testing=False, verbose=False)
Returns an approximate (sparse) matrix.
 
Parameters:
-----------
matrix : a 2d numpy array holding the transition matrix
 
s : a cutoff value for the column sum in the interval ]0.0; 1.0] 
    At least one value per column will always be kept.
 
sparse : output format for the approximated matrix 
    See sparse matrix classes in the scipy.sparse documentation.
    The most frequent will be:
    -   "csc" (compressed column; fast calculations) 
    -   "lil" (list of lists; strong compression)
    Defaults to None, i.e. no sparse format applied
 
testing : execute efficiency test and an accuracy test 
    If True, the function gives out a tuple of values instead of just the
    matrix: (matrix, efficiency ratio, accuracy p-value)
 
verbose : boolean
    If True, print out intermediate results of the approximation process
    
Returns:
--------
sparsematrix : 2d scipy sparse array or 2d numpy array
    The approximate matrix, optionally in a sparse format.
    
If testing is True:
 
sparsematrix : 2d scipy sparse array or 2d numpy array
    The approximate matrix, optionally in a sparse format.
 
efficiency: float
    One minus sparse matrix to original matrix bytesize ratio.
 
p_value: float
    P-value of a G-test on the eigenvectors of the sparse vs. original 
    matrix. SciPy's own G-test function is used from SciPy version 0.13.0
    upwards.
eigenone(matrix, alg='linalg', throwex=False, verbose=False)
Returns the eigenvector corresponding to a single maximal eigenvalue of 
one of a matrix.
 
Parameters:
-----------
matrix : a 2d numpy array or a 2d scipy sparse array
 
alg : string, an algorithm to calculate the vector. 
    Either "linalg" for using LAPACK (numpy matrix) or ARPACK (sparse 
    matrix), or "power" for the power method (accuracy = 1e-15).
 
throwex : boolean
    If true, throw exception in case of bad behavior of the function.
    For the power method, this occurs when there is no convergence after 
    100 steps; for the linalg method, this occurs if the maximal 
    eigenvalue of the matrix is not single or not equal to 1.0.
    If false, there will only be a printed warning.
 
verbose : boolean
    If true, diagnostic intermediate results will be printed out.
 
Note:
-----
It will not be checked previously if the matrix actually has a single 
    maximal eigenvalue of one. If in doubt, use the "linalg" algorithm 
    and turn on the "throwex" option.
 
Returns:
--------
vector : 1d numpy array
    The eigenvector corresponding to a single (= of multiplicity one) 
    maximal eigenvalue of one of a matrix.
    
Warning:
--------
This function automatically returns the normalized absolute of the 
    desired eigenvector. Small negative values may arise due to numerical
    errors (see LAPACK/ARPACK documentation).
histo3d(matrix, visualize=False, axes=None, scaling=None, statenames=None, spacing=0.1, angle=[20, 12], *args, **kwargs)
Create a 3d histogram of a 2d numpy array. 
 
Parameters:
-----------
matrix : a 2d numpy array holding the transition matrix
 
visualize: boolean
    If True, the resulting diagram will be shown directly.
 
axes : an axes object to draw to; None means drawing to current axes.
 
scaling : a scaling applied to all values in the matrix
==========    ===============    =========================================
value         range              description
==========    ===============    =========================================
None          (0,1)              no scaling, values are taken as they are
"log10"       (-inf, 0)          log10 scaling on the values
"-log10"      (0, +inf)          negative log10 scaling on the values
"logit"       (-inf, +inf)       logit scaling on the values
==========    ===============    =========================================
 
statenames : labels for the states
==========    ===============    =========================================
value         range              description
==========    ===============    =========================================
None          (0, n-1)           python array indexes
"1N"          (1, n)             count index of each state 
[...]         list of stings     empty list : print no statenames
==========    ===============    =========================================
 
spacing : float, half distance between bars
 
angle : tuple of numbers, viewing angle (azimut, elevation)
 
Plus any arguments which can be used on Axes3D bar3d objects.
 
Returns:
--------
axes : matplotlib 3d axes object
    Matrix columns are displayed on the y-axis, rows on the x-axis.
    
Warning:
--------
As per version 1.3.1, the 3D plotting function in matplotlib has a bug 
    which results in an incorrect display of the bars at certain viewing 
    angles (wrong plotting order). This also produces a runtime warning 
    ("invalid value encountered in divide for n in normals"). Please 
    ignore the warning and use the "angle" argument to find a correct 
    display angle.
histogrid(matrix, visualize=False, axes=None, scaling=None, statenames=None, *args, **kwargs)
Create a color coded 2d histogram of a 2d numpy array. 
 
Parameters:
-----------
matrix : a 2d numpy array holding the transition matrix
 
visualize: boolean
    If True, the resulting diagram will be shown directly.
 
axes : an axes object to draw to; None means drawing to current axes.
 
scaling : a scaling applied to all values in the matrix
==========    ===============    =========================================
value         range              description
==========    ===============    =========================================
None          (0,1)              no scaling, values are taken as they are
"log10"       (-inf, 0)          log10 scaling on the values
"-log10"      (0, +inf)          negative log10 scaling on the values
"logit"       (-inf, +inf)       logit scaling on the values
==========    ===============    =========================================
 
statenames : labels for the states
==========    ===============    =========================================
value         range              description
==========    ===============    =========================================
None          (0, n-1)           python array indexes
"1N"          (1, n)             count index of each state 
[...]         list of stings     empty list : print no statenames
==========    ===============    =========================================
 
Plus any arguments the matplotlib.pyplot.imshow() function would take.
 
Returns:
--------
axes : matplotlib axes object
    The default colormap is greyscale with increasing luminosity - see 
    comments at matplotlib.pyplot.colormaps() for more information about 
    using color in scientific illustrations.
loadmatrix(filepath=None, loadnames=False, comments=None, verbose=False)
Ask for a filepath and import matrix from there.
 
Parameters:
-----------
filepath : string 
    Optional, gives the path (incl. filename) of a tab-delimited 
    .txt-like matrix file;has to be entered in a prompt if not provided 
    in the function call.
 
loadnames : boolean
    Optional, wether or not to read in statenames from the first line of 
    the matrix file
 
comments : string
    Optional, set a character which turns the following text into a 
    comment; text lines marked as comment will not be imported
 
verbose : boolean
    If True, print out status messages & intermediate results
 
Returns:
--------
matrix : a 2d numpy array with datatype float 
    If the upper left cell of the input file holds "#", row and column 
    names are cut off automatically.
 
If loadnames is True, matrix and names are returned as a tuple:
 
matrix : a 2d numpy array with datatype float 
    Without row and column names.
 
statenames : a list
    Reads out the column names in the input file.
networkplot(matrix, visualize=False, axes=None, mode='', special=[0, 1], openout=True, *args, **kwargs)
Customizable network display of a matrix.
 
Parameters:
-----------
matrix : a 2d numpy array holding the transition matrix
 
visualize: boolean
    If True, the resulting diagram will be shown directly.
 
axes : an axes object to draw to; None means drawing to current axes.
 
mode: different ways to display the data; combine from I & II:
================    =============================================================================
value               description
================    =============================================================================
                    I. edges are drawn:
"mp_neighbor"       from each node to its most probable neighbor(s)
"mp_path"           along the most probable path from the first "special" node to the second
"percolation"       according to a percolation threshold set via "special"
 
"eachedge"          all edges are drawn if none of three options above are specified
"noedge"            overrides edge drawing
 
                    II. nodes are colored according to:
"p_eig"             their probability to be occupied during an infinite run (power method)
"p_stay"            the probability to stay at them for the next time step
"p_out"             the probability to move out of them at the next time step
"p_in_raw"          the probability to arrive at them from anywhere else with equal probability   
"p_in_inf"          the average probability to arrive at them during an infinite run   
"btw_cent"          their value of betweenness-centrality
"d_in"              their degree of incoming edges
"d_out"             their degree of outgoing edges
"d_all"             their degree counting edges in both directions
"special"           wether they are the special nodes (orange) or not (70% grey)
"t_spec"            the expected time to a special state
    
"nonode"            overrides node drawing
  
================    =============================================================================
 
special : a list of two nodes specified by their indices in the matrix
    This list will be used as the source and target for the "percolation threshold" and 
    "shortest path" algorithms.
 
statenames : a list of labels
    Alias for networkx labels.
 
openout : boolean
    If true, plotting is performed as a block, if false the components 
    are returned one by one (more convenient for making legends).
    
Plus any arguments the networkx.draw_networkx() function would take.
 
Returns:
--------
nodes : matplotlib artist or False
    The network nodes.
 
edges : matplotlib artist(s) or False
    The network edges.
 
labels : matplotlib artist or False
    The labels.
 
if open is False:
axes : matplotlib axes object
    Nodelabeling and axis plotting is turned off per default.
percolation(matrix, source, target, verbose=False)
Returns a graph where percolation is just possible between source and 
target.
 
Parameters:
-----------
matrix : a 2d numpy array holding the transition matrix
 
source : a node, given by its index in the matrix
 
target : a node, given by its index in the matrix
 
verbose : print out percentage of edges retained & minimal edge weight
 
Returns:
--------
graph : networkx graph object
    Contains all nodes, but only edges whose weight is above the 
    percolation threshold defined by the two nodes provided. This also 
    works correctly if source and target node are identical.
testvectors(original, fake, ignorext=True, verbose=False)
G-test comparing two vectors.
 
Parameters:
-----------
original : 1D numpy array 
    Distribution against which the test will be performed
 
fake : 1D numpy array 
    Distribution to be tested
 
ignorext : boolean 
    Ignore zeros and negative values in the vector and compare only 
    positive vector entries
 
verbose : boolean 
    If True, print out the summands for the G-statistic 
 
Returns:
--------
g_value : float 
    The value of G
 
p_value : float 
    P-value of the G-test
 
Note:
------
Compare the scipy.stats.power_divergence() function in scipy >= 0.13.0 
    for a better implementation. P-values for G are determined on a 
    chi-squared distribution.