segprobe 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.
- segprobe-0.1.0/CITATION.cff +17 -0
- segprobe-0.1.0/LICENSE +201 -0
- segprobe-0.1.0/MANIFEST.in +2 -0
- segprobe-0.1.0/PKG-INFO +247 -0
- segprobe-0.1.0/README.md +222 -0
- segprobe-0.1.0/examples/basic_usage.py +26 -0
- segprobe-0.1.0/examples/medsam_lidc.py +309 -0
- segprobe-0.1.0/pyproject.toml +42 -0
- segprobe-0.1.0/setup.cfg +4 -0
- segprobe-0.1.0/src/segprobe/__init__.py +33 -0
- segprobe-0.1.0/src/segprobe/evaluate.py +116 -0
- segprobe-0.1.0/src/segprobe/prompts.py +266 -0
- segprobe-0.1.0/src/segprobe.egg-info/PKG-INFO +247 -0
- segprobe-0.1.0/src/segprobe.egg-info/SOURCES.txt +17 -0
- segprobe-0.1.0/src/segprobe.egg-info/dependency_links.txt +1 -0
- segprobe-0.1.0/src/segprobe.egg-info/requires.txt +6 -0
- segprobe-0.1.0/src/segprobe.egg-info/top_level.txt +1 -0
- segprobe-0.1.0/tests/test_evaluate.py +79 -0
- segprobe-0.1.0/tests/test_prompts.py +177 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
cff-version: 1.2.0
|
|
2
|
+
message: "If you use SegProbe in your research, please cite this software."
|
|
3
|
+
title: "SegProbe"
|
|
4
|
+
type: software
|
|
5
|
+
version: 0.1.0
|
|
6
|
+
authors:
|
|
7
|
+
- family-names: "Ebou"
|
|
8
|
+
given-names: "Elhaj Samitt"
|
|
9
|
+
orcid: "https://orcid.org/0009-0004-5324-2385"
|
|
10
|
+
repository-code: "https://github.com/hajteyib/segprobe"
|
|
11
|
+
license: "Apache-2.0"
|
|
12
|
+
keywords:
|
|
13
|
+
- medical image segmentation
|
|
14
|
+
- prompt evaluation
|
|
15
|
+
- interactive segmentation
|
|
16
|
+
- bounding boxes
|
|
17
|
+
- reproducibility
|
segprobe-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
segprobe-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: segprobe
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Reproducible, model-agnostic prompt evaluation for medical segmentation.
|
|
5
|
+
Author: Elhaj Samitt Ebou
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/hajteyib/segprobe
|
|
8
|
+
Project-URL: Issues, https://github.com/hajteyib/segprobe/issues
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
16
|
+
Requires-Python: >=3.10
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: numpy>=1.23
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
22
|
+
Requires-Dist: pytest>=8; extra == "dev"
|
|
23
|
+
Requires-Dist: ruff>=0.6; extra == "dev"
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
|
|
26
|
+
# SegProbe
|
|
27
|
+
|
|
28
|
+
SegProbe makes box-prompt experiments reproducible across medical image
|
|
29
|
+
segmentation models. It generates prompt protocols, records the manual effort,
|
|
30
|
+
and evaluates the resulting masks through one small API.
|
|
31
|
+
|
|
32
|
+

