示例#1
0
def clustering_api(engines, api=None, optionables=None, prefix="clustering"):
        
    def clustering_engine(optionables):
        """ Return a default engine over a lexical graph
        """
        # setup
        engine = Engine("gbuilder", "clustering", "labelling")
        engine.gbuilder.setup(in_name="request", out_name="graph", hidden=True)
        engine.clustering.setup(in_name="graph", out_name="clusters")
        engine.labelling.setup(in_name="clusters", out_name="clusters", hidden=True)

        engine.gbuilder.set(engines.edge_subgraph) 
        engine.clustering.set(*optionables)

        ## Labelling
        from cello.clustering.labelling.model import Label
        from cello.clustering.labelling.basic import VertexAsLabel, TypeFalseLabel, normalize_score_max

        def _labelling(graph, cluster, vtx):
            return  Label(vtx["uuid"], score=1, role="default")
        
        labelling = VertexAsLabel( _labelling ) | normalize_score_max
        engine.labelling.set(labelling)

        return engine
        
    if api is None:
        api = ReliureAPI(name,expose_route = False)
        
    ## Clustering
    from cello.graphs.transform import EdgeAttr
    from cello.clustering.common import Infomap, Walktrap
    # weighted
    walktrap = Walktrap(weighted=True)
    walktrap.name = "Walktrap"
    infomap = Infomap(weighted=True) 
    infomap.name = "Infomap"

    DEFAULTS = [walktrap, infomap]

    if optionables == None : optionables = DEFAULTS
    
    view = EngineView(clustering_engine(optionables))
    view.set_input_type(EdgeList())
    view.add_output("clusters", export_clustering,  vertex_id_attr='uuid')

    api.register_view(view, url_prefix=prefix)
    return api
示例#2
0
def clustering_api(graphdb,
                   engines,
                   api=None,
                   optionables=None,
                   prefix="clustering"):
    def clustering_engine(optionables):
        """ Return a default engine over a lexical graph
        """
        # setup
        engine = Engine("gbuilder", "clustering")
        engine.gbuilder.setup(in_name="request", out_name="graph", hidden=True)
        engine.clustering.setup(in_name="graph", out_name="clusters")

        engine.gbuilder.set(engines.edge_subgraph)
        engine.clustering.set(*optionables)

        return engine

    if api is None:
        api = ReliureAPI(name, expose_route=False)

    ## Clustering
    from cello.graphs.transform import EdgeAttr
    from cello.clustering.common import Infomap, Walktrap
    # weighted
    walktrap = Walktrap(weighted=True)
    walktrap.name = "Walktrap"
    infomap = Infomap(weighted=True)
    infomap.name = "Infomap"

    DEFAULTS = [walktrap, infomap]

    if optionables == None: optionables = DEFAULTS

    from pdgapi.explor import EdgeList
    view = EngineView(clustering_engine(optionables))
    view.set_input_type(EdgeList())
    view.add_output("clusters", export_clustering, vertex_id_attr='uuid')
    api.register_view(view, url_prefix=prefix)

    # cluster labels
    view = EngineView(clusters_labels_engine(graphdb))
    view.set_input_type(Clusters())
    view.add_output("labels", lambda e: e)
    api.register_view(view, url_prefix="labels")

    return api
示例#3
0
def lexical_bigraph_engine(graph):
    """ Return a default engine over a lexical *bipartite* graph
    """
    # setup
    engine = Engine()
    engine.requires("search", "clustering", "labelling", "layout")
    engine.search.setup(in_name="query", out_name="graph")
    engine.clustering.setup(in_name="graph", out_name="clusters")
    engine.labelling.setup(in_name="clusters",
                           out_name="clusters",
                           hidden=True)
    engine.layout.setup(in_name="graph", out_name="layout")

    ## Search
    from cello.graphs.extraction import VtxMatch, VertexIds
    from cello.graphs.extraction import ProxMarkovExtractionGlobalBigraph
    from cello.graphs.builder import Subgraph
    #HACK remove the "id" attribute (if any), it enter in conflict when exporting subgraphs to client
    if 'id' in graph.vs.attributes():
        del graph.vs['id']
    graph_search = VtxMatch(graph, attr_list=[u"label"], default_attr=u"label")
    graph_search |= ProxMarkovExtractionGlobalBigraph(graph)
    graph_search |= Subgraph(graph, score_attr="prox")
    graph_search.name = "ProxSearch"

    #TODO: add better color to vtx
    from cello.graphs.transform import VtxAttr
    graph_search |= VtxAttr(color=[
        (45, 200, 34),
    ])

    #graph_search.change_option_default("vcount", 50)
    engine.search.set(graph_search)

    ## Clustering
    from cello.graphs.transform import EdgeAttr
    from cello.clustering.common import Infomap, Walktrap
    #RMQ infomap veux un pds, donc on en ajoute un bidon
    walktrap = EdgeAttr(weight=1.) | Walktrap()
    infomap = EdgeAttr(weight=1.) | Infomap()
    engine.clustering.set(walktrap, infomap)

    ## Labelling
    from cello.clustering.labelling.model import Label
    from cello.clustering.labelling.basic import VertexAsLabel
    engine.labelling.set(
        VertexAsLabel(
            lambda graph, cluster, vtx: Label(vtx["label"], role="default")))

    ## Layout
    from cello.layout.simple import KamadaKawaiLayout
    from cello.layout.proxlayout import ProxBigraphLayoutPCA
    from cello.layout.transform import Shaker
    engine.layout.set(
        ProxBigraphLayoutPCA(dim=3) | Shaker(),
        KamadaKawaiLayout(dim=3),
    )
    return engine
