neuro-sam 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. neuro_sam-0.1.0/LICENSE +21 -0
  2. neuro_sam-0.1.0/PKG-INFO +269 -0
  3. neuro_sam-0.1.0/README.md +215 -0
  4. neuro_sam-0.1.0/pyproject.toml +56 -0
  5. neuro_sam-0.1.0/setup.cfg +4 -0
  6. neuro_sam-0.1.0/src/neuro_sam/__init__.py +1 -0
  7. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/__init__.py +5 -0
  8. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/algorithm/__init__.py +3 -0
  9. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/algorithm/astar.py +586 -0
  10. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/algorithm/waypointastar.py +449 -0
  11. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/algorithm/waypointastar_speedup.py +1007 -0
  12. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/connected_componen.py +329 -0
  13. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/cost/__init__.py +8 -0
  14. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/cost/cost.py +33 -0
  15. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/cost/reciprocal.py +90 -0
  16. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/cost/reciprocal_transonic.py +86 -0
  17. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/heuristic/__init__.py +2 -0
  18. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/heuristic/euclidean.py +101 -0
  19. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/heuristic/heuristic.py +29 -0
  20. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/image/__init__.py +1 -0
  21. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/image/stats.py +197 -0
  22. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/input/__init__.py +1 -0
  23. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/input/inputs.py +14 -0
  24. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/node/__init__.py +2 -0
  25. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/node/bidirectional_node.py +240 -0
  26. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/node/node.py +125 -0
  27. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/visualization/__init__.py +4 -0
  28. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/visualization/flythrough.py +133 -0
  29. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/visualization/flythrough_all.py +394 -0
  30. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/visualization/tube_data.py +385 -0
  31. neuro_sam-0.1.0/src/neuro_sam/brightest_path_lib/visualization/tube_flythrough.py +227 -0
  32. neuro_sam-0.1.0/src/neuro_sam/napari_utils/anisotropic_scaling.py +503 -0
  33. neuro_sam-0.1.0/src/neuro_sam/napari_utils/color_utils.py +135 -0
  34. neuro_sam-0.1.0/src/neuro_sam/napari_utils/contrasting_color_system.py +169 -0
  35. neuro_sam-0.1.0/src/neuro_sam/napari_utils/main_widget.py +1016 -0
  36. neuro_sam-0.1.0/src/neuro_sam/napari_utils/path_tracing_module.py +1016 -0
  37. neuro_sam-0.1.0/src/neuro_sam/napari_utils/punet_widget.py +424 -0
  38. neuro_sam-0.1.0/src/neuro_sam/napari_utils/segmentation_model.py +769 -0
  39. neuro_sam-0.1.0/src/neuro_sam/napari_utils/segmentation_module.py +649 -0
  40. neuro_sam-0.1.0/src/neuro_sam/napari_utils/visualization_module.py +574 -0
  41. neuro_sam-0.1.0/src/neuro_sam/plugin.py +260 -0
  42. neuro_sam-0.1.0/src/neuro_sam/punet/__init__.py +0 -0
  43. neuro_sam-0.1.0/src/neuro_sam/punet/deepd3_model.py +231 -0
  44. neuro_sam-0.1.0/src/neuro_sam/punet/prob_unet_deepd3.py +431 -0
  45. neuro_sam-0.1.0/src/neuro_sam/punet/prob_unet_with_tversky.py +375 -0
  46. neuro_sam-0.1.0/src/neuro_sam/punet/punet_inference.py +236 -0
  47. neuro_sam-0.1.0/src/neuro_sam/punet/run_inference.py +145 -0
  48. neuro_sam-0.1.0/src/neuro_sam/punet/unet_blocks.py +81 -0
  49. neuro_sam-0.1.0/src/neuro_sam/punet/utils.py +52 -0
  50. neuro_sam-0.1.0/src/neuro_sam.egg-info/PKG-INFO +269 -0
  51. neuro_sam-0.1.0/src/neuro_sam.egg-info/SOURCES.txt +96 -0
  52. neuro_sam-0.1.0/src/neuro_sam.egg-info/dependency_links.txt +1 -0
  53. neuro_sam-0.1.0/src/neuro_sam.egg-info/entry_points.txt +2 -0
  54. neuro_sam-0.1.0/src/neuro_sam.egg-info/requires.txt +16 -0
  55. neuro_sam-0.1.0/src/neuro_sam.egg-info/top_level.txt +2 -0
  56. neuro_sam-0.1.0/src/sam2/__init__.py +11 -0
  57. neuro_sam-0.1.0/src/sam2/automatic_mask_generator.py +454 -0
  58. neuro_sam-0.1.0/src/sam2/benchmark.py +92 -0
  59. neuro_sam-0.1.0/src/sam2/build_sam.py +174 -0
  60. neuro_sam-0.1.0/src/sam2/configs/sam2/sam2_hiera_b+.yaml +113 -0
  61. neuro_sam-0.1.0/src/sam2/configs/sam2/sam2_hiera_l.yaml +117 -0
  62. neuro_sam-0.1.0/src/sam2/configs/sam2/sam2_hiera_s.yaml +116 -0
  63. neuro_sam-0.1.0/src/sam2/configs/sam2/sam2_hiera_t.yaml +118 -0
  64. neuro_sam-0.1.0/src/sam2/configs/sam2.1/sam2.1_hiera_b+.yaml +116 -0
  65. neuro_sam-0.1.0/src/sam2/configs/sam2.1/sam2.1_hiera_l.yaml +120 -0
  66. neuro_sam-0.1.0/src/sam2/configs/sam2.1/sam2.1_hiera_s.yaml +119 -0
  67. neuro_sam-0.1.0/src/sam2/configs/sam2.1/sam2.1_hiera_t.yaml +121 -0
  68. neuro_sam-0.1.0/src/sam2/configs/sam2.1_training/sam2.1_hiera_b+_MOSE_finetune.yaml +339 -0
  69. neuro_sam-0.1.0/src/sam2/configs/train.yaml +335 -0
  70. neuro_sam-0.1.0/src/sam2/modeling/__init__.py +5 -0
  71. neuro_sam-0.1.0/src/sam2/modeling/backbones/__init__.py +5 -0
  72. neuro_sam-0.1.0/src/sam2/modeling/backbones/hieradet.py +317 -0
  73. neuro_sam-0.1.0/src/sam2/modeling/backbones/image_encoder.py +134 -0
  74. neuro_sam-0.1.0/src/sam2/modeling/backbones/utils.py +93 -0
  75. neuro_sam-0.1.0/src/sam2/modeling/memory_attention.py +169 -0
  76. neuro_sam-0.1.0/src/sam2/modeling/memory_encoder.py +181 -0
  77. neuro_sam-0.1.0/src/sam2/modeling/position_encoding.py +239 -0
  78. neuro_sam-0.1.0/src/sam2/modeling/sam/__init__.py +5 -0
  79. neuro_sam-0.1.0/src/sam2/modeling/sam/mask_decoder.py +295 -0
  80. neuro_sam-0.1.0/src/sam2/modeling/sam/prompt_encoder.py +202 -0
  81. neuro_sam-0.1.0/src/sam2/modeling/sam/transformer.py +311 -0
  82. neuro_sam-0.1.0/src/sam2/modeling/sam2_base.py +911 -0
  83. neuro_sam-0.1.0/src/sam2/modeling/sam2_utils.py +323 -0
  84. neuro_sam-0.1.0/src/sam2/sam2.1_hiera_b+.yaml +116 -0
  85. neuro_sam-0.1.0/src/sam2/sam2.1_hiera_l.yaml +120 -0
  86. neuro_sam-0.1.0/src/sam2/sam2.1_hiera_s.yaml +119 -0
  87. neuro_sam-0.1.0/src/sam2/sam2.1_hiera_t.yaml +121 -0
  88. neuro_sam-0.1.0/src/sam2/sam2_hiera_b+.yaml +113 -0
  89. neuro_sam-0.1.0/src/sam2/sam2_hiera_l.yaml +117 -0
  90. neuro_sam-0.1.0/src/sam2/sam2_hiera_s.yaml +116 -0
  91. neuro_sam-0.1.0/src/sam2/sam2_hiera_t.yaml +118 -0
  92. neuro_sam-0.1.0/src/sam2/sam2_image_predictor.py +475 -0
  93. neuro_sam-0.1.0/src/sam2/sam2_video_predictor.py +1222 -0
  94. neuro_sam-0.1.0/src/sam2/sam2_video_predictor_legacy.py +1172 -0
  95. neuro_sam-0.1.0/src/sam2/utils/__init__.py +5 -0
  96. neuro_sam-0.1.0/src/sam2/utils/amg.py +348 -0
  97. neuro_sam-0.1.0/src/sam2/utils/misc.py +349 -0
  98. neuro_sam-0.1.0/src/sam2/utils/transforms.py +118 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Nipun Arora
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,269 @@
1
+ Metadata-Version: 2.4
2
+ Name: neuro-sam
3
+ Version: 0.1.0
4
+ Summary: Neuro-SAM: Foundation Models for Dendrite and Dendritic Spine Segmentation
5
+ Author-email: Nipun Arora <nipun.arora@fau.de>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2024 Nipun Arora
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/nipunarora8/Neuro-SAM
29
+ Project-URL: Bug Tracker, https://github.com/nipunarora8/Neuro-SAM/issues
30
+ Classifier: Programming Language :: Python :: 3
31
+ Classifier: License :: OSI Approved :: MIT License
32
+ Classifier: Operating System :: OS Independent
33
+ Classifier: Framework :: napari
34
+ Requires-Python: >=3.10
35
+ Description-Content-Type: text/markdown
36
+ License-File: LICENSE
37
+ Requires-Dist: napari
38
+ Requires-Dist: numpy
39
+ Requires-Dist: scipy
40
+ Requires-Dist: imageio
41
+ Requires-Dist: torch>=2.0.0
42
+ Requires-Dist: torchvision>=0.15.0
43
+ Requires-Dist: hydra-core>=1.3.2
44
+ Requires-Dist: iopath>=0.1.10
45
+ Requires-Dist: pillow>=9.4.0
46
+ Requires-Dist: tqdm>=4.66.1
47
+ Requires-Dist: vispy
48
+ Requires-Dist: qtpy
49
+ Requires-Dist: superqt
50
+ Requires-Dist: magicgui
51
+ Requires-Dist: scikit-image
52
+ Requires-Dist: tifffile
53
+ Dynamic: license-file
54
+
55
+ <div align="center">
56
+
57
+ # Neuro-SAM
58
+ #### Foundation Models from Dendrite and Dendritic Spine Segmentation
59
+
60
+ [![python](https://img.shields.io/badge/-Python_3.10-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)
61
+ [![pytorch](https://img.shields.io/badge/PyTorch_2.0+-ee4c2c?logo=pytorch&logoColor=white)](https://pytorch.org/get-started/locally/)
62
+ [![wandb](https://img.shields.io/badge/Weights_&_Biases-FFCC33?logo=WeightsAndBiases&logoColor=black)](https://wandb.ai/site)
63
+
64
+ This project demonstrates an interactive UI to segment dendrites and dendritic spines.
65
+ The model of choice is SAMv2 and the framework used is pytorch.
66
+
67
+ ![A stack of neural dendrites and dendritic spines](./assets/merged_dendrites_spines.gif "Dendrites and Dendritic Spines")
68
+
69
+ </div>
70
+
71
+ ### šŸ“ Table of Contents
72
+
73
+ - [Neuro-SAM](#neuro-sam)
74
+ - [šŸ“‘ Table of Contents](#--table-of-contents)
75
+ - [🧠 Overview](#-overview)
76
+ - [šŸ“¦ Built With](#-built-with)
77
+ - [šŸ“‚ Repository Structure](#-repository-structure)
78
+ - [šŸš€ Installation](#-installation)
79
+ - [šŸ“Š Usage](#-usage)
80
+ - [šŸ›  Workflow](#-workflow)
81
+ - [šŸ§‘ā€šŸ’» Model Training](#-model-training)
82
+ - [šŸ“ Data Format](#-data-format)
83
+ - [šŸ“„ License](#-license)
84
+ - [šŸ“¬ Contact](#-contact)
85
+
86
+
87
+ ### 🧠 Overview
88
+
89
+ Neuro-SAM provides an end-to-end pipeline for analyzing neural structures from 3D microscopy data, featuring:
90
+
91
+ - **Path Tracing**: Waypoint-based A* pathfinding
92
+ - **Dendrite Segmentation**: SAM2-based dendrite segmentation
93
+ - **Smart Spine Detection**: Multi-view analysis for spine detection
94
+ - **Spine Segmentation**: Individual spine segmentation using trained SAM2 model
95
+
96
+ ### šŸ“¦ Built With
97
+
98
+ [PyTorch](https://pytorch.org) - an open-source machine learning library for Python, widely used for deep learning applications.
99
+
100
+ [Segment Anything Model](https://segment-anything.com) - a foundation model used for segmentation built by Meta AI.
101
+
102
+ [Weights and Biases](https://wandb.ai/site) - a tool for tracking and visualizing machine learning experiments.
103
+
104
+ [Visual Studio Code](https://code.visualstudio.com/) - a code editor redefined and optimized for building applications.
105
+
106
+ [FAU High Performance Computing](https://doc.nhr.fau.de/) - a high-performance computing cluster at Friedrich-Alexander-Universität Erlangen-Nürnberg.
107
+
108
+ ### šŸ“ Repository Structure
109
+
110
+ ```
111
+ Neuro-SAM/
112
+ ā”œā”€ā”€ Train-SAMv2/ # SAM2 training infrastructure
113
+ │ ā”œā”€ā”€ sam2/ # SAM2 model implementation
114
+ │ ā”œā”€ā”€ checkpoints/ # Model checkpoints
115
+ │ ā”œā”€ā”€ results/ # Trained model outputs
116
+ │ ā”œā”€ā”€ utils/ # Training utilities
117
+ │ ā”œā”€ā”€ train_dendrites.py # Dendrite model training
118
+ │ └── train_spines.py # Spine model training
119
+ ā”œā”€ā”€ brightest_path_lib/ # Advanced pathfinding algorithms
120
+ │ ā”œā”€ā”€ algorithm/ # A* and waypoint search implementations
121
+ │ ā”œā”€ā”€ cost/ # Cost function definitions
122
+ │ ā”œā”€ā”€ heuristic/ # Heuristic functions
123
+ │ ā”œā”€ā”€ visualization/ # Path visualization tools
124
+ │ └── ...
125
+ ā”œā”€ā”€ napari_utils/ # Napari plugin components
126
+ │ ā”œā”€ā”€ main_widget.py # Main interface with anisotropic scaling
127
+ │ ā”œā”€ā”€ path_tracing_module.py # Interactive path tracing
128
+ │ ā”œā”€ā”€ segmentation_module.py # Dendrite segmentation interface
129
+ │ ā”œā”€ā”€ spine_detection_module.py # Spine detection with smart tracking
130
+ │ ā”œā”€ā”€ spine_segmentation_module.py # Individual spine segmentation
131
+ │ └── visualization_module.py # Path management and visualization
132
+ └── neuro_sam_plugin.py # Main plugin entry point
133
+ ```
134
+
135
+ ### šŸš€ Installation
136
+
137
+ #### Prerequisites
138
+
139
+ - Python 3.10+
140
+ - CUDA-compatible GPU (recommended)
141
+ - Conda/Miniconda
142
+
143
+ #### Environment Setup
144
+
145
+ 1. **Clone the repository:**
146
+ ```bash
147
+ git clone https://github.com/nipunarora8/Neuro-SAM.git
148
+ cd Neuro-SAM
149
+ ```
150
+
151
+ 2. **Create local environment:**
152
+ ```bash
153
+ conda create -p ./.venv python=3.10 -c conda-forge
154
+ conda activate ./.venv
155
+ ```
156
+
157
+ 3. **Install dependencies:**
158
+ ```bash
159
+ pip install uv
160
+ uv sync
161
+ ```
162
+
163
+ 4. **Download SAM2 checkpoints:**
164
+ ```bash
165
+ cd Train-SAMv2/checkpoints
166
+ bash download_ckpts.sh
167
+ ```
168
+
169
+ ### šŸ“Š Usage
170
+
171
+ #### Quick Start
172
+
173
+ ```python
174
+ from neuro_sam_plugin import run_neuro_sam
175
+
176
+ # Launch with default spacing (94nm x 94nm x 500nm)
177
+ viewer = run_neuro_sam(image_path="your_image.tif")
178
+
179
+ # Launch with custom voxel spacing
180
+ viewer = run_neuro_sam(
181
+ image_path="your_image.tif",
182
+ spacing_xyz=(100.0, 100.0, 300.0) # X, Y, Z spacing in nm
183
+ )
184
+ ```
185
+
186
+ #### Command Line Interface
187
+
188
+ ```bash
189
+ # Basic usage
190
+ python neuro_sam_plugin.py --image_path /path/to/your/image.tif
191
+
192
+ # Custom spacing
193
+ python neuro_sam_plugin.py --image_path image.tif \
194
+ --x-spacing 100.0 --y-spacing 100.0 --z-spacing 300.0
195
+
196
+ # Load benchmark dataset
197
+ python neuro_sam_plugin.py
198
+ ```
199
+
200
+ ### šŸ”¬ Workflow
201
+
202
+ #### 1. **Configure Voxel Spacing**
203
+ Set accurate X, Y, Z voxel spacing in the "Path Tracing" tab for proper anisotropic scaling:
204
+ - Typical two-photon: 94nm Ɨ 94nm Ɨ 500nm
205
+ - Confocal: varies by objective and zoom
206
+
207
+ #### 2. **Trace Dendritic Paths**
208
+ - Click waypoints along dendrite structures
209
+ - Algorithm automatically finds optimal brightess paths
210
+
211
+ #### 3. **Segment Dendrites**
212
+ - Load pre-trained SAMv2 dendrite model
213
+ - Segment individual path with SAMv2
214
+
215
+ #### 4. **Detect Spines**
216
+ - Smart multi-view detection using tube data generation
217
+ - Angle-based matching between 2D and tubular views
218
+
219
+ #### 5. **Segment Spines**
220
+ - Fine-grained spine segmentation using specialized SAMv2 model
221
+ - Dendrite mask overlay to suppress background signal
222
+ - Manual point extension across frames
223
+ - Contrasting color system for visualization
224
+
225
+ ### šŸ”§ Model Training
226
+
227
+ #### Dendrite Model
228
+ ```bash
229
+ cd Train-SAMv2
230
+ python train_dendrites.py --ppn 20 --pnn 10 --batch_size 32 --model_name "small"
231
+ ```
232
+
233
+ #### Spine Model
234
+ ```bash
235
+ python train_spines.py --model_name "small" --batch_size 16
236
+ ```
237
+
238
+ ### šŸ“ Data Format
239
+
240
+ #### Input Requirements
241
+ - **Image Format**: TIFF, .d3set (to reproduce training results)
242
+ - **Dimensions**: 3D volumes (ZƗYƗX)
243
+ - **Bit Depth**: 8-bit or 16-bit grayscale
244
+ - **Size**: Tested up to 2048Ɨ2048Ɨ500 voxels
245
+
246
+ #### Output Formats
247
+ - **Paths**: NumPy arrays with coordinates
248
+ - **Masks**: Binary TIFF volumes
249
+
250
+ ### šŸ“„ License
251
+
252
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
253
+
254
+
255
+ #### Useful VSCode Extensions
256
+
257
+ - [Remote Explorer](https://marketplace.visualstudio.com/items?itemName=ms-vscode.remote-explorer) - Open projects on remote servers.
258
+ - [Log Viewer](https://marketplace.visualstudio.com/items?itemName=berublan.vscode-log-viewer) - A log monitoring extension.
259
+ - [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter) - Python auto code formatter.
260
+ - [Markdown All in One](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one) - Markdown preview and editing.
261
+
262
+ ### šŸ“¬ Contact
263
+
264
+ Nipun Arora - nipun.arora@fau.de
265
+
266
+ ---
267
+ <div align="center">
268
+ <b>Made with ā™„ļø at Anki Lab 🧠✨</b>
269
+ </div>
@@ -0,0 +1,215 @@
1
+ <div align="center">
2
+
3
+ # Neuro-SAM
4
+ #### Foundation Models from Dendrite and Dendritic Spine Segmentation
5
+
6
+ [![python](https://img.shields.io/badge/-Python_3.10-blue?logo=python&logoColor=white)](https://github.com/pre-commit/pre-commit)
7
+ [![pytorch](https://img.shields.io/badge/PyTorch_2.0+-ee4c2c?logo=pytorch&logoColor=white)](https://pytorch.org/get-started/locally/)
8
+ [![wandb](https://img.shields.io/badge/Weights_&_Biases-FFCC33?logo=WeightsAndBiases&logoColor=black)](https://wandb.ai/site)
9
+
10
+ This project demonstrates an interactive UI to segment dendrites and dendritic spines.
11
+ The model of choice is SAMv2 and the framework used is pytorch.
12
+
13
+ ![A stack of neural dendrites and dendritic spines](./assets/merged_dendrites_spines.gif "Dendrites and Dendritic Spines")
14
+
15
+ </div>
16
+
17
+ ### šŸ“ Table of Contents
18
+
19
+ - [Neuro-SAM](#neuro-sam)
20
+ - [šŸ“‘ Table of Contents](#--table-of-contents)
21
+ - [🧠 Overview](#-overview)
22
+ - [šŸ“¦ Built With](#-built-with)
23
+ - [šŸ“‚ Repository Structure](#-repository-structure)
24
+ - [šŸš€ Installation](#-installation)
25
+ - [šŸ“Š Usage](#-usage)
26
+ - [šŸ›  Workflow](#-workflow)
27
+ - [šŸ§‘ā€šŸ’» Model Training](#-model-training)
28
+ - [šŸ“ Data Format](#-data-format)
29
+ - [šŸ“„ License](#-license)
30
+ - [šŸ“¬ Contact](#-contact)
31
+
32
+
33
+ ### 🧠 Overview
34
+
35
+ Neuro-SAM provides an end-to-end pipeline for analyzing neural structures from 3D microscopy data, featuring:
36
+
37
+ - **Path Tracing**: Waypoint-based A* pathfinding
38
+ - **Dendrite Segmentation**: SAM2-based dendrite segmentation
39
+ - **Smart Spine Detection**: Multi-view analysis for spine detection
40
+ - **Spine Segmentation**: Individual spine segmentation using trained SAM2 model
41
+
42
+ ### šŸ“¦ Built With
43
+
44
+ [PyTorch](https://pytorch.org) - an open-source machine learning library for Python, widely used for deep learning applications.
45
+
46
+ [Segment Anything Model](https://segment-anything.com) - a foundation model used for segmentation built by Meta AI.
47
+
48
+ [Weights and Biases](https://wandb.ai/site) - a tool for tracking and visualizing machine learning experiments.
49
+
50
+ [Visual Studio Code](https://code.visualstudio.com/) - a code editor redefined and optimized for building applications.
51
+
52
+ [FAU High Performance Computing](https://doc.nhr.fau.de/) - a high-performance computing cluster at Friedrich-Alexander-Universität Erlangen-Nürnberg.
53
+
54
+ ### šŸ“ Repository Structure
55
+
56
+ ```
57
+ Neuro-SAM/
58
+ ā”œā”€ā”€ Train-SAMv2/ # SAM2 training infrastructure
59
+ │ ā”œā”€ā”€ sam2/ # SAM2 model implementation
60
+ │ ā”œā”€ā”€ checkpoints/ # Model checkpoints
61
+ │ ā”œā”€ā”€ results/ # Trained model outputs
62
+ │ ā”œā”€ā”€ utils/ # Training utilities
63
+ │ ā”œā”€ā”€ train_dendrites.py # Dendrite model training
64
+ │ └── train_spines.py # Spine model training
65
+ ā”œā”€ā”€ brightest_path_lib/ # Advanced pathfinding algorithms
66
+ │ ā”œā”€ā”€ algorithm/ # A* and waypoint search implementations
67
+ │ ā”œā”€ā”€ cost/ # Cost function definitions
68
+ │ ā”œā”€ā”€ heuristic/ # Heuristic functions
69
+ │ ā”œā”€ā”€ visualization/ # Path visualization tools
70
+ │ └── ...
71
+ ā”œā”€ā”€ napari_utils/ # Napari plugin components
72
+ │ ā”œā”€ā”€ main_widget.py # Main interface with anisotropic scaling
73
+ │ ā”œā”€ā”€ path_tracing_module.py # Interactive path tracing
74
+ │ ā”œā”€ā”€ segmentation_module.py # Dendrite segmentation interface
75
+ │ ā”œā”€ā”€ spine_detection_module.py # Spine detection with smart tracking
76
+ │ ā”œā”€ā”€ spine_segmentation_module.py # Individual spine segmentation
77
+ │ └── visualization_module.py # Path management and visualization
78
+ └── neuro_sam_plugin.py # Main plugin entry point
79
+ ```
80
+
81
+ ### šŸš€ Installation
82
+
83
+ #### Prerequisites
84
+
85
+ - Python 3.10+
86
+ - CUDA-compatible GPU (recommended)
87
+ - Conda/Miniconda
88
+
89
+ #### Environment Setup
90
+
91
+ 1. **Clone the repository:**
92
+ ```bash
93
+ git clone https://github.com/nipunarora8/Neuro-SAM.git
94
+ cd Neuro-SAM
95
+ ```
96
+
97
+ 2. **Create local environment:**
98
+ ```bash
99
+ conda create -p ./.venv python=3.10 -c conda-forge
100
+ conda activate ./.venv
101
+ ```
102
+
103
+ 3. **Install dependencies:**
104
+ ```bash
105
+ pip install uv
106
+ uv sync
107
+ ```
108
+
109
+ 4. **Download SAM2 checkpoints:**
110
+ ```bash
111
+ cd Train-SAMv2/checkpoints
112
+ bash download_ckpts.sh
113
+ ```
114
+
115
+ ### šŸ“Š Usage
116
+
117
+ #### Quick Start
118
+
119
+ ```python
120
+ from neuro_sam_plugin import run_neuro_sam
121
+
122
+ # Launch with default spacing (94nm x 94nm x 500nm)
123
+ viewer = run_neuro_sam(image_path="your_image.tif")
124
+
125
+ # Launch with custom voxel spacing
126
+ viewer = run_neuro_sam(
127
+ image_path="your_image.tif",
128
+ spacing_xyz=(100.0, 100.0, 300.0) # X, Y, Z spacing in nm
129
+ )
130
+ ```
131
+
132
+ #### Command Line Interface
133
+
134
+ ```bash
135
+ # Basic usage
136
+ python neuro_sam_plugin.py --image_path /path/to/your/image.tif
137
+
138
+ # Custom spacing
139
+ python neuro_sam_plugin.py --image_path image.tif \
140
+ --x-spacing 100.0 --y-spacing 100.0 --z-spacing 300.0
141
+
142
+ # Load benchmark dataset
143
+ python neuro_sam_plugin.py
144
+ ```
145
+
146
+ ### šŸ”¬ Workflow
147
+
148
+ #### 1. **Configure Voxel Spacing**
149
+ Set accurate X, Y, Z voxel spacing in the "Path Tracing" tab for proper anisotropic scaling:
150
+ - Typical two-photon: 94nm Ɨ 94nm Ɨ 500nm
151
+ - Confocal: varies by objective and zoom
152
+
153
+ #### 2. **Trace Dendritic Paths**
154
+ - Click waypoints along dendrite structures
155
+ - Algorithm automatically finds optimal brightess paths
156
+
157
+ #### 3. **Segment Dendrites**
158
+ - Load pre-trained SAMv2 dendrite model
159
+ - Segment individual path with SAMv2
160
+
161
+ #### 4. **Detect Spines**
162
+ - Smart multi-view detection using tube data generation
163
+ - Angle-based matching between 2D and tubular views
164
+
165
+ #### 5. **Segment Spines**
166
+ - Fine-grained spine segmentation using specialized SAMv2 model
167
+ - Dendrite mask overlay to suppress background signal
168
+ - Manual point extension across frames
169
+ - Contrasting color system for visualization
170
+
171
+ ### šŸ”§ Model Training
172
+
173
+ #### Dendrite Model
174
+ ```bash
175
+ cd Train-SAMv2
176
+ python train_dendrites.py --ppn 20 --pnn 10 --batch_size 32 --model_name "small"
177
+ ```
178
+
179
+ #### Spine Model
180
+ ```bash
181
+ python train_spines.py --model_name "small" --batch_size 16
182
+ ```
183
+
184
+ ### šŸ“ Data Format
185
+
186
+ #### Input Requirements
187
+ - **Image Format**: TIFF, .d3set (to reproduce training results)
188
+ - **Dimensions**: 3D volumes (ZƗYƗX)
189
+ - **Bit Depth**: 8-bit or 16-bit grayscale
190
+ - **Size**: Tested up to 2048Ɨ2048Ɨ500 voxels
191
+
192
+ #### Output Formats
193
+ - **Paths**: NumPy arrays with coordinates
194
+ - **Masks**: Binary TIFF volumes
195
+
196
+ ### šŸ“„ License
197
+
198
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
199
+
200
+
201
+ #### Useful VSCode Extensions
202
+
203
+ - [Remote Explorer](https://marketplace.visualstudio.com/items?itemName=ms-vscode.remote-explorer) - Open projects on remote servers.
204
+ - [Log Viewer](https://marketplace.visualstudio.com/items?itemName=berublan.vscode-log-viewer) - A log monitoring extension.
205
+ - [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter) - Python auto code formatter.
206
+ - [Markdown All in One](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one) - Markdown preview and editing.
207
+
208
+ ### šŸ“¬ Contact
209
+
210
+ Nipun Arora - nipun.arora@fau.de
211
+
212
+ ---
213
+ <div align="center">
214
+ <b>Made with ā™„ļø at Anki Lab 🧠✨</b>
215
+ </div>
@@ -0,0 +1,56 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "neuro-sam"
7
+ version = "0.1.0"
8
+ description = "Neuro-SAM: Foundation Models for Dendrite and Dendritic Spine Segmentation"
9
+ readme = "README.md"
10
+ authors = [
11
+ { name = "Nipun Arora", email = "nipun.arora@fau.de" },
12
+ ]
13
+ license = { file = "LICENSE" }
14
+ classifiers = [
15
+ "Programming Language :: Python :: 3",
16
+ "License :: OSI Approved :: MIT License",
17
+ "Operating System :: OS Independent",
18
+ "Framework :: napari",
19
+ ]
20
+ dependencies = [
21
+ "napari",
22
+ "numpy",
23
+ "scipy",
24
+ "imageio",
25
+ "torch>=2.0.0",
26
+ "torchvision>=0.15.0",
27
+ "hydra-core>=1.3.2",
28
+ "iopath>=0.1.10",
29
+ "pillow>=9.4.0",
30
+ "tqdm>=4.66.1",
31
+ "vispy",
32
+ "qtpy",
33
+ "superqt",
34
+ "magicgui",
35
+ "scikit-image",
36
+ "tifffile"
37
+ ]
38
+ requires-python = ">=3.10"
39
+
40
+ [project.urls]
41
+ "Homepage" = "https://github.com/nipunarora8/Neuro-SAM"
42
+ "Bug Tracker" = "https://github.com/nipunarora8/Neuro-SAM/issues"
43
+
44
+ [tool.setuptools.packages.find]
45
+ where = ["src"]
46
+ include = ["neuro_sam*", "sam2*"]
47
+
48
+ [tool.setuptools.package-data]
49
+ "sam2" = ["*.yaml", "configs/**/*.yaml"]
50
+ "neuro_sam" = ["napari.yaml"]
51
+
52
+ [project.entry-points."napari.manifest"]
53
+ neuro-sam = "neuro_sam:napari.yaml"
54
+
55
+ [tool.uv]
56
+ package = true
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1 @@
1
+ from .plugin import run_neuro_sam
@@ -0,0 +1,5 @@
1
+ # DO_TRANSONIC = False
2
+
3
+ # __version__ = "1.0.27"
4
+ # from ._version import __version__
5
+ # from ._myVersion import __version__
@@ -0,0 +1,3 @@
1
+ from .astar import BidirectionalAStarSearch
2
+ from .waypointastar import WaypointBidirectionalAStarSearch
3
+ from .waypointastar_speedup import quick_accurate_optimized_search