|
|
33
|
+
|
|
34
|
+
It started as code i used while testing MedSAM. i wanted to answer simple
|
|
35
|
+
questions without rewriting the evaluation each time:
|
|
36
|
+
|
|
37
|
+
- How many boxes must be drawn manually?
|
|
38
|
+
- What happens if boxes are sparse, larger, or slightly misplaced?
|
|
39
|
+
- Can the same protocol be compared across different models?
|
|
40
|
+
|
|
41
|
+
SegProbe has one runtime dependency: NumPy. It does not download a model or a
|
|
42
|
+
dataset.
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
python -m pip install segprobe
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
For development, install it from a local clone:
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
python -m pip install -e .
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Quick start
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from segprobe import evaluate, sparse_boxes
|
|
60
|
+
|
|
61
|
+
plan = sparse_boxes(target_mask, every=3, padding=5)
|
|
62
|
+
|
|
63
|
+
def predictor(volume, z_index, box_xyxy):
|
|
64
|
+
image_slice = volume[z_index]
|
|
65
|
+
return my_model.predict(image_slice, box=box_xyxy)
|
|
66
|
+
|
|
67
|
+
result = evaluate(predictor, image_volume, target_mask, plan)
|
|
68
|
+
|
|
69
|
+
print(result.dice)
|
|
70
|
+
print(result.manual_boxes)
|
|
71
|
+
print(result.generated_boxes)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
The `predictor` function is the only model-specific part. It can call MedSAM,
|
|
75
|
+
SAM2, nnInteractive, or your own slice-based model.
|
|
76
|
+
|
|
77
|
+
## Prompt protocols
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from segprobe import global_box, slice_boxes, sparse_boxes
|
|
81
|
+
|
|
82
|
+
dense = slice_boxes(mask, padding=5)
|
|
83
|
+
global_prompt = global_box(mask, padding=5)
|
|
84
|
+
sparse = sparse_boxes(mask, every=3, padding=5)
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
| Protocol | Manual effort | Boxes sent to the model |
|
|
88
|
+
| --- | ---: | --- |
|
|
89
|
+
| `slice_boxes` | one per positive slice | one manual box on each slice |
|
|
90
|
+
| `global_box` | one per volume | the same box reused on all positive slices |
|
|
91
|
+
| `sparse_boxes` | one every N positive slices, plus the last | manual anchors and interpolated boxes |
|
|
92
|
+
|
|
93
|
+
Each plan reports `manual_boxes`, `generated_boxes`, and `manual_fraction`.
|
|
94
|
+
`plan.to_dict()` returns plain Python values ready for JSON.
|
|
95
|
+
|
|
96
|
+
### Sparse prompts
|
|
97
|
+
|
|
98
|
+
Here, only five boxes are drawn manually. The other fifteen are interpolated.
|
|
99
|
+
|
|
100
|
+

|
|
101
|
+
|
|
102
|
+
Blue boxes are manual anchors. Orange dashed boxes are generated between them.
|
|
103
|
+
The green line is the reference-mask contour.
|
|
104
|
+
|
|
105
|
+
## Prompt robustness
|
|
106
|
+
|
|
107
|
+
Box size and placement can change a promptable model's result. SegProbe can add
|
|
108
|
+
padding or apply deterministic perturbations, so the same stress test can be
|
|
109
|
+
run again with the same seed.
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
from segprobe import jitter_plan
|
|
113
|
+
|
|
114
|
+
noisy = jitter_plan(
|
|
115
|
+
dense,
|
|
116
|
+
max_translate=5,
|
|
117
|
+
max_expand=10,
|
|
118
|
+
seed=42,
|
|
119
|
+
sample_key="case-001",
|
|
120
|
+
)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+

