0

I have a pyvis network amazing graph and I want to change arrows from one side to another, that all leads to red point.

1

Is there any function of it?

Is there any other way to reverse the arrow?

adding my whole messy code

import pandas as pd
import numpy as np
import pyarrow.feather as feather
from pyvis import network as net
from IPython.core.display import display, HTML
import networkx  as nx
from graph_based_clustering import ConnectedComponentsClustering
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import markov_clustering as mc
from node2vec import Node2Vec

df = pd.read_feather('block_100000')


value= df[["input_prev_tx_hash","input_prev_value"]]
value = value.drop_duplicates()
value = value.dropna()

def get_size(node):
    value_list = list()
    for i in node:
        v = value[value["input_prev_tx_hash"]== i]["input_prev_value"].item()
        v = v/1000000000
        value_list.append(v)
    return value_list



#edges
edges = df[["hash", "input_prev_tx_hash"]].copy()
edges = edges.dropna()
edges = edges.drop_duplicates()
edges.drop(edges[edges["hash"]=="0000000000000000000000000000000000000000000000000000000000000000"].index, inplace = True)
edges.drop(edges[edges["input_prev_tx_hash"]=="0000000000000000000000000000000000000000000000000000000000000000"].index, inplace = True)

print(edges)
features = df[["hash"]].copy()
features = features.drop_duplicates()
features.drop(features[features["hash"]=="0000000000000000000000000000000000000000000000000000000000000000"].index, inplace = True)

#features.sort_values(by=["1"])

nodes_1 = features["hash"].to_numpy()
nodes_1 = nodes_1[4].split()

edges_1 = edges[edges['hash'].isin(nodes_1)]

nodes_2 = edges_1['input_prev_tx_hash'].to_numpy()
edges_2 = edges[edges['hash'].isin(edges_1['input_prev_tx_hash'])]
nodes_3 = edges_2['input_prev_tx_hash'].to_numpy()
edges_3 = edges[edges['hash'].isin(edges_2['input_prev_tx_hash'])]
nodes_4 = edges_3['input_prev_tx_hash'].to_numpy()
edges_4 = edges[edges['hash'].isin(edges_3['input_prev_tx_hash'])]
nodes_5 = edges_4['input_prev_tx_hash'].to_numpy()
edges_5 = edges[edges['hash'].isin(edges_4['input_prev_tx_hash'])]
nodes_6 = edges_5['input_prev_tx_hash'].to_numpy()
edges_6 = edges[edges['hash'].isin(edges_5['input_prev_tx_hash'])]
nodes_7 = edges_6['input_prev_tx_hash'].to_numpy()
edges_7 = edges[edges['hash'].isin(edges_6['input_prev_tx_hash'])]
nodes_8 = edges_7['input_prev_tx_hash'].to_numpy()
edges_8 = edges[edges['hash'].isin(edges_7['input_prev_tx_hash'])]
nodes_9 = edges_8['input_prev_tx_hash'].to_numpy()
edges_9 = edges[edges['hash'].isin(edges_8['input_prev_tx_hash'])]
nodes_10 = edges_9['input_prev_tx_hash'].to_numpy()
edges_10 = edges[edges['hash'].isin(edges_9['input_prev_tx_hash'])]
nodes_11 = edges_10['input_prev_tx_hash'].to_numpy()
edges_11 = edges[edges['hash'].isin(edges_10['input_prev_tx_hash'])]
nodes_12 = edges_11['input_prev_tx_hash'].to_numpy()
edges_12 = edges[edges['hash'].isin(edges_11['input_prev_tx_hash'])]
nodes_13 = edges_12['input_prev_tx_hash'].to_numpy()
edges_13 = edges[edges['hash'].isin(edges_12['input_prev_tx_hash'])]
nodes_14 = edges_13['input_prev_tx_hash'].to_numpy()
edges_14 = edges[edges['hash'].isin(edges_13['input_prev_tx_hash'])]
#nodes_1 = nodes_1.tolist()
nodes_2 = nodes_2.tolist()
nodes_3 = nodes_3.tolist()
nodes_4 = nodes_4.tolist()
nodes_5 = nodes_5.tolist()
nodes_6 = nodes_6.tolist()
nodes_7 = nodes_7.tolist()
nodes_8 = nodes_8.tolist()
nodes_9 = nodes_9.tolist()
nodes_10 = nodes_10.tolist()
nodes_11 = nodes_11.tolist()
nodes_12 = nodes_12.tolist()
nodes_13 = nodes_13.tolist()
nodes_14 = nodes_14.tolist()

