Example #1
0
 def graph(self) -> Optional[Digraph]:
     graph = Digraph(comment="AST")
     if self.ast is not None:
         ParseTreeWalker().walk(self._GraphBuilder(graph), self.ast)
         return graph
     return None
def graphviz_visualization(activities_count,
                           dfg,
                           image_format="png",
                           measure="frequency",
                           max_no_of_edges_in_diagram=170,
                           start_activities=None,
                           end_activities=None):
    """
    Do GraphViz visualization of a DFG graph

    Parameters
    -----------
    activities_count
        Count of attributes in the log (may include attributes that are not in the DFG graph)
    dfg
        DFG graph
    image_format
        GraphViz should be represented in this format
    measure
        Describes which measure is assigned to edges in direcly follows graph (frequency/performance)
    max_no_of_edges_in_diagram
        Maximum number of edges in the diagram allowed for visualization

    Returns
    -----------
    viz
        Digraph object
    """
    if start_activities is None:
        start_activities = []
    if end_activities is None:
        end_activities = []

    filename = tempfile.NamedTemporaryFile(suffix='.gv')
    viz = Digraph("",
                  filename=filename.name,
                  engine='dot',
                  graph_attr={'bgcolor': 'transparent'})

    # first, remove edges in diagram that exceeds the maximum number of edges in the diagram
    dfg_key_value_list = []
    for edge in dfg:
        dfg_key_value_list.append([edge, dfg[edge]])
    dfg_key_value_list = sorted(dfg_key_value_list,
                                key=lambda x: x[1],
                                reverse=True)
    dfg_key_value_list = dfg_key_value_list[
        0:min(len(dfg_key_value_list), max_no_of_edges_in_diagram)]
    dfg_allowed_keys = [x[0] for x in dfg_key_value_list]
    dfg_keys = list(dfg.keys())
    for edge in dfg_keys:
        if edge not in dfg_allowed_keys:
            del dfg[edge]

    # calculate edges penwidth
    penwidth = assign_penwidth_edges(dfg)
    activities_in_dfg = set()
    activities_count_int = copy(activities_count)
    ackeys = copy(list(activities_count_int.keys()))

    for edge in dfg:
        activities_in_dfg.add(edge[0])
        activities_in_dfg.add(edge[1])
    """for act in ackeys:
        if act not in activities_in_dfg:
            del activities_count_int[act]"""

    # assign attributes color
    activities_color = get_activities_color(activities_count_int)

    # represent nodes
    viz.attr('node', shape='box')

    if len(activities_in_dfg) == 0:
        activities_to_include = set(activities_count_int)
    else:
        activities_to_include = set(activities_in_dfg)

    activities_map = {}

    for act in activities_to_include:
        if "frequency" in measure and act in activities_count_int:
            viz.node(str(hash(act)),
                     act + " (" + str(activities_count_int[act]) + ")",
                     style='filled',
                     fillcolor=activities_color[act])
            activities_map[act] = str(hash(act))
        else:
            viz.node(str(hash(act)), act)
            activities_map[act] = str(hash(act))

    # represent edges
    for edge in dfg:
        if "frequency" in measure:
            label = str(dfg[edge])
        else:
            label = human_readable_stat(dfg[edge])
        viz.edge(str(hash(edge[0])),
                 str(hash(edge[1])),
                 label=label,
                 penwidth=str(penwidth[edge]))

    start_activities_to_include = [
        act for act in start_activities if act in activities_map
    ]
    end_activities_to_include = [
        act for act in end_activities if act in activities_map
    ]

    if start_activities_to_include:
        viz.node("@@startnode",
                 "@@S",
                 style='filled',
                 shape='circle',
                 fillcolor="#32CD32",
                 fontcolor="#32CD32")
        for act in start_activities_to_include:
            viz.edge("@@startnode", activities_map[act])

    if end_activities_to_include:
        viz.node("@@endnode",
                 "@@E",
                 style='filled',
                 shape='circle',
                 fillcolor="#FFA500",
                 fontcolor="#FFA500")
        for act in end_activities_to_include:
            viz.edge(activities_map[act], "@@endnode")

    viz.attr(overlap='false')
    viz.attr(fontsize='11')

    viz.format = image_format

    return viz
Example #3
0
def main():
    parser = argparse.ArgumentParser(
        description="MP3-treesim tree generation tool")
    parser.add_argument(
        "-n",
        "--nodes",
        dest="n",
        help="Total number of nodes (internal nodes and leafs) [Default: 10]",
        metavar='N',
        type=int,
        action="store",
        default=10)
    parser.add_argument("-l",
                        "--labels",
                        dest="l",
                        help="Total number of labels [Default: 10]",
                        metavar='L',
                        type=int,
                        action="store",
                        default=10)
    parser.add_argument("-s",
                        "--sons",
                        dest="s",
                        help="Maximum number of sons for node [Default: 3]",
                        metavar='S',
                        type=int,
                        action="store",
                        default=3)
    parser.add_argument("-f",
                        "--full",
                        dest="full",
                        help="Generate a complete tree [Default: false]",
                        action="store_true",
                        default=False)
    parser.add_argument("-o",
                        "--out",
                        dest="out",
                        help="Output prefix [Default: out]",
                        metavar="OUT",
                        type=str,
                        action="store",
                        default="out")
    args = parser.parse_args()

    tot_nodes = args.n
    max_sons = args.s
    n_labels = args.l
    out_prefix = args.out
    full = args.full

    g = Digraph('G', filename=out_prefix + ".gv")

    labels = list(string.ascii_uppercase)
    i = 1
    while n_labels > len(labels):
        labels += [x * i for x in list(string.ascii_uppercase)]
        i += 1
    reserved_labels = labels[0:tot_nodes]
    random.shuffle(reserved_labels)
    additional_labels = labels[tot_nodes:tot_nodes + n_labels - tot_nodes]
    nodes2addlabel = {}
    for label in additional_labels:
        node_idx = random.choice(list(range(1, tot_nodes + 1)))
        nodes2addlabel[
            node_idx] = label if node_idx not in nodes2addlabel else nodes2addlabel[
                node_idx] + "," + label

    added_nodes = 0
    label = "root"
    g.node(str(added_nodes), label)
    last_nodes = [added_nodes]
    added_nodes += 1

    while added_nodes != tot_nodes:
        father = last_nodes.pop(0)
        n_sons = max_sons if args.full else random.randint(1, max_sons)
        for i in range(0, n_sons):
            if added_nodes == tot_nodes:
                break
            son_label = reserved_labels[added_nodes - 1]
            if added_nodes in nodes2addlabel:
                son_label += "," + nodes2addlabel[added_nodes]
            g.node(str(added_nodes), son_label)
            g.edge(str(father), str(added_nodes))
            last_nodes.append(added_nodes)
            added_nodes += 1

    g.render()
Example #4
0
    def __funcion_TS():

        ast = Digraph('AST',
                      filename='c:/source/ast.gv',
                      node_attr={
                          'color': 'black',
                          'fillcolor': 'white',
                          'style': 'filled',
                          'shape': 'record'
                      })
        ast.attr(rankdir='TB', ordering='in')
        ast.edge_attr.update(arrowhead='none')

        clus = 'cluster_'
        c_clus = 0
        con = 0
        tag = "t"
        for i in principal.tablaSimbolos.tabla:

            base = principal.tablaSimbolos.tabla[i]

            cl = clus + str(c_clus)
            with ast.subgraph(name=cl) as c:
                c.attr(label="NOMBRE BD: '%s'\\nOWNER: '%s'\\nMODE: '%s'" %
                       (base.nombre, base.owner.valor, base.mode))
                c_clus += 1

                for j in base.tablas:

                    tabla = base.tablas[j]
                    label = ""

                    for k in tabla.columnas:

                        column = tabla.columnas[k]

                        label += "| COLUMNA: " + str(
                            column.nombre) + " | { Tipo | " + str(
                                column.tipo
                            ) + " } | { Llave Primaria | " + str(
                                column.primary_key
                            ) + " } | { LLave Foránea | " + str(
                                column.foreign_key) + " } | { Unique | " + str(
                                    column.unique) + " } | { Default | " + str(
                                        column.default
                                    ) + " } | { Null | " + str(
                                        column.null
                                    ) + " } | { Check |  " + str(
                                        column.check
                                    ) + "  } | { Index | " + str(
                                        column.index) + " } "

                    label2 = label
                    label3 = "| INDICES: " + str(len(tabla.indices))

                    for l in range(len(tabla.indices)):

                        indice = tabla.indices[l]

                        label3 += "| { NOMBRE | " + str(
                            indice.nombre) + " } | { TIPO | " + str(
                                indice.tipo) + " } | { COLUMNA(s) | " + str(
                                    indice.columnas) + " } | { ORDEN | " + str(
                                        indice.orden
                                    ) + " } | { NULLS FIRST | " + str(
                                        indice.null_first
                                    ) + " } | { NULLS LAST | " + str(
                                        indice.null_last
                                    ) + " } | { LOWER | " + str(
                                        indice.lower
                                    ) + " } | { WHERE | " + str(
                                        indice.condicion
                                    ) + " } | { UNIQUE | " + str(
                                        indice.unique) + " } "

                    label4 = label3

                    t = tag + str(con)
                    c.node(
                        t,
                        "{ NOMBRE TABLA: " + str(tabla.nombre) + "\\nPADRE: " +
                        str(tabla.padre) + label2 + label4 + " }")
                    con += 1

        ast.render('tablaS', format='png', view=True)