|
|
124
|
+
|
|
125
|
+
## Public MedSAM example
|
|
126
|
+
|
|
127
|
+
[`examples/medsam_lidc.py`](examples/medsam_lidc.py) connects SegProbe to the
|
|
128
|
+
official [MedSAM ViT-B model](https://github.com/bowang-lab/MedSAM). It expects
|
|
129
|
+
one folder per case containing
|
|
130
|
+
`image.nii.gz` and `mask.nii.gz`.
|
|
131
|
+
|
|
132
|
+
Run it from an environment where MedSAM, PyTorch, NiBabel, and scikit-image are
|
|
133
|
+
available:
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
python examples/medsam_lidc.py \
|
|
137
|
+
--cases-root /path/to/lidc_crops \
|
|
138
|
+
--one-per-patient \
|
|
139
|
+
--limit 5 \
|
|
140
|
+
--medsam-repo /path/to/MedSAM \
|
|
141
|
+
--checkpoint /path/to/medsam_vit_b.pth \
|
|
142
|
+
--output-dir medsam_results \
|
|
143
|
+
--device cpu
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The script saves `results.csv` and `results.json` after every protocol. Running
|
|
147
|
+
the same command again resumes from the saved results.
|
|
148
|
+
|
|
149
|
+
We used it for a small public check with five LIDC-IDRI nodules, one per
|
|
150
|
+
patient. The run used the official MedSAM checkpoint, no private fine-tuning,
|
|
151
|
+
no postprocessing, and an oracle positive z-range from the reference mask.
|
|
152
|
+
|
|
153
|
+
### Prompt density with padding=5
|
|
154
|
+
|
|
155
|
+
| Protocol | Mean manual boxes | Mean generated boxes | Mean Dice ± SD |
|
|
156
|
+
| --- | ---: | ---: | ---: |
|
|
157
|
+
| Global | 1.0 | 16.0 | 0.372 ± 0.173 |
|
|
158
|
+
| Sparse every 5 slices | 4.4 | 12.6 | 0.515 ± 0.267 |
|
|
159
|
+
| Sparse every 3 slices | 7.0 | 10.0 | 0.508 ± 0.270 |
|
|
160
|
+
| Sparse every 2 slices | 9.2 | 7.8 | 0.510 ± 0.270 |
|
|
161
|
+
| Dense | 17.0 | 0.0 | 0.511 ± 0.271 |
|
|
162
|
+
|
|
163
|
+
In this small run, sparse prompting every five slices used about 74% fewer
|
|
164
|
+
manual boxes than dense prompting, with nearly the same mean Dice.
|
|
165
|
+
|
|
166
|
+
### Dense prompt geometry
|
|
167
|
+
|
|
168
|
+
| Box | Mean Dice ± SD |
|
|
169
|
+
| --- | ---: |
|
|
170
|
+
| Tight, `padding=0` | 0.843 ± 0.031 |
|
|
171
|
+
| `padding=5` | 0.511 ± 0.271 |
|
|
172
|
+
| Large, `padding=10` | 0.310 ± 0.224 |
|
|
173
|
+
| `padding=5` with deterministic jitter | 0.363 ± 0.263 |
|
|
174
|
+
|
|
175
|
+
The tight box is derived directly from the reference mask, so it is a strong
|
|
176
|
+
oracle prompt. These five cases are a reproducibility example, not a model
|
|
177
|
+
comparison or a clinical result.
|
|
178
|
+
|
|
179
|
+
## Inputs and image formats
|
|
180
|
+
|
|
181
|
+
SegProbe works with arrays, not a specific medical file format. CT, MRI, PET,
|
|
182
|
+
and other 3D images can use the same API after they are loaded into NumPy.
|
|
183
|
+
|
|
184
|
+
- The mask must have shape `(z, y, x)`.
|
|
185
|
+
- The image must start with the same dimensions: `(z, y, x)` or
|
|
186
|
+
`(z, y, x, channels)`.
|
|
187
|
+
- Boxes use `(x_min, y_min, x_max, y_max)`, with exclusive maximum coordinates.
|
|
188
|
+
|
|
189
|
+
NIfTI, DICOM, NRRD, and other files can be loaded with tools such as NiBabel,
|
|
190
|
+
SimpleITK, or pydicom. File loading stays outside SegProbe so the core package
|
|
191
|
+
remains small and does not impose an imaging stack.
|
|
192
|
+
|
|
193
|
+
## Evaluation output
|
|
194
|
+
|
|
195
|
+
`evaluate` returns the predicted 3D mask together with:
|
|
196
|
+
|
|
197
|
+
- Dice and IoU;
|
|
198
|
+
- target and prediction voxel counts;
|
|
199
|
+
- manual and generated box counts;
|
|
200
|
+
- number of prompted slices.
|
|
201
|
+
|
|
202
|
+
The scalar values are available with `result.to_dict()` for a CSV or JSON
|
|
203
|
+
report.
|
|
204
|
+
|
|
205
|
+
## Scope
|
|
206
|
+
|
|
207
|
+
The current prompt generators use a reference mask. They are made for
|
|
208
|
+
controlled oracle-prompt evaluation, not automatic lesion localization.
|
|
209
|
+
|
|
210
|
+
Only reference-positive slices receive a box, so the positive z-range is known.
|
|
211
|
+
Results should be described as prompt-effort or prompt-robustness experiments,
|
|
212
|
+
not end-to-end detection results.
|
|
213
|
+
|
|
214
|
+
The manual-box count is an effort proxy. It is not a measurement of annotation
|
|
215
|
+
time. SegProbe is a research evaluation tool and is not intended for clinical
|
|
216
|
+
decision-making.
|
|
217
|
+
|
|
218
|
+
## Development
|
|
219
|
+
|
|
220
|
+
```bash
|
|
221
|
+
python -m pip install -e ".[dev]"
|
|
222
|
+
ruff check .
|
|
223
|
+
pytest -q
|
|
224
|
+
python -m build
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
Tests use small synthetic masks. They do not download images, checkpoints, or
|
|
228
|
+
patient data.
|
|
229
|
+
|
|
230
|
+
## Citation
|
|
231
|
+
|
|
232
|
+
If SegProbe supports your work, please cite the software using
|
|
233
|
+
[`CITATION.cff`](CITATION.cff).
|
|
234
|
+
|
|
235
|
+
The images in this README use a cropped, windowed, and annotated case from the
|
|
236
|
+
public [LIDC-IDRI collection](https://www.cancerimagingarchive.net/collection/lidc-idri/):
|
|
237
|
+
|
|
238
|
+
> Armato III, S. G., McLennan, G., Bidaut, L., et al. (2015). Data From
|
|
239
|
+
> LIDC-IDRI. The Cancer Imaging Archive.
|
|
240
|
+
> [https://doi.org/10.7937/K9/TCIA.2015.LO9QL9SX](https://doi.org/10.7937/K9/TCIA.2015.LO9QL9SX)
|
|
241
|
+
|
|
242
|
+
LIDC-IDRI is available under the
|
|
243
|
+
[Creative Commons Attribution 3.0 license](https://creativecommons.org/licenses/by/3.0/).
|
|
244
|
+
|
|
245
|
+
## License
|
|
246
|
+
|
|
247
|
+
SegProbe is released under the [Apache-2.0 license](LICENSE).
|
segprobe-0.1.0/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# SegProbe
|
|
2
|
+
|
|
3
|
+
SegProbe makes box-prompt experiments reproducible across medical image
|
|
4
|
+
segmentation models. It generates prompt protocols, records the manual effort,
|
|
5
|
+
and evaluates the resulting masks through one small API.
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
It started as code i used while testing MedSAM. i wanted to answer simple
|
|
10
|
+
questions without rewriting the evaluation each time:
|
|
11
|
+
|
|
12
|
+
- How many boxes must be drawn manually?
|
|
13
|
+
- What happens if boxes are sparse, larger, or slightly misplaced?
|
|
14
|
+
- Can the same protocol be compared across different models?
|
|
15
|
+
|
|
16
|
+
SegProbe has one runtime dependency: NumPy. It does not download a model or a
|
|
17
|
+
dataset.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
python -m pip install segprobe
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
For development, install it from a local clone:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
python -m pip install -e .
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Quick start
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from segprobe import evaluate, sparse_boxes
|
|
35
|
+
|
|
36
|
+
plan = sparse_boxes(target_mask, every=3, padding=5)
|
|
37
|
+
|
|
38
|
+
def predictor(volume, z_index, box_xyxy):
|
|
39
|
+
image_slice = volume[z_index]
|
|
40
|
+
return my_model.predict(image_slice, box=box_xyxy)
|
|
41
|
+
|
|
42
|
+
result = evaluate(predictor, image_volume, target_mask, plan)
|
|
43
|
+
|
|
44
|
+
print(result.dice)
|
|
45
|
+
print(result.manual_boxes)
|
|
46
|
+
print(result.generated_boxes)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
The `predictor` function is the only model-specific part. It can call MedSAM,
|
|
50
|
+
SAM2, nnInteractive, or your own slice-based model.
|
|
51
|
+
|
|
52
|
+
## Prompt protocols
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from segprobe import global_box, slice_boxes, sparse_boxes
|
|
56
|
+
|
|
57
|
+
dense = slice_boxes(mask, padding=5)
|
|
58
|
+
global_prompt = global_box(mask, padding=5)
|
|
59
|
+
sparse = sparse_boxes(mask, every=3, padding=5)
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| Protocol | Manual effort | Boxes sent to the model |
|
|
63
|
+
| --- | ---: | --- |
|
|
64
|
+
| `slice_boxes` | one per positive slice | one manual box on each slice |
|
|
65
|
+
| `global_box` | one per volume | the same box reused on all positive slices |
|
|
66
|
+
| `sparse_boxes` | one every N positive slices, plus the last | manual anchors and interpolated boxes |
|
|
67
|
+
|
|
68
|
+
Each plan reports `manual_boxes`, `generated_boxes`, and `manual_fraction`.
|
|
69
|
+
`plan.to_dict()` returns plain Python values ready for JSON.
|
|
70
|
+
|
|
71
|
+
### Sparse prompts
|
|
72
|
+
|
|
73
|
+
Here, only five boxes are drawn manually. The other fifteen are interpolated.
|
|
74
|
+
|
|
75
|
+

|
|
76
|
+
|
|
77
|
+
Blue boxes are manual anchors. Orange dashed boxes are generated between them.
|
|
78
|
+
The green line is the reference-mask contour.
|
|
79
|
+
|
|
80
|
+
## Prompt robustness
|
|
81
|
+
|
|
82
|
+
Box size and placement can change a promptable model's result. SegProbe can add
|
|
83
|
+
padding or apply deterministic perturbations, so the same stress test can be
|
|
84
|
+
run again with the same seed.
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from segprobe import jitter_plan
|
|
88
|
+
|
|
89
|
+
noisy = jitter_plan(
|
|
90
|
+
dense,
|
|
91
|
+
max_translate=5,
|
|
92
|
+
max_expand=10,
|
|
93
|
+
seed=42,
|
|
94
|
+
sample_key="case-001",
|
|
95
|
+
)
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+

|
|
99
|
+
|
|
100
|
+
## Public MedSAM example
|
|
101
|
+
|
|
102
|
+
[`examples/medsam_lidc.py`](examples/medsam_lidc.py) connects SegProbe to the
|
|
103
|
+
official [MedSAM ViT-B model](https://github.com/bowang-lab/MedSAM). It expects
|
|
104
|
+
one folder per case containing
|
|
105
|
+
`image.nii.gz` and `mask.nii.gz`.
|
|
106
|
+
|
|
107
|
+
Run it from an environment where MedSAM, PyTorch, NiBabel, and scikit-image are
|
|
108
|
+
available:
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
python examples/medsam_lidc.py \
|
|
112
|
+
--cases-root /path/to/lidc_crops \
|
|
113
|
+
--one-per-patient \
|
|
114
|
+
--limit 5 \
|
|
115
|
+
--medsam-repo /path/to/MedSAM \
|
|
116
|
+
--checkpoint /path/to/medsam_vit_b.pth \
|
|
117
|
+
--output-dir medsam_results \
|
|
118
|
+
--device cpu
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
The script saves `results.csv` and `results.json` after every protocol. Running
|
|
122
|
+
the same command again resumes from the saved results.
|
|
123
|
+
|
|
124
|
+
We used it for a small public check with five LIDC-IDRI nodules, one per
|
|
125
|
+
patient. The run used the official MedSAM checkpoint, no private fine-tuning,
|
|
126
|
+
no postprocessing, and an oracle positive z-range from the reference mask.
|
|
127
|
+
|
|
128
|
+
### Prompt density with padding=5
|
|
129
|
+
|
|
130
|
+
| Protocol | Mean manual boxes | Mean generated boxes | Mean Dice ± SD |
|
|
131
|
+
| --- | ---: | ---: | ---: |
|
|
132
|
+
| Global | 1.0 | 16.0 | 0.372 ± 0.173 |
|
|
133
|
+
| Sparse every 5 slices | 4.4 | 12.6 | 0.515 ± 0.267 |
|
|
134
|
+
| Sparse every 3 slices | 7.0 | 10.0 | 0.508 ± 0.270 |
|
|
135
|
+
| Sparse every 2 slices | 9.2 | 7.8 | 0.510 ± 0.270 |
|
|
136
|
+
| Dense | 17.0 | 0.0 | 0.511 ± 0.271 |
|
|
137
|
+
|
|
138
|
+
In this small run, sparse prompting every five slices used about 74% fewer
|
|
139
|
+
manual boxes than dense prompting, with nearly the same mean Dice.
|
|
140
|
+
|
|
141
|
+
### Dense prompt geometry
|
|
142
|
+
|
|
143
|
+
| Box | Mean Dice ± SD |
|
|
144
|
+
| --- | ---: |
|
|
145
|
+
| Tight, `padding=0` | 0.843 ± 0.031 |
|
|
146
|
+
| `padding=5` | 0.511 ± 0.271 |
|
|
147
|
+
| Large, `padding=10` | 0.310 ± 0.224 |
|
|
148
|
+
| `padding=5` with deterministic jitter | 0.363 ± 0.263 |
|
|
149
|
+
|
|
150
|
+
The tight box is derived directly from the reference mask, so it is a strong
|
|
151
|
+
oracle prompt. These five cases are a reproducibility example, not a model
|
|
152
|
+
comparison or a clinical result.
|
|
153
|
+
|
|
154
|
+
## Inputs and image formats
|
|
155
|
+
|
|
156
|
+
SegProbe works with arrays, not a specific medical file format. CT, MRI, PET,
|
|
157
|
+
and other 3D images can use the same API after they are loaded into NumPy.
|
|
158
|
+
|
|
159
|
+
- The mask must have shape `(z, y, x)`.
|
|
160
|
+
- The image must start with the same dimensions: `(z, y, x)` or
|
|
161
|
+
`(z, y, x, channels)`.
|
|
162
|
+
- Boxes use `(x_min, y_min, x_max, y_max)`, with exclusive maximum coordinates.
|
|
163
|
+
|
|
164
|
+
NIfTI, DICOM, NRRD, and other files can be loaded with tools such as NiBabel,
|
|
165
|
+
SimpleITK, or pydicom. File loading stays outside SegProbe so the core package
|
|
166
|
+
remains small and does not impose an imaging stack.
|
|
167
|
+
|
|
168
|
+
## Evaluation output
|
|
169
|
+
|
|
170
|
+
`evaluate` returns the predicted 3D mask together with:
|
|
171
|
+
|
|
172
|
+
- Dice and IoU;
|
|
173
|
+
- target and prediction voxel counts;
|
|
174
|
+
- manual and generated box counts;
|
|
175
|
+
- number of prompted slices.
|
|
176
|
+
|
|
177
|
+
The scalar values are available with `result.to_dict()` for a CSV or JSON
|
|
178
|
+
report.
|
|
179
|
+
|
|
180
|
+
## Scope
|
|
181
|
+
|
|
182
|
+
The current prompt generators use a reference mask. They are made for
|
|
183
|
+
controlled oracle-prompt evaluation, not automatic lesion localization.
|
|
184
|
+
|
|
185
|
+
Only reference-positive slices receive a box, so the positive z-range is known.
|
|
186
|
+
Results should be described as prompt-effort or prompt-robustness experiments,
|
|
187
|
+
not end-to-end detection results.
|
|
188
|
+
|
|
189
|
+
The manual-box count is an effort proxy. It is not a measurement of annotation
|
|
190
|
+
time. SegProbe is a research evaluation tool and is not intended for clinical
|
|
191
|
+
decision-making.
|
|
192
|
+
|
|
193
|
+
## Development
|
|
194
|
+
|
|
195
|
+
```bash
|
|
196
|
+
python -m pip install -e ".[dev]"
|
|
197
|
+
ruff check .
|
|
198
|
+
pytest -q
|
|
199
|
+
python -m build
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Tests use small synthetic masks. They do not download images, checkpoints, or
|
|
203
|
+
patient data.
|
|
204
|
+
|
|
205
|
+
## Citation
|
|
206
|
+
|
|
207
|
+
If SegProbe supports your work, please cite the software using
|
|
208
|
+
[`CITATION.cff`](CITATION.cff).
|
|
209
|
+
|
|
210
|
+
The images in this README use a cropped, windowed, and annotated case from the
|
|
211
|
+
public [LIDC-IDRI collection](https://www.cancerimagingarchive.net/collection/lidc-idri/):
|
|
212
|
+
|
|
213
|
+
> Armato III, S. G., McLennan, G., Bidaut, L., et al. (2015). Data From
|
|
214
|
+
> LIDC-IDRI. The Cancer Imaging Archive.
|
|
215
|
+
> [https://doi.org/10.7937/K9/TCIA.2015.LO9QL9SX](https://doi.org/10.7937/K9/TCIA.2015.LO9QL9SX)
|
|
216
|
+
|
|
217
|
+
LIDC-IDRI is available under the
|
|
218
|
+
[Creative Commons Attribution 3.0 license](https://creativecommons.org/licenses/by/3.0/).
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
SegProbe is released under the [Apache-2.0 license](LICENSE).
|