Example #1
0
 def test_convenience():
     info('m')
     warning('m')
     debug('m')
     error('m')
     deprecated('o', 'n')
     missing_module('m')
     file_written('f')
Example #2
0
 def test_convenience():
     info('m')
     warning('m')
     debug('m')
     error('m')
     deprecated('o', 'n')
     missing_module('m')
     file_written('f')
Example #3
0
    def test_convenience(self):
        from lingpy.log import info, warn, debug, error, deprecated, missing_module, file_written

        info('m')
        warn('m')
        debug('m')
        error('m')
        deprecated('o', 'n')
        missing_module('m')
        file_written('f')
Example #4
0
    def test_convenience(self):
        from lingpy.log import info, warn, debug, error, deprecated, missing_module, file_written

        info('m')
        warn('m')
        debug('m')
        error('m')
        deprecated('o', 'n')
        missing_module('m')
        file_written('f')
Example #5
0
from __future__ import unicode_literals, print_function, division

from lingpy import log

import numpy as np
import networkx as nx
try:
    import matplotlib.patches as mplPatches
except ImportError:
    log.missing_module('matplotlib')

from .convex_hull import convex_hull


def perp(a):
    """
    code for intersection taken from
    http://stackoverflow.com/questions/3252194/numpy-and-line-intersections
    """
    b = np.empty_like(a)
    b[0] = -a[1]
    b[1] = a[0]
    return b


# line segment a given by endpoints a1, a2
# line segment b given by endpoints b1, b2
# return
def seg_intersect(nA, nB):
    a1 = np.array(nA[0], dtype='float')
    a2 = np.array(nA[1], dtype='float')
Example #6
0
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals
import types

from ...settings import *
from lingpy import log

try:
    import regex as re
    if not isinstance(re, types.ModuleType):
        # this is the case when creating the docs!
        raise ImportError
except ImportError:
    import re
    log.missing_module('regex')

import sys
import os
import codecs

# data for sampa2ipa (Peter Kleiwegs implementation)
path = os.path.split(    
        os.path.abspath(        
            __file__       
            )        
        )[0]

f = codecs.open(path + '/sampa.csv','r','utf-8')
xsdata = []
_xsKeys = [' ']
Example #7
0
"""
Module offers methods to handle colexification patterns in wordlist objects.
"""
from collections import defaultdict

from lingpy import log
from lingpy.util import combinations2, dotjoin, join

try:
    import networkx as nx
except ImportError:
    log.missing_module('networkx')
try:
    import community
except ImportError:
    log.missing_module('community')


def _get_colexifications(wordlist, entry='ipa', concept='concept', family='family'):
    """
    Helper function computes colexifications for a given set of languages in a
    wordlist.
    """
    if family not in wordlist.header:
        family = 'doculect'

    taxa = wordlist.cols
    colexifications = []
    for taxon in taxa:
        log.info('Analyzing taxon {0}...'.format(taxon))
Example #8
0
Notes
-----

This code has been taken from: http://www.scipy.org/Cookbook/Finding_Convex_Hull

