{"cells":[{"metadata":{},"cell_type":"markdown","source":"# Game of life EDA\nIn this 2D grid environement that is the game of life, cell values are influenced by neighboring cells. This was originally meant to model natural interactions between neighboring animals, persons, plants..\nThe fascinating aspect of the game of life is the relative simplicity of the world's rules that yet result in complex and elaborate patterns.\nIn this notebook, we will explore the data, plot grids and delta distribution.","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport pandas as pd\nimport numpy as np\nimport seaborn as sns\nfrom tqdm.notebook import tqdm","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train_csv = pd.read_csv(\"../input/conways-reverse-game-of-life-2020/train.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train_csv.head()","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"## Grid display\nWe will first print a few grids to see what they look like.","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"col = train_csv.columns\nstart_col_filtered = [c for c in col if \"start_\" in c]\nend_col_filtered = [c for c in col if \"stop_\" in c]\nstart_grids = train_csv[start_col_filtered].to_numpy()\nstop_grids = train_csv[end_col_filtered].to_numpy()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_grid(list_of_cells, shape=(25,25), plot=True):\n    mat = list_of_cells.reshape(shape)\n    if (plot):\n        sns.heatmap(mat)\n    return mat\n    \ndef plot_grids(idx):\n    start_cells, end_cells = start_grids[idx], stop_grids[idx]\n    start_mat = get_grid(start_cells, plot=False)\n    end_mat = get_grid(end_cells, plot=False)\n    \n    fig, axes = plt.subplots(1,2, figsize=(25,10))\n    sns.heatmap(start_mat, ax=axes[0])\n    axes[0].set_title(\"Starting board\")\n    sns.heatmap(end_mat, ax=axes[1])\n    axes[1].set_title(\"Ending board\")\n    \nidx = 5\nplot_grids(idx)\n\nidx = 2\nplot_grids(idx)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We notice, in these two plots, some similarities betwee the starting and ending board. We will then plot a change detection heatmap to see better how those interactions play out.\nGiven a pixel $p$ defined by coordinates $(x,y)$, it is a vector of size 2 written as $(p_{start}, p_{end})$ where $p_{start}, p_{end} \\in \\{0,1\\}$. Given this, 4 possible observations exist:\n - $p = (0,0)$ -> the cell was **empty in both start and end board**. We attribute it the color black and value 0;\n - $p = (1,0)$ -> the cell was **taken in the start board** but **absent in the end board**. We attribute it the color red and value 1;\n - $p = (0,1)$ -> the cell was **empty in the start board** but **taken in the end board**. We attribute it the color green and value 2;\n - $p = (1,1)$ -> the cell was **taken in both start and end board**. We attribute it the color yellow and value 3.\n","execution_count":null},{"metadata":{"trusted":true},"cell_type":"code","source":"def get_diff(idx, plot=True):\n    start_cells, end_cells = start_grids[idx], stop_grids[idx]\n    start_grid = get_grid(start_cells, plot=False)\n    end_grid = get_grid(end_cells, plot=False)\n    \n    img = np.zeros((25,25,3))\n    img[:,:,0] = start_grid #every red pixel was pixel present in start grid and absent in end grid\n    img[:,:,1] = end_grid #every green pixel was pixel present in end grid and absent in start grid\n    # every black pixel was absent in both images\n    # every yellow pixel was present in both images\n    diff_mat = np.zeros((25,25))\n    diff_mat = start_grid + 2*end_grid\n    if plot:\n        plt.imshow(img)\n    \n    return diff_mat\n    \ndiff_mat = get_diff(3)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"values = []\nfor idx in tqdm(range(len(start_grids))):\n    diff_mat = get_diff(idx, plot=False)\n    values.extend(list(diff_mat.flatten()))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plt.hist(values)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We now plot an histogram of these 4 values and notice that, apart from empty pixels, the rarest scenario is to have pixels illuminated at start and at the end. ","execution_count":null}],"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}