{"cells":[{"metadata":{"trusted":true,"collapsed":true,"_uuid":"e22784b3d352c764ff11584cf30bf306f40a304e"},"cell_type":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport glob\nfrom IPython.display import display","execution_count":44,"outputs":[]},{"metadata":{"_uuid":"6589bade719df21dc1aa826ad6b6016bef2914ca"},"cell_type":"markdown","source":"### Many of the snippets are taken from [wesamelshamy](https://www.kaggle.com/wesamelshamy/trackml-problem-explanation-and-data-exploration)"},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"collapsed":true},"cell_type":"code","source":"from trackml.dataset import load_event\nfrom trackml.randomize import shuffle_hits\nfrom trackml.score import score_event\n\nimport matplotlib.pyplot as plt\nfrom mpl_toolkits.mplot3d import Axes3D\nimport seaborn as sns\n%matplotlib inline","execution_count":45,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"6c2f80071339ecfceca4c57aef82cc91fb65747c"},"cell_type":"code","source":"### Check the number of events ###\nctmp = '../input/train_1/'\nprint(len(glob.glob(ctmp+'*-hits.csv')))\nprint(os.listdir('../input/train_1')[:5])","execution_count":46,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"ce36d8904774ea1298e9328e40e773f358c6a116"},"cell_type":"code","source":"event_prefix = 'event000001000'\nhits, cells, particles, truth = load_event(os.path.join('../input/train_1', event_prefix))","execution_count":47,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"905edd44a71ef797a924890a8155d6bd4a13e6fa"},"cell_type":"code","source":"### number of hits in this event ###\nprint(len(hits))\ndisplay(hits.head(5))","execution_count":48,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"54a826d89bc6648b4953155a646200fbee37f27f"},"cell_type":"code","source":"volumes = hits.volume_id.unique()\nprint((volumes))","execution_count":49,"outputs":[]},{"metadata":{"_uuid":"2426c95117d36608978abd19270ea0e2c907172b"},"cell_type":"markdown","source":"### Volume-wise distribution "},{"metadata":{"trusted":true,"_uuid":"9f59713fdc6c4bea1855cd348a7d1fc1f771a507"},"cell_type":"code","source":"g = sns.jointplot(hits.x, hits.y,  s=1, size=12) ## This provides univariate and bivariate plots.\n#plt.plot()\ng.ax_joint.cla() #clear current axes of sns\nplt.sca(g.ax_joint) #set current axes of plt\n\nvolumes = hits.volume_id.unique()\nfor volume in volumes:\n    v = hits[hits.volume_id == volume]\n    plt.scatter(v.x, v.y, s=3, label='volume {}'.format(volume))\n\nplt.xlabel('X (mm)')\nplt.ylabel('Y (mm)')\nplt.legend()\nplt.show()","execution_count":50,"outputs":[]},{"metadata":{"_uuid":"771e061c47f5f76acd8f3bd235000e2af4bafe0d"},"cell_type":"markdown","source":"###  Particle Data"},{"metadata":{"trusted":true,"_uuid":"bfd28d8f2db6699e94f2e3bba114ad677da35b1a"},"cell_type":"code","source":"particles.head()","execution_count":51,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cae85e6357f052c786197ae233f4dac70ce8edee"},"cell_type":"code","source":"# a quick check\npos_hit = sum(particles.nhits.values)\nneg_hit = len(particles.nhits.values==0)\nprint((pos_hit+neg_hit))\nprint(len(hits)) #the difference may be because of spurious hits","execution_count":52,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8fae46b37f5441401831a1da48e63d57dbba3a7d"},"cell_type":"code","source":"plt.figure(figsize=(15, 5))\nplt.subplot(1, 2, 1)\nsns.distplot(particles.nhits.values, axlabel='Hits/Particle', bins=50)\nplt.title('Distribution of number of hits per particle for event 1000.')\nplt.subplot(1, 2, 2)\nplt.pie(particles.groupby('q')['vx'].count(),\n        labels=['negative', 'positive'],\n        autopct='%.0f%%',\n        shadow=True,\n        radius=0.8)\nplt.title('Distribution of particle charges.')\nplt.show()","execution_count":53,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"f415ff2c62dd988fc4372d3cf8b01405d958935a"},"cell_type":"markdown","source":"###  Ground truth"},{"metadata":{"trusted":true,"_uuid":"72c0dd279310a5b7586209597774dde74192974a"},"cell_type":"code","source":"truth.head()","execution_count":54,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"cba04c1997b26bdf5e3db350e852b1fef72eef9e"},"cell_type":"code","source":"# Get particle id with max number of hits in this event\nparticle = particles.loc[particles.nhits == particles.nhits.max()].iloc[0]\nparticle2 = particles.loc[particles.nhits == particles.nhits.max()].iloc[1]\n\n# Get points where the same particle intersected subsequent layers of the observation material\np_traj_surface = truth[truth.particle_id == particle.particle_id][['tx', 'ty', 'tz']]\np_traj_surface2 = truth[truth.particle_id == particle2.particle_id][['tx', 'ty', 'tz']]","execution_count":55,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"17a1fde4a752ac15c1e9f770929950149af60e74"},"cell_type":"code","source":"print(p_traj_surface)","execution_count":56,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a48dab2f71e02795f57ab2ec72331f5c2219a449"},"cell_type":"code","source":"len(particles.loc[particles.nhits == particles.nhits.max()])","execution_count":57,"outputs":[]},{"metadata":{"_uuid":"43dee8f2dc8ffd837f422f76d3b1520f8ecc65c3"},"cell_type":"markdown","source":"##### Dataset contains co-ordinates of hits and also the location where they are generated. The location where they are generated is the true location, which the simulator knows, but while test time it won't be given. Only the locations where they are detected, i.e. the 'hits' will be provided. One can see that they differ at few places only. :)"},{"metadata":{"trusted":true,"_uuid":"8798bf74385d621eb0752aa3de9f1643491c77a2"},"cell_type":"code","source":"# Get particle id with max number of hits in this event\nparticle = particles.loc[particles.nhits == particles.nhits.max()-5].iloc[19]\n\n# Get points where the same particle intersected subsequent layers of the observation material\np_traj_surface = truth[truth.particle_id == particle.particle_id][['tx', 'ty', 'tz']]\n\np_traj = (p_traj_surface\n          .append({'tx': particle.vx, 'ty': particle.vy, 'tz': particle.vz}, ignore_index=True)\n          .sort_values(by='tz'))\n\nfig = plt.figure(figsize=(10, 10))\nax = fig.add_subplot(111, projection='3d')\n\nax.plot(\n    xs=p_traj.tx,\n    ys=p_traj.ty,\n    zs=p_traj.tz, marker='o')\nax.plot(\n    xs=p_traj_surface.tx,\n    ys=p_traj_surface.ty,\n    zs=p_traj_surface.tz, marker='o')\n\nax.set_xlabel('X (mm)')\nax.set_ylabel('Y (mm)')\nax.set_zlabel('Z  (mm) -- Detection layers')\nplt.title('Trajectories of two particles as they cross the detection surface ($Z$ axis).')\nplt.show()","execution_count":58,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"70c1cb28e07d48000cd4b870818d408fd4092fd9"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.5","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}