Author: Angus McMorland
Date: 2007-08-16
"""
from lingpy import log

import numpy as n

try:
    import pylab as p
except ImportError:
    log.missing_module('matplotlib')


def _angle_to_point(point, centre):
    '''calculate angle in 2-D between points and x axis'''
    delta = point - centre
    res = n.arctan(delta[1] / delta[0])
    if delta[0] < 0:
        res += n.pi
    return res


def _draw_triangle(p1, p2, p3, **kwargs):
    tmp = n.vstack((p1, p2, p3))
    x, y = [x[0] for x in zip(tmp.transpose())]
    p.fill(x, y, **kwargs)
Example #9
0
def fuzzy(threshold, matrix, taxa, method='upgma', revert=False):
    """
    Create fuzzy cluster of a given distance matrix.

    Parameters
    ----------
    threshold : float
        The threshold that shall be used for the basic clustering of the data.

    matrix : list
        A two-dimensional list containing the distances.

    taxa : list
        An list containing the names of all taxa corresponding to the distances
        in the matrix.

    method : { "upgma", "single", "complete" } (default="upgma")
        Select the method for the flat cluster analysis.

    distances : bool
        If set to "False", only the topology of the tree will be returned.

    revert : bool (default=False)
        Specify whether a reverted dictionary should be returned.

    Returns
    -------
    cluster : dict
        A dictionary with cluster-IDs as keys and a list as value, containing
        the taxa that are assigned to a given cluster-ID.

    Examples
    --------
    The function is automatically imported along with LingPy.

    >>> from lingpy import *
    from lingpy.algorithm import squareform

    Create a list of arbitrary taxa.

    >>> taxa = ['German','Swedish','Icelandic','English','Dutch']

    Create an arbitrary distance matrix.

    >>> matrix = squareform([0.5,0.67,0.8,0.2,0.4,0.7,0.6,0.8,0.8,0.3])
    >>> matrix
    [[0.0, 0.5, 0.67, 0.8, 0.2],
     [0.5, 0.0, 0.4, 0.7, 0.6],
     [0.67, 0.4, 0.0, 0.8, 0.8],
     [0.8, 0.7, 0.8, 0.0, 0.3],
     [0.2, 0.6, 0.8, 0.3, 0.0]]

    Carry out the fuzzy flat cluster analysis.

    >>> fuzzy(0.5,matrix,taxa)
    {1: ['Swedish', 'Icelandic'], 2: ['Dutch', 'German'], 3: ['Dutch', 'English']}

    Notes
    -----
    This is a very simple fuzzy clustering algorithm. It basically does nothing
    else than removing taxa successively from the matrix, flat-clustering the
    remaining taxa with the corresponding threshold, and then returning a
    combined "consensus" cluster in which taxa may be assigned to multiple
    clusters.

    See also
    --------
    link_clustering

    """
    if nx is None:
        log.missing_module('networkx')
        return
    g = nx.Graph()

    for taxon in taxa:
        g.add_node(taxon)

    for idx, taxon in enumerate(taxa):
        new_matrix = []
        for i, line in enumerate(matrix):
            for j, cell in enumerate(line):
                if i < j and i != idx and j != idx:
                    new_matrix += [cell]
        new_matrix = misc.squareform(new_matrix)

        clusters = cluster.flat_cluster(
            method, threshold, new_matrix, [t for t in taxa if t != taxon])

        for clr in clusters:
            for i, tA in enumerate(clusters[clr]):
                for j, tB in enumerate(clusters[clr]):
                    if i < j:
                        try:
                            g.edge[tA][tB]['weight'] += 1
                        except:
                            g.add_edge(tA, tB, weight=1)
    out = {i + 1: c for i, c in enumerate(nx.find_cliques(g))}

    if revert:
        new_out = defaultdict(list)
        for key, val in out.items():
            for v in val:
                new_out[v].append(key)
        return new_out

    return out
Example #10
0
"""
Utility functions for borrowing detection with the PhyBo class.
"""
from lingpy import log
from lingpy.util import write_text_file, as_string

try:
    import scipy.stats as sps
except ImportError:
    log.missing_module('scipy')


def tstats(wordlist,
           glm='',
           network=False,
           acs=False,
           tree=False,
           singletons=True,
           return_dists=False):
    """
    Calculate transmission statistics for a given MLN.
    """

    # check for attributes
    # return if no glm and no network is given
    if not glm and not network:
        raise ValueError(
            "You must specify at least one network or a gain-loss model.")

    # check for acs and network
    if glm:
Example #11
0
"""
Conversion routines for the GML format.
"""
from lingpy import log
from lingpy import util

import networkx as nx

try:
    import igraph as ig
except:
    log.missing_module('igraph')

import numpy as np

from lingpy.thirdparty import cogent as cg


def networkx2igraph(graph):
    """Helper function converts networkx graph to igraph graph object."""
    newgraph = ig.Graph(directed=graph.is_directed())
    nodes = {}
    for i, (node, data) in enumerate(graph.nodes(data=True)):
        data = {a: b for a, b in data.items()}
        newgraph.add_vertex(i,
                            Name=node,
                            **{
                                a: b
                                for a, b in data.items()
                                if a not in [b'Name', b'name', 'Name', 'name']
                            })
Example #12
0
# *-* coding: utf-8 *-*
"""
Utility functions for borrowing detection with the PhyBo class.
"""
from __future__ import unicode_literals, division, print_function

from lingpy import log

try:
    import scipy.stats as sps
except:
    log.missing_module('scipy')


def tstats(
    wordlist,
    glm='',
    network=False,
    acs=False,
    tree=False,
    singletons=True,
    return_dists=False
):
    """
    Calculate transmission statistics for a given MLN.
    """

    # check for attributes
    # return if no glm and no network is given
    if not glm and not network:
        raise ValueError("You must specify at least one network or a gain-loss model.")
Example #13
0
try:
    from .cython import cluster as cluster
except ImportError:
    from .cython import _cluster as cluster

from lingpy.thirdparty import linkcomm as lc
from lingpy.thirdparty import cogent as cg
from lingpy import log
from lingpy import util

try:  # pragma: no cover
    import networkx as nx
except ImportError:
    nx = None
    log.missing_module('networkx')


def flat_upgma(threshold, matrix, taxa=None, revert=False):
    """
    Carry out a flat cluster analysis based on the UPGMA algorithm \
    (:evobib:`Sokal1958`).

    Parameters
    ----------

    threshold : float
        The threshold which terminates the algorithm.

    matrix : list
        A two-dimensional list containing the distances.
