Skip to content

Exporting and visualising your graph

As with Raphtory's connectors for ingesting data, there are a host of different formats and libraries that you can export your graphs into. Below we explore three of these, namely Pandas dataframes, NetworkX graphs and Pyvis graphs.

All functions inside of the export module work on both graphs and graph views, allowing you to specify different windows, layers and subgraphs before conversion.

By default exporting will include all properties and all update history. However, this can be modified via flags on each export function, depending on use case requirements. You can find a description of these flags in the export API.

Info

This page doesn't contain an exhaustive list of all the different conversion methods - please feel free to check the API documentation for this list. If we are missing a format that you believe to be important, please raise an issue and it will be available before you know it!

For the examples in this tutorial we are going to be reusing the network traffic dataset from the ingestion tutorial and the monkey interaction network from the querying tutorial. In the below code segment you can get a refresher of what these datasets looks like.

Graph

from raphtory import Graph
import pandas as pd

server_edges_df = pd.read_csv("data/network_traffic_edges.csv")
server_edges_df["timestamp"] = pd.to_datetime(server_edges_df["timestamp"]).astype(
    "datetime64[ms, UTC]"
)

server_nodes_df = pd.read_csv("data/network_traffic_nodes.csv")
server_nodes_df["timestamp"] = pd.to_datetime(server_nodes_df["timestamp"]).astype(
    "datetime64[ms, UTC]"
)

print("Network Traffic Edges:")
print(f"{server_edges_df}\n")
print("Network Traffic Servers:")
print(f"{server_nodes_df}\n")

traffic_graph = Graph.load_from_pandas(
    edge_df=server_edges_df,
    edge_src="source",
    edge_dst="destination",
    edge_time="timestamp",
    edge_props=["data_size_MB"],
    edge_layer="transaction_type",
    edge_const_props=["is_encrypted"],
    edge_shared_const_props={"datasource": "data/network_traffic_edges.csv"},
    node_df=server_nodes_df,
    node_id="server_id",
    node_time="timestamp",
    node_props=["OS_version", "primary_function", "uptime_days"],
    node_const_props=["server_name", "hardware_type"],
    node_shared_const_props={"datasource": "data/network_traffic_edges.csv"},
)

monkey_edges_df = pd.read_csv(
    "data/OBS_data.txt", sep="\t", header=0, usecols=[0, 1, 2, 3, 4], parse_dates=[0]
)
monkey_edges_df["DateTime"] = pd.to_datetime(monkey_edges_df["DateTime"]).astype(
    "datetime64[ms]"
)
monkey_edges_df.dropna(axis=0, inplace=True)
monkey_edges_df["Weight"] = monkey_edges_df["Category"].apply(
    lambda c: 1 if (c == "Affiliative") else (-1 if (c == "Agonistic") else 0)
)

print("Monkey Interactions:")
print(f"{monkey_edges_df}\n")

monkey_graph = Graph()
monkey_graph.load_edges_from_pandas(
    df=monkey_edges_df,
    src="Actor",
    dst="Recipient",
    time="DateTime",
    layer="Behavior",
    props=["Weight"],
)

Output

Network Traffic Edges:
                  timestamp   source  ...          transaction_type  is_encrypted
0 2023-09-01 08:00:00+00:00  ServerA  ...   Critical System Request          True
1 2023-09-01 08:05:00+00:00  ServerA  ...             File Transfer         False
2 2023-09-01 08:10:00+00:00  ServerB  ...  Standard Service Request          True
3 2023-09-01 08:15:00+00:00  ServerD  ...    Administrative Command         False
4 2023-09-01 08:20:00+00:00  ServerC  ...   Critical System Request          True
5 2023-09-01 08:25:00+00:00  ServerE  ...             File Transfer         False
6 2023-09-01 08:30:00+00:00  ServerD  ...  Standard Service Request          True

[7 rows x 6 columns]

Network Traffic Servers:
                  timestamp server_id  ...    primary_function uptime_days
0 2023-09-01 08:00:00+00:00   ServerA  ...            Database         120
1 2023-09-01 08:05:00+00:00   ServerB  ...          Web Server          45
2 2023-09-01 08:10:00+00:00   ServerC  ...        File Storage          90
3 2023-09-01 08:15:00+00:00   ServerD  ...  Application Server          60
4 2023-09-01 08:20:00+00:00   ServerE  ...              Backup          30

[5 rows x 7 columns]

Monkey Interactions:
                DateTime    Actor Recipient      Behavior     Category  Weight
15   2019-06-13 09:50:00   ANGELE    FELIPE      Grooming  Affiliative       1
17   2019-06-13 09:50:00   ANGELE    FELIPE      Grooming  Affiliative       1
19   2019-06-13 09:51:00   FELIPE    ANGELE       Resting  Affiliative       1
20   2019-06-13 09:51:00   FELIPE      LIPS       Resting  Affiliative       1
21   2019-06-13 09:51:00   ANGELE    FELIPE      Grooming  Affiliative       1
...                  ...      ...       ...           ...          ...     ...
5370 2019-07-10 11:02:00  ARIELLE      LIPS      Touching  Affiliative       1
5371 2019-07-10 11:05:00     LIPS     NEKKE  Playing with  Affiliative       1
5372 2019-07-10 11:05:00     LIPS    FELIPE       Resting  Affiliative       1
5373 2019-07-10 11:05:00     LIPS     NEKKE       Resting  Affiliative       1
5374 2019-07-10 11:05:00     LIPS    FELIPE       Resting  Affiliative       1

[3196 rows x 6 columns]