edges_1 = edges_1.to_records(index=False).tolist()
edges_2 = edges_2.to_records(index=False).tolist()
edges_3 = edges_3.to_records(index=False).tolist()
edges_4 = edges_4.to_records(index=False).tolist()
edges_5 = edges_5.to_records(index=False).tolist()
edges_6 = edges_6.to_records(index=False).tolist()
edges_7 = edges_7.to_records(index=False).tolist()
edges_8 = edges_8.to_records(index=False).tolist()
edges_9 = edges_9.to_records(index=False).tolist()
edges_10 = edges_10.to_records(index=False).tolist()
edges_11 = edges_11.to_records(index=False).tolist()
edges_12 = edges_12.to_records(index=False).tolist()
edges_13 = edges_13.to_records(index=False).tolist()
edges_14 = edges_14.to_records(index=False).tolist()




g=net.Network(bgcolor='#222222', font_color='white', directed=True)
# use 'directed=True' for arrows showing the direction of the edges
g.add_nodes(nodes_1, color=['red'])
g.add_nodes(nodes_2, size=get_size(nodes_2))
g.add_nodes(nodes_3, size=get_size(nodes_3))
g.add_nodes(nodes_4, size=get_size(nodes_4))
g.add_nodes(nodes_5, size=get_size(nodes_5))
g.add_nodes(nodes_6, size=get_size(nodes_6))
g.add_nodes(nodes_7, size=get_size(nodes_7))
g.add_nodes(nodes_8, size=get_size(nodes_8))
g.add_nodes(nodes_9, size=get_size(nodes_9))
g.add_nodes(nodes_10, size=get_size(nodes_10))
g.add_nodes(nodes_11, size=get_size(nodes_11))
g.add_nodes(nodes_12, size=get_size(nodes_12))
g.add_nodes(nodes_13, size=get_size(nodes_13))
g.add_nodes(nodes_14, size=get_size(nodes_14))


g.add_edges(edges_1)
g.add_edges(edges_2)
g.add_edges(edges_3)
g.add_edges(edges_4)
g.add_edges(edges_5)
g.add_edges(edges_6)
g.add_edges(edges_7)
g.add_edges(edges_8)
g.add_edges(edges_9)
g.add_edges(edges_10)
g.add_edges(edges_11)
g.add_edges(edges_12)
g.add_edges(edges_13)
g.add_edges(edges_14)

g.show("example.html")

here is how edges looks like:

enter image description here

The idea is that it I pass the hash from which it starts to create edges, from is in hash column and to is in input_prev_tx_hash

I tried with networkx graph, but then visualization is not the same, I cannot make the same nice visualization with html like here in networkx graph.

ralphito
  • 1
  • 1
  • What is `edges`? In general, an edge is a tuple `(from, to)`. You could simply reverse it. In your code you have a variable called `edges`, which seems to contain that information. However you do not show it. – MSpiller Mar 20 '23 at 17:40
  • Sorry, my code is such a mess. here is edges: #edges edges = df[["hash", "input_prev_tx_hash"]].copy() edges = edges.dropna() edges = edges.drop_duplicates() features = df[["hash"]].copy() features = features.drop_duplicates() so will try to reverse edges – ralphito Mar 20 '23 at 17:48
  • Ok, have no idea how to reverse it... – ralphito Mar 20 '23 at 18:16
  • That does not really help. How does edges look like? Could you print it? And please add that and the code to the question and not in the comments. – MSpiller Mar 20 '23 at 18:26
  • updated the main description now – ralphito Mar 21 '23 at 11:32
  • If I understand your code correctly, you should be able to reverse the edges by adding `[::-1]` to the lines `edges_n = ...`. E.g. `edges_1 = edges_1.to_records(index=False).tolist()[::-1].` – MSpiller Mar 21 '23 at 12:00
  • I tried you rsuggestion but the arrows remained the same. – ralphito Mar 22 '23 at 11:02
  • I used this one edges_1 = [(edg[1], edg[0]) for edg in edges_1] and it swap places in array list. thanks a lot for idea! – ralphito Mar 22 '23 at 11:48

0 Answers0