{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"train = pd.read_csv(\"../input/conways-reverse-game-of-life-2020/train.csv\", index_col='id').values\ntrain_delta = train[:,0]\nX = train[:, 1:626].reshape((-1, 25, 25))\ny = train[:, 626:].reshape((-1, 25, 25))\nprint(X.shape)\nprint(y.shape)\nprint(train_delta.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def life_step(X):\n    \"\"\"Game of life step using scipy tools\"\"\"\n    X_pad = np.zeros((X.shape[0], X.shape[1]+2, X.shape[2]+2))\n    N = np.zeros((X.shape[0], X.shape[1]+2, X.shape[2]+2))\n    X_pad[:,1:-1,1:-1] += X\n    \n    N[:, 1:, 1:] += X_pad[:,:-1,:-1]\n    N[:, 1:, :] += X_pad[:,:-1,:]\n    N[:, 1:, :-1] += X_pad[:,:-1,1:]\n\n    N[:, :, 1:] += X_pad[:,:,:-1]\n    N[:, :, :-1] += X_pad[:,:,1:]\n\n    N[:, :-1, 1:] += X_pad[:,1:,:-1]\n    N[:, :-1, :] += X_pad[:,1:,:]\n    N[:, :-1, :-1] += X_pad[:,1:,1:]\n    \n    N = N[:,1:-1,1:-1]\n\n    return np.logical_or(N == 3, np.logical_and(X, N==2)).astype(np.uint8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"idx = 4\nprint(\"Number of steps for this example: \", train_delta[idx])\n\nprediction = np.expand_dims(X[idx], 0)\nfor i in range(train_delta[idx]):\n    prediction = life_step(prediction)\n    \nprediction = prediction[0]\n\nplt.figure()\n\n#subplot(r,c) provide the no. of rows and columns\nf, axarr = plt.subplots(1,3) \n\n# use the created array to output your multiple images. In this case I have stacked 4 images vertically\naxarr[0].imshow(X[idx])\naxarr[0].set_title('Initial state')\naxarr[1].imshow(y[idx])\naxarr[1].set_title('Final state')\naxarr[2].imshow(prediction)\naxarr[2].set_title('Predicted')\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Conclusion: The view is on a 25x25 grid but the simulation runs on a bigger grid"}],"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}