-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Australian-Imaging-Service/add-nifti
Add nifti module
- Loading branch information
Showing
7 changed files
with
83 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from pathlib import Path | ||
|
||
base_cache_dir = Path.home() / ".medimages4tests" / "cache" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from pathlib import Path | ||
import gzip | ||
import shutil | ||
import numpy as np | ||
import nibabel as nb | ||
|
||
|
||
def sample_image( | ||
out_file: Path, | ||
data: np.ndarray = None, | ||
vox_sizes=(1.0, 1.0, 1.0), | ||
qform=(1, 2, 3, 1), | ||
compressed=False, | ||
) -> Path: | ||
"""Create a random Nifti file to satisfy BIDS parsers""" | ||
if data is None: | ||
data = np.random.randint(0, 1, size=[10, 10, 10]) | ||
|
||
uncompressed = out_file.with_suffix('.nii') | ||
|
||
hdr = nb.Nifti1Header() | ||
hdr.set_data_shape(data.shape) | ||
hdr.set_zooms(vox_sizes) # set voxel size | ||
hdr.set_xyzt_units(2) # millimeters | ||
hdr.set_qform(np.diag(qform)) | ||
nb.save( | ||
nb.Nifti1Image( | ||
data, | ||
hdr.get_best_affine(), | ||
header=hdr, | ||
), | ||
uncompressed, | ||
) | ||
|
||
if compressed: | ||
out_file = out_file.with_suffix('.nii.gz') | ||
with open(uncompressed, 'rb') as f_in: | ||
with gzip.open(out_file, 'wb') as f_out: | ||
shutil.copyfileobj(f_in, f_out) | ||
else: | ||
out_file = uncompressed | ||
|
||
return out_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import gzip | ||
import shutil | ||
import nibabel as nb | ||
import numpy as np | ||
from medimages4tests.nifti import sample_image | ||
|
||
|
||
def test_nifti(work_dir): | ||
|
||
nifti_fpath = sample_image(work_dir / "sample.nii") | ||
|
||
nifti = nb.load(nifti_fpath) | ||
|
||
assert np.array_equal(nifti.header["dim"][:4], [3, 10, 10, 10]) | ||
|
||
|
||
def test_nifti_compressed(work_dir): | ||
|
||
gz_fpath = sample_image(work_dir / "sample.nii.gz", compressed=True) | ||
uncompressed_fpath = work_dir / "nifti.nii" | ||
|
||
with gzip.open(gz_fpath, 'rb') as f_in: | ||
with open(uncompressed_fpath, 'wb') as f_out: | ||
shutil.copyfileobj(f_in, f_out) | ||
nifti = nb.load(uncompressed_fpath) | ||
|
||
assert np.array_equal(nifti.header["dim"][:4], [3, 10, 10, 10]) |