def generar_arbol(datos, numero):
    terminales, _ = obtener_terminales(gramatica_individual)
    nuevos_datos = []
    des = False
    last_long = 0
    for i in range(len(datos)):
        if (datos[i] in [[], '']):
            last_long = len(datos[i])
            continue
        if (i > 0):
            if (len(datos[i]) < last_long):
                des = True

                print("here", len(datos[i]), last_long)
                last_long = len(datos[i])
            else:
                des = False
        if (des and i + 1 < len(datos)):
            if (datos[i - 1][len(datos[i - 1]) - 1] in terminales
                    and len(datos[i + 1]) < len(datos[i])):
                el = nuevos_datos.pop()
                print("eliminando", el)
            else:
                print("terminal", nuevos_datos[len(nuevos_datos) - 1])
        nuevos_datos.append(datos[i])
        last_long = len(datos[i])
    for i in nuevos_datos:
        print(i)
    #datos = nuevos_datos
    g = Digraph('structs',
                filename='structs_revisited%i.gv' % (numero),
                node_attr={'shape': 'record'})
    #s.graph_attr.update(rank='min')
    ns = 1
    rank = 0
    ultima_fila = [datos[0][0]]
    nodos_ultima_fila = []
    conexiones = []
    nodos_ultima_fila.append('struct%i' % (ns))
    g.node('struct%i' % (ns), r'{%s}' % (datos[0][0]))
    ns = 2
    niveles = []
    terminales, _ = obtener_terminales(gramatica_individual)
    for fila in datos[1:]:
        if (len(fila) >= len(ultima_fila)):
            comienzo = len(ultima_fila)
            #if(comienzo > 0):comienzo-=1
            #if(ultima_fila[comienzo] != fila[comienzo]): comienzo -= 1
            try:
                nodo = nodos_ultima_fila.pop()
                print(nodo)
                niveles.append(fila)
                for ij in range(comienzo - 1, len(fila)):
                    print(terminales)
                    if (fila[ij] in terminales):
                        #if(fila[ij] in)
                        print("TIPOS")
                        print(fila[ij], ultima_fila[len(ultima_fila) - 1])
                        print(
                            obtener_tipos(gramatica_individual,
                                          ultima_fila[len(ultima_fila) - 1]))

                        if (fila[ij] not in obtener_tipos(
                                gramatica_individual,
                                ultima_fila[len(ultima_fila) - 1])):
                            ultima_fila = fila
                            nodos_ultima_fila.append('struct%i' % (ns - 1))
                            continue
                        g.node('struct%i' % (ns), r'{%s}' % (fila[ij]))
                    else:
                        g.node('struct%i' % (ns), r'{%s|valor}' % (fila[ij]))
                    print("declaracion nodo", 'struct%i' % (ns), fila[ij])
                    conexiones.append((nodo, 'struct%i' % (ns)))
                    nodos_ultima_fila.append('struct%i' % (ns))
                    ns += 1
                rank += 1
            except:
                pass
        else:
            try:
                nodos_ultima_fila.pop()
            except:
                pass
        print(nodos_ultima_fila)
        ultima_fila = fila
    #g.graph_attr["rankdir"] = "TB"
    #g.graph_attr.update(rank='min')
    g.edges(conexiones)
    g.view()
Example #6
0
    def makeGrapheviz(self,
                      num,
                      intersection=True,
                      mode='full',
                      render="str",
                      prob=None):
        """construction de l'arbre pondéré 

        partant de la variable 1 ou 2
        1: a puis b
        2: b puis a

        :param intersection: indique si on remplit la probabilité de
            l'intersection en bout de branche quand elle est connue
        :type intersection: bool
        :param mode: in ['full', 'partial'] full: indique si on doit afficher 
             toutes les probas d'intersection 
             (éventuellement avec des … de recherche)
             partial: on n'affiche que les intersections connues, 
             et rien ailleurs
        :param prob: dict des probabilités
        :type prob: dict
        :rtype: None
        """
        dot = Digraph(comment="arbre pondéré",
                      node_attr={"shape": "plaintext"})
        dot.attr("graph", rankdir="LR", splines="line")
        dot.format = "png"  # "svg"

        # homogeneite level 0
        z = symbols('z')
        z.name = "."
        level = {1: ["a", "ca"], 2: ["b", "cb"]}
        # création des sous-graphes par niveau
        N = 0
        with dot.subgraph(name=f"cluster_{N}", node_attr={'rank':
                                                          'same'}) as c:
            c.attr(style="invis")  # désactivation du cadre de cluster
            c.node("&#160;")
        N = 1
        with dot.subgraph(name=f"cluster_{N}", node_attr={'rank':
                                                          'same'}) as c:
            c.attr(style="invis")  # désactivation du cadre de cluster
            for e in level[num]:
                c.node(e, f"<{latex(self.titres[e])}>")
                # la str html doit être encadrée de <>
        N = 2
        #L2 = list()
        with dot.subgraph(name=f"cluster_{N}", node_attr={'rank':
                                                          'same'}) as c:
            c.attr(style="invis")  # désactivation du cadre de cluster
            for e in level[num]:
                for f in level[(3 - num)]:
                    nom = f"{f}_{e}"
                    tmp = (e + f if e + f in self.probas else f + e)
                    # formatage de la proba de l'intersection
                    if self.nbformat == 'pourcentage' and prob[tmp] != "…":
                        if tmp in self.known_names:  #le param doit être un float
                            preci = len(
                                str(float(prob[tmp])).partition(".")[2])
                            p = (f"{{:#.{preci-2}%}}").format(float(prob[tmp]))
                        else:
                            p = (f"{{:#.{2*self.precision-2}%}}").format(
                                float(prob[tmp]))
                    elif self.nbformat == 'decimal' and prob[tmp] != "…":
                        if tmp in self.known_names:
                            preci = len(str(float(prob[tmp])).strip(".")[2])
                            p = (f"{{:#.{preci}f}}").format(float(prob[tmp]))
                        else:
                            p = (f"{{:#.{2*self.precision}f}}").format(
                                float(prob[tmp]))
                    elif self.nbformat == 'decimal' and prob[tmp] != "…":
                        p = str(prob[tmp])
                    elif self.nbformat == 'fraction':
                        p = str(prob[tmp])
                    else:
                        p = "…"

                    if mode == 'partial' and prob[
                            tmp] != "…" and tmp in self.known_names:
                        LAB = '<<TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="0" CELLSPACING="4"><TR><TD>{}</TD><TD>{}</TD></TR></TABLE>>'
                        c.node(nom, LAB.format(self.titres[f], p))
                    elif intersection and mode == 'full':
                        LAB = '<<TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="0" CELLSPACING="4"><TR><TD>{}</TD><TD>{}</TD></TR></TABLE>>'
                        c.node(nom, LAB.format(self.titres[f], p))
                    else:  # une case sans la proba d'intersection
                        c.node(
                            nom,
                            '<<TABLE ALIGN="LEFT" BORDER="0" CELLBORDER="0" CELLSPACING="4"><TR><TD>{}</TD><TD>&#160;&#160;&#160;&#160;</TD></TR></TABLE>>'
                            .format(self.titres[f]))
        # edges
        for e in level[num]:
            dot.edge("&#160;",
                     e,
                     label=f"{self.r[render](prob[e])}",
                     arrowhead="none")
        for c in [(level[num], level[3 - num])]:
            for a in c[0]:
                for b in c[1]:
                    nom = f"{b}_{a}"
                    dot.edge(a,
                             nom,
                             label=f"{self.r[render](prob[nom])}",
                             arrowhead="none")
        self.gv = dot
Example #7
0
stariFinale = list(dict.fromkeys(stariFinale))


f = open("rezultat.txt","w")

f.write("AFD pentru ~AFD-lambda este:\n")
f.write("stareinitiala: " + str(nodurifinale[0]) + "\n")
f.write("starifinale: "+ str(stariFinale)+ "\n")

for i in range(len(nodurifinale)):
    for j in range(len(relatiifinale[i])):
        if relatiifinale[i][j] != ():
            f.write(str(nodurifinale[i])+" ---> " + str(relatiifinale[i][j])+" cu " + str(alfabet[j])+ "\n")
f.close()

f = Digraph('fnd', filename='fsm.gv')
f.attr(rankdir='LR', size='4')

if nodurifinale[0] in stariFinale:
    f.attr('node', shape='doublecircle', fillcolor="green", style="filled")
    f.node(str(nodurifinale[0]))
else:
    f.attr('node', shape='circle', fillcolor="green", style="filled")
    f.node(str(nodurifinale[0]))

f.attr('node', shape='doublecircle', fillcolor="white", style="filled")

for i in range(len(stariFinale)):
    f.node(str(stariFinale[i]))

Example #8
0
with open("input.txt", "r") as f:
    #data = [elem.strip().split('->') for elem in f]
    data = [list(map(lambda x: x.strip(), elem.split('->'))) for elem in f]
    x = np.transpose(data)
x.astype(int)

d = {
    1: 'YAHOO',
    2: 'BING',
    3: 'EBAY',
    4: 'IEEE_XPLORE',
    5: 'EXPEDIA',
    6: 'ALLEGRO',
    7: 'WIKIPEDIA',
    8: 'AMAZON',
    9: 'YOUTUBE',
    10: 'GOOGLE',
    11: 'FACEBOOK'
}

size = (np.size(x, 1) - 1)
f = Digraph('Rank', filename='rank.gv')
f.attr(rankdir='LR', size='8,5')
f.attr('node', shape='circle')

for i in range(size):
    a = x.item((0, i))
    b = x.item((1, i))
    f.edge(d[int(a)], d[int(b)])
f.view()
Example #9
0
# Draw all jobs
nx.draw_networkx_nodes(g, layout, nodelist=jobs, node_color='#cccccc', node_size=100)

# Draw all jobs with most promotional ops
hot_jobs = [Job_1 for Job_1 in jobs if g.degree(Job_1) > 1]
nx.draw_networkx_nodes(g, layout, nodelist=hot_jobs, node_color='orange', node_size=100)

nx.draw_networkx_edges(g, layout, width=1, edge_color="#cccccc")

node_labels = dict(zip(promotions, promotions))
nx.draw_networkx_labels(g, layout, labels=node_labels)

plt.axis('off')
plt.title("Promotions")
plt.show()

# In[ ]:


from graphviz import Digraph
dot = Digraph(comment='Promotions')

for index, row in job_graph.iterrows():
    dot.edge(str(row["Job_1"]), str(row["Job_2"]), label='')

dot


