Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Crop Volume BBOX for a json #1096

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scilpy/io/fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def get_testing_files_dict():
"plot.zip": "a1dc54cad7e1d17e55228c2518a1b34e",
"others.zip": "82248b4888a63b0aeffc8070cc206995",
"fodf_filtering.zip": "5985c0644321ecf81fd694fb91e2c898",
"processing.zip": "eece5cdbf437b8e4b5cb89c797872e28",
"processing.zip": "1ba6869c9d8b58a9b911ba71fdd50a07",
"surface_vtk_fib.zip": "241f3afd6344c967d7176b43e4a99a41",
"tractograms.zip": "1eb29085db974b5e58d32b13eb76fbe6",
"mrds.zip": "5abe6092400e11e9bb2423e2c387e774"
Expand Down
46 changes: 42 additions & 4 deletions scilpy/utils/spatial.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-

from dipy.io.utils import get_reference_info
import json
import nibabel.orientations as ornt
import numpy as np

from numpy.lib.index_tricks import r_ as row


Expand Down Expand Up @@ -212,9 +213,46 @@ class WorldBoundingBox(object):
"""

def __init__(self, minimums, maximums, voxel_size):
self.minimums = minimums
self.maximums = maximums
self.voxel_size = voxel_size
self.minimums = np.asarray(minimums)
self.maximums = np.asarray(maximums)
self.voxel_size = np.asarray(voxel_size)

def dump(self, filename):
"""
Save the bounding box to a json file.

Parameters
----------
filename: str
Path to the json file.
"""
with open(filename, 'w+') as bbox_file:
json.dump({
"minimums": self.minimums.tolist(),
"maximums": self.maximums.tolist(),
"voxel_size": self.voxel_size.tolist()}, bbox_file)

@classmethod
def load(cls, filename):
"""
Load a bounding box from a json file.

Parameters
----------
filename: str
Path to the json file.

Returns
-------
WorldBoundingBox
The bounding box object.
"""
with open(filename) as bbox_file:
values = json.load(bbox_file)

return WorldBoundingBox(np.array(values['minimums']),
np.array(values['maximums']),
np.array(values['voxel_size']))


def world_to_voxel(coord, affine):
Expand Down
14 changes: 7 additions & 7 deletions scripts/scil_volume_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
add_verbose_arg,
assert_inputs_exist,
assert_outputs_exist)
from scilpy.utils.spatial import WorldBoundingBox
from scilpy.image.utils import compute_nifti_bounding_box
from scilpy.image.volume_operations import crop_volume

Expand All @@ -54,11 +55,12 @@ def _build_arg_parser():

g1 = p.add_mutually_exclusive_group()
g1.add_argument('--input_bbox',
help='Path of the pickle file from which to take '
help='Path of the json file from which to take '
'the bounding box to crop input file.')
g1.add_argument('--output_bbox',
help='Path of the pickle file where to write the '
'computed bounding box. (.pickle extension)')
help='Path of the json file where to write the '
'computed bounding box.')

return p


Expand All @@ -72,8 +74,7 @@ def main():

img = nib.load(args.in_image)
if args.input_bbox:
with open(args.input_bbox, 'rb') as bbox_file:
wbbox = pickle.load(bbox_file)
wbbox = WorldBoundingBox.load(args.input_bbox)
if not args.ignore_voxel_size:
voxel_size = img.header.get_zooms()[0:3]
if not np.allclose(voxel_size, wbbox.voxel_size[0:3], atol=1e-03):
Expand All @@ -83,8 +84,7 @@ def main():
else:
wbbox = compute_nifti_bounding_box(img)
if args.output_bbox:
with open(args.output_bbox, 'wb') as bbox_file:
pickle.dump(wbbox, bbox_file)
wbbox.dump(args.output_bbox)

out_nifti_file = crop_volume(img, wbbox)
nib.save(out_nifti_file, args.out_image)
Expand Down
4 changes: 2 additions & 2 deletions scripts/tests/test_volume_crop.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ def test_execution_processing(script_runner, monkeypatch):
monkeypatch.chdir(os.path.expanduser(tmp_dir.name))
in_dwi = os.path.join(SCILPY_HOME, 'processing', 'dwi.nii.gz')
ret = script_runner.run('scil_volume_crop.py', in_dwi, 'dwi_crop.nii.gz',
'--output_bbox', 'bbox.pickle')
'--output_bbox', 'bbox.json')
assert ret.success

# Then try to load back the same box
ret = script_runner.run('scil_volume_crop.py', in_dwi, 'dwi_crop2.nii.gz',
'--input_bbox', 'bbox.pickle')
'--input_bbox', 'bbox.json')
assert ret.success