Example #14
0
from __future__ import unicode_literals, print_function, division

from lingpy import log

import numpy as np

try:
    import matplotlib.patches as mplPatches
except:
    log.missing_module('matplotlib')

try:
    import networkx as nx
except:
    log.missing_module('networkx')

from .convex_hull import convex_hull


def perp(a):
    """
    code for intersection taken from
    http://stackoverflow.com/questions/3252194/numpy-and-line-intersections
    """
    b = np.empty_like(a)
    b[0] = -a[1]
    b[1] = a[0]
    return b


# line segment a given by endpoints a1, a2
Example #15
0
# email    : [email protected]
# created  : 2013-09-25 11:47
# modified : 2013-11-22 15:43
"""
Utility functions for borrowing detection with the PhyBo class.
"""

__author__ = "Johann-Mattis List"
__date__ = "2013-11-22"

from lingpy import log

try:
    import networkx as nx
except:
    log.missing_module('networkx')
try:
    import scipy.stats as sps
except:
    log.missing_module('scipy')


def tstats(wordlist,
           glm='',
           network=False,
           acs=False,
           tree=False,
           singletons=True,
           return_dists=False):
    """
    Calculate transmission statistics for a given MLN.
Example #16
0
"""
Conversion routines for the GML format.
"""
from __future__ import unicode_literals, print_function, division

from lingpy import log
from lingpy import util

try:
    import networkx as nx
except:
    log.missing_module('networkx')

try:
    import igraph as ig
except:
    log.missing_module('igraph')

import numpy as np

from lingpy.thirdparty import cogent as cg


def networkx2igraph(graph):
    """Helper function converts networkx graph to igraph graph object."""
    newgraph = ig.Graph(directed=graph.is_directed())
    nodes = {}
    for i,(node, data) in enumerate(graph.nodes(data=True)):
        newgraph.add_vertex(i,Name=node, **dict([(a,b) for a,b in data.items()
            if a not in ['Name','name']]))
        nodes[node] = i
Example #17
0
# imports
from ...settings import rcParams
from lingpy import log

import numpy as np

try:
    import matplotlib.pyplot as plt
    import matplotlib.patches as mplPatches
except:
    log.missing_module('matplotlib')

try:
    import networkx as nx
except:
    log.missing_module('networkx')

from .convex_hull import *


def perp(a):
    """
    code for intersection taken from http://stackoverflow.com/questions/3252194/numpy-and-line-intersections
    """
    b = np.empty_like(a)
    b[0] = -a[1]
    b[1] = a[0]
    return b


# line segment a given by endpoints a1, a2
Example #18
0
"""
Adapting specific cluster algorithms from scikit-learn to LingPy.
"""
from lingpy import log

try:
    from sklearn import cluster
except ImportError:
    log.missing_module('sklearn')
import numpy as np


def dbscan(
        threshold,
        matrix,
        taxa,
        revert=False,
        min_samples=1):
    """
    Compute DBSCAN cluster analysis.
    """
    if not taxa:
        taxa = list(range(1, len(matrix) + 1))

    core_samples, labels = cluster.dbscan(
        matrix, eps=threshold, min_samples=min_samples, metric='precomputed')

    # change to our internal cluster style
    idx = max(labels) + 1
    if idx == 0:
        idx += 1