# # More to come!
Example #10
0
            test_cases += [case]
    attrs = []
    attr_vals = []
    for i in range(0, len(train_cases[0][0])):
        attrs += [i]
        attr_vals += [set()]

    # Determine possible attribute values from training data
    for i in range(0, len(train_cases)):
        for j in range(0, len(train_cases[i][0])):
            attr_vals[j] = attr_vals[j] | {train_cases[i][0][j]}

    dt = DTNode(None, attrs, attr_vals, train_cases)

    if (RENDER == True):
        dot = Digraph(comment='Decision Tree')
        dot.node('0', str(dt.attr + 1))
        dt.make_graph(dot, '0')
        dot.render('decision_tree.gv', view=True)

    correct = 0
    for i in range(0, len(train_cases)):
        if (edible(train_cases[i]) == dt.classify(train_cases[i][0])):
            correct += 1
    print('Training accuracy: ' + str(correct) + ' / ' +
          str(len(train_cases)) + ' = ' + str(correct / len(train_cases)))
    correct = 0
    for i in range(0, len(test_cases)):
        if (edible(test_cases[i]) == dt.classify(test_cases[i][0])):
            correct += 1
    print('Testing accuracy: ' + str(correct) + ' / ' + str(len(test_cases)) +
Example #11
0
    alphas = torch.nn.functional.softmax(torch.load(alphas_path),
                                         1).data.cpu().numpy()
else:
    op_start = 0
    alphas = torch.nn.functional.softmax(
        torch.load(alphas_path)["alphas"].chunk(2)[int(dataset[-1])],
        1).data.cpu().numpy()

n = int(math.sqrt(2 * alphas.shape[0])) + 1

g = Digraph(format='pdf',
            edge_attr=dict(fontsize='20', fontname="times"),
            node_attr=dict(style='filled',
                           shape='rect',
                           align='center',
                           fontsize='20',
                           height='0.5',
                           width='1.5',
                           penwidth='2',
                           fontname="times"),
            engine='dot')
g.body.extend(['rankdir=LR'])

with g.subgraph(name='child',
                node_attr={
                    'shape': 'box',
                    'height': '0.01',
                    'style': 'invisible'
                }) as c:
    if dataset == "cifar10" or dataset == "cifar100":
        c.edge('none',
Example #12
0
    def render(self):
        diagram = Digraph(name='finite_state_machine',
                          graph_attr={
                              'rankdir': 'LR',
                              'splines': 'polyline'
                          },
                          format='png')

        action_cache = {}
        state_cache = {}
        transition_cache = []

        for state in self.states.all():
            state_cache['s{}'.format(state.pk)] = {
                'name': 's{}'.format(state.pk),
                'label': state.label,
                'initial': state.initial,
                'connections': {
                    'origin': 0,
                    'destination': 0
                }
            }

            for action in state.actions.all():
                if action.has_condition():
                    action_label = '{} {}'.format(SYMBOL_MATH_CONDITIONAL,
                                                  action.label)
                else:
                    action_label = action.label

                action_cache['a{}'.format(action.pk)] = {
                    'name': 'a{}'.format(action.pk),
                    'label': action_label,
                    'state': 's{}'.format(state.pk),
                    'when': action.when,
                }

        for transition in self.transitions.all():
            if transition.has_condition():
                transition_label = '{} {}'.format(SYMBOL_MATH_CONDITIONAL,
                                                  transition.label)
            else:
                transition_label = transition.label

            transition_cache.append({
                'tail_name':
                's{}'.format(transition.origin_state.pk),
                'head_name':
                's{}'.format(transition.destination_state.pk),
                'label':
                transition_label
            })
            state_cache['s{}'.format(
                transition.origin_state.pk
            )]['connections']['origin'] = state_cache['s{}'.format(
                transition.origin_state.pk)]['connections']['origin'] + 1
            state_cache['s{}'.format(transition.destination_state.pk
                                     )]['connections']['destination'] += 1

        for key, value in state_cache.items():
            kwargs = {
                'name':
                value['name'],
                'label':
                value['label'],
                'shape':
                'doublecircle' if value['connections']['origin'] == 0
                or value['connections']['destination'] == 0 or value['initial']
                else 'circle',
                'style':
                'filled' if value['initial'] else '',
                'fillcolor':
                '#eeeeee',
            }
            diagram.node(**kwargs)

        for transition in transition_cache:
            diagram.edge(**transition)

        for key, value in action_cache.items():
            kwargs = {
                'name': value['name'],
                'label': value['label'],
                'shape': 'box',
            }
            diagram.node(**kwargs)
            diagram.edge(
                **{
                    'head_name':
                    '{}'.format(value['name']),
                    'tail_name':
                    '{}'.format(value['state']),
                    'label':
                    'On entry' if value['when'] ==
                    WORKFLOW_ACTION_ON_ENTRY else 'On exit',
                    'arrowhead':
                    'dot',
                    'dir':
                    'both',
                    'arrowtail':
                    'dot',
                    'style':
                    'dashed',
                })

        return diagram.pipe()
Example #13
0
 def __init__(self):
     from graphviz import Digraph
     self.f = Digraph('deps', filename='deps.gv')
     # self.f.attr(rankdir='LR', size='8,5')
     self.f.attr(rankdir='LR', size='6,5')
     self.f.attr('node', shape='circle')
    def plot_model(self,
                   independenciesplot,
                   evidence=None,
                   separations=None,
                   source=None,
                   directed=False,
                   showconnectedness=False):
        from graphviz import Digraph, Graph

        if separations:
            if source and evidence:
                separations = {
                    k: v
                    for k, v in separations.items()
                    if v and k not in evidence and k not in source
                }
            elif evidence:
                separations = {
                    k: v
                    for k, v in separations.items() if v and k not in evidence
                }
            elif source:
                separations = {
                    k: v
                    for k, v in separations.items() if v and k not in source
                }

        if not directed:
            dot = Graph('../graphs/bayesian_net',
                        comment=self.model.name,
                        format="png")
        else:
            dot = Digraph('../graphs/bayesian_net',
                          comment=self.model.name,
                          format="png")

        for state in self.model.states:
            # if present, print source node as filled
            if source and state.name in source:
                dot.node(state.name, state.name, style="filled")
            # if any, print evidence nodes in bold
            elif evidence and state.name in evidence:
                dot.node(state.name, state.name, style="bold")
            # if any, print d-separated nodes in light grey
            elif separations and state.name in separations:
                dot.node(state.name,
                         state.name,
                         style="dotted",
                         color="lightgrey",
                         fontcolor="lightgrey")
            else:
                dot.node(state.name, state.name)

        def arc_label(connectedness):
            return str(round(connectedness, 2))

        def arc_width(connectedness):
            return str(abs(10 * connectedness))

        for parent, child in self.model.edges:
            if separations and (parent.name in separations
                                or child.name in separations):
                dot.edge(parent.name,
                         child.name,
                         style="dotted",
                         color="lightgrey")
            else:
                if showconnectedness:
                    connectedness = self.mutual_information(
                        parent.name, child.name, self.evidence)

                    if connectedness == -1:
                        dot.edge(parent.name, child.name)
                    else:
                        dot.edge(parent.name,
                                 child.name,
                                 label=arc_label(connectedness),
                                 penwidth=arc_width(connectedness))
                else:
                    dot.edge(parent.name, child.name)

        if not independenciesplot:
            # basic plot
            label = r'\n\nBayesian Network structure'
        else:
            # d-separation plot
            if source and evidence:
                label = r"\n\nBayesian Network independencies\nFILLED: query source | BOLD: evidence"
            elif source:
                label = r"\n\nBayesian Network independencies\nFILLED: query source"
            elif evidence:
                label = r"\n\nBayesian Network independencies\nBOLD: evidence"
            else:
                label = r"\n\nBayesian Network independencies\n"
        if showconnectedness:
            label += r"\nMutual information shown on edges (not for variables in evidence)"
        dot.attr(label=label)

        dot.render(view=True)
Example #15
0
#!/usr/bin/env python3
"""
My best friend's sister's boyfriend's brother's girlfriend heard
from this guy who knows this kid who's going with the girl who
saw Ferris pass out at 31 Flavors last night.
"""

from graphviz import Digraph
nodes = ('Simone', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'Ferris')
labels = ("best friend's", "sister's", "boyfriend's", "brother's",
          "girlfriend", "heard from", "knows", "going out with", "saw")

dot = Digraph('Simone')
for k in range(len(nodes) - 1):
    dot.edge(nodes[k], nodes[k + 1], label=' ' + labels[k])

dot.render('simone.gv', view=True)
Example #16
0
    def write(self,
              file_name,
              engine='fdp',
              show_publications=True,
              show_subscriptions=True):
        """ write the graph to a file
            :param engine: graphviz engine
                - fdp works for large graphs
                - neato works better for smaller graphs
                - circo works for single modules
                CLI: fdp graph.fv -Tpdf -o test.pdf
        """

        print('Writing to ' + file_name)

        ratio = 1  # aspect ratio

        modules = self._graph.modules
        topics = self._graph.topics
        topic_colors = self._graph.topic_colors
        module_publications = self._graph.module_publications
        module_subscriptions = self._graph.module_subscriptions

        graph_attr = {
            'splines': 'true',
            'ratio': str(ratio),
            'overlap': 'false'
        }
        graph_attr['sep'] = '"+15,15"'  # increase spacing between nodes
        graph = Digraph(
            comment='autogenerated graph with graphviz using uorb_graph.py',
            engine=engine,
            graph_attr=graph_attr)

        # nodes
        for module in modules:
            graph.node('m_' + module,
                       module,
                       shape='box',
                       fontcolor='#ffffff',
                       style='filled',
                       color='#666666',
                       fontsize='16')

        for topic in topics:
            graph.node('t_' + topic,
                       topic,
                       shape='ellipse',
                       fontcolor='#ffffff',
                       style='filled',
                       color=topic_colors[topic])

        # edges
        if show_publications:
            for module in modules:
                if module in module_publications:
                    for topic in module_publications[module]:
                        if topic in topics:
                            graph.edge('m_' + module,
                                       't_' + topic,
                                       color=topic_colors[topic],
                                       style='dashed')

        if show_subscriptions:
            for module in modules:
                if module in module_subscriptions:
                    for topic in module_subscriptions[module]:
                        if topic in topics:
                            graph.edge('t_' + topic,
                                       'm_' + module,
                                       color=topic_colors[topic])

        graph.render(file_name, view=False)
Example #17
0
def create_topo(root, neigh_list):
    nodes = [root]
    edges = []
    for neighbor in neigh_list:
        nodes.append(neighbor['neighborDevice'])
        edges.append([
            root, neighbor['neighborDevice'],
            neighbor['neighborPort'] + "-" + neighbor['port']
        ])
    return [nodes, edges]


my_topo = create_topo('Arista249', neighbors)

#Creating the Topo graphic
dot = Digraph(comment='My Network')


def make_topology(network_name, mytopo):
    dot = Digraph(comment=network_name, format='png')
    dot.graph_attr['splines'] = 'ortho'
    dot.attr('node', shape='box')
    dot.attr('node', style='filled')
    dot.attr('edge', dir='both')
    dot.attr('edge', arrowsize='2')
    dot.body.append(r'label = "\n\nMy Prettier Network Diagram"')
    dot.body.append('fontsize=20')
    for i in mytopo[0]:
        dot.node(i)
    for i in mytopo[1]:
        dot.edge(i[0], i[1], i[2])
Example #18
0
    def parser(self, data):

        global symtab_strings
        global dynstr_strings
        global sequence_index

        self.endian = 'little'  # before 0x10 it doesnt matter

        self.EI_MAG = self.bytes_to_str(data, 1, 3, self.endian)
        firstbyte = self.bytes_to_hex(data, 0, 1, self.endian)

        if self.EI_MAG != "ELF" or firstbyte != '0x7f':
            print(
                "ELF magic code not found or first byte isnt 0x7f! probably a corrupted/malicious ELF file or not a ELF file! : ",
                firstbyte + self.EI_MAG)
            quit()

        self.EI_CLASS = self.bytes_to_hex(data, 4, 1, self.endian)

        if (self.EI_CLASS == '0x1'): self.arch = '32'
        elif (self.EI_CLASS == '0x2'): self.arch = '64'
        else:
            print("EI_CLASS ERROR!")
            quit()

        self.EI_DATA = self.bytes_to_hex(data, 5, 1, self.endian)

        if (self.EI_DATA == '0x1'):
            self.endian = 'little'
            self.endian_operator = '<'
        elif (self.EI_DATA == '0x2'):
            self.endian = 'big'
            self.endian_operator = '>'
        else:
            print("EI_DATA ERROR!")
            quit()

        self.EI_VERSION = self.bytes_to_hex(data, 6, 1, self.endian)

        self.EI_OSABI = self.bytes_to_hex(data, 7, 1, self.endian)

        self.EI_ABIVERSION = self.bytes_to_hex(data, 8, 1, self.endian)

        self.EI_PAD = self.bytes_to_hex(data, 9, 7, self.endian)

        self.e_type = self.bytes_to_hex(data, 16, 2, self.endian)

        self.e_machine = self.bytes_to_hex(data, 18, 2, self.endian)

        self.e_version = self.bytes_to_hex(data, 20, 4, self.endian)

        offset = 24

        if (self.arch == '32'):
            self.e_entry = self.bytes_to_hex(data, offset, 4, self.endian)
            offset += 4
        else:
            self.e_entry = self.bytes_to_hex(data, offset, 8, self.endian)
            offset += 8

        if (self.arch == '32'):
            self.e_phoff = self.bytes_to_hex(data, offset, 4, self.endian)
            offset += 4
        else:
            self.e_phoff = self.bytes_to_hex(data, offset, 8, self.endian)
            offset += 8

        if (self.arch == '32'):
            self.e_shoff = self.bytes_to_hex(data, offset, 4, self.endian)
            offset += 4
        else:
            self.e_shoff = self.bytes_to_hex(data, offset, 8, self.endian)
            offset += 8

        self.e_flags = self.bytes_to_hex(data, offset, 4, self.endian)
        offset += 4

        self.e_ehsize = self.bytes_to_hex(data, offset, 2, self.endian)
        offset += 2

        self.e_phentsize = self.bytes_to_hex(data, offset, 2, self.endian)
        offset += 2

        self.e_phnum = self.bytes_to_hex(data, offset, 2, self.endian)
        offset += 2

        self.e_shentsize = self.bytes_to_hex(data, offset, 2, self.endian)
        offset += 2

        self.e_shnum = self.bytes_to_hex(data, offset, 2, self.endian)
        offset += 2

        self.e_shstrndx = self.bytes_to_hex(data, offset, 2, self.endian)
        offset += 2

        self.printELFheader()

        offset = int(self.e_shoff,
                     0) + (int(self.e_shentsize, 0) * int(self.e_shstrndx, 0))

        if self.arch == '32':
            sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize = struct.unpack(
                self.endian_operator + 'IIIIIIIIII',
                data[offset:offset + int(self.e_shentsize, 0)])
        else:
            sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize = struct.unpack(
                self.endian_operator + 'IIQQQQIIQQ',
                data[offset:offset + int(self.e_shentsize, 0)])

        section_names = data[sh_offset:sh_offset + sh_size]
        string_table = {}
        last_location = 0

        for count, value in enumerate(section_names):
            if value == 0:
                string_table[last_location] = section_names[
                    last_location:count][1:].decode('ascii')
                if (string_table[last_location]
                        == 'rel.plt') or (string_table[last_location]
                                          == 'got.plt'):
                    string_table[last_location + 4] = 'plt'
                elif string_table[last_location] == 'rela.plt':
                    string_table[last_location + 5] = 'plt'
                last_location = count + 1

        self.TABLES = ""
        if (print_tables == True):
            print('\n\n----- PROGRAM HEADER -----')

        table = PrettyTable([
            "Type", "Offset", "Virtual Address", "Physical Address",
            "File Size", "Memory Size", "Flags"
        ])

        for i in range(0, int(self.e_phnum, 0)):
            x = int(self.e_phoff, 0) + (int(self.e_phentsize, 0) * i)

            if self.EI_CLASS == '0x1':
                p_type, p_offset, p_vaddr, p_paddr, p_filesz, p_memsz, p_flags, p_align = struct.unpack(
                    self.endian_operator + 'IIIIIIII', data[x:x + 32])

            else:
                p_type, p_flags, p_offset, p_vaddr, p_paddr, p_filesz, p_memsz, p_align = struct.unpack(
                    self.endian_operator + 'IIQQQQQQ', data[x:x + 56])

            temp = self.mapping(hex(p_type), self.PT_TYPE_LIST)

            if len(temp) != 0: p_type = temp[0] + ' : ' + hex(p_type)
            else: p_type = '(Reserved for O.S) : ' + hex(p_type)

            temp = self.mapping(hex(p_flags), self.PT_FLAGS_LIST)
            if len(temp) != 0: p_flags = temp[0] + ' : ' + hex(p_flags)

            table.add_row([
                p_type, p_offset,
                hex(p_vaddr),
                hex(p_paddr), p_filesz, p_memsz, p_flags
            ])

        if (print_tables == True): print(table)
        # table.

        self.TABLES += str(table.get_html_string())

        if (print_tables == True):
            print('\n\n----- SECTIONS -----')

        table = PrettyTable([
            "Name", "Type", "Flags", "Address", "Offset", "Size", "Link",
            "Info", "AddrAllign", "EntSize"
        ])

        for i in range(0, int(self.e_shnum, 0)):
            x = int(self.e_shoff, 0) + (int(self.e_shentsize, 0) * i)

            if self.arch == '32':

                sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize = struct.unpack(
                    self.endian_operator + 'IIIIIIIIII', data[x:x + 40])
            else:

                sh_name, sh_type, sh_flags, sh_addr, sh_offset, sh_size, sh_link, sh_info, sh_addralign, sh_entsize = struct.unpack(
                    self.endian_operator + 'IIQQQQIIQQ', data[x:x + 64])

            temp = self.mapping(hex(sh_type), self.SH_TYPE_LIST)
            if len(temp) != 0: sh_type = temp[0] + ' : ' + hex(sh_type)
            else: sh_type = hex(sh_type)

            temp = self.masking(hex(sh_flags), self.SH_FLAGS_LIST)
            if len(temp) != 0:
                temp = ' & '.join(temp)
                temp2 = hex(sh_flags)
                sh_flags = temp + ' : ' + temp2

            if sh_name in string_table:
                sh_name = string_table[sh_name]

                if sh_name == 'text':
                    CODE = data[sh_offset:sh_offset + sh_size]
                    if (self.arch == '32'): md = Cs(CS_ARCH_X86, CS_MODE_32)
                    else: md = Cs(CS_ARCH_X86, CS_MODE_64)
                    hex_addr = int(hex(sh_addr), 16)
                    for i in md.disasm(CODE, hex_addr):
                        instruction_list.append(
                            [i.address, i.mnemonic, i.op_str])

                if sh_name == 'plt':
                    CODE = data[sh_offset:sh_offset + sh_size]
                    if (self.arch == '32'): md = Cs(CS_ARCH_X86, CS_MODE_32)
                    else: md = Cs(CS_ARCH_X86, CS_MODE_64)
                    hex_addr = int(hex(sh_addr), 16)
                    for i in md.disasm(CODE, hex_addr):
                        plt_instruction_list.append(
                            [i.address, i.mnemonic, i.op_str])

                if sh_name == 'symtab':
                    symtab_code = data[sh_offset:sh_offset + sh_size]
                    if self.arch == '32': symtab_entry_size = 16
                    else: symtab_entry_size = 24
                    for i in range(0,
                                   int(len(symtab_code) / symtab_entry_size)):
                        if self.arch == '32':
                            st_name, st_value, st_size, st_info, st_other, st_shndx = struct.unpack(
                                self.endian_operator + 'IIIBBH',
                                data[sh_offset + i * 16:sh_offset + i * 16 +
                                     16])
                        else:
                            st_name, st_info, st_other, st_shndx, st_value, st_size = struct.unpack(
                                self.endian_operator + 'IBBHQQ',
                                data[sh_offset + i * 24:sh_offset + i * 24 +
                                     24])

                        symtab_entries.append([
                            st_name, st_value, st_size, st_info, st_other,
                            st_shndx
                        ])

                if sh_name == 'rel.plt':
                    rel_plt = data[sh_offset:sh_offset + sh_size]
                    if self.arch == '32': rel_plt_entrysize = 8
                    else: rel_plt_entrysize = 16
                    for i in range(0, int(len(rel_plt) / rel_plt_entrysize)):
                        if self.arch == '32':
                            r_offset, r_info = struct.unpack(
                                self.endian_operator + 'II',
                                data[sh_offset + i * 8:sh_offset + i * 8 + 8])
                            rel_plt_entries.append(
                                [hex(r_offset), (r_info >> 8)])
                        else:
                            r_offset, r_info = struct.unpack(
                                self.endian_operator + 'QQ',
                                data[sh_offset + i * 16:sh_offset + i * 16 +
                                     16])
                            rel_plt_entries.append(
                                [hex(r_offset), (r_info >> 32)])

                if sh_name == 'rela.plt':
                    global PLT_Rela_exists
                    PLT_Rela_exists = True

                    rel_plt = data[sh_offset:sh_offset + sh_size]
                    if self.arch == '32': rel_plt_entrysize = 8
                    else: rel_plt_entrysize = 24
                    for i in range(0, int(len(rel_plt) / rel_plt_entrysize)):
                        if self.arch == '32':
                            r_offset, r_info, r_addend = struct.unpack(
                                self.endian_operator + 'III',
                                data[sh_offset + i * 12:sh_offset + i * 12 +
                                     12])
                            rel_plt_entries.append(
                                [hex(r_offset), (r_info >> 8)])
                        else:
                            r_offset, r_info, r_addend = struct.unpack(
                                self.endian_operator + 'QQQ',
                                data[sh_offset + i * 24:sh_offset + i * 24 +
                                     24])
                            rel_plt_entries.append(
                                [hex(r_offset), (r_info >> 32)])

                if sh_name == 'dynsym':
                    dynsym_code = data[sh_offset:sh_offset + sh_size]
                    if self.arch == '32': symtab_entry_size = 16
                    else: symtab_entry_size = 24
                    for i in range(0,
                                   int(len(dynsym_code) / symtab_entry_size)):
                        if self.arch == '32':
                            st_name, st_value, st_size, st_info, st_other, st_shndx = struct.unpack(
                                self.endian_operator + 'IIIBBH',
                                data[sh_offset + i * 16:sh_offset + i * 16 +
                                     16])
                        else:
                            st_name, st_info, st_other, st_shndx, st_value, st_size = struct.unpack(
                                self.endian_operator + 'IBBHQQ',
                                data[sh_offset + i * 24:sh_offset + i * 24 +
                                     24])

                        dynsym_entries.append([
                            st_name, st_value, st_size, st_info, st_other,
                            st_shndx
                        ])

                if sh_name == 'strtab':
                    symtab_strings = data[sh_offset:sh_offset + sh_size]

                if sh_name == 'dynstr':
                    dynstr_strings = data[sh_offset:sh_offset + sh_size]

            table.add_row([
                sh_name, sh_type, sh_flags,
                hex(sh_addr), sh_offset, sh_size, sh_link, sh_info,
                sh_addralign, sh_entsize
            ])

        if (print_tables == True):
            print(table)
            exit()

        # self.TABLES += str(table)

        dynamic_symbol_strings = {}
        last_location = 0
        x = 0
        for count, value in enumerate(dynstr_strings):
            if value == 0:
                dynamic_symbol_strings[last_location] = dynstr_strings[
                    last_location:count]
                last_location = count + 1

        x = 0
        for i in dynsym_entries:

            string_index = i[0]
            if string_index != 0:
                try:
                    symb_name = dynamic_symbol_strings[string_index].decode(
                        'ascii')
                    dynamic_functions[x] = symb_name
                    x = x + 1

                except:
                    x = x + 1
                    # print('Error in parsing symbol table!')
                    pass
            else:
                x = x + 1

        self.dot = Digraph(comment='The call graph',
                           strict=True,
                           engine='sfdp',
                           format="png")
        self.dot.node_attr.update(color='lightblue2', style='filled')

        if symtab_strings != '':  # found the symbol table

            found_symtab = 1

            symbol_strings = {}
            last_location = 0
            for count, value in enumerate(symtab_strings):
                if value == 0:
                    symbol_strings[last_location] = symtab_strings[
                        last_location:count]
                    last_location = count + 1

            for i in symtab_entries:
                if (i[3] & 15) == 2:
                    # print(i)

                    string_index = i[0]
                    if string_index != 0:
                        try:
                            symb_name = symbol_strings[string_index].decode(
                                'ascii')
                            program_functions[i[1]] = symb_name

                        except:
                            # print('Error in parsing symbol table!')
                            pass

            if (print_instructions == True):
                print(
                    '[*] Found the symbol table! printing the instructions with function names now...'
                )
                for inst in instruction_list:

                    if inst[0] in program_functions:

                        print('\n', program_functions[inst[0]], ': \n')
                        print('\t', hex(inst[0]), '\t\t', inst[1], '\t\t',
                              inst[2])
                    else:
                        print('\t', hex(inst[0]), '\t\t', inst[1], '\t\t',
                              inst[2])

                exit()

            print(
                '[*] Found the symbol table! trying to find main function address, if not found, we will try __libc_start_main arguments'
            )

            main_addr = self.getkeybyvalue(program_functions, 'main')

            if main_addr != 0:
                print(
                    '[*] Found the main function address! generating the call-graph now...'
                )

                self.dot.node(str(main_addr), 'main()')

                if find_func_sequence == True and input_func_sequence_cmp_list[
                        0] == 'main':
                    func_sequence_cmp_list.append('main')
                    sequence_index = sequence_index + 1

                self.make_callgraph(main_addr)

            else:
                print(
                    '[*] Couldnt find Main in symbol table! trying to use __libc_start_main arguments now...'
                )

                main_addr = self.find_main()

                if main_addr != 0:
                    print(
                        '[*] Found the main function! starting the analysis from there...'
                    )

                    self.dot.node(str(main_addr), 'main()')

                    if find_func_sequence == True and input_func_sequence_cmp_list[
                            0] == 'main':
                        func_sequence_cmp_list.append('main')
                        sequence_index = sequence_index + 1

                    self.make_callgraph(main_addr)

                else:
                    print(
                        '[*] Couldnt find Main function address from __libc_start_main arguments! Trying to start from _start'
                    )

                    self.dot.node(str(instruction_list[0][0]), '_start')
                    self.make_callgraph_no_symboltable(instruction_list[0][0])

        else:  # didnt find the symbol table

            print(
                '[*] couldnt find the symbol string table! looks like the binary is STRIPPED!!'
            )
            print(
                '[*] Trying to find the main function address using arguments passed to __libc_start_main...'
            )
            main_addr = self.find_main()

            found_symtab = 0

            if (print_instructions == True):
                print(
                    '[*] Couldnt find the symbol table! printing the instructions without function names now...'
                )
                for inst in instruction_list:

                    print('\t', hex(inst[0]), '\t\t', inst[1], '\t\t', inst[2])

                exit()

            if main_addr != 0:
                print(
                    '[*] Found the main function! starting the analysis from there...'
                )

                self.dot.node(str(main_addr), 'main()')

                if find_func_sequence == True and input_func_sequence_cmp_list[
                        0] == 'main':
                    func_sequence_cmp_list.append('main')
                    sequence_index = sequence_index + 1

                self.make_callgraph_no_symboltable(main_addr)

            else:
                print(
                    '[*] Couldnt find Main function address from __libc_start_main arguments! Trying to start from _start'
                )

                self.dot.node(str(instruction_list[0][0]), '_start')
                self.make_callgraph_no_symboltable(instruction_list[0][0])

        if (find_sequence == True):
            print('[*] Couldnt find the given call sequence in the program ')
            exit()

        if (sgraph == True):
            print(
                '[*] Analysis Finished! rendering the graph now... the name of the output graph is Call_graph.pdf, it should open automatically'
            )
            self.dot.render('call_graph', view=False)
Example #19
0
# dot.node('S', 'Setup')
# dot.node('F', 'Fetch services')
# dot.node('R', 'RemoteControl')
# dot.node('R', 'RemoteControl')
# dot.node('M', 'Load messages')
# dot.node('U', 'Upgrade database')
# dot.node('G', 'Settings')

# dot.edges(['WL', 'WC', 'WA'])
# dot.edges(['LU', 'LC', 'LA', 'LF', 'LG'])
# dot.edges(['CU', 'CM', 'CA', 'CF'])
# dot.edges(['AU', 'AM', 'AC', 'AF', 'AG','AS'])
# dot.edges(['SA'])
# dot.edges(['RF'])
# # dot.edges(['UM', 'UC', 'UW', 'UA'])
# dot.edges(['GL', 'GF'])
# dot.edges(['FM'])

dot = Digraph(comment='OmniNotes App')
dot.node('W', 'Widgets')
dot.node('R', 'Reminders')
dot.node('E', 'Extensions')
dot.node('S', 'Storage')
dot.node('G', 'Settings')
dot.node('N', 'Notes')
dot.edges(['WN', 'WS'])
dot.edges(['NS', 'NE', 'NG', 'NR'])
dot.edges(['RS', 'RE'])
dot.edges(['GS'])

dot.render('test-output/FAML.gv', view=True)
Example #20
0
    def run(self,
            timesteps=None,
            episodes=None,
            max_episode_timesteps=None,
            deterministic=False,
            episode_finished=None):
        """
        Runs the agent on the environment.

        Args:
            timesteps: Number of timesteps
            episodes: Number of episodes
            max_episode_timesteps: Max number of timesteps per episode
            deterministic: Deterministic flag
            episode_finished: Function handler taking a `Runner` argument and returning a boolean indicating
                whether to continue execution. For instance, useful for reporting intermediate performance or
                integrating termination conditions.
        """

        self.episode = self.agent.episode
        if episodes is not None:
            episodes += self.agent.episode

        self.timestep = self.agent.timestep
        if timesteps is not None:
            timesteps += self.agent.timestep

        # Keep track of episode reward and episode length for statistics.
        self.start_time = time.time()
        self.max_episode_timesteps = max_episode_timesteps
        self.batch_losses = []
        self.q_values = []

        while True:
            search_graph = Digraph()

            state = self.environment.reset()
            self.agent.reset()

            self.counter = count()

            self.nodes_created = 0
            self.nodes_visited = 0
            self.leafs_created = 0
            self.leafs_visited = 0

            root = Node(self.environment, state)
            search_graph.node(root.label, "root")
            node = None
            self.unopened_nodes = PriorityQueue()
            self.unopened_nodes.put((0,
                                0,
                                -next(self.counter),
                                root))

            self.episode_solution = None
            self.upper_bound = -np.inf

            self.episode_timestep = 0
            episode_start_time = time.time()

            while True:
                if node is None:
                    if not self.unopened_nodes.empty():
                        node = self.unopened_nodes.get()[-1]
                        phase = "best_first"
                    else:
                        break
                else:
                    phase = "depth_first"

                highest_lower_bound, node_bound = self.explore_bounds(node)

                if node.terminal:
                    self.nodes_visited += 1
                    self.leafs_visited += 1

                    if node.current_value > self.upper_bound:
                        self.upper_bound = node.current_value
                        self.episode_solution = deepcopy(node)
                        self.print_tree_info(node, highest_lower_bound, phase + ": new bound")
                    else:
                        self.print_tree_info(node, highest_lower_bound, phase + ": leaf")

                    current_solution = copy(node.environment.current_solution)
                    node = root
                    for vertex in current_solution:
                        node = node.child_nodes[vertex]
                        self.agent.current_states = node.state
                        self.agent.current_actions["action"] = node.action

                        updates = self.agent.observe(terminal=node.terminal,
                                                     reward=node.current_value,
                                                     return_loss_per_instance=True)
                        if updates is not None:
                            self.batch_losses.append(np.mean(updates))

                    node = None
                    continue

                if node_bound or (self.max_episode_timesteps is not None and self.episode_timestep == self.max_episode_timesteps):
                    self.print_tree_info(node, highest_lower_bound, phase + ": bounding")
                    # search_graph.node_attr(node.label, color="red")
                    node = None
                    continue
                elif node.layer > 0:
                    self.nodes_visited += 1

                self.agent.act(states=node.state, deterministic=deterministic)
                q_values = copy(self.agent.next_internals[0])
                # q_values /= node.environment.nr_cities
                self.q_values.append(np.mean(q_values))

                for vertex in range(self.environment.nr_vertices):
                    if vertex not in node.environment.current_solution:
                        child_environment = deepcopy(node.environment)
                        state, action, terminal, reward = child_environment.execute(actions=vertex)

                        if action not in node.child_nodes.values():
                            child_node = Node(environment=child_environment, state=state, action=action,
                                              terminal=terminal, parent_node=node, q_value=q_values[action])
                            node.child_nodes[action] = child_node

                            if terminal:
                                self.leafs_created += 1
                            self.nodes_created += 1

                            search_graph.node(child_node.label,
                                              "{}\nub: {:.4f}\nlb: {:.4f}\ncw: {:.4f}\nqv: {:.4f}".format(child_node.label,
                                                                                                          self.upper_bound,
                                                                                                          child_node.current_value + child_node.q_value,
                                                                                                          child_node.current_value,
                                                                                                          child_node.q_value))
                            search_graph.edge(node.label, child_node.label, "{}.".format(self.nodes_created))

                            self.episode_timestep += 1
                            self.timestep += 1
                        else:
                            print()

                self.print_tree_info(node, highest_lower_bound, phase + ": branching")

                sorted_child_nodes = sorted(node.child_nodes.values(), key=lambda x: x.q_value)

                for child_node in sorted_child_nodes[1:]:
                    self.unopened_nodes.put((child_node.q_value,
                                             child_node.current_value,
                                             -next(self.counter),
                                             child_node))

                node = sorted_child_nodes[0]

            if self.episode_solution is not None:
                self.environment = self.episode_solution.environment
                episode_reward = -self.environment.current_value

                time_passed = time.time() - episode_start_time

                self.episode_rewards.append(episode_reward)
                self.episode_timesteps.append(self.episode_timestep)
                self.episode_times.append(time_passed)

                if (timesteps is not None and self.agent.timestep >= timesteps) or \
                        (episodes is not None and self.agent.episode >= episodes):
                    # agent.episode / agent.timestep are globally updated
                    break

                if episode_finished and not self.episode_finished():
                    break

                if self.episode > 500 and self.episode % 500 == 0:
                    # node = self.episode_solution
                    # while node is not None:
                    #     search_graph.attr(node.label, color="green")
                    #     node = node.parent_node

                    search_graph.view(tempfile.mktemp('.gv'))
                self.episode += 1
            else:
                break

        self.agent.close()
        self.environment.close()
def visualize(idx,
              info,
              object_threshold=0.3,
              rel_threshold=0.0,
              only_connected=False,
              nonmax_suppress=1.0,
              top_k=None):

    image_data, graphs, objects, predicates = info

    image = image_data[graphs['idx'][idx]]

    urllib.request.urlretrieve(image['url'],
                               "sample_images/" + image['url'].split("/")[-1])
    im = misc.imread("sample_images/" + image['url'].split("/")[-1])

    fig, ax = plt.subplots(figsize=(int(im.shape[0] / 50),
                                    int(im.shape[1] / 50)))

    h, w = im.shape[0], im.shape[1]
    ax.imshow(im)

    objs, rels = get_graph_matrix(idx, info, object_threshold, rel_threshold,
                                  only_connected, nonmax_suppress, top_k)

    for object_instance in objs:
        rect = patches.Rectangle(
            (int(object_instance[3]), int(object_instance[4])),
            int(object_instance[5] - object_instance[3]),
            int(object_instance[6] - object_instance[4]),
            linewidth=2,
            edgecolor='r',
            facecolor='none')
        rx, ry = rect.get_xy()
        cx = rx + rect.get_width() / 2.0
        cy = ry + rect.get_height() / 2.0
        ax.annotate(objects[int(object_instance[1])], (cx, cy),
                    color='w',
                    weight='bold',
                    fontsize=12,
                    ha='center',
                    va='center')
        ax.add_patch(rect)

    plt.show()

    g = Digraph()

    for idx, obj in enumerate(objs):
        object_class_idx = int(obj[1])
        g.node(str(objects[object_class_idx]) + "," + str(idx))

    for rel in rels:
        sbj_idx = int(rel[1])
        obj_idx = int(rel[2])
        sbj_class_idx = int(objs[sbj_idx][1])
        obj_class_idx = int(objs[obj_idx][1])
        node1 = str(objects[sbj_class_idx]) + "," + str(sbj_idx)
        node2 = str(objects[obj_class_idx]) + "," + str(obj_idx)
        g.edge(node1, node2, label=predicates[int(rel[3])])

    return g
Example #22
0
def generate_graphviz(net, blob, filename="output"):
    """
    Generate a graphviz representation of the network.
    :param net
    :param blob:
    :param filename: Name of output file
    :return:
    """

    print("Generating Profile Report '" + str(filename) + "_report.html'...")
    dot = Digraph(name=filename, format='svg')

    # Legend
    table = '''<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="3">
<TR><TD  BGCOLOR = "#E0E0E0" COLSPAN="3">Layer</TD></TR>
<TR><TD BGCOLOR = "#88FFFF"> Complexity <br/> (MFLOPs) </TD>
<TD BGCOLOR = "#FF88FF"> Bandwidth <br/> (MB/s) </TD>
<TD BGCOLOR = "#FFFF88"> Time <br/> (ms)</TD></TR>
</TABLE>>
'''
    dot.node("Legend", table, shape="plaintext")

    # Input
    table = '''input: {}'''.format(net.inputTensor.shape)
    dot.node('''Input''', table)

    # Nodes
    ms_min, ms_max = net.head[0].minmax("ms", net.head[0].ms, net.head[0].ms)
    for stage in net.head:
        ms_min, ms_max = stage.minmax("ms", ms_min, ms_max)
    bws_min, bws_max = net.head[0].minmax("BWs", net.head[0].BWs,
                                          net.head[0].BWs)
    for stage in net.head:
        bws_min, bws_max = stage.minmax("BWs", bws_min, bws_max)
    flop_min, flop_max = net.head[0].minmax("flops", net.head[0].flops,
                                            net.head[0].flops)
    for stage in net.head:
        flop_min, flop_max = stage.minmax("flops", flop_min, flop_max)
    last_nodes = []
    for stage in net.head:
        dot, last = stage.graphviz(dot, ms_min, ms_max, bws_min, bws_max,
                                   flop_min, flop_max)
        last_nodes.extend(last)

    # Output
    channels = 0
    for shape in net.outputInfo[0]:
        channels = channels + shape[2]
    table = '''output: {}'''.format(
        [net.outputInfo[0][0][0], net.outputInfo[0][0][1], channels])
    dot.node("Output", table)
    for node in last_nodes:
        if net.search(node).isoutput:
            dot.edge(node, "Output")

    total_time = 0
    total_bw = 0
    for stage in net.head:
        time, bw = stage.summaryStats()
        total_time += time
        total_bw += bw

    # Summary
    table = '''<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="3">
<TR><TD  BGCOLOR = "#C60000" COLSPAN="3">Summary</TD></TR>
<TR><TD  BGCOLOR = "#E2E2E2" COLSPAN="3">{0} SHV Processors</TD></TR>
<TR><TD  BGCOLOR = "#DADADA" COLSPAN="3">Inference time {1} ms</TD></TR>
<TR><TD  BGCOLOR = "#E2E2E2" COLSPAN="3">Bandwidth {2} MB/sec</TD></TR>
<TR><TD  BGCOLOR = "#DADADA" COLSPAN="3">This network is Compute bound</TD></TR>
</TABLE>>
'''.format((blob.myriad_params.lastShave.value -
            blob.myriad_params.firstShave.value) + 1,
           format(total_time, ".2f"),
           format((((total_bw / (1024 * 1024)) / (total_time / 1000))), ".2f"))

    dot.node("Summary", table, shape="plaintext")
    dot.render()

    generate_html_report(filename + ".gv.svg", net.name, filename=filename)
Example #23
0
from graphviz import Digraph



dot = Digraph()
dot.node('A', 'A')
dot.node('B', 'B')
dot.node('C', 'C')
dot.edges(['AB', 'AB', 'AB', 'BC', 'BA', 'CB'])

print(dot.source)
dot.render('file_name.pdf', view=False)











Example #24
0
from graphviz import Digraph
from Nodo import Nodo

dot = Digraph(comment='AST')

#dot.render('test-output/round-table.gv', view=True)  # doctest: +SKIP
#'test-output/round-table.gv.jpg'


class AST:
    def __init__(self):
        self.count = 0
        print("constructor")

    def defineTreeNodes(self, root):
        root.setId(str(self.count))
        dot.node(str(self.count), root.getVal())
        self.count += 1
        for node in root.getLista():
            self.defineTreeNodes(node)

    def joinTreeNodes(self, root):
        for node in root.getLista():
            dot.edge(root.getId(), node.getId())
            self.joinTreeNodes(node)

    def drawGraph(self):
        dot.render('test-output/round-table.gv', view=True)  # doctest: +SKIP
        'test-output/round-table.gv.jpg'

from graphviz import Digraph

dot = Digraph(
    comment='First flowchart',
    name='Hello world',
    filename='hello_world.dot',
)

dot.node('1', 'Inicio')
dot.node('2', '"Hola mundo"', shape='invhouse')
#dot.node('2', '"Hola mundo"', shapefile='assets/print.svg')
dot.node('3', 'Fin')
dot.node('4', 'Fin 2')

dot.edge('1', '2')
#dot.attr('graph', splines='ortho', nodesep='1')
dot.edge('2', '3')  # constraint='false')
dot.edge('2', '4')

dot.format = 'png'
dot.render(view=True)
Example #26
0
def apply(bpmn_graph, parameters=None):
    """
    Layouts a BPMN graph (inserting the positions of the nodes and the layouting of the edges)

    Parameters
    -------------
    bpmn_graph
        BPMN graph
    parameters
        Parameters of the algorithm

    Returns
    -------------
    bpmn_graph
        BPMN graph with layout information
    """
    from graphviz import Digraph
    from pm4py.visualization.common import save as gsave

    if parameters is None:
        parameters = {}

    nodes = bpmn_graph.get_nodes()
    flows = bpmn_graph.get_flows()

    filename_gv = tempfile.NamedTemporaryFile(suffix='.gv')
    filename_gv.close()
    filename_svg = tempfile.NamedTemporaryFile(suffix='.svg')
    filename_svg.close()
    viz = Digraph(bpmn_graph.get_name(),
                  filename=filename_gv.name,
                  engine='dot')
    viz.format = "svg"
    viz.graph_attr['rankdir'] = 'LR'

    graph_nodes, graph_edges = get_sorted_nodes_edges(bpmn_graph)

    nodes_dict = {}
    inv_nodes_dict = {}
    for n in graph_nodes:
        node_uuid = str(uuid.uuid4()).replace("-", "")
        nodes_dict[n] = node_uuid
        inv_nodes_dict[node_uuid] = n
        viz.node(node_uuid, label=" ", shape="box")

    for tup in graph_edges:
        viz.edge(nodes_dict[tup[0]], nodes_dict[tup[1]])

    gsave.save(viz, filename_svg.name)

    nodes_pos = {}

    content = open(filename_svg.name, "r").read()
    viz_nodes = content.split("class=\"node\">")[1:]
    for node in viz_nodes:
        this_id = node.split("<title>")[1].split("</title>")[0]
        points = node.split("points=\"")[1].split("\"")[0]
        nodes_pos[inv_nodes_dict[this_id]] = points

    task_wh = exec_utils.get_param_value(Parameters.TASK_WH, parameters, 60)

    # add node positions to BPMN nodes
    for n in graph_nodes:
        node_pos = nodes_pos[n].split(" ")[0].split(",")

        pos_x = float(node_pos[0])
        pos_y = float(node_pos[1])
        n.set_x(pos_x)
        n.set_y(pos_y)
        n.set_height(task_wh)
        if isinstance(n, BPMN.Task):
            this_width = min(
                round(2 * task_wh),
                round(2 * (len(n.get_name()) + 7) * task_wh / 22.0))
            n.set_width(this_width)
        else:
            n.set_width(task_wh)

    max_x = max(1, max(abs(node.get_x()) for node in nodes))
    max_y = max(1, max(abs(node.get_y()) for node in nodes))
    different_x = len(set(node.get_x() for node in nodes))
    different_y = len(set(node.get_y() for node in nodes))

    stretch_fact_x = 1.25 * 1920.0 / max_x
    stretch_fact_y = 1080.0 / max_y

    for node in nodes:
        node.set_x(round(node.get_x() * stretch_fact_x))
        node.set_y(round(node.get_y() * stretch_fact_y))

    outgoing_edges = dict()
    ingoing_edges = dict()
    sources_dict = dict()
    targets_dict = dict()

    for flow in flows:
        source = flow.get_source()
        target = flow.get_target()

        x_src = source.get_x()
        x_trg = target.get_x()
        y_src = source.get_y()
        y_trg = target.get_y()

        sources_dict[(x_src, y_src)] = source
        targets_dict[(x_trg, y_trg)] = target

        diff_x = abs(x_trg - x_src)
        diff_y = abs(y_src - y_trg)

        if not (x_src, y_src) in outgoing_edges:
            outgoing_edges[(x_src, y_src)] = {}
        outgoing_edges[(x_src, y_src)][(x_trg, y_trg)] = {
            EndpointDirection.RIGHT: 0.0,
            EndpointDirection.LEFT: 0.0,
            EndpointDirection.TOP: 0.0,
            EndpointDirection.BOTTOM: 0.0
        }
        if not (x_trg, y_trg) in ingoing_edges:
            ingoing_edges[(x_trg, y_trg)] = {}
        ingoing_edges[(x_trg, y_trg)][(x_src, y_src)] = {
            EndpointDirection.RIGHT: 0.0,
            EndpointDirection.LEFT: 0.0,
            EndpointDirection.TOP: 0.0,
            EndpointDirection.BOTTOM: 0.0
        }

        if x_trg > x_src:
            outgoing_edges[(x_src, y_src)][(
                x_trg,
                y_trg)][EndpointDirection.RIGHT] = diff_x / (diff_x + diff_y)
            ingoing_edges[(x_trg, y_trg)][(
                x_src,
                y_src)][EndpointDirection.LEFT] = diff_x / (diff_x + diff_y)
        else:
            outgoing_edges[(x_src, y_src)][(
                x_trg,
                y_trg)][EndpointDirection.LEFT] = diff_x / (diff_x + diff_y)
            ingoing_edges[(x_trg, y_trg)][(
                x_src,
                y_src)][EndpointDirection.RIGHT] = diff_x / (diff_x + diff_y)

        if y_src > y_trg:
            outgoing_edges[(x_src, y_src)][(
                x_trg,
                y_trg)][EndpointDirection.TOP] = diff_y / (diff_x + diff_y)
            ingoing_edges[(x_trg, y_trg)][(
                x_src,
                y_src)][EndpointDirection.BOTTOM] = diff_y / (diff_x + diff_y)
        else:
            outgoing_edges[(x_src, y_src)][(
                x_trg,
                y_trg)][EndpointDirection.BOTTOM] = diff_y / (diff_x + diff_y)
            ingoing_edges[(x_trg, y_trg)][(
                x_src,
                y_src)][EndpointDirection.TOP] = diff_y / (diff_x + diff_y)

    # normalization
    outgoing_edges0 = deepcopy(outgoing_edges)
    ingoing_edges0 = deepcopy(ingoing_edges)

    for p1 in outgoing_edges:
        sum_right = 0.0
        sum_left = 0.0
        sum_top = 0.0
        sum_bottom = 0.0
        for p2 in outgoing_edges[p1]:
            sum_right += outgoing_edges0[p1][p2][EndpointDirection.RIGHT]
            sum_left += outgoing_edges0[p1][p2][EndpointDirection.LEFT]
            sum_top += outgoing_edges0[p1][p2][EndpointDirection.TOP]
            sum_bottom += outgoing_edges0[p1][p2][EndpointDirection.BOTTOM]
        if p1 in ingoing_edges:
            for p2 in ingoing_edges[p1]:
                sum_right += ingoing_edges0[p1][p2][EndpointDirection.RIGHT]
                sum_left += ingoing_edges0[p1][p2][EndpointDirection.LEFT]
                sum_top += ingoing_edges0[p1][p2][EndpointDirection.TOP]
                sum_bottom += ingoing_edges0[p1][p2][EndpointDirection.BOTTOM]
        for p2 in outgoing_edges[p1]:
            if sum_right > 0:
                outgoing_edges[p1][p2][
                    EndpointDirection.RIGHT] = outgoing_edges[p1][p2][
                        EndpointDirection.RIGHT]**2 / sum_right
            if sum_left > 0:
                outgoing_edges[p1][p2][
                    EndpointDirection.LEFT] = outgoing_edges[p1][p2][
                        EndpointDirection.LEFT]**2 / sum_left
            if sum_top > 0:
                outgoing_edges[p1][p2][EndpointDirection.TOP] = outgoing_edges[
                    p1][p2][EndpointDirection.TOP]**2 / sum_top
            if sum_bottom > 0:
                outgoing_edges[p1][p2][
                    EndpointDirection.BOTTOM] = outgoing_edges[p1][p2][
                        EndpointDirection.BOTTOM]**2 / sum_bottom
    for p1 in ingoing_edges:
        sum_right = 0.0
        sum_left = 0.0
        sum_top = 0.0
        sum_bottom = 0.0
        for p2 in ingoing_edges[p1]:
            sum_right += ingoing_edges0[p1][p2][EndpointDirection.RIGHT]
            sum_left += ingoing_edges0[p1][p2][EndpointDirection.LEFT]
            sum_top += ingoing_edges0[p1][p2][EndpointDirection.TOP]
            sum_bottom += ingoing_edges0[p1][p2][EndpointDirection.BOTTOM]
        if p1 in outgoing_edges:
            for p2 in outgoing_edges[p1]:
                sum_right += outgoing_edges0[p1][p2][EndpointDirection.RIGHT]
                sum_left += outgoing_edges0[p1][p2][EndpointDirection.LEFT]
                sum_top += outgoing_edges0[p1][p2][EndpointDirection.TOP]
                sum_bottom += outgoing_edges0[p1][p2][EndpointDirection.BOTTOM]
        for p2 in ingoing_edges[p1]:
            if sum_right > 0:
                ingoing_edges[p1][p2][EndpointDirection.RIGHT] = ingoing_edges[
                    p1][p2][EndpointDirection.RIGHT]**2 / sum_right
            if sum_left > 0:
                ingoing_edges[p1][p2][EndpointDirection.LEFT] = ingoing_edges[
                    p1][p2][EndpointDirection.LEFT]**2 / sum_left
            if sum_top > 0:
                ingoing_edges[p1][p2][EndpointDirection.TOP] = ingoing_edges[
                    p1][p2][EndpointDirection.TOP]**2 / sum_top
            if sum_bottom > 0:
                ingoing_edges[p1][p2][
                    EndpointDirection.BOTTOM] = ingoing_edges[p1][p2][
                        EndpointDirection.BOTTOM]**2 / sum_bottom

    # keep best direction
    for p1 in outgoing_edges:
        for p2 in outgoing_edges[p1]:
            vals = sorted([(x, y) for x, y in outgoing_edges[p1][p2].items()],
                          key=lambda x: x[1],
                          reverse=True)
            outgoing_edges[p1][p2] = vals[0][0]
    for p1 in ingoing_edges:
        for p2 in ingoing_edges[p1]:
            vals = sorted([(x, y) for x, y in ingoing_edges[p1][p2].items()],
                          key=lambda x: x[1],
                          reverse=True)
            ingoing_edges[p1][p2] = vals[0][0]

    total_counter = dict()
    partial_counter = dict()
    for p1 in outgoing_edges:
        if p1 not in total_counter:
            total_counter[p1] = Counter()
        for p2 in outgoing_edges[p1]:
            dir = outgoing_edges[p1][p2]
            total_counter[p1][dir] += 1
    for p1 in ingoing_edges:
        if p1 not in total_counter:
            total_counter[p1] = Counter()
        for p2 in ingoing_edges[p1]:
            dir = ingoing_edges[p1][p2]
            total_counter[p1][dir] += 1

    outgoing_edges_dirs = deepcopy(outgoing_edges)
    ingoing_edges_dirs = deepcopy(ingoing_edges)

    # decide exiting/entering point for edges
    for p1 in outgoing_edges:
        node = sources_dict[p1]
        if p1 not in partial_counter:
            partial_counter[p1] = Counter()
        sorted_outgoing_edges = sorted(outgoing_edges[p1],
                                       key=lambda x: x,
                                       reverse=False)
        for p2 in sorted_outgoing_edges:
            dir = outgoing_edges[p1][p2]
            partial_counter[p1][dir] += 1
            if dir == EndpointDirection.RIGHT:
                outgoing_edges[p1][p2] = get_right_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
            elif dir == EndpointDirection.LEFT:
                outgoing_edges[p1][p2] = get_left_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
            elif dir == EndpointDirection.TOP:
                outgoing_edges[p1][p2] = get_top_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
            elif dir == EndpointDirection.BOTTOM:
                outgoing_edges[p1][p2] = get_bottom_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
    for p1 in ingoing_edges:
        node = targets_dict[p1]
        if p1 not in partial_counter:
            partial_counter[p1] = Counter()
        sorted_ingoing_edges = sorted(ingoing_edges[p1],
                                      key=lambda x: x,
                                      reverse=False)
        for p2 in sorted_ingoing_edges:
            dir = ingoing_edges[p1][p2]
            partial_counter[p1][dir] += 1
            if dir == EndpointDirection.RIGHT:
                ingoing_edges[p1][p2] = get_right_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
            elif dir == EndpointDirection.LEFT:
                ingoing_edges[p1][p2] = get_left_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
            elif dir == EndpointDirection.TOP:
                ingoing_edges[p1][p2] = get_top_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])
            elif dir == EndpointDirection.BOTTOM:
                ingoing_edges[p1][p2] = get_bottom_edge_coord(
                    node, p1, partial_counter[p1], total_counter[p1])

    # order the left-entering ingoing edges better
    for p1 in ingoing_edges:
        vals = [(x, y) for x, y in ingoing_edges[p1].items() if y[0] == p1[0]]
        if len(vals) > 1:
            vals_x = [x[0] for x in vals]
            vals_y = [x[1] for x in vals]
            vals_x = sorted(vals_x)
            vals_y = sorted(vals_y)
            for i in range(len(vals_x)):
                ingoing_edges[p1][vals_x[i]] = vals_y[i]

    # set waypoints for edges
    for flow in flows:
        source = flow.get_source()
        target = flow.get_target()

        flow.del_waypoints()

        x_src = source.get_x()
        x_trg = target.get_x()
        y_src = source.get_y()
        y_trg = target.get_y()
        p1 = (x_src, y_src)
        p2 = (x_trg, y_trg)

        source_x = outgoing_edges[p1][p2][0]
        source_y = outgoing_edges[p1][p2][1]
        target_x = ingoing_edges[p2][p1][0]
        target_y = ingoing_edges[p2][p1][1]
        dir_source = outgoing_edges_dirs[p1][p2]
        dir_target = ingoing_edges_dirs[p2][p1]

        middle_x = (source_x + target_x) / 2.0
        middle_y = (source_y + target_y) / 2.0

        flow.add_waypoint((source_x, source_y))
        if dir_source in [EndpointDirection.LEFT, EndpointDirection.RIGHT]:
            if dir_target in [EndpointDirection.LEFT, EndpointDirection.RIGHT]:
                flow.add_waypoint((middle_x, source_y))
                flow.add_waypoint((middle_x, target_y))
            elif dir_target in [
                    EndpointDirection.TOP, EndpointDirection.BOTTOM
            ]:
                flow.add_waypoint((target_x, source_y))
        elif dir_source in [EndpointDirection.TOP, EndpointDirection.BOTTOM]:
            if dir_target in [EndpointDirection.TOP, EndpointDirection.BOTTOM]:
                flow.add_waypoint((source_x, middle_y))
                flow.add_waypoint((target_x, middle_y))
            elif dir_target in [
                    EndpointDirection.LEFT, EndpointDirection.RIGHT
            ]:
                flow.add_waypoint((source_x, target_y))

        flow.add_waypoint((target_x, target_y))

    return bpmn_graph
