%matplotlib inline
import pandas as pd
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt

"""
Explanation: Network analysis of the data.
The goal of this notebook is to uncover several constructions inside the dataset which may help us to uncover fraud. With that, we can see whether we can create features related to connections inside the network.
In this network analysis, we see if we can use the connections an account has in order to predict behavior. We will build a graph with the following properties:
- Every account is a node (internal and external)
- Every transaction is an edge.  
Once we have this, we can see if we can use different graph properties to see whether or not an account is suspicious of unwanted behaviour.
End of explanation
"""


client_info = pd.read_csv('data/client_info.csv')
demographic_info = pd.read_csv('data/demographic_data.csv')
transaction_info = pd.read_csv('data/transction_info.csv')
order_info = pd.read_csv('data/order_info.csv')

"""
Explanation: Load data.
End of explanation
"""


transaction_info['bank_account'] = transaction_info['bank'] + transaction_info['account'].map(str)

"""
Explanation: Creata a unique bank account (bank + account)
End of explanation
"""


G = nx.Graph()
G.add_nodes_from(transaction_info['account_id'].unique().tolist())
G.add_nodes_from(transaction_info['bank_account'][transaction_info['bank_account'].notnull()].unique())

"""
Explanation: Build the graph.
We initiate the graph, add the nodes from the internal account id and the non-zero external accounts.
End of explanation
"""


nonEmpty = transaction_info[transaction_info['bank_account'].notnull()]
edges = zip(nonEmpty['account_id'],nonEmpty['bank_account'], nonEmpty['amount'])
G.add_weighted_edges_from(edges)

"""
Explanation: Add non-empty edges.
End of explanation
"""


giant = max(nx.connected_component_subgraphs(G), key=len)
nx.draw_circular(giant)

"""
Explanation: Look at the largest connected components.
A connected component is a cluster of nodes who are connected by an edge. This can uncover certain structures of possible unwanted behavor.
End of explanation
"""


Gdi = nx.DiGraph()
Gdi.add_nodes_from(giant.nodes())
Gdi.add_edges_from(giant.edges())
nx.draw_circular(Gdi)

"""
Explanation: Look at it in a directed graph.
End of explanation
"""


