{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom numpy.fft import fft\nfrom statsmodels.tsa.arima_model import ARIMA\nimport os\nprint(os.listdir(\"../input\"))\nimport io\nimport base64\nfrom IPython.display import HTML\nimport matplotlib.animation as animation","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"train_df = pd.read_csv('../input/training_set.csv')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"26fbf2354024a693973eee66616876ea410d7c97"},"cell_type":"markdown","source":"At this notebook I tried to visualize evolution of object flux in very naive way. "},{"metadata":{"trusted":true,"_uuid":"d44914a95e833a5647d6bb117cd47fc13c85c9f0"},"cell_type":"code","source":"def calc_frames(train_df, object_id):\n    object_vals = train_df[train_df.object_id == object_id].copy()\n    \n    object_vals.mjd = object_vals.mjd.round()\n    \n    start_time = object_vals.mjd.min()\n    end_time = object_vals.mjd.max()\n    \n    cur_time = start_time - 3\n    \n    max_frames = int((end_time - start_time)//3)\n    frames = np.zeros((max_frames, 6))*np.nan\n    \n    frame_time = np.zeros(max_frames)\n\n    for i in range(max_frames):\n        cur_time = cur_time + 3\n        frames[i, :] = np.array([object_vals[(object_vals.passband == b) & ((object_vals.mjd - cur_time).abs() < 3)].pipe(lambda x: np.nan if x.shape[0] == 0 else x.flux.iloc[0])  for b in range(6)])        \n        frame_time[i] = cur_time\n    \n    frame_time = frame_time[~np.isnan(frames).all(axis = 1)]\n    frames = frames[~np.isnan(frames).all(axis = 1)]\n    \n    def _nan_helper(y):\n        return np.isnan(y), lambda z: z.nonzero()[0]\n\n    nans, x = _nan_helper(frames)\n    frames[nans]= np.interp(x(nans), x(~nans), frames[~nans])\n\n    return frames, frame_time","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c5eecf2eb9c7cab0ff296919a25d3ac05a79cfe8"},"cell_type":"code","source":"fr, tm = calc_frames(train_df, 53925325)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b4508e3300f465b372fe147ffc60e7656f24d99a"},"cell_type":"code","source":"def animate(frames, frame_time, name):\n    fig, ax = plt.subplots()\n\n    x = [350, 500, 600, 700, 875, 1000] \n    y = [-1000, 1000, 0,0,0,0]\n\n    line, = ax.plot(x, y, lw=2)\n    text = ax.text(0.85, 0.95,  '', transform=ax.transAxes)\n\n    def init():\n        line.set_data(x, [np.nan] * 6)\n        return line,\n\n    def animate(i):\n        line.set_ydata(frames[i, :])\n        text.set_text(frame_time[i])\n        return line,\n\n    ani = animation.FuncAnimation(fig, animate, init_func=init, interval=2,save_count=50, frames=frames.shape[0], blit=True)\n\n    plt.close()\n\n    filename = name\n    ani.save(filename, writer='imagemagick', fps=8)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a34d0676911aa80905562ad117feff06d9ea04b2"},"cell_type":"code","source":"animate(*calc_frames(train_df, 615), \"615.gif\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8c34790291bec99321ce53a5f39ef710e4810751"},"cell_type":"code","source":"filename = '615.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e6819abe130e8c9b633da1eccb2ade92771db240"},"cell_type":"code","source":"animate(*calc_frames(train_df, 53925325), \"53925325.gif\")\nfilename = '53925325.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"b8d9e89e71a10da8f8eb2f73b5548e3e6ea51e0a"},"cell_type":"code","source":"animate(*calc_frames(train_df, 713), \"713.gif\")\nfilename = '713.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"da3c1b90e8aadd3c16379b7999ef76c43c885815"},"cell_type":"code","source":"train_df.object_id.unique()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c793e4d93f1bc8de8b4053c9c46a75c0826bd58f"},"cell_type":"code","source":"animate(*calc_frames(train_df, 730), \"730.gif\")\nfilename = '730.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"4e1b2da1c20e84e12fcaced48052cf46dd9f2044"},"cell_type":"code","source":"animate(*calc_frames(train_df, 130762946), \"130762946.gif\")\nfilename = '130762946.gif'\nvideo = io.open(filename, 'r+b').read()\nencoded = base64.b64encode(video)\nHTML(data='''<img src=\"data:image/gif;base64,{0}\" type=\"gif\" />'''.format(encoded.decode('ascii')))","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}