from graphviz import Digraph

g = Digraph('G')

g.edge('Hello', 'World')

display(g)

print(g.source)

# png download (svg is also supported)
g.render(filename='hello_world', format='png', scale=2)
Example #28
0
def run(loc_choice, loc_lat, loc_lng, dst_choice, dst_lat, dst_lng,
        visualize_graph):
    # Create folder for final images if it doesn't exist
    pathlib.Path("./static").mkdir(parents=True, exist_ok=True)

    # Remove any old images in the folder
    image_dir = os.listdir('static/')
    for file in image_dir:
        if file.endswith(".png"):
            os.remove(os.path.join('static/', file))

    global SRC_ID
    global DST_ID

    if loc_choice:
        location = location_lookup[loc_choice]
        SRC_ID = loc_choice
    else:
        location = loc_lat + ',' + loc_lng
        SRC_ID = "location"

    if dst_choice:
        destination = location_lookup[dst_choice]
        DST_ID = dst_choice
    else:
        destination = dst_lat + ',' + dst_lng
        DST_ID = "destination"

    live = True
    setup_logging(False)

    if live:
        # URLs for the TransLoc API
        stops_url = "https://transloc-api-1-2.p.rapidapi.com/stops.json"
        routes_url = "https://transloc-api-1-2.p.rapidapi.com/routes.json"
        arrival_estimates_url = "https://transloc-api-1-2.p.rapidapi.com/arrival-estimates.json"

        host = "transloc-api-1-2.p.rapidapi.com"
        agencies = "347"  # UVA

        try:
            with open("TransLocKey.txt", "r") as fp:
                transloc_key = fp.readline()
        except FileNotFoundError:
            err = f"Unable to open TransLoc key file"
            critical(err)
            sys.exit(err)

        headers = {"x-rapidapi-host": host, "x-rapidapi-key": transloc_key}
        payload = {"agencies": agencies}

        # Get stops information from TransLoc
        try:
            stops_response = requests.get(stops_url,
                                          headers=headers,
                                          params=payload)
        except ConnectionError:
            err = f"Connection error when attempting to access {stops_url}"
            critical(err)
            sys.exit(err)

        if not stops_response.ok:
            critical(
                f"When attempting to access {stops_url}, received Status Code {stops_response.status_code} for Reason {stops_response.reason}."
            )
            sys.exit("Unable to access TransLoc API")

        stops_json = stops_response.json()

        # Get route information from TransLoc
        try:
            routes_response = requests.get(routes_url,
                                           headers=headers,
                                           params=payload)
        except ConnectionError:
            err = f"Connection error when attempting to access {routes_url}"
            critical(err)
            sys.exit(err)

        if not routes_response.ok:
            critical(
                f"When attempting to access {routes_url}, received Status Code {routes_response.status_code} for Reason {routes_response.reason}."
            )
            sys.exit("Unable to access TransLoc API")

        routes_json = routes_response.json()

        # Get arrival estimates information from TransLoc
        try:
            arrival_estimates_response = requests.get(arrival_estimates_url,
                                                      headers=headers,
                                                      params=payload)
        except ConnectionError:
            err = f"Connection error when attempting to accesss {arrival_estimates_url}"
            critical(err)
            sys.exit(err)

        if not arrival_estimates_response.ok:
            critical(
                f"When attempting to access {arrival_estimates_url}, received Status Code {arrival_estimates_response.status_code} for Reason {arrival_estimates_response.reason}."
            )
            sys.exit("Unable to access TransLoc API")

        arrival_estimates_json = arrival_estimates_response.json()

        # Get current time
        current_time = tm.time()

        # Save TransLoc results to log files
        try:
            with open("logs/stops.log", "w") as fp:
                json.dump(stops_json, fp)

            with open("logs/routes.log", "w") as fp:
                json.dump(routes_json, fp)

            with open("logs/arrival_estimates.log", "w") as fp:
                json.dump(arrival_estimates_json, fp)

            with open("logs/current_time.log", "w") as fp:
                fp.write(str(current_time))
        except FileNotFoundError:
            err = f"Unable to save TransLoc results to logs"
            error(err)

    else:

        # Read in TransLoc data from log files
        try:
            with open("logs/stops.log", "r") as fp:
                stops_json = json.load(fp)

            with open("logs/routes.log", "r") as fp:
                routes_json = json.load(fp)

            with open("logs/arrival_estimates.log", "r") as fp:
                arrival_estimates_json = json.load(fp)

            with open("logs/current_time.log", "r") as fp:
                current_time = float(fp.readline()[:-1])

        except FileNotFoundError:
            err = "Unable to read saved information from logs"
            critical(err)
            sys.exit(err)

    # Create empty graph
    g = Graph()

    # Build graph with data returned by TransLoc API
    parse_uts_data(g, stops_json, routes_json, arrival_estimates_json)

    # Add source node
    g.nodes[SRC_ID] = Node(SRC_ID, SRC_ID, location)
    g.adj_list[SRC_ID] = []
    debug(f"Added source node at {location}")

    # Add destination node
    g.nodes[DST_ID] = Node(DST_ID, DST_ID, destination)
    g.adj_list[DST_ID] = []
    debug(f"Added destination node at {destination}")

    # Add edges from SRC to every stop and from every stop to DST
    add_walking_edges(g)

    # Run Dijkstra's algorithm on graph
    path = dijkstra(g, current_time)

    # Use different image name to force Flask to reload image after each iteration
    map_image_name = f"{current_time}_map.png"

    # Create image file with Google Static Maps API to show path from SRC to DST
    display_routes(g, path, stops_json, routes_json,
                   f"static/{map_image_name}")

    # Create directions to print
    directions = []
    label = 'A'
    for stop in path:
        if stop.name == SRC_ID:
            directions.append(
                f'{tm.strftime("%I:%M", tm.localtime(stop.time))} Walk from {SRC_ID} ({label}) to {g.nodes[stop.n.to_stop].name} ({chr(ord(label) + 1)}).'
            )
            label = chr(ord(label) + 1)

            # If we just ahd to walk from SRC to DST, this is the only direction we need
            if len(path) == 2:
                break

        elif stop.name == DST_ID:
            directions.append(
                f'{tm.strftime("%I:%M", tm.localtime(stop.time))} Walk from {g.nodes[stop.p.from_stop].name} ({label}) to {DST_ID} ({chr(ord(label) + 1)}).'
            )

        # If we're getting onto a bus for the first time
        elif g.nodes[stop.p.from_stop].name == SRC_ID:
            directions.append(
                f'{tm.strftime("%I:%M", tm.localtime(stop.time))} Take {stop.n.name} from {stop.name} ({label}) to '
            )
            label = chr(ord(label) + 1)

        # If we're getting off of a bus
        elif stop.p.route_id != stop.n.route_id:
            directions[-1] += f"{stop.name} ({label})."

            # If this isn't the last bus stop
            if g.nodes[stop.n.to_stop].name != DST_ID:
                directions.append(
                    f'{tm.strftime("%I:%M", tm.localtime(stop.time))} Take {stop.n.name} from {stop.name} ({label}) to '
                )
                label = chr(ord(label) + 1)

    graph_image_name = None
    if visualize_graph:
        color_lookup = {}
        for route in routes_json["data"]["347"]:
            color_lookup[route["route_id"]] = route["color"]

        edges_on_path = [node.n for node in path[:-1]]

        G = Digraph()

        for node in g.nodes:
            G.node(g.nodes[node].name)

            for e in g.adj_list[node]:
                if e.name == "walking":
                    color = "gray"
                else:
                    color = f'#{color_lookup[e.route_id]}'

                if e in edges_on_path:
                    penwidth = "5.0"
                else:
                    penwidth = "1.0"

                G.edge(g.nodes[e.from_stop].name,
                       g.nodes[e.to_stop].name,
                       color=color,
                       penwidth=penwidth)

            graph_image_name = f"{current_time}_graph.png"
            G.render(f"static/{graph_image_name[:-4]}",
                     format="png",
                     cleanup=True)

    return map_image_name, directions, graph_image_name
    uniprot = splitline[0].strip(',')
    lig1 = splitline[2].strip(',')
    lig2 = splitline[3].strip(',')

    if (uniprot != uniprot_old):
        if uniprot_old != '':
            print dot_edges
            #dot.edges(dot_edges)
            #dot.edge('B', 'L', constraint='false')
            print(dot.source)  # doctest: +NORMALIZE_WHITESPACE
            dot.render(uniprot_old + '.gv', view=True)
            print "stop " + uniprot_old
            #exit()
        print "start " + uniprot_old
        dot = Digraph(comment='Ligands for ' + uniprot)
        lig_dic = {}
        dot_edges = []
        count = 0
        #fcount = 1
        uniprot_old = uniprot

    #if (count > 8):
    #     continue

    print count, uniprot, lig1, lig2

    if not (lig1 in lig_dic):
        dot.node(str(count), lig1)
        lig_dic[lig1] = count
        count = count + 1
Example #30
0
 def __init__(self, G):
     self.G = G
     self.dot = Digraph(format='pdf')
     self.plot_nodes()
     self.plot_edges()