anonslide 0.1.0b1__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.
- anonslide-0.1.0b1/LICENSE +21 -0
- anonslide-0.1.0b1/PKG-INFO +177 -0
- anonslide-0.1.0b1/README.md +149 -0
- anonslide-0.1.0b1/pyproject.toml +74 -0
- anonslide-0.1.0b1/pyproject.toml.orig +66 -0
- anonslide-0.1.0b1/src/anonslide/__init__.py +23 -0
- anonslide-0.1.0b1/src/anonslide/__main__.py +3 -0
- anonslide-0.1.0b1/src/anonslide/api.py +44 -0
- anonslide-0.1.0b1/src/anonslide/cli.py +77 -0
- anonslide-0.1.0b1/src/anonslide/exceptions.py +21 -0
- anonslide-0.1.0b1/src/anonslide/formats/__init__.py +1 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/__init__.py +12 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/manager.py +193 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/mirax_composite.py +185 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/mirax_ini.py +107 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/mirax_label_patcher.py +225 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/mirax_parser.py +365 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/mirax_renamer.py +115 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/mirax_slide_model.py +107 -0
- anonslide-0.1.0b1/src/anonslide/formats/mirax/transaction.py +72 -0
- anonslide-0.1.0b1/src/anonslide/labels/__init__.py +3 -0
- anonslide-0.1.0b1/src/anonslide/labels/fonts/JetBrainsMonoNLNerdFontPropo-Regular.ttf +0 -0
- anonslide-0.1.0b1/src/anonslide/labels/fonts/OFL.txt +93 -0
- anonslide-0.1.0b1/src/anonslide/labels/labels.py +202 -0
- anonslide-0.1.0b1/src/anonslide/managers/__init__.py +4 -0
- anonslide-0.1.0b1/src/anonslide/managers/factory.py +63 -0
- anonslide-0.1.0b1/src/anonslide/managers/slide_manager.py +32 -0
- anonslide-0.1.0b1/src/anonslide/models/__init__.py +4 -0
- anonslide-0.1.0b1/src/anonslide/models/result.py +13 -0
- anonslide-0.1.0b1/src/anonslide/models/slide_model.py +40 -0
- anonslide-0.1.0b1/src/anonslide/py.typed +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nikhil Ravikumar
|
|
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,177 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: anonslide
|
|
3
|
+
Version: 0.1.0b1
|
|
4
|
+
Summary: Pseudonymize whole-slide images without rescanning
|
|
5
|
+
Keywords: whole-slide imaging,WSI,MIRAX,pathology,pseudonymization
|
|
6
|
+
Author: Nikhil Ravikumar
|
|
7
|
+
Author-email: Nikhil Ravikumar <nikhdpro@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
License-File: src/anonslide/labels/fonts/OFL.txt
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
18
|
+
Requires-Dist: openslide-bin>=4.0.1.2
|
|
19
|
+
Requires-Dist: openslide-python>=1.4.6
|
|
20
|
+
Requires-Dist: pictex>=2.3.0
|
|
21
|
+
Requires-Dist: pillow>=12.3.0
|
|
22
|
+
Requires-Dist: pydantic>=2.13.5
|
|
23
|
+
Requires-Python: >=3.12
|
|
24
|
+
Project-URL: Homepage, https://github.com/pixel-pathologist/anonslide
|
|
25
|
+
Project-URL: Repository, https://github.com/pixel-pathologist/anonslide
|
|
26
|
+
Project-URL: Issues, https://github.com/pixel-pathologist/anonslide/issues
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# anonslide
|
|
30
|
+
|
|
31
|
+
**Beta software for MIRAX whole-slide image pseudonymization.**
|
|
32
|
+
|
|
33
|
+
Anonslide changes identifying slide filenames, the MIRAX slide name, and label imagery without rescanning or re-exporting the diagnostic image pyramid. It provides a small Python API, an extensible label renderer, and a command-line interface.
|
|
34
|
+
|
|
35
|
+
> **Destructive operation:** anonslide modifies the supplied slide **in place** and removes its optional XML sidecar. Keep an independent backup of the original slide. Test the processed copy in your viewer and follow your institution's de-identification procedure before transferring it.
|
|
36
|
+
|
|
37
|
+
## Supported software and formats
|
|
38
|
+
|
|
39
|
+
- Python 3.12 or later
|
|
40
|
+
- MIRAX `.mrxs` slides with their matching data directory
|
|
41
|
+
- Other WSI formats are not supported in this beta
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
python -m pip install anonslide
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Or add it to a uv project:
|
|
50
|
+
|
|
51
|
+
```bash
|
|
52
|
+
uv add anonslide
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Python quickstart
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
from anonslide import anonymize_slide
|
|
59
|
+
|
|
60
|
+
result = anonymize_slide(
|
|
61
|
+
slide_path="original-slide.mrxs",
|
|
62
|
+
new_slide_name="STUDY_004_A1",
|
|
63
|
+
label_info={
|
|
64
|
+
"Case": "STUDY_004",
|
|
65
|
+
"Block": "A1",
|
|
66
|
+
"Stain": "H&E",
|
|
67
|
+
},
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
print(result.slide_path) # STUDY_004_A1.mrxs
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The dictionary order determines the top-to-bottom label row order. The caller supplies all pseudonyms; anonslide does not create or retain an identity mapping.
|
|
74
|
+
|
|
75
|
+
The new name must contain 1-128 ASCII letters, numbers, spaces, underscores, or hyphens, and must start with a letter or number. Do not include a file extension.
|
|
76
|
+
|
|
77
|
+
### Custom label styling
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from anonslide import LabelStyle, anonymize_slide
|
|
81
|
+
|
|
82
|
+
style = LabelStyle(
|
|
83
|
+
font_path="/path/to/AnotherFont.ttf",
|
|
84
|
+
font_size=54,
|
|
85
|
+
background_color="#ffffff",
|
|
86
|
+
text_color="#202020",
|
|
87
|
+
key_color="#005a9c",
|
|
88
|
+
border_color="#005a9c",
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
result = anonymize_slide(
|
|
92
|
+
slide_path="original-slide.mrxs",
|
|
93
|
+
new_slide_name="STUDY_004_A1",
|
|
94
|
+
label_info={"Case": "STUDY_004", "Block": "A1"},
|
|
95
|
+
label_style=style,
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Omit `font_path` to use the font packaged with anonslide. Colors accept PicTex-compatible named colors and hexadecimal values. `font_size` is a maximum: anonslide reduces it when needed to fit the label dimensions and raises an error rather than silently clipping text that still cannot fit. Font, color, and layout validation occurs before slide mutation.
|
|
100
|
+
|
|
101
|
+
## Command line
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
anonslide original-slide.mrxs \
|
|
105
|
+
--new-name STUDY_004_A1 \
|
|
106
|
+
--label Case=STUDY_004 \
|
|
107
|
+
--label Block=A1 \
|
|
108
|
+
--label Stain=H\&E
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Repeat `--label` in the desired row order. Quote values containing spaces or shell-special characters. If no `--label` is supplied, the replacement label is blank.
|
|
112
|
+
|
|
113
|
+
On success, the command prints only the new slide path. Errors go to stderr and produce a nonzero exit status.
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
anonslide --help
|
|
117
|
+
anonslide --version
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
## Runnable CMU-2 example
|
|
121
|
+
|
|
122
|
+
If you have the public CMU-2 MIRAX sample at `WSI/CMU-2/CMU-2.mrxs`, this example copies it and pseudonymizes only the copy:
|
|
123
|
+
|
|
124
|
+
```bash
|
|
125
|
+
uv run python examples/anonymize_cmu2_copy.py
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
It writes `anonslide-cmu2-demo/STUDY_CMU_2.mrxs`, checks the renamed bundle and `SLIDE_NAME`, compares OpenSlide dimensions, and exports `replacement-label.png` for visual inspection. The output directory is ignored by Git and the script refuses to overwrite an existing demo. Use `--output-dir` to choose another location.
|
|
129
|
+
|
|
130
|
+
## What MIRAX pseudonymization does
|
|
131
|
+
|
|
132
|
+
| Component | Behavior |
|
|
133
|
+
| --- | --- |
|
|
134
|
+
| `.mrxs` file and matching data directory | Renamed to the supplied pseudonym |
|
|
135
|
+
| `Slidedat.ini` `SLIDE_NAME` | Replaced with an uppercase, underscore-separated pseudonym |
|
|
136
|
+
| Standalone label image | Replaced with the generated label at the original dimensions |
|
|
137
|
+
| Label/barcode region in `ScanDataLayer_WholeSlide` | Replaced with the generated label while retaining the surrounding composite |
|
|
138
|
+
| Optional `.xml` sidecar | Removed because it can retain original metadata and is not required by viewers |
|
|
139
|
+
| `SLIDE_ID` | **Retained intentionally** as internal numeric metadata |
|
|
140
|
+
| `BARCODE_VALUE` | **Retained** because it stores stain type in the supported workflow |
|
|
141
|
+
| Other acquisition metadata | Retained unless explicitly listed above |
|
|
142
|
+
| Diagnostic pyramid | Not re-exported or re-scanned |
|
|
143
|
+
|
|
144
|
+
Only the standalone label and declared composite label region are edited in the current implementation. Other associated images should still be inspected for your scanner and workflow. Anonslide does not perform general OCR or guarantee removal of every possible direct or indirect identifier.
|
|
145
|
+
|
|
146
|
+
The original INI line endings, BOM, comments, and unrelated fields are preserved when changing `SLIDE_NAME`. Destination collisions and invalid names are rejected before mutation. Temporary recovery copies are used to restore changed files if processing fails; they are not a substitute for your independent backup.
|
|
147
|
+
|
|
148
|
+
## Privacy and compatibility limitations
|
|
149
|
+
|
|
150
|
+
- This is pseudonymization software, not a guarantee of legal or regulatory anonymization.
|
|
151
|
+
- Retained numeric IDs, acquisition metadata, tissue appearance, and other technical metadata may permit linkage.
|
|
152
|
+
- Overwriting file bytes does not guarantee secure physical erasure from SSDs, snapshots, journals, backups, or copy-on-write filesystems.
|
|
153
|
+
- MIRAX layouts vary. Unsupported or inconsistent structures should cause an error rather than silent partial processing.
|
|
154
|
+
- Verify both the standalone label and whole-slide composite, reopen the result in OpenSlide and your clinical/research viewer, and inspect any sidecars before transfer.
|
|
155
|
+
- Do not open or modify the only copy of an original research slide.
|
|
156
|
+
|
|
157
|
+
## Development
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
uv sync
|
|
161
|
+
uv run pytest
|
|
162
|
+
uv run ruff check .
|
|
163
|
+
uv run ruff format --check .
|
|
164
|
+
uv build
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Portable tests use temporary files. Opt-in MIRAX integration tests require a local slide:
|
|
168
|
+
|
|
169
|
+
```bash
|
|
170
|
+
ANONSLIDE_TEST_MRXS=/path/to/slide.mrxs uv run pytest -m integration
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Mutation tests copy the slide to a temporary directory first. Never point a mutation test at an original slide. See `AGENTS.md` for architecture, privacy, testing, and branching rules.
|
|
174
|
+
|
|
175
|
+
## License
|
|
176
|
+
|
|
177
|
+
Anonslide is available under the MIT License. The bundled JetBrains Mono Nerd Font remains under the SIL Open Font License 1.1; see `THIRD_PARTY_NOTICES.md` and the included `OFL.txt`.
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# anonslide
|
|
2
|
+
|
|
3
|
+
**Beta software for MIRAX whole-slide image pseudonymization.**
|
|
4
|
+
|
|
5
|
+
Anonslide changes identifying slide filenames, the MIRAX slide name, and label imagery without rescanning or re-exporting the diagnostic image pyramid. It provides a small Python API, an extensible label renderer, and a command-line interface.
|
|
6
|
+
|
|
7
|
+
> **Destructive operation:** anonslide modifies the supplied slide **in place** and removes its optional XML sidecar. Keep an independent backup of the original slide. Test the processed copy in your viewer and follow your institution's de-identification procedure before transferring it.
|
|
8
|
+
|
|
9
|
+
## Supported software and formats
|
|
10
|
+
|
|
11
|
+
- Python 3.12 or later
|
|
12
|
+
- MIRAX `.mrxs` slides with their matching data directory
|
|
13
|
+
- Other WSI formats are not supported in this beta
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
python -m pip install anonslide
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or add it to a uv project:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
uv add anonslide
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Python quickstart
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from anonslide import anonymize_slide
|
|
31
|
+
|
|
32
|
+
result = anonymize_slide(
|
|
33
|
+
slide_path="original-slide.mrxs",
|
|
34
|
+
new_slide_name="STUDY_004_A1",
|
|
35
|
+
label_info={
|
|
36
|
+
"Case": "STUDY_004",
|
|
37
|
+
"Block": "A1",
|
|
38
|
+
"Stain": "H&E",
|
|
39
|
+
},
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
print(result.slide_path) # STUDY_004_A1.mrxs
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The dictionary order determines the top-to-bottom label row order. The caller supplies all pseudonyms; anonslide does not create or retain an identity mapping.
|
|
46
|
+
|
|
47
|
+
The new name must contain 1-128 ASCII letters, numbers, spaces, underscores, or hyphens, and must start with a letter or number. Do not include a file extension.
|
|
48
|
+
|
|
49
|
+
### Custom label styling
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
from anonslide import LabelStyle, anonymize_slide
|
|
53
|
+
|
|
54
|
+
style = LabelStyle(
|
|
55
|
+
font_path="/path/to/AnotherFont.ttf",
|
|
56
|
+
font_size=54,
|
|
57
|
+
background_color="#ffffff",
|
|
58
|
+
text_color="#202020",
|
|
59
|
+
key_color="#005a9c",
|
|
60
|
+
border_color="#005a9c",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
result = anonymize_slide(
|
|
64
|
+
slide_path="original-slide.mrxs",
|
|
65
|
+
new_slide_name="STUDY_004_A1",
|
|
66
|
+
label_info={"Case": "STUDY_004", "Block": "A1"},
|
|
67
|
+
label_style=style,
|
|
68
|
+
)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Omit `font_path` to use the font packaged with anonslide. Colors accept PicTex-compatible named colors and hexadecimal values. `font_size` is a maximum: anonslide reduces it when needed to fit the label dimensions and raises an error rather than silently clipping text that still cannot fit. Font, color, and layout validation occurs before slide mutation.
|
|
72
|
+
|
|
73
|
+
## Command line
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
anonslide original-slide.mrxs \
|
|
77
|
+
--new-name STUDY_004_A1 \
|
|
78
|
+
--label Case=STUDY_004 \
|
|
79
|
+
--label Block=A1 \
|
|
80
|
+
--label Stain=H\&E
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Repeat `--label` in the desired row order. Quote values containing spaces or shell-special characters. If no `--label` is supplied, the replacement label is blank.
|
|
84
|
+
|
|
85
|
+
On success, the command prints only the new slide path. Errors go to stderr and produce a nonzero exit status.
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
anonslide --help
|
|
89
|
+
anonslide --version
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Runnable CMU-2 example
|
|
93
|
+
|
|
94
|
+
If you have the public CMU-2 MIRAX sample at `WSI/CMU-2/CMU-2.mrxs`, this example copies it and pseudonymizes only the copy:
|
|
95
|
+
|
|
96
|
+
```bash
|
|
97
|
+
uv run python examples/anonymize_cmu2_copy.py
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
It writes `anonslide-cmu2-demo/STUDY_CMU_2.mrxs`, checks the renamed bundle and `SLIDE_NAME`, compares OpenSlide dimensions, and exports `replacement-label.png` for visual inspection. The output directory is ignored by Git and the script refuses to overwrite an existing demo. Use `--output-dir` to choose another location.
|
|
101
|
+
|
|
102
|
+
## What MIRAX pseudonymization does
|
|
103
|
+
|
|
104
|
+
| Component | Behavior |
|
|
105
|
+
| --- | --- |
|
|
106
|
+
| `.mrxs` file and matching data directory | Renamed to the supplied pseudonym |
|
|
107
|
+
| `Slidedat.ini` `SLIDE_NAME` | Replaced with an uppercase, underscore-separated pseudonym |
|
|
108
|
+
| Standalone label image | Replaced with the generated label at the original dimensions |
|
|
109
|
+
| Label/barcode region in `ScanDataLayer_WholeSlide` | Replaced with the generated label while retaining the surrounding composite |
|
|
110
|
+
| Optional `.xml` sidecar | Removed because it can retain original metadata and is not required by viewers |
|
|
111
|
+
| `SLIDE_ID` | **Retained intentionally** as internal numeric metadata |
|
|
112
|
+
| `BARCODE_VALUE` | **Retained** because it stores stain type in the supported workflow |
|
|
113
|
+
| Other acquisition metadata | Retained unless explicitly listed above |
|
|
114
|
+
| Diagnostic pyramid | Not re-exported or re-scanned |
|
|
115
|
+
|
|
116
|
+
Only the standalone label and declared composite label region are edited in the current implementation. Other associated images should still be inspected for your scanner and workflow. Anonslide does not perform general OCR or guarantee removal of every possible direct or indirect identifier.
|
|
117
|
+
|
|
118
|
+
The original INI line endings, BOM, comments, and unrelated fields are preserved when changing `SLIDE_NAME`. Destination collisions and invalid names are rejected before mutation. Temporary recovery copies are used to restore changed files if processing fails; they are not a substitute for your independent backup.
|
|
119
|
+
|
|
120
|
+
## Privacy and compatibility limitations
|
|
121
|
+
|
|
122
|
+
- This is pseudonymization software, not a guarantee of legal or regulatory anonymization.
|
|
123
|
+
- Retained numeric IDs, acquisition metadata, tissue appearance, and other technical metadata may permit linkage.
|
|
124
|
+
- Overwriting file bytes does not guarantee secure physical erasure from SSDs, snapshots, journals, backups, or copy-on-write filesystems.
|
|
125
|
+
- MIRAX layouts vary. Unsupported or inconsistent structures should cause an error rather than silent partial processing.
|
|
126
|
+
- Verify both the standalone label and whole-slide composite, reopen the result in OpenSlide and your clinical/research viewer, and inspect any sidecars before transfer.
|
|
127
|
+
- Do not open or modify the only copy of an original research slide.
|
|
128
|
+
|
|
129
|
+
## Development
|
|
130
|
+
|
|
131
|
+
```bash
|
|
132
|
+
uv sync
|
|
133
|
+
uv run pytest
|
|
134
|
+
uv run ruff check .
|
|
135
|
+
uv run ruff format --check .
|
|
136
|
+
uv build
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Portable tests use temporary files. Opt-in MIRAX integration tests require a local slide:
|
|
140
|
+
|
|
141
|
+
```bash
|
|
142
|
+
ANONSLIDE_TEST_MRXS=/path/to/slide.mrxs uv run pytest -m integration
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Mutation tests copy the slide to a temporary directory first. Never point a mutation test at an original slide. See `AGENTS.md` for architecture, privacy, testing, and branching rules.
|
|
146
|
+
|
|
147
|
+
## License
|
|
148
|
+
|
|
149
|
+
Anonslide is available under the MIT License. The bundled JetBrains Mono Nerd Font remains under the SIL Open Font License 1.1; see `THIRD_PARTY_NOTICES.md` and the included `OFL.txt`.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "anonslide"
|
|
3
|
+
version = "0.1.0b1"
|
|
4
|
+
description = "Pseudonymize whole-slide images without rescanning"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "MIT"
|
|
7
|
+
license-files = [
|
|
8
|
+
"LICENSE",
|
|
9
|
+
"src/anonslide/labels/fonts/OFL.txt",
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
keywords = [
|
|
13
|
+
"whole-slide imaging",
|
|
14
|
+
"WSI",
|
|
15
|
+
"MIRAX",
|
|
16
|
+
"pathology",
|
|
17
|
+
"pseudonymization",
|
|
18
|
+
]
|
|
19
|
+
classifiers = [
|
|
20
|
+
"Development Status :: 4 - Beta",
|
|
21
|
+
"Intended Audience :: Science/Research",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"openslide-bin>=4.0.1.2",
|
|
30
|
+
"openslide-python>=1.4.6",
|
|
31
|
+
"pictex>=2.3.0",
|
|
32
|
+
"pillow>=12.3.0",
|
|
33
|
+
"pydantic>=2.13.5",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[[project.authors]]
|
|
37
|
+
name = "Nikhil Ravikumar"
|
|
38
|
+
email = "nikhdpro@gmail.com"
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Homepage = "https://github.com/pixel-pathologist/anonslide"
|
|
42
|
+
Repository = "https://github.com/pixel-pathologist/anonslide"
|
|
43
|
+
Issues = "https://github.com/pixel-pathologist/anonslide/issues"
|
|
44
|
+
|
|
45
|
+
[project.scripts]
|
|
46
|
+
anonslide = "anonslide.cli:main"
|
|
47
|
+
|
|
48
|
+
[dependency-groups]
|
|
49
|
+
dev = [
|
|
50
|
+
"coverage>=7.16.2",
|
|
51
|
+
"pytest>=9.1.1",
|
|
52
|
+
"ruff>=0.16.10",
|
|
53
|
+
]
|
|
54
|
+
|
|
55
|
+
[build-system]
|
|
56
|
+
requires = ["uv_build>=0.12.3,<0.14.0"]
|
|
57
|
+
build-backend = "uv_build"
|
|
58
|
+
|
|
59
|
+
[tool.uv]
|
|
60
|
+
override-dependencies = ["skia-python==144.0.post2"]
|
|
61
|
+
|
|
62
|
+
[tool.pyright]
|
|
63
|
+
venvPath = "."
|
|
64
|
+
venv = ".venv"
|
|
65
|
+
|
|
66
|
+
[[tool.pyright.executionEnvironments]]
|
|
67
|
+
root = "src"
|
|
68
|
+
|
|
69
|
+
[tool.pytest.ini_options]
|
|
70
|
+
testpaths = ["tests"]
|
|
71
|
+
markers = ["integration: opt-in tests requiring a local whole-slide image"]
|
|
72
|
+
|
|
73
|
+
[tool.ruff]
|
|
74
|
+
target-version = "py312"
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "anonslide"
|
|
3
|
+
version = "0.1.0b1"
|
|
4
|
+
description = "Pseudonymize whole-slide images without rescanning"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Nikhil Ravikumar", email = "nikhdpro@gmail.com" }
|
|
8
|
+
]
|
|
9
|
+
license = "MIT"
|
|
10
|
+
license-files = ["LICENSE", "src/anonslide/labels/fonts/OFL.txt"]
|
|
11
|
+
requires-python = ">=3.12"
|
|
12
|
+
keywords = ["whole-slide imaging", "WSI", "MIRAX", "pathology", "pseudonymization"]
|
|
13
|
+
classifiers = [
|
|
14
|
+
"Development Status :: 4 - Beta",
|
|
15
|
+
"Intended Audience :: Science/Research",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Topic :: Scientific/Engineering :: Medical Science Apps.",
|
|
21
|
+
]
|
|
22
|
+
dependencies = [
|
|
23
|
+
"openslide-bin>=4.0.1.2",
|
|
24
|
+
"openslide-python>=1.4.6",
|
|
25
|
+
"pictex>=2.3.0",
|
|
26
|
+
"pillow>=12.3.0",
|
|
27
|
+
"pydantic>=2.13.5",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/pixel-pathologist/anonslide"
|
|
32
|
+
Repository = "https://github.com/pixel-pathologist/anonslide"
|
|
33
|
+
Issues = "https://github.com/pixel-pathologist/anonslide/issues"
|
|
34
|
+
|
|
35
|
+
[project.scripts]
|
|
36
|
+
anonslide = "anonslide.cli:main"
|
|
37
|
+
|
|
38
|
+
[dependency-groups]
|
|
39
|
+
dev = [
|
|
40
|
+
"coverage>=7.16.2",
|
|
41
|
+
"pytest>=9.1.1",
|
|
42
|
+
"ruff>=0.16.10",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[build-system]
|
|
46
|
+
requires = ["uv_build>=0.12.3,<0.14.0"]
|
|
47
|
+
build-backend = "uv_build"
|
|
48
|
+
|
|
49
|
+
[tool.uv]
|
|
50
|
+
override-dependencies = [
|
|
51
|
+
"skia-python==144.0.post2",
|
|
52
|
+
]
|
|
53
|
+
|
|
54
|
+
[tool.pyright]
|
|
55
|
+
executionEnvironments = [{root = "src"}]
|
|
56
|
+
venvPath = "."
|
|
57
|
+
venv = ".venv"
|
|
58
|
+
|
|
59
|
+
[tool.pytest.ini_options]
|
|
60
|
+
testpaths = ["tests"]
|
|
61
|
+
markers = [
|
|
62
|
+
"integration: opt-in tests requiring a local whole-slide image",
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.ruff]
|
|
66
|
+
target-version = "py312"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""Pseudonymize whole-slide images without rescanning."""
|
|
2
|
+
|
|
3
|
+
from anonslide.api import anonymize_slide
|
|
4
|
+
from anonslide.exceptions import (
|
|
5
|
+
AnonslideError,
|
|
6
|
+
SlideCollisionError,
|
|
7
|
+
SlideProcessingError,
|
|
8
|
+
SlideValidationError,
|
|
9
|
+
UnsupportedSlideFormatError,
|
|
10
|
+
)
|
|
11
|
+
from anonslide.labels import LabelStyle
|
|
12
|
+
from anonslide.models import AnonymizationResult
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"AnonslideError",
|
|
16
|
+
"AnonymizationResult",
|
|
17
|
+
"LabelStyle",
|
|
18
|
+
"SlideCollisionError",
|
|
19
|
+
"SlideProcessingError",
|
|
20
|
+
"SlideValidationError",
|
|
21
|
+
"UnsupportedSlideFormatError",
|
|
22
|
+
"anonymize_slide",
|
|
23
|
+
]
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"""High-level programmer API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from anonslide.labels import LabelStyle
|
|
8
|
+
from anonslide.managers import SlideManagerFactory
|
|
9
|
+
from anonslide.models import AnonymizationResult
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def anonymize_slide(
|
|
13
|
+
slide_path: str | Path,
|
|
14
|
+
new_slide_name: str,
|
|
15
|
+
label_info: dict[str, str],
|
|
16
|
+
*,
|
|
17
|
+
label_style: LabelStyle | None = None,
|
|
18
|
+
) -> AnonymizationResult:
|
|
19
|
+
"""Pseudonymize a supported slide in place.
|
|
20
|
+
|
|
21
|
+
This operation is destructive. Keep an independent backup of the original
|
|
22
|
+
slide before calling it. MIRAX XML sidecars are removed, while ``SLIDE_ID``
|
|
23
|
+
and ``BARCODE_VALUE`` are retained according to the MIRAX policy.
|
|
24
|
+
"""
|
|
25
|
+
if not isinstance(label_info, dict) or not all(
|
|
26
|
+
isinstance(key, str) and isinstance(value, str)
|
|
27
|
+
for key, value in label_info.items()
|
|
28
|
+
):
|
|
29
|
+
raise TypeError("label_info must be a dictionary of string keys and values")
|
|
30
|
+
if label_style is not None and not isinstance(label_style, LabelStyle):
|
|
31
|
+
raise TypeError("label_style must be a LabelStyle or None")
|
|
32
|
+
|
|
33
|
+
factory = SlideManagerFactory()
|
|
34
|
+
manager = factory.create_manager(Path(slide_path))
|
|
35
|
+
model = manager.anonymize(
|
|
36
|
+
new_slide_name=new_slide_name,
|
|
37
|
+
label_info=label_info,
|
|
38
|
+
label_style=label_style,
|
|
39
|
+
)
|
|
40
|
+
return AnonymizationResult(
|
|
41
|
+
slide_path=model.file_path,
|
|
42
|
+
format=manager.slide_format,
|
|
43
|
+
warnings=tuple(getattr(manager, "warnings", ())),
|
|
44
|
+
)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""Command-line interface for anonslide."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import argparse
|
|
6
|
+
import sys
|
|
7
|
+
from collections.abc import Sequence
|
|
8
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
9
|
+
|
|
10
|
+
from anonslide.api import anonymize_slide
|
|
11
|
+
from anonslide.exceptions import AnonslideError
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _package_version() -> str:
|
|
15
|
+
try:
|
|
16
|
+
return version("anonslide")
|
|
17
|
+
except PackageNotFoundError:
|
|
18
|
+
return "unknown"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def build_parser() -> argparse.ArgumentParser:
|
|
22
|
+
parser = argparse.ArgumentParser(
|
|
23
|
+
prog="anonslide",
|
|
24
|
+
description="Pseudonymize a MIRAX whole-slide image in place.",
|
|
25
|
+
epilog=(
|
|
26
|
+
"WARNING: this modifies the slide destructively and removes its optional "
|
|
27
|
+
"XML sidecar. Keep an independent backup. Repeat --label to add rows."
|
|
28
|
+
),
|
|
29
|
+
)
|
|
30
|
+
parser.add_argument("slide_path", help="path to the .mrxs slide")
|
|
31
|
+
parser.add_argument(
|
|
32
|
+
"--new-name", required=True, help="new filename stem and SLIDE_NAME pseudonym"
|
|
33
|
+
)
|
|
34
|
+
parser.add_argument(
|
|
35
|
+
"--label",
|
|
36
|
+
action="append",
|
|
37
|
+
default=[],
|
|
38
|
+
metavar="KEY=VALUE",
|
|
39
|
+
help="label row; repeat in the desired top-to-bottom order",
|
|
40
|
+
)
|
|
41
|
+
parser.add_argument(
|
|
42
|
+
"--version", action="version", version=f"%(prog)s {_package_version()}"
|
|
43
|
+
)
|
|
44
|
+
return parser
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _parse_labels(items: list[str]) -> dict[str, str]:
|
|
48
|
+
labels: dict[str, str] = {}
|
|
49
|
+
for item in items:
|
|
50
|
+
key, separator, value = item.partition("=")
|
|
51
|
+
if not separator or not key.strip() or not value.strip():
|
|
52
|
+
raise ValueError(f"Invalid --label {item!r}; expected non-empty KEY=VALUE")
|
|
53
|
+
key = key.strip()
|
|
54
|
+
if key in labels:
|
|
55
|
+
raise ValueError(f"Duplicate label key: {key}")
|
|
56
|
+
labels[key] = value.strip()
|
|
57
|
+
return labels
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def main(argv: Sequence[str] | None = None) -> int:
|
|
61
|
+
parser = build_parser()
|
|
62
|
+
arguments = parser.parse_args(argv)
|
|
63
|
+
try:
|
|
64
|
+
labels = _parse_labels(arguments.label)
|
|
65
|
+
result = anonymize_slide(
|
|
66
|
+
slide_path=arguments.slide_path,
|
|
67
|
+
new_slide_name=arguments.new_name,
|
|
68
|
+
label_info=labels,
|
|
69
|
+
)
|
|
70
|
+
except (AnonslideError, OSError, ValueError) as error:
|
|
71
|
+
print(f"anonslide: error: {error}", file=sys.stderr)
|
|
72
|
+
return 1
|
|
73
|
+
|
|
74
|
+
for warning in result.warnings:
|
|
75
|
+
print(f"anonslide: warning: {warning}", file=sys.stderr)
|
|
76
|
+
print(result.slide_path)
|
|
77
|
+
return 0
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Public exceptions raised by anonslide."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class AnonslideError(Exception):
|
|
5
|
+
"""Base class for anonslide errors."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class UnsupportedSlideFormatError(AnonslideError, ValueError):
|
|
9
|
+
"""Raised when no manager supports the supplied slide."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class SlideValidationError(AnonslideError, ValueError):
|
|
13
|
+
"""Raised when a slide or requested pseudonym is invalid."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class SlideCollisionError(AnonslideError, FileExistsError):
|
|
17
|
+
"""Raised when anonymization would overwrite an existing path."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class SlideProcessingError(AnonslideError, RuntimeError):
|
|
21
|
+
"""Raised when a slide cannot be processed safely."""
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Format-specific slide implementations."""
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from .manager import MiraxSlideManager
|
|
2
|
+
from .mirax_label_patcher import MiraxLabelPatcher
|
|
3
|
+
from .mirax_parser import MiraxSlideParser
|
|
4
|
+
from .mirax_slide_model import MiraxAssociatedImage, MiraxSlideModel
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"MiraxAssociatedImage",
|
|
8
|
+
"MiraxLabelPatcher",
|
|
9
|
+
"MiraxSlideManager",
|
|
10
|
+
"MiraxSlideModel",
|
|
11
|
+
"MiraxSlideParser",
|
|
12
|
+
]
|