{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# TPS1022 Game Animation Sample","metadata":{"papermill":{"duration":0.009834,"end_time":"2022-09-02T13:30:23.713866","exception":false,"start_time":"2022-09-02T13:30:23.704032","status":"completed"},"tags":[]}},{"cell_type":"code","source":"!conda install -c conda-forge python-kaleido -y -q","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:51:29.831124Z","iopub.execute_input":"2022-10-02T10:51:29.831813Z","iopub.status.idle":"2022-10-02T10:52:07.177444Z","shell.execute_reply.started":"2022-10-02T10:51:29.831771Z","shell.execute_reply":"2022-10-02T10:52:07.176201Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport cv2\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport plotly.express as px\nimport plotly.graph_objects as go\nfrom plotly.subplots import make_subplots\nfrom matplotlib import animation, rc\nrc('animation', html='jshtml')","metadata":{"papermill":{"duration":2.648732,"end_time":"2022-09-02T13:30:26.736587","exception":false,"start_time":"2022-09-02T13:30:24.087855","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-02T10:52:07.179942Z","iopub.execute_input":"2022-10-02T10:52:07.180285Z","iopub.status.idle":"2022-10-02T10:52:07.187160Z","shell.execute_reply.started":"2022-10-02T10:52:07.180255Z","shell.execute_reply":"2022-10-02T10:52:07.186217Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data=pd.read_csv('../input/tabular-playground-series-oct-2022/train_0.csv')\ndisplay(data)\nprint(data.columns.tolist())\ndata0=data.iloc[0:1000]","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:07.188413Z","iopub.execute_input":"2022-10-02T10:52:07.189189Z","iopub.status.idle":"2022-10-02T10:52:23.226351Z","shell.execute_reply.started":"2022-10-02T10:52:07.189155Z","shell.execute_reply":"2022-10-02T10:52:23.224059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(specs=[[{\"secondary_y\":False}]])\nfig.add_trace(go.Scatter(x=data0['ball_pos_x'],y=data0['ball_pos_y'],name='ball_pos_xy'),secondary_y=False,)\nfig.update_layout(autosize=False,width=700,height=500,title_text=\"ball_pos_xy\")\nfig.update_xaxes(title_text='ball_pos_x')\nfig.update_yaxes(title_text='ball_pos_y',secondary_y=False)\nfig.show()","metadata":{"papermill":{"duration":0.729353,"end_time":"2022-09-02T14:31:35.722211","exception":false,"start_time":"2022-09-02T14:31:34.992858","status":"completed"},"tags":[],"execution":{"iopub.status.busy":"2022-10-02T10:52:23.227965Z","iopub.execute_input":"2022-10-02T10:52:23.228650Z","iopub.status.idle":"2022-10-02T10:52:23.257024Z","shell.execute_reply.started":"2022-10-02T10:52:23.228613Z","shell.execute_reply":"2022-10-02T10:52:23.255828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(specs=[[{\"secondary_y\":False}]])\nfig.add_trace(go.Scatter(x=data0['ball_vel_x'],y=data0['ball_vel_y'],name='ball_vel_xy'),secondary_y=False,)\nfig.update_layout(autosize=False,width=700,height=500,title_text=\"ball_vel_xy\")\nfig.update_xaxes(title_text='ball_vel_x')\nfig.update_yaxes(title_text='ball_vel_y',secondary_y=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:23.260515Z","iopub.execute_input":"2022-10-02T10:52:23.260897Z","iopub.status.idle":"2022-10-02T10:52:23.290663Z","shell.execute_reply.started":"2022-10-02T10:52:23.260835Z","shell.execute_reply":"2022-10-02T10:52:23.289730Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(specs=[[{\"secondary_y\":False}]])\nfig.add_trace(go.Scatter(x=data0['p0_pos_x'],y=data0['p0_pos_y'],name='p0_pos_xy'),secondary_y=False,)\nfig.update_layout(autosize=False,width=700,height=500,title_text=\"p0_pos_xy\")\nfig.update_xaxes(title_text='p0_pos_x')\nfig.update_yaxes(title_text='p0_pos_y',secondary_y=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:23.292036Z","iopub.execute_input":"2022-10-02T10:52:23.292419Z","iopub.status.idle":"2022-10-02T10:52:23.322247Z","shell.execute_reply.started":"2022-10-02T10:52:23.292387Z","shell.execute_reply":"2022-10-02T10:52:23.321341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(specs=[[{\"secondary_y\":False}]])\nfig.add_trace(go.Scatter(x=data0['p0_vel_x'],y=data0['p0_vel_y'],name='p0_vel_xy'),secondary_y=False,)\nfig.update_layout(autosize=False,width=700,height=500,title_text=\"p0_vel_xy\")\nfig.update_xaxes(title_text='p0_vel_x')\nfig.update_yaxes(title_text='p0_vel_y',secondary_y=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:23.323626Z","iopub.execute_input":"2022-10-02T10:52:23.324059Z","iopub.status.idle":"2022-10-02T10:52:23.353916Z","shell.execute_reply.started":"2022-10-02T10:52:23.324021Z","shell.execute_reply":"2022-10-02T10:52:23.353036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(specs=[[{\"secondary_y\":False}]])\nfig.add_trace(go.Scatter(x=data0['event_time'],y=data0['p0_vel_x'],name='p0_vel_x'),secondary_y=False,)\nfig.update_layout(autosize=False,width=700,height=500,title_text=\"p0_vel_x\")\nfig.update_xaxes(title_text='event_time')\nfig.update_yaxes(title_text='p0_vel_x',secondary_y=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:23.355947Z","iopub.execute_input":"2022-10-02T10:52:23.356290Z","iopub.status.idle":"2022-10-02T10:52:23.384333Z","shell.execute_reply.started":"2022-10-02T10:52:23.356257Z","shell.execute_reply":"2022-10-02T10:52:23.383525Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=make_subplots(specs=[[{\"secondary_y\":False}]])\nfig.add_trace(go.Scatter(x=data0['event_time'],y=data0['p0_vel_y'],name='p0_vel_y'),secondary_y=False,)\nfig.update_layout(autosize=False,width=700,height=500,title_text=\"p0_vel_y\")\nfig.update_xaxes(title_text='event_time')\nfig.update_yaxes(title_text='p0_vel_y',secondary_y=False)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:23.385978Z","iopub.execute_input":"2022-10-02T10:52:23.386351Z","iopub.status.idle":"2022-10-02T10:52:23.414123Z","shell.execute_reply.started":"2022-10-02T10:52:23.386318Z","shell.execute_reply":"2022-10-02T10:52:23.413038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!rm -rf figs\n!mkdir figs","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:23.454991Z","iopub.execute_input":"2022-10-02T10:52:23.455382Z","iopub.status.idle":"2022-10-02T10:52:25.413423Z","shell.execute_reply.started":"2022-10-02T10:52:23.455347Z","shell.execute_reply":"2022-10-02T10:52:25.412024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for j in range(100):\n    i=j*3\n    fig=make_subplots(specs=[[{\"secondary_y\":False}]])\n    fig.add_trace(go.Scatter(x=[data.loc[i,'ball_pos_x']],y=[data.loc[i,'ball_pos_y']],name='ball_pos_xy'),secondary_y=False,)\n    fig.add_trace(go.Scatter(x=[data.loc[i,'p0_pos_x']],y=[data.loc[i,'p0_pos_y']],name='p0_pos_xy'),secondary_y=False,)\n    fig.add_trace(go.Scatter(x=[data.loc[i,'p1_pos_x']],y=[data.loc[i,'p1_pos_y']],name='p1_pos_xy'),secondary_y=False,)\n    fig.add_trace(go.Scatter(x=[data.loc[i,'p2_pos_x']],y=[data.loc[i,'p2_pos_y']],name='p2_pos_xy'),secondary_y=False,)\n    fig.add_trace(go.Scatter(x=[data.loc[i,'p3_pos_x']],y=[data.loc[i,'p3_pos_y']],name='p3_pos_xy'),secondary_y=False,)\n    fig.add_trace(go.Scatter(x=[data.loc[i,'p4_pos_x']],y=[data.loc[i,'p4_pos_y']],name='p4_pos_xy'),secondary_y=False,)\n    fig.add_trace(go.Scatter(x=[data.loc[i,'p5_pos_x']],y=[data.loc[i,'p5_pos_y']],name='p5_pos_xy'),secondary_y=False,)\n    fig.update_layout(autosize=False,width=700,height=500,title_text=\"ball,p0,p1,p2,p3,p4,p5_pos_xy\")\n    fig.update_xaxes(title_text='pos_x')\n    fig.update_yaxes(title_text='pos_y',secondary_y=False)\n    fig.write_image(\"./figs/\"+str(i).zfill(5)+\".png\")\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:25.417397Z","iopub.execute_input":"2022-10-02T10:52:25.417716Z","iopub.status.idle":"2022-10-02T10:52:26.325961Z","shell.execute_reply.started":"2022-10-02T10:52:25.417685Z","shell.execute_reply":"2022-10-02T10:52:26.325047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"impaths0=[]\nfiles=os.listdir('./figs')\nfor item in files:\n    impaths0+=[os.path.join('./figs',item)]\nimpaths=sorted(impaths0)\n\nimages1=[]\nfor item in impaths:\n    images1+=[plt.imread(item)]   ","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:26.339226Z","iopub.status.idle":"2022-10-02T10:52:26.340293Z","shell.execute_reply.started":"2022-10-02T10:52:26.340014Z","shell.execute_reply":"2022-10-02T10:52:26.340040Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def create_animation(ims):    \n    fig=plt.figure(figsize=(15,12))\n    im=plt.imshow(ims[0]) \n    plt.axis('off')\n    plt.close() ####\n    \n    def animate_func(i):\n        im.set_array(ims[i])\n        return [im]\n\n    return animation.FuncAnimation(fig, animate_func, frames=len(ims), interval=1000//5)","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:26.342509Z","iopub.status.idle":"2022-10-02T10:52:26.343098Z","shell.execute_reply.started":"2022-10-02T10:52:26.342825Z","shell.execute_reply":"2022-10-02T10:52:26.342849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"create_animation(images1)","metadata":{"execution":{"iopub.status.busy":"2022-10-02T10:52:26.344818Z","iopub.status.idle":"2022-10-02T10:52:26.345704Z","shell.execute_reply.started":"2022-10-02T10:52:26.345268Z","shell.execute_reply":"2022-10-02T10:52:26.345292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}