|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# deeptrack.backend.core\n", |
| 8 | + "\n", |
| 9 | + "<a href=\"https://colab.research.google.com/github/DeepTrackAI/DeepTrack2/blob/develop/tutorials/3-advanced-topics/DTAT399A_backend.core.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": null, |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "# !pip install deeptrack # Uncomment if running on Colab/Kaggle." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "This advanced tutorial introduces the backend.core module." |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "## 1. What is `core`?\n", |
| 33 | + "\n", |
| 34 | + "The `core` module provides fundamental utilities and functions to manage and process data on a low level.\n", |
| 35 | + "\n", |
| 36 | + "In particular it provide tools to store, validate, and manage data and computational nodes with dependency tracking.\n" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "## 2. Basic Node Usage with Parent-Child Dependency" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": null, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [ |
| 51 | + { |
| 52 | + "name": "stdout", |
| 53 | + "output_type": "stream", |
| 54 | + "text": [ |
| 55 | + "30 40\n", |
| 56 | + "False\n", |
| 57 | + "False\n", |
| 58 | + "50\n" |
| 59 | + ] |
| 60 | + } |
| 61 | + ], |
| 62 | + "source": [ |
| 63 | + "from deeptrack.backend.core import DeepTrackNode\n", |
| 64 | + "\n", |
| 65 | + "parent = DeepTrackNode(action=lambda: 10)\n", |
| 66 | + "child = DeepTrackNode(action=lambda _ID=None: parent(_ID) * 2)\n", |
| 67 | + "\n", |
| 68 | + "# Establish parent-child dependency.\n", |
| 69 | + "parent.add_child(child)\n", |
| 70 | + "\n", |
| 71 | + "# Store values.\n", |
| 72 | + "parent.store(15, _ID=(0,))\n", |
| 73 | + "parent.store(20, _ID=(1,))\n", |
| 74 | + "\n", |
| 75 | + "# Compute values based on parent values.\n", |
| 76 | + "child_value_0 = child(_ID=(0,))\n", |
| 77 | + "child_value_1 = child(_ID=(1,))\n", |
| 78 | + "print(child_value_0, child_value_1)\n", |
| 79 | + "\n", |
| 80 | + "# Invalidate parent data for a given ID.\n", |
| 81 | + "parent.invalidate((0,))\n", |
| 82 | + "print(parent.is_valid((0,)))\n", |
| 83 | + "\n", |
| 84 | + "# Update the parent value and recompute the child value:\n", |
| 85 | + "print(child.is_valid((0,)))\n", |
| 86 | + "parent.store(25, _ID=(0,))\n", |
| 87 | + "child_value_recomputed = child(_ID=(0,))\n", |
| 88 | + "print(child_value_recomputed)" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "cell_type": "markdown", |
| 93 | + "metadata": {}, |
| 94 | + "source": [ |
| 95 | + "## 3. Lazy evaluation and Caching\n", |
| 96 | + "Here we add a function to a `DeepTrackNode` which retuns a constant value and updates a global counter variable when called." |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 95, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "name": "stdout", |
| 106 | + "output_type": "stream", |
| 107 | + "text": [ |
| 108 | + "10 1\n", |
| 109 | + "10 2\n", |
| 110 | + "10 3\n" |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "source": [ |
| 115 | + "# Create counter node with side effect\n", |
| 116 | + "call_count = 0\n", |
| 117 | + "def calculation():\n", |
| 118 | + " global call_count\n", |
| 119 | + " call_count += 1\n", |
| 120 | + " return 10\n", |
| 121 | + "\n", |
| 122 | + "node = DeepTrackNode(calculation)\n", |
| 123 | + "\n", |
| 124 | + "# First call computes value.\n", |
| 125 | + "print(node(), call_count) \n", |
| 126 | + "\n", |
| 127 | + "# Subsequent call uses cached value.\n", |
| 128 | + "node.invalidate()\n", |
| 129 | + "print(node(), call_count) \n", |
| 130 | + "\n", |
| 131 | + "# Invalidate and call again.\n", |
| 132 | + "node.invalidate()\n", |
| 133 | + "print(node(), call_count) " |
| 134 | + ] |
| 135 | + }, |
| 136 | + { |
| 137 | + "cell_type": "markdown", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "## 4. Data Management with IDs\n", |
| 141 | + "\n", |
| 142 | + "Map IDs to stored `DeepTrackData` objects lika a dictionary." |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "metadata": {}, |
| 149 | + "outputs": [ |
| 150 | + { |
| 151 | + "name": "stdout", |
| 152 | + "output_type": "stream", |
| 153 | + "text": [ |
| 154 | + "Cat\n", |
| 155 | + "Bird\n", |
| 156 | + "{(0, 0): <deeptrack.backend.core.DeepTrackDataObject object at 0x7f0dc3022b90>, (0, 1): <deeptrack.backend.core.DeepTrackDataObject object at 0x7f0dc302f790>}\n" |
| 157 | + ] |
| 158 | + } |
| 159 | + ], |
| 160 | + "source": [ |
| 161 | + "from deeptrack.backend.core import DeepTrackDataDict\n", |
| 162 | + "\n", |
| 163 | + "data_dict = DeepTrackDataDict()\n", |
| 164 | + "\n", |
| 165 | + "# Create listings with unique indices.\n", |
| 166 | + "data_dict.create_index((0, 0))\n", |
| 167 | + "data_dict.create_index((0, 1))\n", |
| 168 | + "data_dict.create_index((1, 0))\n", |
| 169 | + "data_dict.create_index((1, 1))\n", |
| 170 | + "\n", |
| 171 | + "# Store some data for the indices.\n", |
| 172 | + "data_dict[(0, 0)].store(\"Cat\")\n", |
| 173 | + "data_dict[(0, 1)].store(\"Dog\")\n", |
| 174 | + "data_dict[(1, 0)].store(\"Mouse\")\n", |
| 175 | + "data_dict[(1, 1)].store(\"Bird\")\n", |
| 176 | + "\n", |
| 177 | + "# Print the indices.\n", |
| 178 | + "print(data_dict[(0, 0)].current_value())\n", |
| 179 | + "print(data_dict[(1, 1)].current_value())\n", |
| 180 | + "print(data_dict[(0, )])" |
| 181 | + ] |
| 182 | + }, |
| 183 | + { |
| 184 | + "cell_type": "markdown", |
| 185 | + "metadata": {}, |
| 186 | + "source": [ |
| 187 | + "## 5. Propagating operators\n", |
| 188 | + "Nodes can also be used as simple handles for functions." |
| 189 | + ] |
| 190 | + }, |
| 191 | + { |
| 192 | + "cell_type": "code", |
| 193 | + "execution_count": 92, |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [ |
| 196 | + { |
| 197 | + "name": "stdout", |
| 198 | + "output_type": "stream", |
| 199 | + "text": [ |
| 200 | + "16\n", |
| 201 | + "60\n" |
| 202 | + ] |
| 203 | + } |
| 204 | + ], |
| 205 | + "source": [ |
| 206 | + "a = DeepTrackNode(lambda: 5 + 5)\n", |
| 207 | + "b = DeepTrackNode(lambda: 3 + 3)\n", |
| 208 | + "\n", |
| 209 | + "sum_node = a + b\n", |
| 210 | + "product_node = a * b\n", |
| 211 | + "\n", |
| 212 | + "print(sum_node())\n", |
| 213 | + "print(product_node())" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "markdown", |
| 218 | + "metadata": {}, |
| 219 | + "source": [ |
| 220 | + "## 6. Validation control\n", |
| 221 | + "Validate or invalidate nodes manually to enable/disable storing data." |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": 88, |
| 227 | + "metadata": {}, |
| 228 | + "outputs": [ |
| 229 | + { |
| 230 | + "name": "stdout", |
| 231 | + "output_type": "stream", |
| 232 | + "text": [ |
| 233 | + "100\n", |
| 234 | + "True\n", |
| 235 | + "100\n", |
| 236 | + "False\n", |
| 237 | + "42\n" |
| 238 | + ] |
| 239 | + } |
| 240 | + ], |
| 241 | + "source": [ |
| 242 | + "node = DeepTrackNode(lambda: 42)\n", |
| 243 | + "node.store(100)\n", |
| 244 | + "\n", |
| 245 | + "print(node())\n", |
| 246 | + "\n", |
| 247 | + "# Validate.\n", |
| 248 | + "node.validate()\n", |
| 249 | + "print(node.is_valid())\n", |
| 250 | + "print(node()) \n", |
| 251 | + "\n", |
| 252 | + "# Invalidate.\n", |
| 253 | + "node.invalidate()\n", |
| 254 | + "print(node.is_valid())\n", |
| 255 | + "print(node())" |
| 256 | + ] |
| 257 | + }, |
| 258 | + { |
| 259 | + "cell_type": "markdown", |
| 260 | + "metadata": {}, |
| 261 | + "source": [ |
| 262 | + "## 7. Get Citations\n", |
| 263 | + "The `DeepTrackNode` class can also be used to obtain citations." |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "code", |
| 268 | + "execution_count": 101, |
| 269 | + "metadata": {}, |
| 270 | + "outputs": [ |
| 271 | + { |
| 272 | + "data": { |
| 273 | + "text/plain": [ |
| 274 | + "{'\\n@article{Midtvet2021DeepTrack,\\n author = {Midtvedt,Benjamin and \\n Helgadottir,Saga and \\n Argun,Aykut and \\n Pineda,Jesús and \\n Midtvedt,Daniel and \\n Volpe,Giovanni},\\n title = {Quantitative digital microscopy with deep learning},\\n journal = {Applied Physics Reviews},\\n volume = {8},\\n number = {1},\\n pages = {011310},\\n year = {2021},\\n doi = {10.1063/5.0034891}\\n}\\n'}" |
| 275 | + ] |
| 276 | + }, |
| 277 | + "execution_count": 101, |
| 278 | + "metadata": {}, |
| 279 | + "output_type": "execute_result" |
| 280 | + } |
| 281 | + ], |
| 282 | + "source": [ |
| 283 | + "DeepTrackNode().get_citations()" |
| 284 | + ] |
| 285 | + } |
| 286 | + ], |
| 287 | + "metadata": { |
| 288 | + "kernelspec": { |
| 289 | + "display_name": ".venv", |
| 290 | + "language": "python", |
| 291 | + "name": "python3" |
| 292 | + }, |
| 293 | + "language_info": { |
| 294 | + "codemirror_mode": { |
| 295 | + "name": "ipython", |
| 296 | + "version": 3 |
| 297 | + }, |
| 298 | + "file_extension": ".py", |
| 299 | + "mimetype": "text/x-python", |
| 300 | + "name": "python", |
| 301 | + "nbconvert_exporter": "python", |
| 302 | + "pygments_lexer": "ipython3", |
| 303 | + "version": "3.10.12" |
| 304 | + } |
| 305 | + }, |
| 306 | + "nbformat": 4, |
| 307 | + "nbformat_minor": 2 |
| 308 | +} |
0 commit comments