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/__init__.py ADDED
@@ -0,0 +1,54 @@
1
+ """A standardized blosc2 image reader and writer for medical images.."""
2
+
3
+ from importlib import metadata as _metadata
4
+ from typing import TYPE_CHECKING
5
+
6
+ if TYPE_CHECKING:
7
+ from mlarray.mlarray import MLArray, MLARRAY_DEFAULT_PATCH_SIZE
8
+ from mlarray.meta import Meta, MetaBlosc2, MetaSpatial
9
+ from mlarray.utils import is_serializable
10
+ from mlarray.cli import cli_print_header, cli_convert_to_mlarray
11
+
12
+ __all__ = [
13
+ "__version__",
14
+ "MLArray",
15
+ "MLARRAY_DEFAULT_PATCH_SIZE",
16
+ "Meta",
17
+ "MetaBlosc2",
18
+ "MetaSpatial",
19
+ "is_serializable",
20
+ "cli_print_header",
21
+ "cli_convert_to_mlarray",
22
+ ]
23
+
24
+ try:
25
+ __version__ = _metadata.version(__name__)
26
+ except _metadata.PackageNotFoundError: # pragma: no cover - during editable installs pre-build
27
+ __version__ = "0.0.0"
28
+
29
+
30
+ def __getattr__(name: str):
31
+ if name in {"MLArray", "MLARRAY_DEFAULT_PATCH_SIZE"}:
32
+ from mlarray.mlarray import MLArray, MLARRAY_DEFAULT_PATCH_SIZE
33
+
34
+ return MLArray if name == "MLArray" else MLARRAY_DEFAULT_PATCH_SIZE
35
+ if name in {"Meta", "MetaBlosc2", "MetaSpatial"}:
36
+ from mlarray.meta import Meta, MetaBlosc2, MetaSpatial
37
+
38
+ return {"Meta": Meta, "MetaBlosc2": MetaBlosc2, "MetaSpatial": MetaSpatial}[name]
39
+ if name == "is_serializable":
40
+ from mlarray.utils import is_serializable
41
+
42
+ return is_serializable
43
+ if name in {"cli_print_header", "cli_convert_to_mlarray"}:
44
+ from mlarray.cli import cli_print_header, cli_convert_to_mlarray
45
+
46
+ return {
47
+ "cli_print_header": cli_print_header,
48
+ "cli_convert_to_mlarray": cli_convert_to_mlarray,
49
+ }[name]
50
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
51
+
52
+
53
+ def __dir__():
54
+ return sorted(__all__)
mlarray/cli.py ADDED
@@ -0,0 +1,58 @@
1
+ import argparse
2
+ import json
3
+ from typing import Union
4
+ from pathlib import Path
5
+ from mlarray import MLArray
6
+
7
+ try:
8
+ from medvol import MedVol
9
+ except ImportError:
10
+ MedVol = None
11
+
12
+
13
+ def print_header(filepath: Union[str, Path]) -> None:
14
+ """Print the MLArray metadata header for a file.
15
+
16
+ Args:
17
+ filepath: Path to a ".mla" or ".b2nd" file.
18
+ """
19
+ meta = MLArray(filepath).meta
20
+ if meta is None:
21
+ print("null")
22
+ return
23
+ print(json.dumps(meta.to_dict(include_none=True), indent=2, sort_keys=True))
24
+
25
+
26
+ def convert_to_mlarray(load_filepath: Union[str, Path], save_filepath: Union[str, Path]):
27
+ if MedVol is None:
28
+ raise RuntimeError("medvol is required for mlarray_convert; install with 'pip install mlarray[all]'.")
29
+ image_meta_format = None
30
+ if str(load_filepath).endswith(f".nii.gz") or str(load_filepath).endswith(f".nii"):
31
+ image_meta_format = "nifti"
32
+ elif str(load_filepath).endswith(f".nrrd"):
33
+ image_meta_format = "nrrd"
34
+ image_medvol = MedVol(load_filepath)
35
+ image_mlarray = MLArray(image_medvol.array, spacing=image_medvol.spacing, origin=image_medvol.origin, direction=image_medvol.direction, meta=image_medvol.header)
36
+ image_mlarray.meta._image_meta_format = image_meta_format
37
+ image_mlarray.save(save_filepath)
38
+
39
+
40
+ def cli_print_header() -> None:
41
+ parser = argparse.ArgumentParser(
42
+ prog="mlarray_header",
43
+ description="Print the MLArray metadata header for a file.",
44
+ )
45
+ parser.add_argument("filepath", help="Path to a .mla or .b2nd file.")
46
+ args = parser.parse_args()
47
+ print_header(args.filepath)
48
+
49
+
50
+ def cli_convert_to_mlarray() -> None:
51
+ parser = argparse.ArgumentParser(
52
+ prog="mlarray_convert",
53
+ description="Convert a NiFTi or NRRD file to MLArray and copy all metadata.",
54
+ )
55
+ parser.add_argument("load_filepath", help="Path to the NiFTi (.nii.gz, .nii) or NRRD (.nrrd) file to load.")
56
+ parser.add_argument("save_filepath", help="Path to the MLArray (.mla) file to save.")
57
+ args = parser.parse_args()
58
+ convert_to_mlarray(args.load_filepath, args.save_filepath)