forked from Lightning-AI/litData
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnifti.py
More file actions
32 lines (24 loc) · 763 Bytes
/
Copy pathnifti.py
File metadata and controls
32 lines (24 loc) · 763 Bytes
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
"""Nifti: encode a volume, stream a nibabel image.
Needs nibabel.
"""
import numpy as np
from litdata import Nifti, StreamingDataset, optimize
def make_sample(index: int) -> dict:
vol = np.random.randn(16, 16, 16).astype(np.float32)
return {
"index": index,
# Nifti(path="v.nii.gz")
"volume": Nifti(array=vol, affine=np.eye(4)),
}
if __name__ == "__main__":
optimize(
fn=make_sample,
inputs=list(range(4)),
output_dir="example_optimize_dataset/nifti",
num_workers=2,
chunk_bytes="64MB",
)
sample = StreamingDataset("example_optimize_dataset/nifti")[0]
nifti = sample["volume"] # Nibabel
volume = nifti.get_fdata()
print(volume.shape, nifti.affine.shape)