{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 5GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"import zipfile\n\nzf = zipfile.ZipFile('../input/bosch-production-line-performance/train_date.csv.zip') \ntrain_date_chunks = pd.read_csv(zf.open('train_date.csv'), iterator=True, chunksize=100000)\npd.options.display.max_columns = None\npd.options.display.max_rows = None\npd.options.display.max_colwidth = None","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_date_frame():\n    for data_frame in train_date_chunks:\n        yield data_frame\n        \nget_df_date = get_date_frame()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"df_date = next(get_df_date)\n\nstation_list = []\nfirst_features_in_each_station = [] \n\ndf_date_columns = df_date.columns.tolist()\n\nfor feature in df_date_columns[1:]:\n    station = feature[:feature.index('_D')]\n    if station in station_list:\n        continue\n    else:\n        station_list.append(station)\n        first_features_in_each_station.append(feature)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"global_station_pairs = {}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"while True:\n    temp_df = pd.DataFrame (np.array(df_date[first_features_in_each_station]), columns = station_list)\n    station_list_for_each_part = temp_df.stack().reset_index(level=1).groupby(level=0, sort=False)['level_1'].apply(list)\n    \n    \n    temp_station_df = pd.DataFrame({\"Id\": np.array(df_date[\"Id\"])})\n    temp_station_df[\"Stations\"] = station_list_for_each_part\n    \n    try:\n        station_df = station_df.append(temp_station_df, ignore_index=True)\n    except:\n        station_df = pd.DataFrame({\"Id\": np.array(df_date[\"Id\"])})\n        station_df[\"Stations\"] = station_list_for_each_part\n    \n    \n    for each_part in station_list_for_each_part:\n        for station_cursor in range(1, len(each_part)):\n            pair = (each_part[station_cursor-1], each_part[station_cursor])\n            try:\n                global_station_pairs[pair] += 1\n            except:\n                global_station_pairs[pair] = 1\n                \n    try:\n        df_date = next(get_df_date)\n    except:\n        break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"station_df.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"global_station_pairs = sorted(global_station_pairs.items())","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"global_station_pairs","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"node_occurrences = {}\nfor line in global_station_pairs:\n    pair = line[0]\n    try:\n        node_occurrences[pair[0]] += 1\n    except:\n        node_occurrences[pair[0]] = 1\n    try:\n        node_occurrences[pair[1]] += 1\n    except:\n        node_occurrences[pair[1]] = 1","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"node_occurrences","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"'''\nstation_df.to_csv('part_station_info.csv', index=False)  \n\nwith zipfile.ZipFile('part_station_info.zip', 'w') as zipObj:\n    zipObj.write(\"./part_station_info.csv\")\n    print(\"part_station_info.csv file is successfuly zipped\")\n\ntry:\n    os.remove(\"./part_station_info.csv\")\n    print(\"part_station_info.csv file is successfuly removed\")\nexcept:\n    print(\"No such file\")\n\n'''","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import networkx as nx\nimport matplotlib.pyplot as plt\n\nimport plotly.offline as py\nimport plotly.graph_objects as go","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_pair_list(station_list):\n    pair_list = []\n    for each in station_list:\n        for station_cursor in range(1, len(each)):\n            pair = (each[station_cursor-1], each[station_cursor])\n            pair_list.append(pair)\n    return pair_list","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def draw_station_pairs_in_a_part(pair_list, title):\n    g = nx.Graph()\n    for pair in pair_list:\n        g.add_node(pair[0])\n        g.add_node(pair[1])\n        g.add_edge(pair[0], pair[1])\n    \n    fig = plt.figure(figsize=(10,5))\n    nx.draw(g, with_labels=True)\n    plt.title(title)\n    plt.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random_sample = station_df.sample()\nrandom_sample","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_station_pairs_in_a_part(create_pair_list(random_sample['Stations']), int(random_sample['Id']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"random_sample = station_df.sample()\nrandom_sample","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"draw_station_pairs_in_a_part(create_pair_list(random_sample['Stations']), int(random_sample['Id']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"g = nx.Graph()\nfor pair in global_station_pairs:\n    stations = pair[0]\n\n    g.add_node(stations[0], size = node_occurrences[stations[0]])\n    g.add_node(stations[1], size = node_occurrences[stations[1]])\n    g.add_edge(pair[0][0], pair[0][1], occurrence = pair[1])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"pos_ = nx.spring_layout(g)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def make_edge(x, y, text, width):\n    return  go.Scatter(x         = x,\n                       y         = y,\n                       line      = dict(width = width,\n                                   color = 'red'),\n                       hoverinfo = 'text',\n                       text      = ([text]),\n                       mode      = 'lines')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"edge_trace = []\nfor edge in g.edges():\n    ch1 = edge[0]\n    ch2 = edge[1]\n    \n    x0, y0 = pos_[ch1]\n    x1, y1 = pos_[ch2]\n    \n    trace  = make_edge([x0, x1, None], \n                       [y0, y1, None], \n                       text = g.edges()[edge][\"occurrence\"],\n                       width = 0.000005*g.edges()[edge][\"occurrence\"])\n    edge_trace.append(trace)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"node_trace = go.Scatter(x         = [],\n                        y         = [],\n                        text      = [],\n                        textposition = \"top center\",\n                        textfont_size = 10,\n                        mode      = 'markers+text',\n                        hoverinfo = 'none',\n                        marker    = dict(color = [],\n                                         size  = [],\n                                         line  = None))\n\nfor node in g.nodes():\n    x, y = pos_[node]\n    node_trace['x'] += tuple([x])\n    node_trace['y'] += tuple([y])\n    node_trace['marker']['color'] += tuple(['cornflowerblue'])\n    node_trace['marker']['size'] += tuple([1*g.nodes()[node]['size']])\n    node_trace['text'] += tuple(['<b>' + node + '</b>'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# Customize layout\nlayout = go.Layout(\n    paper_bgcolor='rgba(0,0,0,0)', # transparent background\n    plot_bgcolor='rgba(0,0,0,0)', # transparent 2nd background\n    xaxis =  {'showgrid': False, 'zeroline': False}, # no gridlines\n    yaxis = {'showgrid': False, 'zeroline': False}, # no gridlines\n)\n# Create figure\nfig = go.Figure(layout = layout)\n# Add all edge traces\nfor trace in edge_trace:\n    fig.add_trace(trace)\n# Add node trace\nfig.add_trace(node_trace)\n# Remove legend\nfig.update_layout(showlegend = False)\n# Remove tick labels\nfig.update_xaxes(showticklabels = False)\nfig.update_yaxes(showticklabels = False)\n# Show figure\nfig.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(20,20))\nnx.draw(g, with_labels=True)\nplt.show()","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}