Instructions to use MedMLX/musclemap-mlx with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- MLX
How to use MedMLX/musclemap-mlx with MLX:
# Download the model from the Hub pip install huggingface_hub[hf_xet] hf download MedMLX/musclemap-mlx --local-dir musclemap-mlx
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- LM Studio
- Atomic Chat
File size: 23,063 Bytes
05fa11f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | """Record independent MONAI 1.3.2 reference outputs for the MuscleMap port.
Run in the pinned oracle environment (Python 3.11, torch 2.4.1, MONAI 1.3.2):
PYTHONPATH=src environment/oracle/.venv/bin/python \
scripts/make_reference_fixtures.py --assets weights --output build/msk-porting/fixtures
The script uses only synthetic arrays and the publisher checkpoint. It records
the exact upstream preprocessing stages, 64x64 network logits, the full small
pipeline labels and inverted logits, and a larger sliding-window case. The
resulting .npz files are development artifacts and stay ignored.
"""
from __future__ import annotations
import argparse
import json
import time
from pathlib import Path
from tempfile import TemporaryDirectory
import nibabel as nib
import numpy as np
import scipy
import torch
from monai.inferers import SliceInferer
from monai.networks.layers import Norm
from monai.networks.nets import UNet
from monai.transforms import (
AsDiscreted,
Compose,
CropForegroundd,
EnsureChannelFirstd,
EnsureTyped,
Invertd,
LoadImaged,
MapTransform,
NormalizeIntensityd,
Orientationd,
Spacingd,
SpatialPadd,
)
from scipy import ndimage as ndi
from musclemap_mlx.assets import verify_assets
from musclemap_mlx.config import CONFIG_NAME, load_model_config
SCHEMA_VERSION = 1
SMALL_SHAPE = (100, 90, 4)
WINDOW_SHAPE = (320, 220, 2)
OBLIQUE_SHAPE = (44, 40, 3)
LARGE_SHAPE = (280, 260, 20)
AFFINE = np.array(
[
[-0.9, 0.0, 0.0, 12.0],
[0.0, -1.3, 0.0, -7.0],
[0.0, 0.0, 2.5, 30.0],
[0.0, 0.0, 0.0, 1.0],
],
dtype=np.float64,
)
def _rotated_affine(
angle_deg: float, spacing: tuple[float, float, float], translation: tuple[float, float, float]
) -> np.ndarray:
"""An in-plane rotated, anisotropic RAS-like affine for geometry coverage."""
angle = np.deg2rad(angle_deg)
rotation = np.array(
[
[np.cos(angle), -np.sin(angle), 0.0],
[np.sin(angle), np.cos(angle), 0.0],
[0.0, 0.0, 1.0],
],
dtype=np.float64,
)
scale = np.diag(np.asarray(spacing, dtype=np.float64))
affine = np.eye(4, dtype=np.float64)
affine[:3, :3] = rotation @ scale
affine[:3, 3] = np.asarray(translation, dtype=np.float64)
return affine
OBLIQUE_AFFINE = _rotated_affine(18.0, (0.9, 1.25, 2.5), (10.0, -6.0, 20.0))
LARGE_AFFINE = _rotated_affine(8.0, (1.0, 1.2, 2.5), (15.0, -9.0, 25.0))
class SqueezeTransform(MapTransform):
"""Remove a leading singleton channel after argparse, as upstream does."""
def __init__(self, keys: tuple[str, ...]) -> None:
super().__init__(keys)
def __call__(self, data: dict[str, object]) -> dict[str, object]:
output = dict(data)
for key in self.keys:
value = output[key]
if isinstance(value, torch.Tensor) and value.dim() > 3 and value.shape[0] == 1:
output[key] = value.squeeze(0)
return output
class RemapLabels(MapTransform):
"""Restore the publisher's anatomical IDs from network channel indices."""
def __init__(self, keys: tuple[str, ...], id_map: dict[int, int]) -> None:
super().__init__(keys)
self.id_map = id_map
def __call__(self, data: dict[str, object]) -> dict[str, object]:
output = dict(data)
for key in self.keys:
value = output[key]
if not isinstance(value, torch.Tensor):
continue
result = value.clone()
for source, target in self.id_map.items():
result[value == source] = target
output[key] = result
return output
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--assets", required=True, type=Path)
parser.add_argument("--output", required=True, type=Path)
parser.add_argument("--image", type=Path, help="Record a real NIfTI instead of synthetic cases")
parser.add_argument("--chunk-size", type=int, default=8)
args = parser.parse_args(argv)
assets = args.assets.expanduser().absolute()
output = args.output.expanduser().absolute()
output.mkdir(parents=True, exist_ok=True)
verify_assets(assets)
config_path = assets / CONFIG_NAME
load_model_config(config_path)
torch.set_num_threads(1)
torch.manual_seed(0)
state = torch.load(
assets / "contrast_agnostic_wholebody_model.pth",
map_location="cpu",
weights_only=True,
)
network = _network(config_path)
network.load_state_dict(state)
network.eval()
if args.image is not None:
_record_real(args.image, network, config_path, output, args.chunk_size)
return 0
small_image = _phantom(SMALL_SHAPE, (50, 45, 2), (13, 10, 2), seed=7)
small_path = output / "small_input.nii.gz"
_write_nifti(small_path, small_image, AFFINE)
preprocess, stages, final = _run_stages(small_path, config_path)
_save_preprocess(output / "preprocess.npz", small_image, stages, final)
inverted, raw_labels = _run_pipeline(
preprocess, final, network, config_path, filter_connected=False
)
labels = connected_chunks(raw_labels)
reader_affine = np.asarray(stages["loaded_affine"], dtype=np.float64)
np.savez_compressed(
output / "pipeline.npz",
input=small_image,
input_affine=reader_affine,
inverted_logits=np.ascontiguousarray(inverted),
labels_unfiltered=np.ascontiguousarray(raw_labels),
labels=np.ascontiguousarray(labels),
)
chunked_labels = _run_chunked_pipeline(small_path, network, config_path, chunk_size=2)
np.savez_compressed(
output / "pipeline_chunked.npz",
input=small_image,
input_affine=reader_affine,
labels=np.ascontiguousarray(chunked_labels),
)
inverse_rng = np.random.default_rng(99)
padded_logits = inverse_rng.normal(size=(4, 256, 256, 4)).astype(np.float32)
inverted_logits = _invert_logits(preprocess, final, padded_logits)
np.savez_compressed(
output / "inverse.npz",
padded_logits=padded_logits,
inverted_logits=np.ascontiguousarray(inverted_logits),
)
rng = np.random.default_rng(20261102)
network_input = rng.normal(size=(1, 1, 64, 64)).astype(np.float32)
with torch.inference_mode():
network_logits = network(torch.from_numpy(network_input)).numpy()
np.savez_compressed(
output / "network64.npz",
input=network_input,
logits=np.ascontiguousarray(network_logits),
)
window_image = _slab(WINDOW_SHAPE)
window_path = output / "window_input.nii.gz"
_write_nifti(window_path, window_image, AFFINE)
window_preprocess, window_stages, window_final = _run_stages(window_path, config_path)
_, window_labels = _run_pipeline(window_preprocess, window_final, network, config_path)
window_affine = np.asarray(window_stages["loaded_affine"], dtype=np.float64)
np.savez_compressed(
output / "windows.npz",
input=window_image,
input_affine=window_affine,
labels=np.ascontiguousarray(window_labels),
)
oblique_image = _phantom(OBLIQUE_SHAPE, (22, 20, 1), (9, 8, 1), seed=11)
oblique_path = output / "oblique_input.nii.gz"
_write_nifti(oblique_path, oblique_image, OBLIQUE_AFFINE)
oblique_preprocess, oblique_stages, oblique_final = _run_stages(oblique_path, config_path)
_save_preprocess(
output / "preprocess_oblique.npz", oblique_image, oblique_stages, oblique_final
)
oblique_pad_shape = np.asarray(oblique_stages["pad_data"]).shape[1:]
oblique_rng = np.random.default_rng(101)
oblique_padded = oblique_rng.normal(size=(4, *oblique_pad_shape)).astype(np.float32)
oblique_inverted = _invert_logits(oblique_preprocess, oblique_final, oblique_padded)
np.savez_compressed(
output / "inverse_oblique.npz",
padded_logits=oblique_padded,
inverted_logits=np.ascontiguousarray(oblique_inverted),
)
large_image = _large_slab(LARGE_SHAPE)
large_path = output / "large_oblique_input.nii.gz"
_write_nifti(large_path, large_image, LARGE_AFFINE)
large_preprocess, large_stages, large_final = _run_stages(large_path, config_path)
_save_preprocess(output / "preprocess_large.npz", large_image, large_stages, large_final)
_, raw_whole = _run_pipeline(
large_preprocess, large_final, network, config_path, filter_connected=False
)
large_whole = connected_chunks(raw_whole)
large_chunked = _run_chunked_pipeline(large_path, network, config_path, chunk_size=8)
large_chunked2 = _run_chunked_pipeline(large_path, network, config_path, chunk_size=2)
large_reader_affine = np.asarray(large_stages["loaded_affine"], dtype=np.float64)
np.savez_compressed(
output / "large_oblique.npz",
input=large_image,
input_affine=large_reader_affine,
labels_whole=np.ascontiguousarray(large_whole),
labels_chunked=np.ascontiguousarray(large_chunked),
labels_chunked2=np.ascontiguousarray(large_chunked2),
)
metadata = {
"schema_version": SCHEMA_VERSION,
"upstream": {
"repository": "https://github.com/MuscleMap/MuscleMap",
"revision": "35a2138a4a3a63be713bae5584632ffc6433b960",
"zenodo_record": "https://zenodo.org/records/21929873",
},
"versions": {
"torch": torch.__version__,
"monai": _module_version("monai"),
"numpy": np.__version__,
"nibabel": nib.__version__,
"scipy": scipy.__version__,
"torch_threads": torch.get_num_threads(),
},
"cases": {
"preprocess": {"shape": list(SMALL_SHAPE), "affine": AFFINE.tolist()},
"network64": {"shape": [1, 1, 64, 64], "seed": 20261102},
"pipeline": {"shape": list(SMALL_SHAPE), "affine": AFFINE.tolist()},
"windows": {"shape": list(WINDOW_SHAPE), "affine": AFFINE.tolist()},
"oblique": {"shape": list(OBLIQUE_SHAPE), "affine": OBLIQUE_AFFINE.tolist()},
"large": {"shape": list(LARGE_SHAPE), "affine": LARGE_AFFINE.tolist()},
},
}
(output / "metadata.json").write_text(
json.dumps(metadata, indent=2, sort_keys=True) + "\n", encoding="utf-8"
)
print(json.dumps(metadata, indent=2, sort_keys=True))
return 0
def _record_real(
path: Path, network: UNet, config: Path, output: Path, chunk_size: int
) -> None:
"""Record upstream float32 chunk logits and the assembled anatomical mask.
Chunk NIfTI construction follows upstream ``_write_temp_chunk``. Component
filtering runs once after assembly, never independently on each chunk.
"""
if chunk_size < 1:
raise ValueError("chunk-size must be positive")
started = time.perf_counter()
image = nib.load(str(path))
data = np.asarray(image.dataobj, dtype=np.float32)
if data.ndim != 3:
raise ValueError("A real reference input must be three-dimensional")
full = np.zeros(data.shape, dtype=np.int16)
with TemporaryDirectory(prefix="chunks-", dir=output) as temporary:
for start in range(0, data.shape[2], chunk_size):
end = min(start + chunk_size, data.shape[2])
chunk = data[..., start:end]
chunk_path = Path(temporary) / "input.nii"
nib.save(nib.Nifti1Image(chunk, image.affine, image.header.copy()), str(chunk_path))
transforms, stages, final = _run_stages(chunk_path, config)
inverted, labels = _run_pipeline(
transforms, final, network, config, filter_connected=False
)
full[..., start:end] = labels
prepared = final["image"]
window = prepared[None, :, :256, :256, :1].squeeze(-1)
with torch.inference_mode():
network_logits = network(window).detach().cpu().numpy()
np.savez_compressed(
output / f"chunk-{start}.npz", input=np.asarray(stages["loaded_data"]),
affine=np.asarray(stages["loaded_affine"]),
prepared=np.asarray(prepared), network_input=np.asarray(window),
network_logits=network_logits, inverted_logits=inverted,
)
print(f"Recorded slices {start}:{end}", flush=True)
np.save(output / "labels.npy", connected_chunks(full))
metadata = {
"upstream_revision": "35a2138a4a3a63be713bae5584632ffc6433b960",
"checkpoint_record": "https://zenodo.org/records/21929873",
"input_name": path.name, "shape": list(data.shape),
"affine": image.affine.tolist(), "units": image.header.get_xyzt_units(),
"chunk_size": chunk_size, "overlap": 0.9, "dtype": "float32",
"torch": torch.__version__, "monai": _module_version("monai"),
"numpy": np.__version__, "nibabel": nib.__version__, "scipy": scipy.__version__,
"duration_seconds": time.perf_counter() - started,
}
(output / "metadata.json").write_text(json.dumps(metadata, indent=2) + "\n")
def _network(config_path: Path) -> UNet:
spec = load_model_config(config_path)
return UNet(
spatial_dims=spec.spatial_dims,
in_channels=spec.in_channels,
out_channels=spec.out_channels,
channels=spec.channels,
act=spec.act,
strides=spec.strides,
num_res_units=spec.num_res_units,
norm=Norm.INSTANCE,
)
def _transforms(config_path: Path) -> Compose:
spec = load_model_config(config_path)
return Compose(
[
LoadImaged(keys=["image"], image_only=False),
EnsureChannelFirstd(keys=["image"]),
Orientationd(keys=["image"], axcodes="RAS"),
Spacingd(keys=["image"], pixdim=spec.pix_dim, mode="bilinear"),
NormalizeIntensityd(keys=["image"], nonzero=True),
CropForegroundd(keys=["image"], source_key="image", margin=20),
SpatialPadd(
keys=["image"],
spatial_size=(256, 256, 1),
method="end",
mode="constant",
),
EnsureTyped(keys=["image"]),
]
)
def _run_stages(
path: Path, config_path: Path
) -> tuple[Compose, dict[str, object], dict[str, object]]:
transforms = _transforms(config_path)
data: dict[str, object] = {"image": str(path)}
stages: dict[str, object] = {}
names = ["loaded", "channel", "orientation", "spacing", "normalize", "crop", "pad", "typed"]
for name, transform in zip(names, transforms.transforms, strict=True):
data = transform(data)
image = data["image"]
if isinstance(image, torch.Tensor):
stages[f"{name}_data"] = np.array(image.detach().cpu(), copy=True)
affine = getattr(image, "affine", None)
if affine is not None:
stages[f"{name}_affine"] = np.array(affine.detach().cpu(), copy=True)
if "foreground_start_coord" in data:
stages["crop_start"] = np.asarray(data["foreground_start_coord"], dtype=np.int64)
stages["crop_end"] = np.asarray(data["foreground_end_coord"], dtype=np.int64)
return transforms, stages, data
def _invert_logits(
transforms: Compose, data: dict[str, object], padded_logits: np.ndarray
) -> np.ndarray:
image = data["image"]
if not isinstance(image, torch.Tensor):
raise TypeError("upstream transform did not produce a tensor")
invert = Invertd(
keys="pred",
transform=transforms,
orig_keys="image",
meta_keys="pred_meta_dict",
orig_meta_keys="image_meta_dict",
meta_key_postfix="meta_dict",
nearest_interp=False,
to_tensor=True,
device=torch.device("cpu"),
)
post = {
"pred": torch.from_numpy(padded_logits),
"image": image,
"image_meta_dict": data["image_meta_dict"],
"image_transforms": image.applied_operations,
}
inverted = invert(post)["pred"]
if not isinstance(inverted, torch.Tensor):
raise TypeError("Invertd did not produce a tensor")
return np.asarray(inverted.detach().cpu(), dtype=np.float32)
def _run_pipeline(
transforms: Compose,
data: dict[str, object],
network: UNet,
config_path: Path,
*,
filter_connected: bool = True,
) -> tuple[np.ndarray, np.ndarray]:
spec = load_model_config(config_path)
labels = sorted({label.value for label in spec.labels})
id_map = {0: 0}
for new_id, original in enumerate(labels, start=1):
id_map[original] = new_id
inverse_map = {new_id: original for original, new_id in id_map.items()}
image = data["image"]
if not isinstance(image, torch.Tensor):
raise TypeError("upstream transform did not produce a tensor")
tensor = image
if tensor.ndim == 4:
tensor = tensor.unsqueeze(0)
inferer = SliceInferer(
roi_size=spec.roi_size,
sw_batch_size=spec.spatial_window_batch_size,
spatial_dim=2,
mode="gaussian",
overlap=0.9,
)
with torch.inference_mode():
prediction = inferer(tensor, network)
single = prediction.squeeze(0).squeeze(0)
invert = Invertd(
keys="pred",
transform=transforms,
orig_keys="image",
meta_keys="pred_meta_dict",
orig_meta_keys="image_meta_dict",
meta_key_postfix="meta_dict",
nearest_interp=False,
to_tensor=True,
device=torch.device("cpu"),
)
post = {"pred": single, "image": data["image"], "image_meta_dict": data["image_meta_dict"]}
inverted = invert(post)
inverted_tensor = inverted["pred"]
if not isinstance(inverted_tensor, torch.Tensor):
raise TypeError("Invertd did not produce a tensor")
inverted_logits = np.asarray(inverted_tensor.detach().cpu(), dtype=np.float32)
discrete = AsDiscreted(keys="pred", argmax=True)(inverted)
squeezed = SqueezeTransform(keys=("pred",))(discrete)
remapped = RemapLabels(keys=("pred",), id_map=inverse_map)(squeezed)
result = remapped["pred"]
if not isinstance(result, torch.Tensor):
raise TypeError("upstream postprocessing did not produce a tensor")
labels = np.asarray(result.detach().cpu(), dtype=np.int16)
return inverted_logits, connected_chunks(labels) if filter_connected else labels
def _run_chunked_pipeline(
path: Path, network: UNet, config_path: Path, *, chunk_size: int
) -> np.ndarray:
image = nib.load(str(path))
affine = np.asarray(image.affine, dtype=np.float64)
data = np.asanyarray(image.dataobj)
header = image.header.copy()
full = np.zeros(data.shape, dtype=np.int16)
depth = int(data.shape[-1])
for start in range(0, depth, chunk_size):
end = min(start + chunk_size, depth)
chunk_path = path.parent / f"chunk_{start}_{end}.nii"
chunk = np.asarray(data[..., start:end], dtype=np.float32)
chunk_image = nib.Nifti1Image(chunk, affine, header.copy())
chunk_image.set_qform(affine, 1)
chunk_image.set_sform(affine, 1)
nib.save(chunk_image, str(chunk_path))
transforms, _stages, final = _run_stages(chunk_path, config_path)
_, labels = _run_pipeline(
transforms, final, network, config_path, filter_connected=False
)
full[..., start:end] = labels
chunk_path.unlink()
return connected_chunks(full)
def connected_chunks(seg: np.ndarray, connectivity: int = 1) -> np.ndarray:
"""Keep the largest 3D connected component of each nonzero label.
Copied from ``mm_util.connected_chunks`` at the pinned upstream revision.
"""
labels = np.unique(seg)
labels = labels[labels != 0]
result = np.array(seg, dtype=np.int16, copy=True)
if labels.size == 0:
return result
structure = ndi.generate_binary_structure(rank=3, connectivity=connectivity)
mask3d = np.empty(result.shape, dtype=bool)
lab3d = np.empty(result.shape, dtype=np.int32)
for label in labels:
np.equal(result, label, out=mask3d)
if not mask3d.any():
continue
ndi.label(mask3d, structure=structure, output=lab3d)
max_lab = int(lab3d.max())
if max_lab <= 1:
continue
counts = np.bincount(lab3d.ravel())
keep = counts[1:].argmax() + 1
np.logical_and(mask3d, lab3d != keep, out=mask3d)
result[mask3d] = 0
return result
def _save_preprocess(
path: Path, image: np.ndarray, stages: dict[str, object], final: dict[str, object]
) -> None:
final_image = final["image"]
if not isinstance(final_image, torch.Tensor):
raise TypeError("final transform did not produce a tensor")
stages["input"] = image
stages["final"] = np.asarray(final_image.detach().cpu())
np.savez_compressed(path, **stages)
def _phantom(
shape: tuple[int, int, int],
center: tuple[int, int, int],
radii: tuple[int, int, int],
*,
seed: int,
) -> np.ndarray:
rng = np.random.default_rng(seed)
axes = [np.arange(size, dtype=np.float32) for size in shape]
grid = np.meshgrid(*axes, indexing="ij")
distance = sum(
((coordinate - center[index]) / radii[index]) ** 2
for index, coordinate in enumerate(grid)
)
data = np.zeros(shape, dtype=np.float32)
inside = distance <= 1.0
phase = distance[inside] * np.float32(np.pi)
data[inside] = (
np.float32(40.0)
+ np.float32(30.0) * np.cos(phase)
+ np.float32(3.0) * rng.normal(size=int(inside.sum())).astype(np.float32)
)
data[distance <= 0.12] = np.float32(-25.0)
return np.ascontiguousarray(data)
def _slab(shape: tuple[int, int, int]) -> np.ndarray:
data = np.zeros(shape, dtype=np.float32)
data[20:300, 10:210, :] = np.float32(100.0)
data[20:300, 10:210, :] += np.linspace(-5.0, 5.0, 280, dtype=np.float32)[:, None, None]
data[120:180, 80:130, :] = np.float32(-20.0)
return np.ascontiguousarray(data)
def _large_slab(shape: tuple[int, int, int]) -> np.ndarray:
"""A deterministic large volume made by tiling the labelled synthetic phantom.
A uniform slab made the released model predict background everywhere; tiling the
smaller phantom keeps the multi-window and multi-chunk cases numerically active.
"""
small = _phantom((100, 90, 4), (50, 45, 2), (13, 10, 2), seed=7)
tiled = np.tile(small, (3, 3, 5))
return np.ascontiguousarray(tiled[: shape[0], : shape[1], : shape[2]])
def _write_nifti(path: Path, data: np.ndarray, affine: np.ndarray) -> None:
image = nib.Nifti1Image(data, affine)
image.set_qform(affine, 1)
image.set_sform(affine, 1)
image.header.set_xyzt_units("mm", "sec")
nib.save(image, str(path))
def _module_version(name: str) -> str:
module = __import__(name)
return str(getattr(module, "__version__", "unknown"))
if __name__ == "__main__":
raise SystemExit(main())
|