Skip to content
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
9 changes: 5 additions & 4 deletions dicom2nifti/convert_philips.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ def _multiframe_to_nifti(dicom_input, output_file):
logger.info('Creating bval en bvec files')
bval_file = '%s/%s.bval' % (base_path, base_name)
bvec_file = '%s/%s.bvec' % (base_path, base_name)
bval, bvec, bval_file, bvec_file = _create_bvals_bvecs(multiframe_dicom, bval_file, bvec_file, nii_image,
output_file)
nii_image, bval, bvec, bval_file, bvec_file = _create_bvals_bvecs(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Including the nii_image in the tuple of variables to.

multiframe_dicom, bval_file, bvec_file, nii_image, output_file
)

return {'NII_FILE': output_file,
'BVAL_FILE': bval_file,
Expand Down Expand Up @@ -433,15 +434,15 @@ def _create_bvals_bvecs(multiframe_dicom, bval_file, bvec_file, nifti, nifti_fil
bvals = None
bvecs = None

return bvals, bvecs, bval_file, bvec_file
return nifti, bvals, bvecs, bval_file, bvec_file
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key change - to return the modified nifti i.e. the ADC was removed a few lines earlier.



def _fix_diffusion_images(bvals, bvecs, nifti, nifti_file):
"""
This function will remove the last timepoint from the nifti, bvals and bvecs if the last vector is 0,0,0
This is sometimes added at the end by philips
"""
# if all zero continue of if the last bvec is not all zero continue
# if all zero continue or if the last bvec is not all zero continue
if numpy.count_nonzero(bvecs) == 0 or not numpy.count_nonzero(bvals[-1]) == 0:
# nothing needs to be done here
return nifti, bvals, bvecs
Expand Down
Binary file not shown.
1 change: 1 addition & 0 deletions tests/data/philips_enhanced/dti/adc_ground_truth.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 1000 1000
3 changes: 3 additions & 0 deletions tests/data/philips_enhanced/dti/adc_ground_truth.bvec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.0 -0.9939979910850525 -0.0821085199713707
0.0 0.08382682502269745 -0.9962577819824219
0.0 -0.07029245793819427 -0.02699320949614048
Binary file not shown.
3 changes: 3 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@
PHILIPS_ENHANCED_DTI_IMPLICIT = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'data', 'philips_enhanced', 'dti', '001_implicit')

PHILIPS_ENHANCED_DTI_ADC = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'data', 'philips_enhanced', 'dti', 'adc')

PHILIPS_ENHANCED_FMRI = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'data', 'philips_enhanced', 'fmri', '001')

Expand Down
18 changes: 17 additions & 1 deletion tests/test_philips.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import dicom2nifti.settings as settings
from dicom2nifti.common import read_dicom_directory
from dicom2nifti.exceptions import ConversionError
from tests.test_tools import assert_compare_nifti, assert_compare_bval, assert_compare_bvec, ground_thruth_filenames
from tests.test_tools import assert_compare_nifti, assert_compare_bval, assert_compare_bvec, ground_thruth_filenames, assert_compare_length_of_nii_to_bval


class TestConversionPhilips(unittest.TestCase):
Expand Down Expand Up @@ -109,6 +109,22 @@ def test_diffusion_imaging(self):
read_dicom_directory(test_data.PHILIPS_ENHANCED_DTI_IMPLICIT),
os.path.join(tmp_output_dir, 'test.nii.gz'))

# check PHILIPS_ENHANCED_ADC
results = convert_philips.dicom_to_nifti(read_dicom_directory(test_data.PHILIPS_ENHANCED_DTI_ADC),
os.path.join(tmp_output_dir, 'test.nii.gz'))
assert_compare_nifti(results['NII_FILE'],
ground_thruth_filenames(test_data.PHILIPS_ENHANCED_DTI_ADC)[0])
self.assertTrue(isinstance(results['NII'], nibabel.nifti1.Nifti1Image))
assert_compare_bval(results['BVAL_FILE'],
ground_thruth_filenames(test_data.PHILIPS_ENHANCED_DTI_ADC)[2])
self.assertTrue(isinstance(results['BVAL'], numpy.ndarray))
assert_compare_bval(results['BVEC_FILE'],
ground_thruth_filenames(test_data.PHILIPS_ENHANCED_DTI_ADC)[3])
self.assertTrue(isinstance(results['BVEC'], numpy.ndarray))
# Also check the length of the BVAL & BVEC
assert_compare_length_of_nii_to_bval(results['NII_FILE'],results['BVAL_FILE'])


finally:
shutil.rmtree(tmp_output_dir)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ def assert_compare_bvec(bvec_file_1, bvec_file_2):
equal = numpy.allclose(bvec_1, bvec_2)
if not equal:
raise Exception('bvecs not equal\n%s\n%s' %(numpy.array2string(bvec_1), numpy.array2string(bvec_2)))

def assert_compare_length_of_nii_to_bval(nifti_file, bval_file):
nifti = nibabel.load(nifti_file)
nifti_num_of_directions = nifti.header['dim'][4]
bval_1 = numpy.loadtxt(bval_file)
bval_num_of_directions = len(bval_1)
if not nifti_num_of_directions == bval_num_of_directions:
raise Exception('BVAL not equal length to the NIfTI\n%s\n%s' %(bval_num_of_directions, nifti_num_of_directions))