示例#4
0
def unipartite_clustering_methods():
    """ Returns standart clustering method for unipartite graphs
    
    >>> methods = unipartite_clustering_methods()
    >>> len(methods)
    2
    """
    methods = []
    methods.append(Infomap())
    methods.append(Walktrap())
    return methods
示例#5
0
def lexical_graph_engine(graph):
    """ Return a default engine over a lexical graph
    """
    # setup
    engine = Engine()
    engine.requires("search", "clustering", "labelling", "layout")
    engine.search.setup(in_name="query", out_name="graph")
    engine.clustering.setup(in_name="graph", out_name="clusters")
    engine.labelling.setup(in_name="clusters", out_name="clusters", hidden=True)
    engine.layout.setup(in_name="graph", out_name="layout")

    ## Search
    from cello.graphs.extraction import VtxMatch, ProxMarkovExtractionGlobal, VertexIds
    from cello.graphs.builder import Subgraph
    #HACK remove the "id" attribute (if any), it enter in conflict when exporting subgraphs to client
    if 'id' in graph.vs.attributes():
        del graph.vs['id']
    

    # Pour avoir une recherche "_all"
    # http://localhost:5000/verb/q/_all
    @Composable
    def nothing_if_all(query):
        """ Make the query be nothing if it is '_all'
        """
        if query in ("_all", None):
            return ""   #Note: p0 to [] make start from all vertices
        return query

    # real match search
    match = VtxMatch(graph, attr_list=[u"label"], default_attr=u"label")

    graph_search = nothing_if_all | match
    graph_search |= ProxMarkovExtractionGlobal(graph)
    graph_search |= Subgraph(graph, score_attr="prox", gdeg_attr="gdeg")
    graph_search |= ProxColors(graph, graph['vertices_color'], length=5)
    graph_search.name = "ProxSearch"

    graph_search.change_option_default("vcount", 50)
    engine.search.set(graph_search)

    ## Clustering
    from cello.graphs.transform import EdgeAttr
    from cello.clustering.common import Infomap, Walktrap
    #RMQ infomap veux un pds, donc on en ajoute un bidon
    walktrap = EdgeAttr(weight=1.) |Walktrap()
    infomap = EdgeAttr(weight=1.) | Infomap()
    engine.clustering.set(walktrap, infomap)

    ## Labelling
    from cello.clustering.labelling.model import Label
    from cello.clustering.labelling.basic import VertexAsLabel
    vertex_from_vtx = lambda graph, cluster, vtx: Label(vtx["label"], role="default", score=vtx["gdeg"])
    engine.labelling.set(VertexAsLabel(vertex_from_vtx))

    ## Layout
    from cello.layout.simple import KamadaKawaiLayout
    from cello.layout.proxlayout import ProxLayoutRandomProj
    from cello.layout.proxlayout import ProxLayoutPCA
    from cello.layout.transform import Shaker
    engine.layout.set(
        ProxLayoutPCA(dim=3) | Shaker(),
        KamadaKawaiLayout(dim=3),
    )
    return engine
示例#6
0
def engine(index):
    """ Return a default engine over a lexical graph
    """
    # setup
    from reliure.engine import Engine

    engine = Engine("graph", "clustering", "labelling", "layout")
    engine.graph.setup(in_name="query", out_name="graph")
    engine.clustering.setup(in_name="graph", out_name="clusters")
    engine.labelling.setup(in_name="clusters",
                           out_name="clusters",
                           hidden=True)
    engine.layout.setup(in_name="graph", out_name="layout")

    ## Search
    def tmuse_subgraph(query, length=50):
        return tmuse.subgraph(index, query, length=length)

    graph_search = Optionable("GraphSearch")
    graph_search._func = Composable(tmuse_subgraph)
    graph_search.add_option("length", Numeric(vtype=int, default=50))

    from cello.graphs.transform import VtxAttr
    graph_search |= VtxAttr(color=[
        (45, 200, 34),
    ])
    graph_search |= VtxAttr(type=1)

    engine.graph.set(graph_search)

    ## Clustering
    from cello.graphs.transform import EdgeAttr
    from cello.clustering.common import Infomap, Walktrap
    #RMQ infomap veux un pds, donc on en ajoute un bidon
    walktrap = EdgeAttr(weight=1.) | Walktrap()
    infomap = EdgeAttr(weight=1.) | Infomap()
    engine.clustering.set(infomap, walktrap)

    ## Labelling
    from cello.clustering.labelling.model import Label
    from cello.clustering.labelling.basic import VertexAsLabel, TypeFalseLabel, normalize_score_max

    def _labelling(graph, cluster, vtx):
        score = TypeFalseLabel.scoring_prop_ofclust(graph, cluster, vtx)
        return Label(vtx["form"], score=score, role="default")

    labelling = VertexAsLabel(_labelling) | normalize_score_max
    engine.labelling.set(labelling)

    ## Layout
    from cello.layout.simple import KamadaKawaiLayout
    #from cello.layout.proxlayout import ProxLayoutRandomProj
    from cello.layout.proxlayout import ProxLayoutPCA
    from cello.layout.transform import Shaker
    from cello.layout.transform import ByConnectedComponent, normalise

    default_layout = ProxLayoutPCA(dim=3,
                                   name="ProxPca3d") | Shaker(kelastic=.9)
    default_layout = KamadaKawaiLayout(
        dim=3, name="KamadaKawaiLayout") | Shaker(kelastic=.9)

    engine.layout.set(
        ByConnectedComponent(default_layout) | normalise,
        #KamadaKawaiLayout(dim=3, name="KamadaKawai3D"),
        #ProxLayoutPCA(dim=2, name="ProxPca2d") | Shaker(kelastic=1.8),
        #KamadaKawaiLayout(dim=2, name="KamadaKawai2D")
    )
    return engine