{"cells":[{"metadata":{"_uuid":"37eddf11071adc5c49377435a3e32b9b7086964a","_cell_guid":"a8c989d4-e387-4434-9ab8-eb74482e34e2"},"cell_type":"markdown","source":"# Visualizing the detectors"},{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","collapsed":true,"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n\n# import Axes3D for 3D visualization\nfrom mpl_toolkits.mplot3d import Axes3D","execution_count":9,"outputs":[]},{"metadata":{"_uuid":"5c18d1cc7996693849ce0bb10c6fc31d05ab4308","_cell_guid":"82970ff9-c1bf-4ea1-be92-41cc05c7d129"},"cell_type":"markdown","source":"Read the `detectors.csv` file:"},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"df = pd.read_csv('../input/detectors.csv')\n\ndf[['volume_id', 'layer_id', 'module_id', 'cx', 'cy', 'cz']].head()","execution_count":10,"outputs":[]},{"metadata":{"_uuid":"ff3f31197808d95ab103885109458921d6648ee6","_cell_guid":"e913cd12-10fd-4b1a-b0fb-30d86c1e63d0"},"cell_type":"markdown","source":"Get the maximum extent of each axis:"},{"metadata":{"_cell_guid":"83b3ca0c-347d-478f-af1a-f31ccd7e4d5c","_uuid":"818d7b401be0b30cd331dff76f803975782b9e60","trusted":true},"cell_type":"code","source":"x_min, x_max = df['cx'].min(), df['cx'].max()\ny_min, y_max = df['cy'].min(), df['cy'].max()\nz_min, z_max = df['cz'].min(), df['cz'].max()\n\nprint('x: %10.2f %10.2f' % (x_min, x_max))\nprint('y: %10.2f %10.2f' % (y_min, y_max))\nprint('z: %10.2f %10.2f' % (z_min, z_max))","execution_count":11,"outputs":[]},{"metadata":{"_uuid":"262122b09990043cc31eaf6597e8eec232218315","_cell_guid":"5cf0e16f-41b4-4ede-b3b9-fd463be06768"},"cell_type":"markdown","source":"Bring `cx`, `cy`, `cz` into a single column `xyz`:"},{"metadata":{"_cell_guid":"ef5ba28f-02df-4db7-94ab-e33f7b59245e","_uuid":"2e487e4a50eeb61e9f2a826d198d98899af3317b","trusted":true},"cell_type":"code","source":"df['xyz'] = df[['cx', 'cy', 'cz']].values.tolist()\n\ndf[['volume_id', 'layer_id', 'module_id', 'xyz']].head()","execution_count":12,"outputs":[]},{"metadata":{"_uuid":"066f9bbb7354a1cdc549b99087bfff61fa665fa1","_cell_guid":"3c7ed757-761c-4bcc-8496-ab65fbf612b6"},"cell_type":"markdown","source":"## Visualizing the volumes"},{"metadata":{"_uuid":"9ce93085394d66b8875c488835006a51a96000dc","_cell_guid":"da29bc9e-1313-42be-a3aa-cfabe706a74e"},"cell_type":"markdown","source":"Group `xyz` by `volume_id`:"},{"metadata":{"_cell_guid":"b5c19ab5-6fd4-486f-bf0e-9d64b19292e2","_uuid":"39f94f358d6561a96366f3bacc3b3b54448bf306","trusted":true},"cell_type":"code","source":"groupby = df.groupby('volume_id')['xyz'].apply(list).to_frame()\n\ngroupby","execution_count":13,"outputs":[]},{"metadata":{"_uuid":"a8fb9b4df2aae107cbf419cedc6a7aaf8db11d28","_cell_guid":"8a7f5503-fb98-4054-8d5a-3d8af7d03671"},"cell_type":"markdown","source":"Plot each volume with a single color and label:"},{"metadata":{"_cell_guid":"69030c06-5465-448c-890f-8b4e958a2140","_uuid":"7d00aba819c12d43f97ced72c2aad2b063c2ef2e","trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(15, 15))\n\nfor k in range(groupby.shape[0]):\n    ax = fig.add_subplot(3, 3, k+1, projection='3d')\n    ax.set_aspect('equal')\n    ax.set_xlabel('x')\n    ax.set_ylabel('y')\n    ax.set_zlabel('z')\n    ax.set_xlim(x_min, x_max)\n    ax.set_ylim(y_min, y_max)\n    ax.set_zlim(z_min, z_max)\n    for (idx, row) in groupby.iloc[:k+1].iterrows():\n        xyz = np.array(row['xyz'])\n        x, y, z = xyz[:,0], xyz[:,1], xyz[:,2]\n        ax.plot(x, y, z, linewidth=0.5)\n        ax.text(x[0], y[0], z[0], str(idx), None)\n\nplt.tight_layout(pad=0., w_pad=0., h_pad=0.)\nplt.show()","execution_count":15,"outputs":[]},{"metadata":{"_uuid":"987850a7d16c0c530085596f4e8a8fdb01d72a6a","_cell_guid":"22f3a3a4-99ae-48c8-9abf-08f025eee4e5"},"cell_type":"markdown","source":"## Visualizing the layers"},{"metadata":{"_uuid":"676c1e0db6d6261ee563434d1989bfefd7db2dff","_cell_guid":"e8660f2e-647c-402d-9fb9-effcc107f589"},"cell_type":"markdown","source":"Group `xyz` by `volume_id` and `layer_id`:"},{"metadata":{"_cell_guid":"3b1a1e9b-7219-45e5-8b69-2aa41273c41a","_uuid":"73a021223cb658110ae60e4be44e23fea45fd8ac","trusted":true},"cell_type":"code","source":"groupby = df.groupby(['volume_id', 'layer_id'])['xyz'].apply(list).to_frame()\n\ngroupby","execution_count":16,"outputs":[]},{"metadata":{"_uuid":"1d5fb21c0e3de40664b68d3710e6fcc6d888ebf6","_cell_guid":"506e0af9-e399-49d2-b480-778f460ecb88"},"cell_type":"markdown","source":"Plot each layer with a single color, and show label only for the last layer:"},{"metadata":{"_cell_guid":"41f89f85-4b36-4df9-9920-81481eaaf723","_uuid":"54d3b0b99de8dcda2dae080693e716d0c81d16d5","trusted":true},"cell_type":"code","source":"fig = plt.figure(figsize=(15, 80))\n\nfor k in range(groupby.shape[0]):\n    ax = fig.add_subplot(16, 3, k+1, projection='3d')\n    ax.set_aspect('equal')\n    ax.set_xlabel('x')\n    ax.set_ylabel('y')\n    ax.set_zlabel('z')\n    ax.set_xlim(x_min, x_max)\n    ax.set_ylim(y_min, y_max)\n    ax.set_zlim(z_min, z_max)\n    for (idx, row) in groupby.iloc[:k+1].iterrows():\n        xyz = np.array(row['xyz'])\n        x, y, z = xyz[:,0], xyz[:,1], xyz[:,2]\n        ax.plot(x, y, z, linewidth=0.5)\n    ax.text(x[0], y[0], z[0], str(idx), None)\n\nplt.tight_layout(pad=0., w_pad=0., h_pad=0.)\nplt.show()","execution_count":17,"outputs":[]}],"metadata":{"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"},"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":1}