mlarray 0.0.23__py3-none-any.whl

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.
mlarray/utils.py ADDED
@@ -0,0 +1,17 @@
1
+ import json
2
+
3
+
4
+ def is_serializable(d: dict) -> bool:
5
+ """Checks whether a dictionary is JSON-serializable.
6
+
7
+ Args:
8
+ d (dict): Input dictionary to test.
9
+
10
+ Returns:
11
+ bool: True when serializable, otherwise False.
12
+ """
13
+ try:
14
+ json.dumps(d)
15
+ return True
16
+ except (TypeError, OverflowError):
17
+ return False
@@ -0,0 +1,246 @@
1
+ Metadata-Version: 2.4
2
+ Name: mlarray
3
+ Version: 0.0.23
4
+ Summary: A standardized blosc2 image reader and writer for medical images.
5
+ Author-email: Karol Gotkowski <karol.gotkowski@dkfz.de>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Karol-G/mlarray
8
+ Project-URL: Source, https://github.com/Karol-G/mlarray
9
+ Project-URL: Issues, https://github.com/Karol-G/mlarray/issues
10
+ Keywords: copier,template,python
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Operating System :: OS Independent
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: blosc2>=3
20
+ Requires-Dist: numpy>=2
21
+ Provides-Extra: dev
22
+ Requires-Dist: build>=1.0; extra == "dev"
23
+ Requires-Dist: pytest>=7.0; extra == "dev"
24
+ Requires-Dist: twine>=4.0; extra == "dev"
25
+ Requires-Dist: setuptools_scm[toml]>=8.0; extra == "dev"
26
+ Provides-Extra: docs
27
+ Requires-Dist: mkdocs-material>=9.5; extra == "docs"
28
+ Requires-Dist: mkdocs-include-markdown-plugin>=6.0; extra == "docs"
29
+ Requires-Dist: mkdocstrings[python]>=0.25; extra == "docs"
30
+ Requires-Dist: pymdown-extensions>=10.0; extra == "docs"
31
+ Provides-Extra: all
32
+ Requires-Dist: medvol; extra == "all"
33
+ Dynamic: license-file
34
+
35
+ <p align="center">
36
+ <img src="https://raw.githubusercontent.com/Karol-G/mlarray/main/assets/banner.png" alt="{ML Array} banner" width="700" />
37
+ </p>
38
+
39
+ <p align="center">
40
+ <a href="https://pypi.org/project/mlarray/"><img src="https://img.shields.io/pypi/v/mlarray?logo=pypi&color=brightgreen&cacheSeconds=300&v" alt="PyPI" align="middle" /></a>
41
+ <a href="https://pypi.org/project/mlarray/"><img src="https://img.shields.io/pypi/pyversions/mlarray?logo=python&cacheSeconds=300&v" alt="Python Version" align="middle" /></a>
42
+ <a href="https://github.com/Karol-G/mlarray/actions"><img src="https://img.shields.io/github/actions/workflow/status/Karol-G/mlarray/workflow.yml?branch=main&logo=github" alt="Tests" align="middle" /></a>
43
+ <a href="https://karol-g.github.io/mlarray/"><img src="https://img.shields.io/badge/docs-mlarray-blue?logo=readthedocs&logoColor=white" alt="Docs" align="middle" /></a>
44
+ <a href="https://github.com/Karol-G/mlarray/blob/main/LICENSE"><img src="https://img.shields.io/github/license/Karol-G/mlarray" alt="License" align="middle" /></a>
45
+ </p>
46
+
47
+ **tl;dr:** Working with large medical or scientific images for machine learning? -> Use MLArray.
48
+
49
+ MLArray is a purpose-built file format for *N*-dimensional medical and scientific array data in machine learning workflows. It replaces the usual patchwork of source formats and late-stage conversions to NumPy/Zarr/Blosc2 by layering **standardized metadata** on top of a **Blosc2-backed** storage layout, so the same files work reliably across training, analysis, and visualization tools (including [Napari](https://napari.org) and [MITK](https://www.mitk.org/wiki/The_Medical_Imaging_Interaction_Toolkit_%28MITK%29)).
50
+
51
+ ## Installation
52
+
53
+ You can install mlarray via [pip](https://pypi.org/project/mlarray/):
54
+ ```bash
55
+ pip install mlarray
56
+ ```
57
+
58
+ To enable the `mlarray_convert` CLI command, install MLArray with the necessary extra dependencies:
59
+ ```bash
60
+ pip install mlarray[all]
61
+ ```
62
+
63
+ ## Documentaion
64
+
65
+ See the [documentation](https://karol-g.github.io/mlarray/) for the [API reference](https://karol-g.github.io/mlarray/api/), the [metadata schema](https://karol-g.github.io/mlarray/schema/), [usage examples](https://karol-g.github.io/mlarray/usage/) or [CLI usage](https://karol-g.github.io/mlarray/cli/).
66
+
67
+ ## Usage
68
+
69
+ Below are common usage patterns for loading, saving, and working with metadata.
70
+
71
+ ### Default usage
72
+
73
+ ```python
74
+ import numpy as np
75
+ from mlarray import MLArray
76
+
77
+ array = np.random.random((128, 256, 256))
78
+ image = MLArray(array) # Create MLArray image
79
+ image.save("sample.mla")
80
+
81
+ image = MLArray("sample.mla") # Loads image
82
+ ```
83
+
84
+ ### Memory-mapped usage
85
+
86
+ ```python
87
+ from mlarray import MLArray
88
+ import numpy as np
89
+
90
+ # read-only, partial access (default)
91
+ image = MLArray().open("sample.mla", mmap='r')
92
+ crop = image[10:20, 50:60] # Read crop
93
+
94
+ # read/write, partial access
95
+ image = MLArray().open("sample.mla", mmap='r+')
96
+ image[10:20, 50:60] *= 5 # Modify crop in memory and disk
97
+
98
+ # read/write, partial access, create/overwrite
99
+ array = np.random.random((128, 256, 256))
100
+ image = MLArray().open("sample.mla", shape=array.shape, dtype=array.dtype, mmap='w+')
101
+ image[...] = array # Modify image in memory and disk
102
+ ```
103
+
104
+ ### Metadata inspection and manipulation
105
+
106
+ ```python
107
+ import numpy as np
108
+ from mlarray import MLArray
109
+
110
+ array = np.random.random((64, 128, 128))
111
+ image = MLArray(
112
+ array,
113
+ spacing=(1.0, 1.0, 1.5),
114
+ origin=(10.0, 10.0, 30.0),
115
+ direction=[[1, 0, 0], [0, 1, 0], [0, 0, 1]],
116
+ meta={"patient_id": "123", "modality": "CT"}, # Any image metadata (for example raw DICOM metadata)
117
+ )
118
+
119
+ print(image.spacing) # [1.0, 1.0, 1.5]
120
+ print(image.origin) # [10.0, 10.0, 30.0]
121
+ print(image.meta.image) # {"patient_id": "123", "modality": "CT"}
122
+
123
+ image.spacing[1] = 5.3
124
+ image.meta.image["study_id"] = "study-001"
125
+ image.save("with-metadata.mla")
126
+
127
+ # Open memory-mapped
128
+ image = MLArray().open("with-metadata.mla", mmap='r+')
129
+ image.meta.image["study_id"] = "new-study" # Modify metadata
130
+ image.close() # Close and save metadata, only necessary to save modified metadata
131
+ ```
132
+
133
+ ### Copy metadata with overrides
134
+
135
+ ```python
136
+ import numpy as np
137
+ from mlarray import MLArray
138
+
139
+ base = MLArray("sample.mla")
140
+ array = np.random.random(base.shape)
141
+
142
+ image = MLArray(
143
+ array,
144
+ spacing=(0.8, 0.8, 1.0),
145
+ copy=base, # Copies all non-explicitly set arguments from base
146
+ )
147
+
148
+ image.save("copied-metadata.mla")
149
+ ```
150
+
151
+ ### Standardized metadata usage
152
+
153
+ ```python
154
+ import numpy as np
155
+ from mlarray import MLArray, Meta
156
+
157
+ array = np.random.random((64, 128, 128))
158
+ image = MLArray(
159
+ array,
160
+ meta=Meta(image={"patient_id": "123", "modality": "CT"}, is_seg=True), # Add metadata in a pre-defined format
161
+ )
162
+
163
+ print(image.meta.image) # {"patient_id": "123", "modality": "CT"}
164
+ print(image.meta.is_seg) # True
165
+
166
+ image.meta.image["study_id"] = "study-001"
167
+ image.meta.is_seg = False
168
+ image.save("with-metadata.mla")
169
+ ```
170
+
171
+ ### Patch size variants
172
+
173
+ Default patch size (192):
174
+ ```python
175
+ from mlarray import MLArray
176
+
177
+ image = MLArray("sample.mla")
178
+ image.save("default-patch.mla") # Default patch_size is 'default' -> Isotropic patch size of 192 pixels
179
+ image.save("default-patch.mla", patch_size='default')
180
+ ```
181
+
182
+ Custom isotropic patch size (512):
183
+ ```python
184
+ from mlarray import MLArray
185
+
186
+ image = MLArray("sample.mla")
187
+ image.save("patch-512.mla", patch_size=512)
188
+ ```
189
+
190
+ Custom non-isotropic patch size:
191
+ ```python
192
+ from mlarray import MLArray
193
+
194
+ image = MLArray("sample.mla")
195
+ image.save("patch-non-iso.mla", patch_size=(128, 192, 256))
196
+ ```
197
+
198
+ Manual chunk/block size:
199
+ ```python
200
+ from mlarray import MLArray
201
+
202
+ image = MLArray("sample.mla")
203
+ image.save("manual-chunk-block.mla", chunk_size=(1, 128, 128), block_size=(1, 32, 32))
204
+ ```
205
+
206
+ Let Blosc2 itself configure chunk/block size:
207
+ ```python
208
+ from mlarray import MLArray
209
+
210
+ image = MLArray("sample.mla")
211
+ # If patch_size, chunk_size and block_size are all None, Blosc2 will auto-configure chunk and block size
212
+ image.save("manual-chunk-block.mla", patch_size=None)
213
+ ```
214
+
215
+ ## CLI
216
+
217
+ ### mlarray_header
218
+
219
+ Print the metadata header from a `.mla` or `.b2nd` file.
220
+
221
+ ```bash
222
+ mlarray_header sample.mla
223
+ ```
224
+
225
+ ### mlarray_convert
226
+
227
+ Convert a NIfTI or NRRD file to MLArray and copy metadata.
228
+
229
+ ```bash
230
+ mlarray_convert sample.nii.gz output.mla
231
+ ```
232
+
233
+ ## Contributing
234
+
235
+ Contributions are welcome! Please open a pull request with clear changes and add tests when appropriate.
236
+
237
+ ## Acknowledgments
238
+
239
+ <p align="left">
240
+ <img src="https://github.com/MIC-DKFZ/vidata/raw/main/imgs/Logos/HI_Logo.png" width="150"> &nbsp;&nbsp;&nbsp;&nbsp;
241
+ <img src="https://github.com/MIC-DKFZ/vidata/raw/main/imgs/Logos/DKFZ_Logo.png" width="500">
242
+ </p>
243
+
244
+ This repository is developed and maintained by the Applied Computer Vision Lab (ACVL)
245
+ of [Helmholtz Imaging](https://www.helmholtz-imaging.de/) and the
246
+ [Division of Medical Image Computing](https://www.dkfz.de/en/medical-image-computing) at DKFZ.
@@ -0,0 +1,13 @@
1
+ mlarray/__init__.py,sha256=TOcW98poFXaGIlgFxoxtndaXa4cM34a7JWydWjqET0E,1757
2
+ mlarray/cli.py,sha256=W2ZBtEc6RU3g40L4ohsjEcmGPqkvL0S8I4imSSaRX9Y,2116
3
+ mlarray/meta.py,sha256=DYzkRViA0pztPMYcTJQGCKOORIE628DpinsT7_0dxKg,28009
4
+ mlarray/mlarray.py,sha256=tRnnh82r1YBoDV7cSVrTbfmj6rBVPGcEzMx1X30dZ7o,34717
5
+ mlarray/utils.py,sha256=wJR_vNDh-6fTcsJs6KpPgfDEr740ZHbTF1DW50X1YPk,351
6
+ mlarray-0.0.23.data/data/mlarray/assets/banner.png,sha256=sx280YW77U34X_jxxPW7w0eVI8OVT1SfI0lAkGgK-Hg,543044
7
+ mlarray-0.0.23.data/data/mlarray/assets/banner.png~,sha256=8odiUrP_Kc69NEEmkjiueyTeugE3Q882s4B7_UWP-ho,495025
8
+ mlarray-0.0.23.dist-info/licenses/LICENSE,sha256=FZhh2cOkja5ou8nKBnh8MPNFs84XSzSiM8sBQWv6XGA,1151
9
+ mlarray-0.0.23.dist-info/METADATA,sha256=DTTd6dPoNQzgTxL9nrgqwKx0-0uFXUkFD9H8jvDYn_s,8159
10
+ mlarray-0.0.23.dist-info/WHEEL,sha256=qELbo2s1Yzl39ZmrAibXA2jjPLUYfnVhUNTlyF1rq0Y,92
11
+ mlarray-0.0.23.dist-info/entry_points.txt,sha256=_jak1nJItC8tmlbrjWewg4_2kyioVt2ZgXP4LAqaeV4,117
12
+ mlarray-0.0.23.dist-info/top_level.txt,sha256=739PKX_F3WKzLltjsUDX8lJlJJUrr67hqNwrx9ZlBNU,8
13
+ mlarray-0.0.23.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.10.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ mlarray_convert = mlarray.cli:cli_convert_to_mlarray
3
+ mlarray_header = mlarray.cli:cli_print_header
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Division of Medical Image Computing, German Cancer Research Center (DKFZ), Heidelberg, Germany
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 @@
1
+ mlarray