{ "cells": [ { "cell_type": "markdown", "id": "0c774b86", "metadata": {}, "source": [ "Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n", "\n", "SPDX-License-Identifier: Apache-2.0" ] }, { "cell_type": "markdown", "id": "14d32ce8", "metadata": {}, "source": [ "# Visualize the dynamic graph using Networkx\n", "\n", "This notebook provides visualizes the dynamic graph using Network X. The nodes are customers and merchants, and the edges are transactions between them.\n", "\n", "## Table of Contents\n", "1. Load processed node and edge data\n", "2. Load node attributes from raw data\n", "3. Build graph with networkx and visualize a specific snapshot\n" ] }, { "cell_type": "code", "execution_count": null, "id": "62e6e470", "metadata": {}, "outputs": [], "source": [ "import sys \n", "sys.path.append('../../src/anomaly_detection_spatial_temporal_data/')" ] }, { "cell_type": "code", "execution_count": null, "id": "7acb3990", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "from visualization.dynamic_graph import DynamicHomoGraph" ] }, { "cell_type": "code", "execution_count": null, "id": "f61d7ea1", "metadata": {}, "outputs": [], "source": [ "#rerun this cell again if your plot looks very small\n", "from matplotlib import pyplot as plt\n", "import matplotlib\n", "\n", "font = {'family': 'normal', 'size': 18}\n", "matplotlib.rc('font', **font)\n", "\n", "import matplotlib.cm as cm\n", "plt.rcParams[\"figure.figsize\"] = (30,18)" ] }, { "cell_type": "markdown", "id": "98f7f726", "metadata": {}, "source": [ "## Load node and edge data from 1.1 processing notebook" ] }, { "cell_type": "code", "execution_count": null, "id": "1a3e9f28", "metadata": {}, "outputs": [], "source": [ "edge_list_w_label_filepath = '../../data/02_intermediate/financial_fraud/edge_list.npz'\n", "node_id_mapping_filepath = '../../data/02_intermediate/financial_fraud/node_id.csv'" ] }, { "cell_type": "code", "execution_count": null, "id": "e4c0d631", "metadata": {}, "outputs": [], "source": [ "edge_list = np.load(edge_list_w_label_filepath)\n", "\n", "node_id = pd.read_csv(node_id_mapping_filepath)" ] }, { "cell_type": "markdown", "id": "2e8f3dc7", "metadata": {}, "source": [ "## Load node attribute value \n", "\n", "The customer/merchant IDs are used to color the nodes in the visualization." ] }, { "cell_type": "code", "execution_count": null, "id": "0337b689", "metadata": {}, "outputs": [], "source": [ "raw_data_path = '../../data/01_raw/financial_fraud/bs140513_032310.csv'\n", "\n", "raw_trans_data = pd.read_csv(raw_data_path)" ] }, { "cell_type": "code", "execution_count": null, "id": "ad63b59a", "metadata": {}, "outputs": [], "source": [ "customer_attribute_data = raw_trans_data[['step','customer','age','gender','zipcodeOri']]\n", "merchant_attribute_data = raw_trans_data[['step','merchant','zipMerchant','category']]" ] }, { "cell_type": "code", "execution_count": null, "id": "34068940", "metadata": {}, "outputs": [], "source": [ "# customer_attribute_data.head(10)" ] }, { "cell_type": "code", "execution_count": null, "id": "31a4386d", "metadata": {}, "outputs": [], "source": [ "# merchant_attribute_data.head(10)" ] }, { "cell_type": "code", "execution_count": null, "id": "5798b91c", "metadata": {}, "outputs": [], "source": [ "customer_attribute_data_deduped = customer_attribute_data.drop_duplicates(subset=['customer'], keep='last', )\n", "merchant_attribute_data_deduped = merchant_attribute_data.drop_duplicates(subset=['merchant'], keep='last', )" ] }, { "cell_type": "code", "execution_count": null, "id": "4dd9fc02", "metadata": {}, "outputs": [], "source": [ "# customer_attribute_data.shape, customer_attribute_data_deduped.shape" ] }, { "cell_type": "code", "execution_count": null, "id": "d10c3fd6", "metadata": {}, "outputs": [], "source": [ "# merchant_attribute_data.shape, merchant_attribute_data_deduped.shape" ] }, { "cell_type": "markdown", "id": "a4ce0552", "metadata": {}, "source": [ "## Build graph with networkx and visualize a specific snapshot" ] }, { "cell_type": "code", "execution_count": null, "id": "7747ea10", "metadata": {}, "outputs": [], "source": [ "dynamic_payment_network = DynamicHomoGraph(\n", " node_id, edge_list['data'], \n", " src_node_attribute=customer_attribute_data_deduped, \n", " dst_node_attribute=merchant_attribute_data_deduped, \n", " load_time_steps=10\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "d1ed1bdb", "metadata": {}, "outputs": [], "source": [ "# dynamic_payment_network._determine_object_type(4112)" ] }, { "cell_type": "code", "execution_count": null, "id": "8581c633", "metadata": {}, "outputs": [], "source": [ "dynamic_payment_network.draw_nx_graph_w_edge_label_at_specific_time(1)" ] }, { "cell_type": "markdown", "id": "bb5eae52", "metadata": {}, "source": [ "#### check some edge label" ] }, { "cell_type": "code", "execution_count": null, "id": "35da38ad", "metadata": {}, "outputs": [], "source": [ "edge_list['data'][np.where((edge_list['data'][:,0]==1444)&(edge_list['data'][:,1]==4138))[0], :],edge_list['data'][np.where((edge_list['data'][:,0]==3358)&(edge_list['data'][:,1]==4138))[0], :]" ] }, { "cell_type": "markdown", "id": "557ac4de", "metadata": {}, "source": [ "#### check node features" ] }, { "cell_type": "code", "execution_count": null, "id": "53e13daf", "metadata": {}, "outputs": [], "source": [ "dynamic_payment_network.src_node_static_attribute[1444],dynamic_payment_network.dst_node_static_attribute[4138]" ] }, { "cell_type": "code", "execution_count": null, "id": "499a44bc", "metadata": {}, "outputs": [], "source": [ "# plot a different snapshot\n", "dynamic_payment_network.draw_nx_graph_w_edge_label_at_specific_time(2)" ] }, { "cell_type": "code", "execution_count": null, "id": "92a99d23", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "kedro-taddy-venv", "language": "python", "name": "kedro-taddy-venv" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.12" } }, "nbformat": 4, "nbformat_minor": 5 }