MINI MINI MANI MO

Path : /opt/oracle/product/18c/dbhomeXE/md/property_graph/pyopg/pyopg/
File Upload :
Current File : //opt/oracle/product/18c/dbhomeXE/md/property_graph/pyopg/pyopg/janalyst.py

from pgx_proxies import *
"""
Java analyst class wrapper
Copyright 2015, Oracle and/or its affiliates. All rights reserved.
"""
def _fetch_promise(promise):
    try:
        return promise.get()
    except Exception as e:
        print e
        return None

class Janalyst():
    analyst_context = None
    def __init__(self, pgxGraph, a):
        self.graph = pgxGraph
        self.analyst_context = a

    def undirect(self):
        """
        Returns a new analyst with an undirected version of the graph
        """
        new_graph = self.graph.undirect()
        return Janalyst(new_graph, self.analyst_context)

    def pgx_graph(self):
        """
        Returns a PgxGraph object for futher manipulation
        """
        return self.graph

    def adamicAdar(self):
        """
        computes 'Adamic-Adar measure' for each edge in graph.
        """
        promise = self.analyst_context.adamicAdarCountingAsync(self.graph)
        return PropertyProxy(_fetch_promise(promise))

    def countTriangles(self):
        """
        Counts the number of 'triads' in the given undirected graph
        """
        promise = self.analyst_context.countTrianglesAsync(self.graph, True)
        return _fetch_promise(promise)

    def eigenvectorCentrality(self, max=100, maxDiff=0.01, useL2Norm=True, useInEdge=True):
        """
        compute eigenvector centrality using power iteration (with L1 norm).
        """
        promise = self.analyst_context.eigenvectorCentralityAsync(self.graph, max, maxDiff, useL2Norm, useInEdge)
        return PropertyProxy(_fetch_promise(promise))

    def betweenness(self, k=10):
        """
        approximate node betweenness centrality (without considering edge length).
        """
        promise = self.analyst_context.approximateNodeBetweennessCentralityAsync(self.graph, k)
        return PropertyProxy(_fetch_promise(promise))

    def closenessCentrality(self, costPropertyName):
        """
        compute closed centrality.
        """
        promise = self.analyst_context.closenessCentralityDoubleLengthAsync(self.graph, costPropertyName)
        return PropertyProxy(_fetch_promise(promise))

    def closenessCentralityUnit(self):
        """
        compute closed centrality.
        """
        promise = self.analyst_context.closenessCentralityUnitLengthAsync(self.graph)
        return PropertyProxy(_fetch_promise(promise))

    def communities(self):
        """
        detect communities using parallel label propagation.
        """
        promise = self.analyst_context.communitiesLabelPropagationAsync(self.graph)
        return ComponentProxy(_fetch_promise(promise))

    def stronglyConnected(self, algo="tarjan"):
        """
        Find strongly connected components using Tarjan's or Kosaraju's algorithm.
        """
        promise = None
        if algo == "tarjan":
            promise = self.analyst_context.sccTarjanAsync(self.graph)
        else:
            promise = self.analsyt_context.sccKosarajuAsync(self.graph)

        return ComponentProxy(_fetch_promise(promise))

    def shortestPath(self, src, dst, cost="weight", algo="dijkstra", filter=""):
        """
        Compute shortest path using Dijkstra's algorithm.
        algo=dijkstra_bi yeilds a bi-directional variane
        algo=dijkistra_filter allows pasing a filter expression
        """
        promise = None

        if algo == "dijkstra":
            promise = self.analyst_context.shortestPathDijkstraAsync(self.graph, src.getId(), dst.getId(), cost)

        elif algo == "dijkstra_bi":
            promise = self.analyst_context.shortestPathDijkstraBidirectionalAsync(self.graph, src.getId(), dst.getId(), cost)

        elif algo == "dijkstra_filter":
            promise = self.analyst_context.shortestPathDijkstraBidirectionalAsync(self.graph, src.getId(), dst.getId(), cost, filter)

        return PathProxy(_fetch_promise(promise))

    def hopDistance(self, src, reverse=False):
        """
        compute hop-distance from given node to every other node time complexity: O(E * d) with E = number of edges, d = diameter of graph
        reverse = True will compute the reverse hop distance
        """
        promise = None

        if not reverse:
            promise = self.analyst_context.shortestPathHopDistAsync(self.graph, src)

        else:
            promise = self.analyst_context.shortestPathHopDistReverseAsync(self.graph, src)

        return AllPathsProxy(_fetch_promise(promise))

    def bellmanFord(self, src, propName, reverse=False):
        """
        compute single source shortest paths using Bellman & Ford algorithm time complexity: O(E * D) with E = number of edges, D = number edges in the shortest length
        """
        promise = None

        if not reverse:
            promise = self.analyst_context.shortestPathBellmanFordAsync(self.graph, src, propName)
        else:
            promise = self.analyst_context.shortestPathBellmanFordReverseAsync(self.graph, src, propName)

        return AllPathsProxy(_fetch_promise(promise))


    def weaklyConnected(self):
        """
        Find weakly connected components through label propagation time complexity: O(E * D)
        """
        promise = self.analyst_context.wccAsync(self.graph)
        return ComponentProxy(_fetch_promise(promise))

    def follow(self, node, max=3):
        """
        Link prediction using Twitter's algorithm
        """
        promise = self.analyst_context.whomToFollow(self.graph, node, max)
        return CollectionProxy(_fetch_promise(promise))

    def pagerank(self, e=1e-16, d=0.85, max=150):
        """
        Classic pagerank algorithm.
        """
        promise = self.analyst_context.pagerankAsync(self.graph, e,d,max)
        return PropertyProxy(_fetch_promise(promise))

    def personalPageRank(self, v, e=1e-16, d=0.85, max=150):
        """
        personalized pagerank (random walk with restart) evaluates relative importance of nodes in a graph with respect to a given node v.
        """
        promise = self.analyst_context.personalizedPagerankAsync(self.graph, v, e, d, max)
        return PropertyProxy(_fetch_promise(promise))

    def outDegreeDistribution(self):
        """
        computes the outdegree distribution of the given graph and stores it in the map.
        """
        promise = self.analyst_context.outDegreeDistributionAsync(self.graph)
        return _fetch_promise(promise)

    def inDegreeDistribution(self):
        """
        computes the indegree distribution of the given graph and stores it in the map.
        """
        promise = self.analyst_context.inDegreeDistributionAsync(self.graph)
        return _fetch_promise(promise)

    def outDegreeCentrality(self):
        """
        compute out-degree centrality.
        """
        promise = self.analyst_context.outDegreeCentralityAsync(self.graph)
        return PropertyProxy(_fetch_promise(promise))

    def inDegreeCentrality(self):
        """
        compute in-degree centrality.
        """
        promise = self.analyst_context.inDegreeCentralityAsync(self.graph)
        return PropertyProxy(_fetch_promise(promise))

    def hits(self, max=150):
        """
        classic HITS algorithm (Hyperlink-Induced Topic Search)
        """
        promise = self.analyst_context.hitsAsync(self.graph, max)
        return HitsProxy(_fetch_promise(promise))

    def fattestPath(self, src, capacityName):
        """
        Fattest Tree Algorithm - Computes the fattest path from a source node to all nodes in the graph.
        """
        promise = self.analyst_context.fattestPathAsync(self.graph, src, capacityName)
        return AllPathsProxy(_fetch_promise(promise))

    def getRandomNode(self):
        """
        gets a random Node from the analyst
        """
        return _fetch_promise(self.analyst_context.getRandomNodeAsync(self.graph))

    def mstPrim(self, src, costName):
        """
        Computes a minimum spanning tree
        """
        promise = self.analyst_context.mstPrimAsync(self.graph, src, costName)
        return _fetch_promise(promise)

    def partitionConductance(self, components):
        """
        compute average conductance among all partitions Note: this algorithm is in-efficient if numComponents is big
        """
        promise = self.analyst_context.partitionConductanceAsync(self.graph, components)
        return ConductanceProxy(_fetch_promise(promise))

    def partitionModularity(self, components):
        """
        compute modularity of partitions
        """
        promise = self.analyst_context.partitionModularityAsync(self.graph, components)
        return _fetch_promise(components)

OHA YOOOO