|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Simularium Conversion Tutorial : Mem3DG .nc Data" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": null, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "from IPython.display import Image\n", |
| 17 | + "\n", |
| 18 | + "import numpy as np\n", |
| 19 | + "\n", |
| 20 | + "from simulariumio.mem3dg import Mem3dgConverter, Mem3dgData\n", |
| 21 | + "from simulariumio import MetaData, CameraData, UnitData" |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "markdown", |
| 26 | + "metadata": {}, |
| 27 | + "source": [ |
| 28 | + "This notebook provides example python code for converting your own simulation trajectories into the format consumed by the Simularium Viewer. It creates a .simularium file which you can drag and drop onto the viewer like this:" |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "markdown", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "# _Note:_\n", |
| 43 | + "To install simulariumio with all depencies needed for Mem3DG conversion, use `pip install simulariumio[mem3dg]`" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "metadata": {}, |
| 49 | + "source": [ |
| 50 | + "***\n", |
| 51 | + "## Prepare your spatial data" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "metadata": {}, |
| 57 | + "source": [ |
| 58 | + "The Simularium `Mem3dgConverter` consumes spatiotemporal data from Mem3DG .nc output files using the netCDF4 Python package. \n", |
| 59 | + "\n", |
| 60 | + "The converter requires a `Mem3dgData` object as a parameter.\n", |
| 61 | + "\n", |
| 62 | + "Unlike other simulariumio converters, the `Mem3dgConverter` will generate several `.obj` files (one per frame) in addition to the `.simularium` file. Use `output_obj_file_path` to specify where the generated obj files should be saved.\n", |
| 63 | + "\n", |
| 64 | + "The test input .nc data for this example was created using [Mem3DG's Jupyter Notebook tutorials](https://github.com/RangamaniLabUCSD/Mem3DG/blob/main/tests/python/tutorial/tutorial1.ipynb)" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "cell_type": "code", |
| 69 | + "execution_count": null, |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [], |
| 72 | + "source": [ |
| 73 | + "box_size = 5.\n", |
| 74 | + "scale_factor = 10\n", |
| 75 | + "\n", |
| 76 | + "example_data = Mem3dgData(\n", |
| 77 | + " input_file_path=\"../simulariumio/tests/data/mem3dg/traj.nc\",\n", |
| 78 | + " output_obj_file_path=\".\",\n", |
| 79 | + " meta_data=MetaData(\n", |
| 80 | + " box_size=np.array([box_size, box_size, box_size]),\n", |
| 81 | + " trajectory_title=\"Some parameter set\",\n", |
| 82 | + " camera_defaults=CameraData(position=np.array([0, 0, 200])),\n", |
| 83 | + " scale_factor=scale_factor\n", |
| 84 | + " ),\n", |
| 85 | + " agent_color=\"#a38fba\",\n", |
| 86 | + " agent_name=\"my-object\",\n", |
| 87 | + " time_units=UnitData(\"us\", 0.2),\n", |
| 88 | + ")" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## Convert and save as .simularium file" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "Once your data is shaped like in the `example_data` object, you can use the converter to generate the file at the given path" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 3, |
| 108 | + "metadata": {}, |
| 109 | + "outputs": [ |
| 110 | + { |
| 111 | + "name": "stdout", |
| 112 | + "output_type": "stream", |
| 113 | + "text": [ |
| 114 | + "Reading Mem3DG Data -------------\n", |
| 115 | + "Converting Trajectory Data to JSON -------------\n", |
| 116 | + "Writing JSON -------------\n", |
| 117 | + "saved to example_mem3dg.simularium\n" |
| 118 | + ] |
| 119 | + } |
| 120 | + ], |
| 121 | + "source": [ |
| 122 | + "converter = Mem3dgConverter(example_data).save(\"example_mem3dg\", binary=False)" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "markdown", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "## Visualize in the Simularium viewer" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
| 134 | + "metadata": {}, |
| 135 | + "source": [ |
| 136 | + "In a supported web-browser (Firefox or Chrome), navigate to https://simularium.allencell.org/ and import your file into the view.\n", |
| 137 | + "\n", |
| 138 | + "**Note:** In order to view your data in simularium, you must import the generated .simularium file _and_ all of the generated .obj files!" |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": null, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [], |
| 146 | + "source": [] |
| 147 | + } |
| 148 | + ], |
| 149 | + "metadata": { |
| 150 | + "kernelspec": { |
| 151 | + "display_name": "Python 3 (ipykernel)", |
| 152 | + "language": "python", |
| 153 | + "name": "python3" |
| 154 | + }, |
| 155 | + "language_info": { |
| 156 | + "codemirror_mode": { |
| 157 | + "name": "ipython", |
| 158 | + "version": 3 |
| 159 | + }, |
| 160 | + "file_extension": ".py", |
| 161 | + "mimetype": "text/x-python", |
| 162 | + "name": "python", |
| 163 | + "nbconvert_exporter": "python", |
| 164 | + "pygments_lexer": "ipython3", |
| 165 | + "version": "3.9.16" |
| 166 | + } |
| 167 | + }, |
| 168 | + "nbformat": 4, |
| 169 | + "nbformat_minor": 4 |
| 170 | +} |
0 commit comments